1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
Ë
hñúhJ<ãóæ—dZddlZddlZddlmZddlmZddlmZe    edej«Z
de fd„Z ejejzfd    efd
„Zd    ed efd „Zej$sd ZeZdZdZdZdZd„Znqej4sdZdZdZdZdZdZed«Zd„ZnMd ZeZej8rdZndZdZej8rdZndZdZej:r ed«Zd„Zn ed«Zd„Zedezd zezd!zezd"zezd#zezd$zezd%zezd&zezd'z«Zed(ezd)zezd*z«Zd+efd,„Z d-ed.ed/e fd0„Z!d-ed.ed/e"fd1„Z#d.ed/e"fd2„Z$d8d3e%d.ed/e&fd4„Z'd.ed/e&fd5„Z(d6e fd7„Z)y)9aX
Tools for searching bytecode for key statements that indicate the need for additional resources, such as data files
and package metadata.
 
By *bytecode* I mean the ``code`` object given by ``compile()``, accessible from the ``__code__`` attribute of any
non-builtin function or, in PyInstallerLand, the ``PyiModuleGraph.node("some.module").code`` attribute. The best
guide for bytecode format I have found is the disassembler reference: https://docs.python.org/3/library/dis.html
 
This parser implementation aims to combine the flexibility and speed of regex with the clarity of the output of
``dis.dis(code)``. It has not achieved the 2nd, but C'est la vie...
 
The biggest clarity killer here is the ``EXTENDED_ARG`` opcode which can appear almost anywhere and therefore needs
to be tiptoed around at every step. If this code needs to expand significantly, I would recommend an upgrade to a
regex-based grammar parsing library such as Reparse. This way, little steps like unpacking ``EXTENDED_ARGS`` can be
defined once then simply referenced forming a nice hierarchy rather than copied everywhere its needed.
éN)ÚCodeType)ÚPattern)ÚcompatÚ
_all_opmapÚxcóN—tjtt|g««S)zG
    Get a regex-escaped opcode byte from its human readable name.
    )ÚreÚescapeÚbytesÚopmap)rs úNH:\Change_password\venv_build\Lib\site-packages\PyInstaller/depend/bytecode.pyÚ_instruction_to_regexr)s€ô 9‰9”UœE !™H˜:Ó&Ó 'Ð'óÚpatterncó„—t|t«sJ‚tjdd„|«}tj||¬«S)aÂ
    A regex-powered Python bytecode matcher.
 
    ``bytecode_regex`` provides a very thin wrapper around :func:`re.compile`.
 
      * Any opcode names wrapped in backticks are substituted for their corresponding opcode bytes.
      * Patterns are compiled in VERBOSE mode by default so that whitespace and comments may be used.
 
    This aims to mirror the output of :func:`dis.dis`, which is far more readable than looking at raw byte strings.
    s`(\w+)`có:—t|dj««S)Né)rÚdecode)Úms r ú<lambda>z bytecode_regex.<locals>.<lambda>@s€Ô'¨¨!©¯ © « Ó6€r)Úflags)Ú
isinstancer r    ÚsubÚcompile)rrs  r Úbytecode_regexr0s@€ô gœuÔ %Ð%Ð %ôf‰fØÙ6Øó€Gô
:‰:g UÔ +Ð+rÚstringc#óøK—t|t«sJ‚t|«}|j|«}    |D]A}|j    «dzdk(r|–—Œ|j||j    «dz«}nyŒH­w)a
    Call ``pattern.finditer(string)``, but remove any matches beginning on an odd byte (i.e., matches where
    match.start() is not a multiple of 2).
 
    This should be used to avoid false positive matches where a bytecode pair's argument is mistaken for an opcode.
    érrN)rr Ú_cleanup_bytecode_stringÚfinditerÚstart)rrÚmatchesÚmatchs    r r r Fsˆèø€ô fœeÔ $Ð$Ð $Ü % fÓ -€FØ×јvÓ&€GØ
