MCPcopy Index your code
hub / github.com/python/cpython / testAFakeZlib

Method testAFakeZlib

Lib/test/test_zipimport.py:176–226  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

174 *modules) + expected_ext)
175
176 def testAFakeZlib(self):
177 #
178 # This could cause a stack overflow before: importing zlib.py
179 # from a compressed archive would cause zlib to be imported
180 # which would find zlib.py in the archive, which would... etc.
181 #
182 # This test *must* be executed first: it must be the first one
183 # to trigger zipimport to import zlib (zipimport caches the
184 # zlib.decompress function object, after which the problem being
185 # tested here wouldn't be a problem anymore...
186 # (Hence the 'A' in the test method name: to make it the first
187 # item in a list sorted by name, like
188 # unittest.TestLoader.getTestCaseNames() does.)
189 #
190 # This test fails on platforms on which the zlib module is
191 # statically linked, but the problem it tests for can't
192 # occur in that case (builtin modules are always found first),
193 # so we'll simply skip it then. Bug #765456.
194 #
195 if self.compression == ZIP_DEFLATED:
196 mod_name = "zlib"
197 if zipimport._zlib_decompress: # validate attr name
198 # reset the cached import to avoid test order dependencies
199 zipimport._zlib_decompress = None # reset cache
200 elif self.compression == ZIP_ZSTANDARD:
201 mod_name = "_zstd"
202 if zipimport._zstd_decompressor_class: # validate attr name
203 # reset the cached import to avoid test order dependencies
204 zipimport._zstd_decompressor_class = None
205 else:
206 mod_name = "zlib" # the ZIP_STORED case below
207
208 if mod_name in sys.builtin_module_names:
209 self.skipTest(f"{mod_name} is a builtin module")
210 if mod_name in sys.modules:
211 del sys.modules[mod_name]
212 files = {f"{mod_name}.py": test_src}
213 try:
214 self.doTest(".py", files, mod_name)
215 except ImportError:
216 if self.compression != ZIP_STORED:
217 # Expected - fake compression module can't decompress
218 pass
219 else:
220 self.fail("expected test to not raise ImportError for uncompressed")
221 else:
222 if self.compression == ZIP_STORED:
223 # Expected - no compression needed, so fake module works
224 pass
225 else:
226 self.fail("expected test to raise ImportError for compressed zip with fake compression module")
227
228 def testPy(self):
229 files = {TESTMOD + ".py": test_src}

Callers

nothing calls this directly

Calls 3

doTestMethod · 0.95
skipTestMethod · 0.80
failMethod · 0.45

Tested by

no test coverage detected