zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
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
U
W±d 1ã@sÎdZddlZddlZddlmZddlmZddlmZe    edej
ƒZ
e dœdd    „Z ej ejBfed
œd d „Zeed œdd„Zejs¦dZeZdZdZdZdZdd„ZnPejsÎdZeZdZdZdZdZdd„Zn(dZdZdZdZdZdZedƒZdd„Zeded ed!ed"ed#ed$ed%ed&ed'ƒZed(ed)ed*ƒZed+œd,d-„Zeee d.œd/d0„Zeee d.œd1d2„Z!ee d3œd4d5„Z"d>e#ee$d6œd7d8„Z%ee$d3œd9d:„Z&e d;œd<d=„Z'dS)?aX
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)ÚcompatZ
_all_opmap©ÚxcCst tt|gƒ¡S)zG
    Get a regex-escaped opcode byte from its human readable name.
    )ÚreÚescapeÚbytesÚopmapr©r úRd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\PyInstaller/depend/bytecode.pyÚ_instruction_to_regex sr )ÚpatterncCs.t|tƒst‚t ddd„|¡}tj||d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+)`cSst|d ¡ƒS)Né)r Údecode)Úmr r r Ú<lambda>7óz bytecode_regex.<locals>.<lambda>)Úflags)Ú
isinstancer    ÚAssertionErrorrÚsubÚcompile)rrr r r Úbytecode_regex's ýr)rÚstringccsbt|tƒst‚t|ƒ}| |¡}|D]4}| ¡ddkr@|Vq$| || ¡d¡}q q$q^q dS)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    rÚ_cleanup_bytecode_stringÚfinditerÚstart)rrÚmatchesÚmatchr r r r=s
rs`EXTENDED_ARG`s%`LOAD_NAME`|`LOAD_GLOBAL`|`LOAD_FAST`s `LOAD_ATTR`s `LOAD_CONST`s"`CALL_FUNCTION`|`CALL_FUNCTION_EX`cCs|S©Nr ©Úbytecoder r r rfsrs`LOAD_ATTR`|`LOAD_METHOD`s0`CALL_FUNCTION`|`CALL_METHOD`|`CALL_FUNCTION_EX`cCs|Sr!r r"r r r rqss#`EXTENDED_ARG`|`EXTENDED_ARG_QUICK`s"`EXTENDED_ARG``EXTENDED_ARG_QUICK`s`PRECALL`|`CALL_FUNCTION_EX`s(`CACHE`.)|(..)cCs t d|¡S)Ns\2)Ú_cache_instruction_filterrr"r r r r„ss¼
    # 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].
)©Z extended_argscCst |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_bytesr%r r r Úextended_argumentsºs r))ÚrawÚcodeÚreturncCsjt|ƒ}|dtdkr"|j|S|dtdkr<|j|S|dtdkr`tjr`|j|d?S|j|S)z3
    Parse an (extended) LOAD_xxx instruction.
    éþÿÿÿÚ    LOAD_FASTÚ
LOAD_CONSTÚ LOAD_GLOBALr)r)r
Ú co_varnamesÚ    co_constsrÚis_py311Úco_names)r*r+Úindexr r r ÚloadÈs
 
r6cs‡fdd„t |¡DƒS)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)``.
    csg|]}t|ˆƒ‘qSr )r6)Ú.0Úi©r+r r Ú
<listcomp>æszloads.<locals>.<listcomp>)Ú_extended_arg_bytecodeÚfindall)r*r+r r9r Úloadsàsr=)r+r,c
CsÈg}tt|jƒD]²}| ¡\}}}}t||ƒ}t||ƒ}d |g|¡}t||ƒ}|dtdkržt|ƒ}|dkrtqt    |ƒdkst
|dt ƒsqt |dƒ}nt|ƒ}    |    t    |ƒkr´q|  ||f¡q|S)zJ
    Scan a code object for all function calls on constant arguments.
    Ú.rÚCALL_FUNCTION_EXr)rÚ_call_function_bytecodeÚco_codeÚgroupsr6r=Újoinr
r)ÚlenrÚtupleÚlistÚappend)
r+Úoutr Z function_rootÚmethodsÚargsZ function_callÚfunctionrZ    arg_countr r r Úfunction_callsés&
 
 
 rL)Úsearchr+r,cCsF|dkr i}||krB||ƒ||<|jD]}t|tƒr&t|||ƒq&|S)zm
    Apply a search function to a code object, recursing into child code objects (function definitions).
    N)r2rrÚsearch_recursively)rMr+Z_memoÚconstr r r rNs 
 
rNcCs
tt|ƒS)z‘
    Scan a code object for function calls on constant arguments, recursing into function definitions and bodies of
    comprehension loops.
    )rNrLr9r r r Úrecursive_function_calls srP)Ú    full_nameccs,| d¡}|r(d |¡V|dd…}q
dS)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.
    r>rN)ÚsplitrC)rQÚpartsr r r Ú    any_alias(s
 rT)N)(Ú__doc__ÚdisrÚtypesrÚtypingrZ PyInstallerrÚgetattrr
Ústrr ÚVERBOSEÚDOTALLr    rrZis_py37Z_OPCODES_EXTENDED_ARGZ_OPCODES_EXTENDED_ARG2Z_OPCODES_FUNCTION_GLOBALZ_OPCODES_FUNCTION_LOADZ_OPCODES_FUNCTION_ARGSZ_OPCODES_FUNCTION_CALLrr3r$r@r;r)r6rFr=rLÚcallableÚdictrNrPrTr r r r Ú<module>s¬   !
 
ûûúú ó óòòêêééââááÿ&þþûûÿ     )