Øò     ˆE؏{‰{‹}˜qÑ  AÒ%à“ ð "×*Ñ*¨6°5·;±;³=À1Ñ3DÓEÙð     ð ð ùs‚A8A:s`EXTENDED_ARG`s%`LOAD_NAME`|`LOAD_GLOBAL`|`LOAD_FAST`s`LOAD_ATTR`|`LOAD_METHOD`s `LOAD_CONST`s0`CALL_FUNCTION`|`CALL_METHOD`|`CALL_FUNCTION_EX`có—|S©N©©Úbytecodes r rrps€Øˆrs#`EXTENDED_ARG`|`EXTENDED_ARG_QUICK`s"`EXTENDED_ARG``EXTENDED_ARG_QUICK`s`PRECALL`|`CALL_FUNCTION_EX`s(`CACHE`.)|(..)có.—tjd|«S©Ns\2©Ú_cache_instruction_filterrr's r rrƒs€Ü(×,Ñ,¨V°XÓ>Ð>rs8`LOAD_NAME`|`LOAD_GLOBAL`|`LOAD_FAST`|`LOAD_FAST_BORROW`s `LOAD_ATTR`s3`LOAD_CONST`|`LOAD_SMALL_INT`|`LOAD_CONST_IMMORTAL`s`CALL`|`CALL_FUNCTION_EX`s(`CACHE`.)|(`PUSH_NULL`.)|(..)có.—tjd|«S)Ns\3r+r's r rr ó€Ü,×0Ñ0°¸ÓBÐ Brcó.—tjd|«Sr*r+r's r rr¥r.rs¼
    # Matches `global_function('some', 'constant', 'arguments')`.
 
    # Load the global function. In code with >256 of names, this may require extended name references.
    (
     (?:(?:s).)*
     (?:(?:sÎ).)
    )
 
    # For foo.bar.whizz(), the above is the 'foo', below is the 'bar.whizz' (one opcode per name component, each
    # possibly preceded by name reference extension).
    (
     (?:
       (?:(?:s).)*
       (?:sÈ).
     )*
    )
 
    # Load however many arguments it takes. These (for now) must all be constants.
    # Again, code with >256 constants may need extended enumeration.
    (
      (?:
        (?:(?:s).)*
        (?:sÉ).
      )*
    )
 
    # Call the function. If opcode is CALL_FUNCTION_EX, the parameter are flags. For other opcodes, the parameter
    # is the argument count (which may be > 256).
    (
      (?:(?:s).)*
      (?:s    ).
    )
s:(
    # Arbitrary number of EXTENDED_ARG pairs.
    (?:(?:sG).)*
 
    # Followed by some other instruction (usually a LOAD).
    [^s].
)Ú extended_argscó:—tj|ddd…d«S)aQ
    Unpack the (extended) integer used to reference names or constants.
 
    The input should be a bytecode snippet of the following form::
 
        EXTENDED_ARG    ?      # Repeated 0-4 times.
        LOAD_xxx        ?      # Any of LOAD_NAME/LOAD_CONST/LOAD_METHOD/...
 
    Each ? byte combined together gives the number we want.
    rNrÚbig)ÚintÚ
from_bytes)r0s r Úextended_argumentsr5Ûs €ô >‰>˜-¨¨¨1¨Ñ-¨uÓ 5Ð5rÚrawÚcodeÚreturncól—t|«}|dtdk(r|j|S|dtdk(r|j|Stj
r!|dtdk(r|j |dz    Stjr!|dtdk(r|j |dz    Stjr|dtdk(r|Stjr|dtdk(r|j|Stjr|dtd    k(r|j|S|j |S)
z3
    Parse an (extended) LOAD_xxx instruction.
    éþÿÿÿÚ    LOAD_FASTÚ
LOAD_CONSTÚ LOAD_GLOBALrÚ    LOAD_ATTRÚLOAD_SMALL_INTÚLOAD_CONST_IMMORTALÚLOAD_FAST_BORROW)    r5r Ú co_varnamesÚ    co_constsrÚis_py311Úco_namesÚis_py312Úis_py314)r6r7Úindexs   r ÚloadrIés&€ô
˜sÓ #€Eð
 ˆ2w”%˜ Ñ$Ò$à×Ñ Ñ&Ð&à
ˆ2w”%˜ Ñ%Ò%à~‰~˜eÑ$Ð$ä ‡‚˜3˜r™7¤e¨MÑ&:Ò:à}‰}˜U a™ZÑ(Ð(Ü ‡‚˜3˜r™7¤e¨KÑ&8Ò8à}‰}˜U a™ZÑ(Ð(Ü ‡‚˜3˜r™7¤eÐ,<Ñ&=Ò=àˆ Ü ‡‚˜3˜r™7¤eÐ,AÑ&BÒBð~‰~˜eÑ$Ð$Ü ‡‚˜3˜r™7¤eÐ,>Ñ&?Ò?ð×Ñ Ñ&Ð&à =‰=˜Ñ Ðrcóf—tj|«Dcgc]}t||«‘Œc}Scc}w)zµ
    Parse multiple consecutive LOAD_xxx instructions. Or load() in a for loop.
 
    May be used to unpack a function's parameters or nested attributes ``(foo.bar.pop.whack)``.
    )Ú_extended_arg_bytecodeÚfindallrI)r6r7Úis   r ÚloadsrNs)€ô $:×#AÑ#AÀ#Ó#FÖ G˜aŒDDMÒ GÐGùÒ Gs˜.cóà—g}tt|j«D]Î}|j«\}}}}t    ||«}t ||«}dj |g|z«}t ||«}|dtdk(rBt|«}|dk7rŒqt|«dk7st|dt«sŒ“t|d«}nt|«}    |    t|«k7rŒ¼|j||f«ŒÐ|S)zJ
    Scan a code object for all function calls on constant arguments.
    ú.rÚCALL_FUNCTION_EXr)r Ú_call_function_bytecodeÚco_codeÚgroupsrIrNÚjoinr r5ÚlenrÚtupleÚlistÚappend)
