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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
U
X±d‹Vã@s°dZddlZddlZddlZddlZddlZddlZddlmZm    Z    ej
ej Z e e ƒZeeƒZejjrxejrxdd„Zndd„Zdd„ZGd    d
„d
ƒZGd d „d ƒZd d„ZdS)z8
PEP-302 and PEP-451 importers for frozen applications.
éN)ÚArchiveReadErrorÚZlibArchiveReadercGs tj ||¡tj d¡dS)NÚ
)ÚsysÚstderrÚwrite©ÚmsgÚa©r ú\d:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\PyInstaller/loader/pyimod02_importers.pyÚtrace'sr cGsdS©Nr rr r r r ,scCs8t |¡j}t |¡}tjddd}| | |d¡¡S)a
    Decode bytes representing source code and return the string. Universal newline support is used in the decoding.
    Based on CPython's implementation of the same functionality:
    https://github.com/python/cpython/blob/3.9/Lib/importlib/_bootstrap_external.py#L679-L688
    NT)ÚdecoderÚ    translater)ÚioÚBytesIOÚreadlineÚtokenizeÚdetect_encodingÚIncrementalNewlineDecoderÚdecode)Ú source_bytesÚsource_bytes_readlineÚencodingÚnewline_decoderr r r Ú_decode_source0s 
rc@sjeZdZdZdd„Zdd„Zdd„Zdd    „Zd
d „Zd d „Z    dd„Z
ddd„Z dd„Z dd„Z dd„ZdS)ÚFrozenImportera{
    Load bytecode of Python modules from the executable created by PyInstaller.
 
    Python bytecode is zipped and appended to the executable.
 
    NOTE: PYZ format cannot be replaced by zipimport module.
 
    The problem is that we have no control over zipimport; for instance, it does not work if the zip file is embedded
    into a PKG that is appended to an executable, like we create in one-file mode.
 
    This used to be PEP-302 finder and loader class for the ``sys.meta_path`` hook. A PEP-302 finder requires method
    find_module() to return loader class with method load_module(). However, both of these methods were deprecated in
    python 3.4 by PEP-451 (see below). Therefore, this class now provides only optional extensions to the PEP-302
    importer protocol.
 
    This is also a PEP-451 finder and loader class for the ModuleSpec type import system. A PEP-451 finder requires
    method find_spec(), a PEP-451 loader requires methods exec_module(), load_module() and (optionally) create_module().
    All these methods are implemented in this one class.
 
    To use this class just call:
        FrozenImporter.install()
    c    Cs†tjD]r}z>t|dd|_tj |¡t|jj ¡ƒ|_td|ƒWdSt    k
