1
+ # Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2
+ # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3
+ #
4
+ # The Universal Permissive License (UPL), Version 1.0
5
+ #
6
+ # Subject to the condition set forth below, permission is hereby granted to any
7
+ # person obtaining a copy of this software, associated documentation and/or
8
+ # data (collectively the "Software"), free of charge and under any and all
9
+ # copyright rights in the Software, and any and all patent rights owned or
10
+ # freely licensable by each licensor hereunder covering either (i) the
11
+ # unmodified Software as contributed to or provided by such licensor, or (ii)
12
+ # the Larger Works (as defined below), to deal in both
13
+ #
14
+ # (a) the Software, and
15
+ #
16
+ # (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17
+ # one is included with the Software each a "Larger Work" to which the Software
18
+ # is contributed by such licensors),
19
+ #
20
+ # without restriction, including without limitation the rights to copy, create
21
+ # derivative works of, display, perform, and distribute the Software and make,
22
+ # use, sell, offer for sale, import, export, have made, and have sold the
23
+ # Software and the Larger Work(s), and to sublicense the foregoing rights on
24
+ # either these or other terms.
25
+ #
26
+ # This license is subject to the following condition:
27
+ #
28
+ # The above copyright notice and either this complete permission notice or at a
29
+ # minimum a reference to the UPL must be included in all copies or substantial
30
+ # portions of the Software.
31
+ #
32
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38
+ # SOFTWARE.
39
+
40
+ import sys
41
+ import os
42
+ import unittest
43
+ import time
44
+ import importlib
45
+
46
+ import zipimport
47
+ from zipimport import ZipImportError
48
+
49
+
50
+ from test import support
51
+ from zipfile import ZipFile , ZipInfo , ZIP_STORED , ZIP_DEFLATED
52
+
53
+
54
+ test_src = """\
55
+ def get_name():
56
+ return __name__
57
+ def get_file():
58
+ return __file__
59
+ """
60
+
61
+ ZIP_FILE_NAME = 'testzipfile.zip'
62
+ DIR_PATH = os .path .dirname (os .path .realpath (__file__ ))
63
+ ZIP_PATH = os .path .join (DIR_PATH , ZIP_FILE_NAME )
64
+ ZIP_ABS_PATH = os .path .abspath (ZIP_PATH );
65
+
66
+
67
+ class ZipImportBaseTestCase (unittest .TestCase ):
68
+
69
+ def setUp (self ):
70
+ zipimport ._zip_directory_cache .clear ()
71
+ self .path = sys .path [:]
72
+ self .meta_path = sys .meta_path [:]
73
+ self .path_hooks = sys .path_hooks [:]
74
+ sys .path_importer_cache .clear ()
75
+ self .modules_before = support .modules_setup ()
76
+
77
+ def tearDown (self ):
78
+ sys .path [:] = self .path
79
+ sys .meta_path [:] = self .meta_path
80
+ sys .path_hooks [:] = self .path_hooks
81
+ sys .path_importer_cache .clear ()
82
+ support .modules_cleanup (* self .modules_before )
83
+
84
+ class BasicZipImportTests (ZipImportBaseTestCase ):
85
+
86
+ def setUp (self ):
87
+ ZipImportBaseTestCase .setUp (self )
88
+ self .z = zipimport .zipimporter (ZIP_PATH )
89
+
90
+
91
+ def test_zipimporter_attribute (self ):
92
+ self .assertTrue (self .z .prefix == "" )
93
+ self .assertTrue (self .z .archive == ZIP_ABS_PATH )
94
+ self .assertTrue (type (self .z ._files ) is dict )
95
+ self .assertTrue (self .z ._files ["MyTestModule.py" ] is not None )
96
+ self .assertTrue (self .z ._files ["empty.txt" ] is not None )
97
+ self .assertTrue (self .z ._files ["packageA/moduleC.py" ] is not None )
98
+ self .assertTrue (self .z ._files ["cesta/moduleA.py" ] is not None )
99
+
100
+ def test_create_zipimport_from_string (self ):
101
+ zipimport ._zip_directory_cache .clear ()
102
+ z = zipimport .zipimporter (ZIP_PATH )
103
+ self .assertTrue (zipimport ._zip_directory_cache [ZIP_ABS_PATH ] is not None )
104
+
105
+ def test_create_zipimport_from_bytes (self ):
106
+ zipimport ._zip_directory_cache .clear ()
107
+ a = bytes (ZIP_PATH , 'UTF-8' )
108
+ z = zipimport .zipimporter (a )
109
+ self .assertTrue (zipimport ._zip_directory_cache [os .path .abspath (ZIP_PATH )] is not None )
110
+
111
+ def test_create_zipimport_from_pathlike (self ):
112
+ class MyPath ():
113
+ def __init__ (self , path ):
114
+ self .value = path
115
+ def __fspath__ (self ):
116
+ return self .value
117
+
118
+ zipimport ._zip_directory_cache .clear ()
119
+ mp = MyPath (ZIP_PATH )
120
+ z = zipimport .zipimporter (mp )
121
+ self .assertTrue (zipimport ._zip_directory_cache [os .path .abspath (ZIP_PATH )] is not None )
122
+
123
+ def test_zipimporter_find_module (self ):
124
+ self .assertTrue (self .z is self .z .find_module ("MyTestModule" ))
125
+ self .assertTrue (self .z is self .z .find_module ("packageA" ))
126
+ self .assertTrue (self .z is self .z .find_module ("packageA/moduleC" ))
127
+ self .assertTrue (None is self .z .find_module ("packageA.moduleC" ))
128
+ self .assertTrue (self .z is self .z .find_module ("cesta/moduleA" ))
129
+
130
+ def test_zipimporter_get_code (self ):
131
+ self .assertTrue (self .z .get_code ("MyTestModule" ).co_filename .endswith ("MyTestModule.py" ))
132
+ self .assertTrue (self .z .get_code ("packageA" ).co_filename .endswith ("packageA/__init__.py" ))
133
+ self .assertTrue (self .z .get_code ("packageA/moduleC" ).co_filename .endswith ("packageA/moduleC.py" ))
134
+ self .assertTrue (self .z .get_code ("cesta/moduleA" ).co_filename .endswith ("cesta/moduleA.py" ))
135
+ self .assertRaises (ZipImportError , self .z .get_code , "wrongname" )
136
+ self .assertRaises (ZipImportError , self .z .get_code , "" )
137
+
138
+ def test_zipimporter_get_data (self ):
139
+ self .assertTrue (type (self .z .get_data ("MyTestModule.py" )) is bytes )
140
+ self .assertTrue (type (self .z .get_data ("packageA/moduleC.py" )) is bytes )
141
+ self .assertTrue (type (self .z .get_data ("cesta/moduleA.py" )) is bytes )
142
+ self .assertRaises (OSError , self .z .get_data , "" )
143
+ self .assertRaises (OSError , self .z .get_data , "MyTestModule" )
144
+ self .assertRaises (OSError , self .z .get_data , "packageA" )
145
+ self .assertTrue (type (self .z .get_data (ZIP_ABS_PATH + "/MyTestModule.py" )) is bytes )
146
+ self .assertTrue (type (self .z .get_data (ZIP_ABS_PATH + "/packageA/moduleC.py" )) is bytes )
147
+ self .assertTrue (type (self .z .get_data (ZIP_ABS_PATH + "/cesta/moduleA.py" )) is bytes )
148
+ self .assertTrue (type (self .z .get_data (ZIP_ABS_PATH + "/read.me" )) is bytes )
149
+ self .assertTrue (type (self .z .get_data ("empty.txt" )) is bytes )
150
+ self .assertRaises (OSError , self .z .get_data , "/empty.txt" )
151
+
152
+ def test_zipimporter_get_filename (self ):
153
+ self .assertEqual (self .z .get_filename ("packageA" ), ZIP_ABS_PATH + "/packageA/__init__.py" )
154
+ self .assertEqual (self .z .get_filename ("MyTestModule" ), ZIP_ABS_PATH + "/MyTestModule.py" )
155
+ self .assertRaises (ZipImportError , self .z .get_filename , "empty.txt" )
156
+ self .assertRaises (ZipImportError , self .z .get_filename , "empty" )
157
+
158
+ def test_zipimporter_get_source (self ):
159
+ self .assertTrue (type (self .z .get_source ("MyTestModule" )) is str )
160
+ self .assertTrue (type (self .z .get_source ("packageA" )) is str )
161
+ self .assertTrue (type (self .z .get_source ("packageA/moduleC" )) is str )
162
+ self .assertRaises (ZipImportError , self .z .get_source , "packageA.moduleC" )
163
+
164
+ def test_zipimporter_is_package (self ):
165
+ self .assertTrue (self .z .is_package ("packageA" ))
166
+ self .assertFalse (self .z .is_package ("MyTestModule" ))
167
+ self .assertFalse (self .z .is_package ("packageA/moduleC" ))
168
+ self .assertRaises (ZipImportError , self .z .is_package , "empty" )
169
+ self .assertRaises (ZipImportError , self .z .is_package , "cesta" )
170
+ self .assertRaises (ZipImportError , self .z .is_package , "packageA.moduleC" )
171
+
172
+ def test_zipimporter_load_module (self ):
173
+ self .assertTrue (self .z .load_module ("MyTestModule" ).__loader__ is self .z )
174
+ self .assertTrue (self .z .load_module ("packageA" ).__loader__ is self .z )
175
+ self .assertTrue (self .z .load_module ("packageA/moduleC" ).__loader__ is self .z )
176
+ self .assertTrue (self .z .load_module ("cesta/moduleA" ).__loader__ is self .z )
177
+ self .assertRaises (ZipImportError , self .z .load_module , "packageA.moduleC" )
178
+
179
+ class ZipImportWithPrefixTests (ZipImportBaseTestCase ):
180
+
181
+ def setUp (self ):
182
+ ZipImportBaseTestCase .setUp (self )
183
+ self .z = zipimport .zipimporter (ZIP_PATH + "/cesta" )
184
+
185
+ def tearDown (self ):
186
+ zipimport ._zip_directory_cache .clear ()
187
+
188
+ def test_zipimporter_with_prefix_attribute (self ):
189
+ self .assertTrue (self .z .prefix == "cesta/" )
190
+ self .assertTrue (self .z .archive == ZIP_ABS_PATH )
191
+ self .assertTrue (type (self .z ._files ) is dict )
192
+ self .assertTrue (self .z ._files ["MyTestModule.py" ] is not None )
193
+ self .assertTrue (self .z ._files ["empty.txt" ] is not None )
194
+ self .assertTrue (self .z ._files ["packageA/moduleC.py" ] is not None )
195
+ self .assertTrue (self .z ._files ["cesta/moduleA.py" ] is not None )
196
+
197
+ def test_zipimporter_with_prefix_find_module (self ):
198
+ self .assertTrue (None is self .z .find_module ("MyTestModule" ))
199
+ self .assertTrue (None is self .z .find_module ("packageA" ))
200
+ self .assertTrue (None is self .z .find_module ("packageA/moduleC" ))
201
+ self .assertTrue (None is self .z .find_module ("packageA.moduleC" ))
202
+ self .assertTrue (self .z is self .z .find_module ("moduleA" ))
203
+
204
+ class ImportTests (ZipImportBaseTestCase ):
205
+
206
+ def setUp (self ):
207
+ ZipImportBaseTestCase .setUp (self )
208
+ sys .path .insert (0 , ZIP_PATH )
209
+
210
+ def test_module_import (self ):
211
+ m = importlib .import_module ("MyTestModule" )
212
+ self .assertTrue (m .get_file () == ZIP_ABS_PATH + "/MyTestModule.py" )
213
+ p = importlib .import_module ("packageA.moduleC" )
214
+ self .assertTrue (p .get_file () == ZIP_ABS_PATH + "/packageA/moduleC.py" )
215
+
216
+
0 commit comments