r7Úoutr#Ú function_rootÚmethodsÚargsÚ function_callÚfunctionrÚ    arg_counts
          r Úfunction_callsrasõ€ð
€CäÔ1°4·<±<Ó@ò%ˆØ6;·l±l³nÑ3ˆ w  mô ˜]¨DÓ1ˆ ܘ Ó&ˆØ—8‘8˜]˜O¨gÑ5Ó6ˆäT˜4Ó ˆØ ˜Ñ œuÐ%7Ñ8Ò 8Ü& }Ó5ˆEؘŠzàô4‹y˜AŠ~¤Z°°Q±¼Ô%?ØÜ˜˜Q™“=‰Dä*¨=Ó9ˆIàœC ›IÒ%àà 
‰
H˜dÐ#Õ$ð;%ð> €JrÚsearchcóŠ—|€i}||vr:||«||<|jD] }t|t«sŒt|||«Œ"|S)zm
    Apply a search function to a code object, recursing into child code objects (function definitions).
    )rCrrÚsearch_recursively)rbr7Ú_memoÚconsts    r rdrdBsS€ð €}ØˆØ 5ÑÙ˜T“lˆˆd‰ Ø—^‘^ò    9ˆEܘ%¤Õ*Ü" 6¨5°%Õ8ð    9ð €Lrcó"—tt|«S)z‘
    Scan a code object for function calls on constant arguments, recursing into function definitions and bodies of
    comprehension loops.
    )rdra)r7s r Úrecursive_function_callsrhPs€ô
œn¨dÓ 3Ð3rÚ    full_namec#ójK—|jd«}|rdj|«–—|dd}|rŒyy­w)aÏList possible aliases of a fully qualified Python name.
 
        >>> list(any_alias("foo.bar.wizz"))
        ['foo.bar.wizz', 'bar.wizz', 'wizz']
 
    This crudely allows us to capture uses of wizz() under any of
    ::
        import foo
        foo.bar.wizz()
    ::
        from foo import bar
        bar.wizz()
    ::
        from foo.bar import wizz
        wizz()
 
    However, it will fail for any form of aliases and quite likely find false matches.
    rPrN)ÚsplitrU)riÚpartss  r Ú    any_aliasrmXs7èø€ð& O‰O˜CÓ  €EÙ
؏h‰hu‹oÒØab    ˆô ùs‚.3±3r%)*Ú__doc__Údisr    ÚtypesrÚtypingrÚ PyInstallerrÚgetattrr ÚstrrÚVERBOSEÚDOTALLr rr rDÚ_OPCODES_EXTENDED_ARGÚ_OPCODES_EXTENDED_ARG2Ú_OPCODES_FUNCTION_GLOBALÚ_OPCODES_FUNCTION_LOADÚ_OPCODES_FUNCTION_ARGSÚ_OPCODES_FUNCTION_CALLrrFr,rGÚis_py313rRrKr5rIrXrNraÚcallableÚdictrdrhrmr&rr ú<module>r€sÏðñó" Û    ÝÝåñ
    \ 3§9¡9Ó-€ð(˜Só(ð*,¯©°b·i±iÑ)?ñ,˜Eó,ð,gð uóðB‡‚à.ÐØ2ÐØHÐØ:ÐØ-ÐØQÐóà    Šð
DÐØCÐØHÐØ:ÐØ-ÐØ=Ðñ
!/Ð/BÓ CÐó?ð
/ÐØ2ÐØ ‡‚à#_Ñ à#LРØ,ÐØ ‡‚ð"YÑà!1ÐØ:Ðð‡‚Ù$2Ð3UÓ$VÐ!ó    Cñ%3Ð3FÓ$GÐ!ò    Cñ
)ðð
'ñ 'ð
*ñ ð *ñ *ð -ñ ð)ñ )ð,ñð'ñ'ð*ñð,*ñ-*ð,-ñ-ð.(ñ/(ð.+ñ/ð<(ñ=(ð<+ ñ= ð>&ñ?&ð>)ñ?!ó#ÐñL(ðà%ñ&ð)
ñ
ð
#ñ #ð
&ñ óÐð 6 eó 6ð$ ˆeð$ ˜8ð$ ¨ó$ ðNHˆuðH˜HðH¨óHð&˜ð& dó&ñR ˜xð ¨xð Èó ð4 8ð4°ó4ð˜ôr