r`YqYqt
k
rvYqYqXqt dƒ‚dS)zY
        Load, unzip and initialize the Zip archive bundled with the executable.
        T)Z check_pymagicz!# PyInstaller: FrozenImporter(%s)NzCannot load frozen modules.) rÚpathrÚ _pyz_archiveÚremoveÚsetÚtocÚkeysr ÚIOErrorrÚ ImportError)ÚselfZ pyz_filepathr r r Ú__init__Ss
 
 zFrozenImporter.__init__c
Cs\||jkrLz|j |¡WStk
rH}ztd|ƒ|‚W5d}~XYqXXn td|ƒ‚dS©Nú+Loader FrozenImporter cannot handle module )r"rZis_pep420_namespace_packageÚ    Exceptionr%©r&ÚfullnameÚer r r Ú_is_pep420_namespace_packageus 
"z+FrozenImporter._is_pep420_namespace_packagec
Cs\||jkrLz|j |¡WStk
rH}ztd|ƒ|‚W5d}~XYqXXn td|ƒ‚dSr()r"rÚ
is_packager*r%r+r r r r/€s 
"zFrozenImporter.is_packagec
CsZz$|dkrtjdjWS|j |¡WStk
rT}ztd|ƒ|‚W5d}~XYnXdS)z|
        Get the code object associated with the module.
 
        ImportError should be raised if module not found.
        Ú__main__r)N)rÚmodulesZ _pyi_main_corÚextractr*r%r+r r r Úget_code‰s zFrozenImporter.get_codec    CsŠ||jkrz| |¡r|d7}tj t| dtj¡d¡}z(t|dƒ}|     ¡}W5QRXt
|ƒWSt k
rtYnXdSt d|ƒ‚dS)zò
        Method should return the source code for the module as a string.
        But frozen modules does not contain source code.
 
        Return None, unless the corresponding source file was explicitly collected to the filesystem.
        z    .__init__Ú.z.pyÚrbNzNo module named ) r"r/ÚosrÚjoinÚ
SYS_PREFIXÚreplaceÚsepÚopenÚreadrÚFileNotFoundErrorr%)r&r,ÚfilenameÚfprr r r Ú
get_source›s
 
 
zFrozenImporter.get_sourcec
CsZ| t¡st‚|td…}||jkr0|j |¡St|dƒ}| ¡W5QR£SQRXdS)až
        Returns the data as a string, or raises IOError if the "file" was not found. The data is always returned as if
        "binary" mode was used.
 
        This method is useful for getting resources with 'pkg_resources' that are bundled with Python modules in the
        PYZ archive.
 
        The 'path' argument is a path that can be constructed by munging module.__file__ (or pkg.__path__ items).
        Nr5)    Ú
startswithr8ÚAssertionErrorÚ SYS_PREFIXLENr"rr2r;r<)r&rr,r?r r r Úget_data³s
 
  zFrozenImporter.get_datacCsJ| |¡r(tj t| dtjj¡d¡}ntj t| dtjj¡d¡}|S)z¸
        This method should return the value that __file__ would be set to if the named module was loaded. If the module
        is not found, an ImportError should be raised.
        r4z __init__.pycz.pyc)r/r6rr7r8r9r:)r&r,r>r r r Ú get_filenameÈs    
zFrozenImporter.get_filenameNc CsPd}|dk    r–| d¡d}|D]r}| t¡s.q|td…}| tj¡}|sLq|ds`|dd…}| |¡d |¡}||j    krt
d|||ƒq–qd}|dkr¶||j    kr¶|}t
d|ƒ|dkrÌt
d|ƒdS|  |¡rt j |dd    d
}tj | |¡¡g|_|S| |¡}    | |¡}
t j |||
|    |d }d    |_|
rLtj | |¡¡g|_|S) aì
        PEP-451 finder.find_spec() method for the ``sys.meta_path`` hook.
 
        fullname     fully qualified name of the module
        path         None for a top-level module, or package.__path__ for
                     submodules or subpackages.
        target       unused by this Finder
 
        Finders are still responsible for identifying, and typically creating, the loader that should be used to load a
        module. That loader will now be stored in the module spec returned by find_spec() rather than returned directly.
        As is currently the case without the PEP-452, if a loader would be costly to create, that loader can be designed
        to defer the cost until later.
 
        Finders must return ModuleSpec objects when find_spec() is called. This new method replaces find_module() and
        find_loader() (in the PathEntryFinder case). If a loader does not have find_spec(), find_module() and
        find_loader() are used instead, for backward-compatibility.
        Nr4éÿÿÿÿréz9import %s as %s # PyInstaller PYZ (__path__ override: %s)zimport %s # PyInstaller PYZz# %s not found in PYZT)r/)r/ÚoriginÚ loader_state)ÚrsplitrAr8rCÚsplitr6r:Úappendr7r"r r.Ú_frozen_importlibÚ
ModuleSpecrÚdirnamerEÚsubmodule_search_locationsr/Ú has_location) r&r,rÚtargetZ
entry_nameÚmodnameÚpÚpartsÚspecrHZis_pkgr r r Ú    find_spec×sT
 
 
 
 
 
 
 
 
ú zFrozenImporter.find_speccCsdS)a†
        PEP-451 loader.create_module() method for the ``sys.meta_path`` hook.
 
        Loaders may also implement create_module() that will return a new module to exec. It may return None to indicate
        that the default module creation code should be used. One use case, though atypical, for create_module() is to
        provide a module that is a subclass of the builtin module type. Most loaders will not need to implement
        create_module().
 
        create_module() should properly handle the case where it is called more than once for the same spec/module. This
        may include returning None or raising ImportError.
        Nr )r&rVr r r Ú create_module1szFrozenImporter.create_modulecCsL|j}| |j¡}t|dƒs t‚|jdk    r<tj |j    ¡g|_
t ||j ƒdS)aô
        PEP-451 loader.exec_module() method for the ``sys.meta_path`` hook.
 
        Loaders will have a new method, exec_module(). Its only job is to "exec" the module and consequently populate
        the module's namespace. It is not responsible for creating or preparing the module object, nor for any cleanup
        afterward. It has no return value. exec_module() will be used during both loading and reloading.
 
        exec_module() should properly handle the case where it is called more than once. For some kinds of modules this
        may mean raising ImportError every time after the first time the method is called. This is particularly relevant
        for reloading, where some kinds of modules do not support in-place reloading.
        Ú__file__N) Ú__spec__r3rIÚhasattrrBrPr6rrOrYÚ__path__ÚexecÚ__dict__)r&ÚmodulerVÚbytecoder r r Ú exec_moduleBs  
    zFrozenImporter.exec_modulecCs
t||ƒS)zG
        Return importlib.resource-compatible resource reader.
        )ÚFrozenResourceReader)r&r,r r r Úget_resource_readerbsz"FrozenImporter.get_resource_reader)NN)Ú__name__Ú
__module__Ú __qualname__Ú__doc__r'r.r/r3r@rDrErWrXrarcr r r r r<s"     
Z rc@s@eZdZdZdd„Zdd„Zdd„Zdd    „Zd
d „Zd d „Z    dS)rbag    
    Resource reader for importlib.resources / importlib_resources support.
 
    Currently supports only on-disk resources (support for resources from the embedded archive is missing).
    However, this should cover the typical use cases (access to data files), as PyInstaller collects data files onto
    filesystem, and only .pyc modules are collected into embedded archive. One exception are resources collected from
    zipped eggs (which end up collected into embedded archive), but those should be rare anyway.
 
    When listing resources, source .py files will not be listed as they are not collected by default. Similarly,
    sub-directories that contained only .py files are not reconstructed on filesystem, so they will not be listed,
    either. If access to .py files is required for whatever reason, they need to be explicitly collected as data files
    anyway, which will place them on filesystem and make them appear as resources.
 
    For on-disk resources, we *must* return path compatible with pathlib.Path() in order to avoid copy to a temporary
    file, which might break under some circumstances, e.g., metpy with importlib_resources back-port, due to:
    https://github.com/Unidata/MetPy/blob/a3424de66a44bf3a92b0dcacf4dff82ad7b86712/src/metpy/plots/wx_symbols.py#L24-L25
    (importlib_resources tries to use 'fonts/wx_symbols.ttf' as a temporary filename suffix, which fails as it contains
    a separator).
 
    Furthermore, some packages expect files() to return either pathlib.Path or zipfile.Path, e.g.,
    https://github.com/tensorflow/datasets/blob/master/tensorflow_datasets/core/utils/resource_utils.py#L81-L97
    This makes implementation of mixed support for on-disk and embedded resources using importlib.abc.Traversable
    protocol rather difficult.
 
    So in order to maximize compatibility with unfrozen behavior, the below implementation is basically equivalent of
    importlib.readers.FileReader from python 3.10:
      https://github.com/python/cpython/blob/839d7893943782ee803536a47f1d4de160314f85/Lib/importlib/readers.py#L11
    and its underlying classes, importlib.abc.TraversableResources and importlib.abc.ResourceReader:
      https://github.com/python/cpython/blob/839d7893943782ee803536a47f1d4de160314f85/Lib/importlib/abc.py#L422
      https://github.com/python/cpython/blob/839d7893943782ee803536a47f1d4de160314f85/Lib/importlib/abc.py#L312
    cCs$||_t tj¡j| d¡Ž|_dS)Nr4)ÚimporterÚpathlibÚPathrÚ_MEIPASSÚjoinpathrKr)r&rhÚnamer r r r'‰szFrozenResourceReader.__init__cCs| ¡ |¡ d¡S)Nr5)Úfilesrlr;©r&Úresourcer r r Ú open_resourcesz"FrozenResourceReader.open_resourcecCst|j |¡ƒSr)Ústrrrlror r r Ú resource_pathsz"FrozenResourceReader.resource_pathcCs| ¡ |¡ ¡Sr)rnrlÚis_file)r&rr r r Ú is_resource“sz FrozenResourceReader.is_resourcecCsdd„| ¡ ¡DƒS)Ncss|] }|jVqdSr)rm)Ú.0Úitemr r r Ú    <genexpr>—sz0FrozenResourceReader.contents.<locals>.<genexpr>)rnÚiterdir©r&r r r Úcontents–szFrozenResourceReader.contentscCs|jSr)rrzr r r rn™szFrozenResourceReader.filesN)
rdrerfrgr'rqrsrur{rnr r r r rbisrbcCs¾tƒ}tj |¡tjD](}t|dƒr|jdkrtj |¡qBqg}ttjƒD]2}t|ddƒdkrPtj |¡||krP| |¡qPtj     t|ƒ¡z|tj
d_ Wnt k
r¸YnXdS)a 
    Install FrozenImporter class and other classes into the import machinery.
 
    This function installs the FrozenImporter class into the import machinery of the running process. The importer is
    added to sys.meta_path. It could be added to sys.path_hooks, but sys.meta_path is processed by Python before
    looking at sys.path!
 
    The order of processing import hooks in sys.meta_path:
 
    1. built-in modules
    2. modules from the bundled ZIP archive
    3. C extension modules
    4. Modules from sys.path
    rdÚWindowsRegistryFinderNÚ
PathFinderr0) rrÚ    meta_pathrLr[rdr ÚreversedÚgetattrÚextendr1Ú
__loader__r*)ZfimprwZ pathFindersr r r Úinstalls" 
   rƒ)rgrr6rirrrMZpyimod01_archiverrrkr:r8ÚlenrCÚtypeZimp_new_moduleÚflagsÚverboserr rrrbrƒr r r r Ú<module> s&     
 /4