././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1687859820.7167187 zombie-imp-0.0.2/0000755000175000017500000000000014446531155014131 5ustar00pviktoripviktori././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1687768351.0 zombie-imp-0.0.2/LICENSE0000644000175000017500000003316014446246437015147 0ustar00pviktoripviktoriA. HISTORY OF THE SOFTWARE ========================== Python was created in the early 1990s by Guido van Rossum at Stichting Mathematisch Centrum (CWI, see https://www.cwi.nl) in the Netherlands as a successor of a language called ABC. Guido remains Python's principal author, although it includes many contributions from others. In 1995, Guido continued his work on Python at the Corporation for National Research Initiatives (CNRI, see https://www.cnri.reston.va.us) in Reston, Virginia where he released several versions of the software. In May 2000, Guido and the Python core development team moved to BeOpen.com to form the BeOpen PythonLabs team. In October of the same year, the PythonLabs team moved to Digital Creations, which became Zope Corporation. In 2001, the Python Software Foundation (PSF, see https://www.python.org/psf/) was formed, a non-profit organization created specifically to own Python-related Intellectual Property. Zope Corporation was a sponsoring member of the PSF. All Python releases are Open Source (see https://opensource.org for the Open Source Definition). Historically, most, but not all, Python releases have also been GPL-compatible; the table below summarizes the various releases. Release Derived Year Owner GPL- from compatible? (1) 0.9.0 thru 1.2 1991-1995 CWI yes 1.3 thru 1.5.2 1.2 1995-1999 CNRI yes 1.6 1.5.2 2000 CNRI no 2.0 1.6 2000 BeOpen.com no 1.6.1 1.6 2001 CNRI yes (2) 2.1 2.0+1.6.1 2001 PSF no 2.0.1 2.0+1.6.1 2001 PSF yes 2.1.1 2.1+2.0.1 2001 PSF yes 2.1.2 2.1.1 2002 PSF yes 2.1.3 2.1.2 2002 PSF yes 2.2 and above 2.1.1 2001-now PSF yes Footnotes: (1) GPL-compatible doesn't mean that we're distributing Python under the GPL. All Python licenses, unlike the GPL, let you distribute a modified version without making your changes open source. The GPL-compatible licenses make it possible to combine Python with other software that is released under the GPL; the others don't. (2) According to Richard Stallman, 1.6.1 is not GPL-compatible, because its license has a choice of law clause. According to CNRI, however, Stallman's lawyer has told CNRI's lawyer that 1.6.1 is "not incompatible" with the GPL. Thanks to the many outside volunteers who have worked under Guido's direction to make these releases possible. B. TERMS AND CONDITIONS FOR ACCESSING OR OTHERWISE USING PYTHON =============================================================== Python software and documentation are licensed under the Python Software Foundation License Version 2. Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Version 2 and the Zero-Clause BSD license. Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2 -------------------------------------------- 1. This LICENSE AGREEMENT is between the Python Software Foundation ("PSF"), and the Individual or Organization ("Licensee") accessing and otherwise using this software ("Python") in source or binary form and its associated documentation. 2. Subject to the terms and conditions of this License Agreement, PSF hereby grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce, analyze, test, perform and/or display publicly, prepare derivative works, distribute, and otherwise use Python alone or in any derivative version, provided, however, that PSF's License Agreement and PSF's notice of copyright, i.e., "Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Python Software Foundation; All Rights Reserved" are retained in Python alone or in any derivative version prepared by Licensee. 3. In the event Licensee prepares a derivative work that is based on or incorporates Python or any part thereof, and wants to make the derivative work available to others as provided herein, then Licensee hereby agrees to include in any such work a brief summary of the changes made to Python. 4. PSF is making Python available to Licensee on an "AS IS" basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON WILL NOT INFRINGE ANY THIRD PARTY RIGHTS. 5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON, OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. 6. This License Agreement will automatically terminate upon a material breach of its terms and conditions. 7. Nothing in this License Agreement shall be deemed to create any relationship of agency, partnership, or joint venture between PSF and Licensee. This License Agreement does not grant permission to use PSF trademarks or trade name in a trademark sense to endorse or promote products or services of Licensee, or any third party. 8. By copying, installing or otherwise using Python, Licensee agrees to be bound by the terms and conditions of this License Agreement. BEOPEN.COM LICENSE AGREEMENT FOR PYTHON 2.0 ------------------------------------------- BEOPEN PYTHON OPEN SOURCE LICENSE AGREEMENT VERSION 1 1. This LICENSE AGREEMENT is between BeOpen.com ("BeOpen"), having an office at 160 Saratoga Avenue, Santa Clara, CA 95051, and the Individual or Organization ("Licensee") accessing and otherwise using this software in source or binary form and its associated documentation ("the Software"). 2. Subject to the terms and conditions of this BeOpen Python License Agreement, BeOpen hereby grants Licensee a non-exclusive, royalty-free, world-wide license to reproduce, analyze, test, perform and/or display publicly, prepare derivative works, distribute, and otherwise use the Software alone or in any derivative version, provided, however, that the BeOpen Python License is retained in the Software, alone or in any derivative version prepared by Licensee. 3. BeOpen is making the Software available to Licensee on an "AS IS" basis. BEOPEN MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, BEOPEN MAKES NO AND DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE WILL NOT INFRINGE ANY THIRD PARTY RIGHTS. 4. BEOPEN SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF THE SOFTWARE FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE SOFTWARE, OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. 5. This License Agreement will automatically terminate upon a material breach of its terms and conditions. 6. This License Agreement shall be governed by and interpreted in all respects by the law of the State of California, excluding conflict of law provisions. Nothing in this License Agreement shall be deemed to create any relationship of agency, partnership, or joint venture between BeOpen and Licensee. This License Agreement does not grant permission to use BeOpen trademarks or trade names in a trademark sense to endorse or promote products or services of Licensee, or any third party. As an exception, the "BeOpen Python" logos available at http://www.pythonlabs.com/logos.html may be used according to the permissions granted on that web page. 7. By copying, installing or otherwise using the software, Licensee agrees to be bound by the terms and conditions of this License Agreement. CNRI LICENSE AGREEMENT FOR PYTHON 1.6.1 --------------------------------------- 1. This LICENSE AGREEMENT is between the Corporation for National Research Initiatives, having an office at 1895 Preston White Drive, Reston, VA 20191 ("CNRI"), and the Individual or Organization ("Licensee") accessing and otherwise using Python 1.6.1 software in source or binary form and its associated documentation. 2. Subject to the terms and conditions of this License Agreement, CNRI hereby grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce, analyze, test, perform and/or display publicly, prepare derivative works, distribute, and otherwise use Python 1.6.1 alone or in any derivative version, provided, however, that CNRI's License Agreement and CNRI's notice of copyright, i.e., "Copyright (c) 1995-2001 Corporation for National Research Initiatives; All Rights Reserved" are retained in Python 1.6.1 alone or in any derivative version prepared by Licensee. Alternately, in lieu of CNRI's License Agreement, Licensee may substitute the following text (omitting the quotes): "Python 1.6.1 is made available subject to the terms and conditions in CNRI's License Agreement. This Agreement together with Python 1.6.1 may be located on the internet using the following unique, persistent identifier (known as a handle): 1895.22/1013. This Agreement may also be obtained from a proxy server on the internet using the following URL: http://hdl.handle.net/1895.22/1013". 3. In the event Licensee prepares a derivative work that is based on or incorporates Python 1.6.1 or any part thereof, and wants to make the derivative work available to others as provided herein, then Licensee hereby agrees to include in any such work a brief summary of the changes made to Python 1.6.1. 4. CNRI is making Python 1.6.1 available to Licensee on an "AS IS" basis. CNRI MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, CNRI MAKES NO AND DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON 1.6.1 WILL NOT INFRINGE ANY THIRD PARTY RIGHTS. 5. CNRI SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON 1.6.1 FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 1.6.1, OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. 6. This License Agreement will automatically terminate upon a material breach of its terms and conditions. 7. This License Agreement shall be governed by the federal intellectual property law of the United States, including without limitation the federal copyright law, and, to the extent such U.S. federal law does not apply, by the law of the Commonwealth of Virginia, excluding Virginia's conflict of law provisions. Notwithstanding the foregoing, with regard to derivative works based on Python 1.6.1 that incorporate non-separable material that was previously distributed under the GNU General Public License (GPL), the law of the Commonwealth of Virginia shall govern this License Agreement only as to issues arising under or with respect to Paragraphs 4, 5, and 7 of this License Agreement. Nothing in this License Agreement shall be deemed to create any relationship of agency, partnership, or joint venture between CNRI and Licensee. This License Agreement does not grant permission to use CNRI trademarks or trade name in a trademark sense to endorse or promote products or services of Licensee, or any third party. 8. By clicking on the "ACCEPT" button where indicated, or by copying, installing or otherwise using Python 1.6.1, Licensee agrees to be bound by the terms and conditions of this License Agreement. ACCEPT CWI LICENSE AGREEMENT FOR PYTHON 0.9.0 THROUGH 1.2 -------------------------------------------------- Copyright (c) 1991 - 1995, Stichting Mathematisch Centrum Amsterdam, The Netherlands. All rights reserved. Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the name of Stichting Mathematisch Centrum or CWI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ZERO-CLAUSE BSD LICENSE FOR CODE IN THE PYTHON DOCUMENTATION ---------------------------------------------------------------------- Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1687859766.0 zombie-imp-0.0.2/MANIFEST.in0000644000175000017500000000006714446531066015673 0ustar00pviktoripviktoriinclude tox.ini recursive-include test_zombie_imp *.py ././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1687859820.7167187 zombie-imp-0.0.2/PKG-INFO0000644000175000017500000000253114446531155015227 0ustar00pviktoripviktoriMetadata-Version: 2.1 Name: zombie-imp Version: 0.0.2 Summary: A copy of the `imp` module that was removed in Python 3.12 Maintainer-email: Petr Viktorin Project-URL: Homepage, https://github.com/encukou/zombie-imp Project-URL: Bug Tracker, https://github.com/encukou/zombie-imp/issues Classifier: Programming Language :: Python :: 3 Classifier: License :: OSI Approved :: Python Software Foundation License Classifier: Operating System :: OS Independent Requires-Python: >=3.7 Description-Content-Type: text/markdown License-File: LICENSE # zombie-imp ~~A particularly mischevious act of necromancy. That is,~~ A copy of the `imp` module that was removed in Python 3.12. Don't use this, it'll probably trick and bite you. # Usage Can be summoned by `import zombie_imp`. On Python versions where `imp` was banished, reanimate it using `import imp`. It promises (with a sneer) to be the same as before. Some functionality that was severed from `pkgutil` is interred in `zombie_imp.pkgutil`, ready for reattachment: - `ImpImporter` - `ImpLoader` # Development You want to help it? Think you'll be rewarded? Great! It loves gullible brains. Seriously, **run!** Find a project that needs this and port *that* to `importlib`. ## License The code was snatched from CPython, and is available under CPython's license (SPDX: `Python-2.0.1`). ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1687772439.0 zombie-imp-0.0.2/README.md0000644000175000017500000000145014446256427015416 0ustar00pviktoripviktori# zombie-imp ~~A particularly mischevious act of necromancy. That is,~~ A copy of the `imp` module that was removed in Python 3.12. Don't use this, it'll probably trick and bite you. # Usage Can be summoned by `import zombie_imp`. On Python versions where `imp` was banished, reanimate it using `import imp`. It promises (with a sneer) to be the same as before. Some functionality that was severed from `pkgutil` is interred in `zombie_imp.pkgutil`, ready for reattachment: - `ImpImporter` - `ImpLoader` # Development You want to help it? Think you'll be rewarded? Great! It loves gullible brains. Seriously, **run!** Find a project that needs this and port *that* to `importlib`. ## License The code was snatched from CPython, and is available under CPython's license (SPDX: `Python-2.0.1`). ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1687858107.0 zombie-imp-0.0.2/imp.py0000644000175000017500000000052514446525673015302 0ustar00pviktoripviktorifrom zombie_imp import * from zombie_imp.imp_3_11 import _fix_co_filename try: from zombie_imp.imp_3_11 import _frozen_module_names except ImportError: pass from zombie_imp.imp_3_11 import _ERR_MSG, _exec, _load, _builtin_from_name from zombie_imp.imp_3_11 import _HackedGetData, _LoadSourceCompatibility, _LoadCompiledCompatibility ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1687859766.0 zombie-imp-0.0.2/pyproject.toml0000644000175000017500000000127514446531066017053 0ustar00pviktoripviktori[build-system] requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] name = "zombie-imp" version = "0.0.2" maintainers = [ { name="Petr Viktorin", email="encukou@gmail.com" }, ] description = "A copy of the `imp` module that was removed in Python 3.12" readme = "README.md" requires-python = ">=3.7" classifiers = [ "Programming Language :: Python :: 3", "License :: OSI Approved :: Python Software Foundation License", "Operating System :: OS Independent", ] [project.urls] "Homepage" = "https://github.com/encukou/zombie-imp" "Bug Tracker" = "https://github.com/encukou/zombie-imp/issues" [tool.setuptools] packages = ["zombie_imp"] py-modules = ["imp"] ././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1687859820.7177188 zombie-imp-0.0.2/setup.cfg0000644000175000017500000000004614446531155015752 0ustar00pviktoripviktori[egg_info] tag_build = tag_date = 0 ././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1687859820.7137187 zombie-imp-0.0.2/test_zombie_imp/0000755000175000017500000000000014446531155017322 5ustar00pviktoripviktori././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1687858107.0 zombie-imp-0.0.2/test_zombie_imp/test_imp.py0000644000175000017500000005121114446525673021530 0ustar00pviktoripviktoriimport gc import importlib import importlib.util import os import os.path import py_compile import sys import test from test import support try: from test.support import import_helper except ImportError: from test import support as import_helper try: from test.support import os_helper except ImportError: from test import support as os_helper from test.support import script_helper try: from test.support import warnings_helper except ImportError: from test import support as warnings_helper import unittest import warnings import imp #!! this should be the only difference between test_imp and test_zombie_imp! import _imp orig__file__ = os.path.join(test.__path__[0], os.path.split(__file__)[-1]) OS_PATH_NAME = os.path.__name__ def requires_load_dynamic(meth): """Decorator to skip a test if not running under CPython or lacking imp.load_dynamic().""" meth = support.cpython_only(meth) return unittest.skipIf(getattr(imp, 'load_dynamic', None) is None, 'imp.load_dynamic() required')(meth) class LockTests(unittest.TestCase): """Very basic test of import lock functions.""" def verify_lock_state(self, expected): self.assertEqual(imp.lock_held(), expected, "expected imp.lock_held() to be %r" % expected) def testLock(self): LOOPS = 50 # The import lock may already be held, e.g. if the test suite is run # via "import test.autotest". lock_held_at_start = imp.lock_held() self.verify_lock_state(lock_held_at_start) for i in range(LOOPS): imp.acquire_lock() self.verify_lock_state(True) for i in range(LOOPS): imp.release_lock() # The original state should be restored now. self.verify_lock_state(lock_held_at_start) if not lock_held_at_start: try: imp.release_lock() except RuntimeError: pass else: self.fail("release_lock() without lock should raise " "RuntimeError") class ImportTests(unittest.TestCase): def setUp(self): mod = importlib.import_module('test.encoded_modules') self.test_strings = mod.test_strings self.test_path = mod.__path__ # test_import_encoded_module moved to test_source_encoding.py def test_find_module_encoding(self): for mod, encoding, _ in self.test_strings: with imp.find_module('module_' + mod, self.test_path)[0] as fd: self.assertEqual(fd.encoding, encoding) path = [os.path.dirname(orig__file__)] with self.assertRaises(SyntaxError): imp.find_module('badsyntax_pep3120', path) def test_issue1267(self): for mod, encoding, _ in self.test_strings: fp, filename, info = imp.find_module('module_' + mod, self.test_path) with fp: self.assertNotEqual(fp, None) self.assertEqual(fp.encoding, encoding) self.assertEqual(fp.tell(), 0) self.assertEqual(fp.readline(), '# test %s encoding\n' % encoding) fp, filename, info = imp.find_module("tokenize") with fp: self.assertNotEqual(fp, None) self.assertEqual(fp.encoding, "utf-8") self.assertEqual(fp.tell(), 0) self.assertEqual(fp.readline(), '"""Tokenization help for Python programs.\n') def test_issue3594(self): temp_mod_name = 'test_imp_helper' sys.path.insert(0, '.') try: with open(temp_mod_name + '.py', 'w', encoding="latin-1") as file: file.write("# coding: cp1252\nu = 'test.test_imp'\n") file, filename, info = imp.find_module(temp_mod_name) file.close() self.assertEqual(file.encoding, 'cp1252') finally: del sys.path[0] os_helper.unlink(temp_mod_name + '.py') os_helper.unlink(temp_mod_name + '.pyc') def test_issue5604(self): # Test cannot cover imp.load_compiled function. # Martin von Loewis note what shared library cannot have non-ascii # character because init_xxx function cannot be compiled # and issue never happens for dynamic modules. # But sources modified to follow generic way for processing paths. # the return encoding could be uppercase or None fs_encoding = sys.getfilesystemencoding() # covers utf-8 and Windows ANSI code pages # one non-space symbol from every page # (http://en.wikipedia.org/wiki/Code_page) known_locales = { 'utf-8' : b'\xc3\xa4', 'cp1250' : b'\x8C', 'cp1251' : b'\xc0', 'cp1252' : b'\xc0', 'cp1253' : b'\xc1', 'cp1254' : b'\xc0', 'cp1255' : b'\xe0', 'cp1256' : b'\xe0', 'cp1257' : b'\xc0', 'cp1258' : b'\xc0', } if sys.platform == 'darwin': self.assertEqual(fs_encoding, 'utf-8') # Mac OS X uses the Normal Form D decomposition # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html special_char = b'a\xcc\x88' else: special_char = known_locales.get(fs_encoding) if not special_char: self.skipTest("can't run this test with %s as filesystem encoding" % fs_encoding) decoded_char = special_char.decode(fs_encoding) temp_mod_name = 'test_imp_helper_' + decoded_char test_package_name = 'test_imp_helper_package_' + decoded_char init_file_name = os.path.join(test_package_name, '__init__.py') try: # if the curdir is not in sys.path the test fails when run with # ./python ./Lib/test/regrtest.py test_imp sys.path.insert(0, os.curdir) with open(temp_mod_name + '.py', 'w', encoding="utf-8") as file: file.write('a = 1\n') file, filename, info = imp.find_module(temp_mod_name) with file: self.assertIsNotNone(file) self.assertTrue(filename[:-3].endswith(temp_mod_name)) self.assertEqual(info[0], '.py') self.assertEqual(info[1], 'r') self.assertEqual(info[2], imp.PY_SOURCE) mod = imp.load_module(temp_mod_name, file, filename, info) self.assertEqual(mod.a, 1) with warnings.catch_warnings(): warnings.simplefilter('ignore') mod = imp.load_source(temp_mod_name, temp_mod_name + '.py') self.assertEqual(mod.a, 1) with warnings.catch_warnings(): warnings.simplefilter('ignore') if not sys.dont_write_bytecode: mod = imp.load_compiled( temp_mod_name, imp.cache_from_source(temp_mod_name + '.py')) self.assertEqual(mod.a, 1) if not os.path.exists(test_package_name): os.mkdir(test_package_name) with open(init_file_name, 'w', encoding="utf-8") as file: file.write('b = 2\n') with warnings.catch_warnings(): warnings.simplefilter('ignore') package = imp.load_package(test_package_name, test_package_name) self.assertEqual(package.b, 2) finally: del sys.path[0] for ext in ('.py', '.pyc'): os_helper.unlink(temp_mod_name + ext) os_helper.unlink(init_file_name + ext) os_helper.rmtree(test_package_name) os_helper.rmtree('__pycache__') def test_issue9319(self): path = os.path.dirname(orig__file__) self.assertRaises(SyntaxError, imp.find_module, "badsyntax_pep3120", [path]) def test_load_from_source(self): # Verify that the imp module can correctly load and find .py files # XXX (ncoghlan): It would be nice to use import_helper.CleanImport # here, but that breaks because the os module registers some # handlers in copy_reg on import. Since CleanImport doesn't # revert that registration, the module is left in a broken # state after reversion. Reinitialising the module contents # and just reverting os.environ to its previous state is an OK # workaround with import_helper.CleanImport('os', 'os.path', OS_PATH_NAME): import os orig_path = os.path orig_getenv = os.getenv with os_helper.EnvironmentVarGuard(): x = imp.find_module("os") self.addCleanup(x[0].close) new_os = imp.load_module("os", *x) self.assertIs(os, new_os) self.assertIs(orig_path, new_os.path) self.assertIsNot(orig_getenv, new_os.getenv) @requires_load_dynamic def test_issue15828_load_extensions(self): # Issue 15828 picked up that the adapter between the old imp API # and importlib couldn't handle C extensions example = "_heapq" x = imp.find_module(example) file_ = x[0] if file_ is not None: self.addCleanup(file_.close) mod = imp.load_module(example, *x) self.assertEqual(mod.__name__, example) @requires_load_dynamic def test_issue16421_multiple_modules_in_one_dll(self): # Issue 16421: loading several modules from the same compiled file fails m = '_testimportmultiple' fileobj, pathname, description = imp.find_module(m) fileobj.close() mod0 = imp.load_dynamic(m, pathname) mod1 = imp.load_dynamic('_testimportmultiple_foo', pathname) mod2 = imp.load_dynamic('_testimportmultiple_bar', pathname) self.assertEqual(mod0.__name__, m) self.assertEqual(mod1.__name__, '_testimportmultiple_foo') self.assertEqual(mod2.__name__, '_testimportmultiple_bar') with self.assertRaises(ImportError): imp.load_dynamic('nonexistent', pathname) @requires_load_dynamic def test_load_dynamic_ImportError_path(self): # Issue #1559549 added `name` and `path` attributes to ImportError # in order to provide better detail. Issue #10854 implemented those # attributes on import failures of extensions on Windows. path = 'bogus file path' name = 'extension' with self.assertRaises(ImportError) as err: imp.load_dynamic(name, path) self.assertIn(path, err.exception.path) self.assertEqual(name, err.exception.name) @requires_load_dynamic def test_load_module_extension_file_is_None(self): # When loading an extension module and the file is None, open one # on the behalf of imp.load_dynamic(). # Issue #15902 name = '_testimportmultiple' found = imp.find_module(name) if found[0] is not None: found[0].close() if found[2][2] != imp.C_EXTENSION: self.skipTest("found module doesn't appear to be a C extension") imp.load_module(name, None, *found[1:]) @requires_load_dynamic def test_issue24748_load_module_skips_sys_modules_check(self): name = 'x' try: del sys.modules[name] except KeyError: pass try: spec = importlib.util.find_spec('_testmultiphase') module = imp.load_dynamic(name, spec.origin) self.assertEqual(module.__name__, name) self.assertEqual(module.__spec__.name, name) self.assertEqual(module.__spec__.origin, spec.origin) self.assertRaises(AttributeError, getattr, module, 'x') self.assertEqual(module.int_const, 1969) self.assertIs(sys.modules[name], module) finally: try: del sys.modules[name] except KeyError: pass @unittest.skipIf(sys.dont_write_bytecode, "test meaningful only when writing bytecode") def test_bug7732(self): with os_helper.temp_cwd(): source = os_helper.TESTFN + '.py' os.mkdir(source) self.assertRaisesRegex(ImportError, '^No module', imp.find_module, os_helper.TESTFN, ["."]) def test_multiple_calls_to_get_data(self): # Issue #18755: make sure multiple calls to get_data() can succeed. loader = imp._LoadSourceCompatibility('imp', imp.__file__, open(imp.__file__, encoding="utf-8")) loader.get_data(imp.__file__) # File should be closed loader.get_data(imp.__file__) # Will need to create a newly opened file def test_load_source(self): # Create a temporary module since load_source(name) modifies # sys.modules[name] attributes like __loader___ modname = f"tmp{__name__}" mod = type(sys.modules[__name__])(modname) with support.swap_item(sys.modules, modname, mod): with self.assertRaisesRegex(ValueError, 'embedded null'): imp.load_source(modname, orig__file__ + "\0") @support.cpython_only def test_issue31315(self): # There shouldn't be an assertion failure in imp.create_dynamic(), # when spec.name is not a string. create_dynamic = support.get_attribute(imp, 'create_dynamic') class BadSpec: name = None origin = 'foo' with self.assertRaises(TypeError): create_dynamic(BadSpec()) @unittest.skipIf(sys.version_info < (3, 8), "missing bug fix") def test_issue_35321(self): # Both _frozen_importlib and _frozen_importlib_external # should have a spec origin of "frozen" and # no need to clean up imports in this case. import _frozen_importlib_external self.assertEqual(_frozen_importlib_external.__spec__.origin, "frozen") import _frozen_importlib self.assertEqual(_frozen_importlib.__spec__.origin, "frozen") def test_source_hash(self): if sys.version_info < (3, 11): self.assertEqual(_imp.source_hash(42, b'hi'), b'\xc6\xe7Z\r\x03:}\xab') self.assertEqual(_imp.source_hash(43, b'hi'), b'\x85\x9765\xf8\x9a\x8b9') else: self.assertEqual(_imp.source_hash(42, b'hi'), b'\xfb\xd9G\x05\xaf$\x9b~') self.assertEqual(_imp.source_hash(43, b'hi'), b'\xd0/\x87C\xccC\xff\xe2') def test_pyc_invalidation_mode_from_cmdline(self): cases = [ ([], "default"), (["--check-hash-based-pycs", "default"], "default"), (["--check-hash-based-pycs", "always"], "always"), (["--check-hash-based-pycs", "never"], "never"), ] for interp_args, expected in cases: args = interp_args + [ "-c", "import _imp; print(_imp.check_hash_based_pycs)", ] res = script_helper.assert_python_ok(*args) self.assertEqual(res.out.strip().decode('utf-8'), expected) def test_find_and_load_checked_pyc(self): # issue 34056 with os_helper.temp_cwd(): with open('mymod.py', 'wb') as fp: fp.write(b'x = 42\n') py_compile.compile( 'mymod.py', doraise=True, invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH, ) file, path, description = imp.find_module('mymod', path=['.']) mod = imp.load_module('mymod', file, path, description) self.assertEqual(mod.x, 42) def test_issue98354(self): # _imp.create_builtin should raise TypeError # if 'name' attribute of 'spec' argument is not a 'str' instance create_builtin = support.get_attribute(_imp, "create_builtin") class FakeSpec: def __init__(self, name): self.name = self spec = FakeSpec("time") if sys.version_info >= (3, 12): with self.assertRaises(TypeError): create_builtin(spec) class FakeSpec2: name = [1, 2, 3, 4] spec = FakeSpec2() with self.assertRaises(TypeError): create_builtin(spec) import builtins class UnicodeSubclass(str): pass class GoodSpec: name = UnicodeSubclass("builtins") spec = GoodSpec() bltin = create_builtin(spec) self.assertEqual(bltin, builtins) class UnicodeSubclassFakeSpec(str): def __init__(self, name): self.name = self spec = UnicodeSubclassFakeSpec("builtins") bltin = create_builtin(spec) self.assertEqual(bltin, builtins) @support.cpython_only @unittest.skipIf(sys.version_info < (3, 11), "missing bug fix") def test_create_builtin_subinterp(self): # gh-99578: create_builtin() behavior changes after the creation of the # first sub-interpreter. Test both code paths, before and after the # creation of a sub-interpreter. Previously, create_builtin() had # a reference leak after the creation of the first sub-interpreter. import builtins create_builtin = support.get_attribute(_imp, "create_builtin") class Spec: name = "builtins" spec = Spec() def check_get_builtins(): refcnt = sys.getrefcount(builtins) mod = _imp.create_builtin(spec) self.assertIs(mod, builtins) self.assertEqual(sys.getrefcount(builtins), refcnt + 1) # Check that a GC collection doesn't crash gc.collect() check_get_builtins() ret = support.run_in_subinterp("import builtins") self.assertEqual(ret, 0) check_get_builtins() class ReloadTests(unittest.TestCase): """Very basic tests to make sure that imp.reload() operates just like reload().""" def test_source(self): # XXX (ncoghlan): It would be nice to use test.import_helper.CleanImport # here, but that breaks because the os module registers some # handlers in copy_reg on import. Since CleanImport doesn't # revert that registration, the module is left in a broken # state after reversion. Reinitialising the module contents # and just reverting os.environ to its previous state is an OK # workaround with os_helper.EnvironmentVarGuard(): import os imp.reload(os) def test_extension(self): with import_helper.CleanImport('time'): import time imp.reload(time) def test_builtin(self): with import_helper.CleanImport('marshal'): import marshal imp.reload(marshal) def test_with_deleted_parent(self): # see #18681 from html import parser html = sys.modules.pop('html') def cleanup(): sys.modules['html'] = html self.addCleanup(cleanup) with self.assertRaisesRegex(ImportError, 'html'): imp.reload(parser) class PEP3147Tests(unittest.TestCase): """Tests of PEP 3147.""" tag = imp.get_tag() @unittest.skipUnless(sys.implementation.cache_tag is not None, 'requires sys.implementation.cache_tag not be None') def test_cache_from_source(self): # Given the path to a .py file, return the path to its PEP 3147 # defined .pyc file (i.e. under __pycache__). path = os.path.join('foo', 'bar', 'baz', 'qux.py') expect = os.path.join('foo', 'bar', 'baz', '__pycache__', 'qux.{}.pyc'.format(self.tag)) self.assertEqual(imp.cache_from_source(path, True), expect) @unittest.skipUnless(sys.implementation.cache_tag is not None, 'requires sys.implementation.cache_tag to not be ' 'None') def test_source_from_cache(self): # Given the path to a PEP 3147 defined .pyc file, return the path to # its source. This tests the good path. path = os.path.join('foo', 'bar', 'baz', '__pycache__', 'qux.{}.pyc'.format(self.tag)) expect = os.path.join('foo', 'bar', 'baz', 'qux.py') self.assertEqual(imp.source_from_cache(path), expect) class NullImporterTests(unittest.TestCase): @unittest.skipIf(os_helper.TESTFN_UNENCODABLE is None, "Need an undecodeable filename") def test_unencodeable(self): name = os_helper.TESTFN_UNENCODABLE os.mkdir(name) try: self.assertRaises(ImportError, imp.NullImporter, name) finally: os.rmdir(name) if __name__ == "__main__": unittest.main() ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1687858107.0 zombie-imp-0.0.2/test_zombie_imp/test_pkgutil_reanimations.py0000644000175000017500000000202614446525673025173 0ustar00pviktoripviktoriimport unittest import warnings from pathlib import Path try: from test.support.import_helper import CleanImport except ImportError: from test.support import CleanImport import sys try: from test.support.warnings_helper import check_warnings except ImportError: from test.support import check_warnings from zombie_imp import pkgutil class ImportlibMigrationTests(unittest.TestCase): # With full PEP 302 support in the standard import machinery, the # PEP 302 emulation in this module is in the process of being # deprecated in favour of importlib proper def check_deprecated(self): return check_warnings( ("This emulation is deprecated and slated for removal in " "Python 3.12; use 'importlib' instead", DeprecationWarning)) def test_importer_deprecated(self): with self.check_deprecated(): pkgutil.ImpImporter("") def test_loader_deprecated(self): with self.check_deprecated(): pkgutil.ImpLoader("", "", "", "") ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1687858107.0 zombie-imp-0.0.2/test_zombie_imp/test_same_files.py0000644000175000017500000000155714446525673023062 0ustar00pviktoripviktori"""`test_imp.py` and `test_zombie_imp.py` should match""" import unittest from pathlib import Path import difflib import collections from contextlib import ExitStack class FilesMatchTests(unittest.TestCase): def test_tests_same(self): with ExitStack() as cm: t_imp = cm.enter_context(Path(__file__).parent.joinpath('test_imp.py').open()) t_z_imp = cm.enter_context(Path(__file__).parent.joinpath('test_zombie_imp.py').open()) diff_lines = list(difflib.unified_diff(list(t_imp), list(t_z_imp))) prefix_counts = collections.Counter(line[:1] for line in diff_lines) try: self.assertEqual(prefix_counts['+'], 2) self.assertEqual(prefix_counts['-'], 2) except: print(''.join(diff_lines)) print(prefix_counts) raise ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1687858107.0 zombie-imp-0.0.2/test_zombie_imp/test_zombie_imp.py0000644000175000017500000005122614446525673023103 0ustar00pviktoripviktoriimport gc import importlib import importlib.util import os import os.path import py_compile import sys import test from test import support try: from test.support import import_helper except ImportError: from test import support as import_helper try: from test.support import os_helper except ImportError: from test import support as os_helper from test.support import script_helper try: from test.support import warnings_helper except ImportError: from test import support as warnings_helper import unittest import warnings import zombie_imp as imp #!! this should be the only difference between test_imp and test_zombie_imp import _imp orig__file__ = os.path.join(test.__path__[0], os.path.split(__file__)[-1]) OS_PATH_NAME = os.path.__name__ def requires_load_dynamic(meth): """Decorator to skip a test if not running under CPython or lacking imp.load_dynamic().""" meth = support.cpython_only(meth) return unittest.skipIf(getattr(imp, 'load_dynamic', None) is None, 'imp.load_dynamic() required')(meth) class LockTests(unittest.TestCase): """Very basic test of import lock functions.""" def verify_lock_state(self, expected): self.assertEqual(imp.lock_held(), expected, "expected imp.lock_held() to be %r" % expected) def testLock(self): LOOPS = 50 # The import lock may already be held, e.g. if the test suite is run # via "import test.autotest". lock_held_at_start = imp.lock_held() self.verify_lock_state(lock_held_at_start) for i in range(LOOPS): imp.acquire_lock() self.verify_lock_state(True) for i in range(LOOPS): imp.release_lock() # The original state should be restored now. self.verify_lock_state(lock_held_at_start) if not lock_held_at_start: try: imp.release_lock() except RuntimeError: pass else: self.fail("release_lock() without lock should raise " "RuntimeError") class ImportTests(unittest.TestCase): def setUp(self): mod = importlib.import_module('test.encoded_modules') self.test_strings = mod.test_strings self.test_path = mod.__path__ # test_import_encoded_module moved to test_source_encoding.py def test_find_module_encoding(self): for mod, encoding, _ in self.test_strings: with imp.find_module('module_' + mod, self.test_path)[0] as fd: self.assertEqual(fd.encoding, encoding) path = [os.path.dirname(orig__file__)] with self.assertRaises(SyntaxError): imp.find_module('badsyntax_pep3120', path) def test_issue1267(self): for mod, encoding, _ in self.test_strings: fp, filename, info = imp.find_module('module_' + mod, self.test_path) with fp: self.assertNotEqual(fp, None) self.assertEqual(fp.encoding, encoding) self.assertEqual(fp.tell(), 0) self.assertEqual(fp.readline(), '# test %s encoding\n' % encoding) fp, filename, info = imp.find_module("tokenize") with fp: self.assertNotEqual(fp, None) self.assertEqual(fp.encoding, "utf-8") self.assertEqual(fp.tell(), 0) self.assertEqual(fp.readline(), '"""Tokenization help for Python programs.\n') def test_issue3594(self): temp_mod_name = 'test_imp_helper' sys.path.insert(0, '.') try: with open(temp_mod_name + '.py', 'w', encoding="latin-1") as file: file.write("# coding: cp1252\nu = 'test.test_imp'\n") file, filename, info = imp.find_module(temp_mod_name) file.close() self.assertEqual(file.encoding, 'cp1252') finally: del sys.path[0] os_helper.unlink(temp_mod_name + '.py') os_helper.unlink(temp_mod_name + '.pyc') def test_issue5604(self): # Test cannot cover imp.load_compiled function. # Martin von Loewis note what shared library cannot have non-ascii # character because init_xxx function cannot be compiled # and issue never happens for dynamic modules. # But sources modified to follow generic way for processing paths. # the return encoding could be uppercase or None fs_encoding = sys.getfilesystemencoding() # covers utf-8 and Windows ANSI code pages # one non-space symbol from every page # (http://en.wikipedia.org/wiki/Code_page) known_locales = { 'utf-8' : b'\xc3\xa4', 'cp1250' : b'\x8C', 'cp1251' : b'\xc0', 'cp1252' : b'\xc0', 'cp1253' : b'\xc1', 'cp1254' : b'\xc0', 'cp1255' : b'\xe0', 'cp1256' : b'\xe0', 'cp1257' : b'\xc0', 'cp1258' : b'\xc0', } if sys.platform == 'darwin': self.assertEqual(fs_encoding, 'utf-8') # Mac OS X uses the Normal Form D decomposition # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html special_char = b'a\xcc\x88' else: special_char = known_locales.get(fs_encoding) if not special_char: self.skipTest("can't run this test with %s as filesystem encoding" % fs_encoding) decoded_char = special_char.decode(fs_encoding) temp_mod_name = 'test_imp_helper_' + decoded_char test_package_name = 'test_imp_helper_package_' + decoded_char init_file_name = os.path.join(test_package_name, '__init__.py') try: # if the curdir is not in sys.path the test fails when run with # ./python ./Lib/test/regrtest.py test_imp sys.path.insert(0, os.curdir) with open(temp_mod_name + '.py', 'w', encoding="utf-8") as file: file.write('a = 1\n') file, filename, info = imp.find_module(temp_mod_name) with file: self.assertIsNotNone(file) self.assertTrue(filename[:-3].endswith(temp_mod_name)) self.assertEqual(info[0], '.py') self.assertEqual(info[1], 'r') self.assertEqual(info[2], imp.PY_SOURCE) mod = imp.load_module(temp_mod_name, file, filename, info) self.assertEqual(mod.a, 1) with warnings.catch_warnings(): warnings.simplefilter('ignore') mod = imp.load_source(temp_mod_name, temp_mod_name + '.py') self.assertEqual(mod.a, 1) with warnings.catch_warnings(): warnings.simplefilter('ignore') if not sys.dont_write_bytecode: mod = imp.load_compiled( temp_mod_name, imp.cache_from_source(temp_mod_name + '.py')) self.assertEqual(mod.a, 1) if not os.path.exists(test_package_name): os.mkdir(test_package_name) with open(init_file_name, 'w', encoding="utf-8") as file: file.write('b = 2\n') with warnings.catch_warnings(): warnings.simplefilter('ignore') package = imp.load_package(test_package_name, test_package_name) self.assertEqual(package.b, 2) finally: del sys.path[0] for ext in ('.py', '.pyc'): os_helper.unlink(temp_mod_name + ext) os_helper.unlink(init_file_name + ext) os_helper.rmtree(test_package_name) os_helper.rmtree('__pycache__') def test_issue9319(self): path = os.path.dirname(orig__file__) self.assertRaises(SyntaxError, imp.find_module, "badsyntax_pep3120", [path]) def test_load_from_source(self): # Verify that the imp module can correctly load and find .py files # XXX (ncoghlan): It would be nice to use import_helper.CleanImport # here, but that breaks because the os module registers some # handlers in copy_reg on import. Since CleanImport doesn't # revert that registration, the module is left in a broken # state after reversion. Reinitialising the module contents # and just reverting os.environ to its previous state is an OK # workaround with import_helper.CleanImport('os', 'os.path', OS_PATH_NAME): import os orig_path = os.path orig_getenv = os.getenv with os_helper.EnvironmentVarGuard(): x = imp.find_module("os") self.addCleanup(x[0].close) new_os = imp.load_module("os", *x) self.assertIs(os, new_os) self.assertIs(orig_path, new_os.path) self.assertIsNot(orig_getenv, new_os.getenv) @requires_load_dynamic def test_issue15828_load_extensions(self): # Issue 15828 picked up that the adapter between the old imp API # and importlib couldn't handle C extensions example = "_heapq" x = imp.find_module(example) file_ = x[0] if file_ is not None: self.addCleanup(file_.close) mod = imp.load_module(example, *x) self.assertEqual(mod.__name__, example) @requires_load_dynamic def test_issue16421_multiple_modules_in_one_dll(self): # Issue 16421: loading several modules from the same compiled file fails m = '_testimportmultiple' fileobj, pathname, description = imp.find_module(m) fileobj.close() mod0 = imp.load_dynamic(m, pathname) mod1 = imp.load_dynamic('_testimportmultiple_foo', pathname) mod2 = imp.load_dynamic('_testimportmultiple_bar', pathname) self.assertEqual(mod0.__name__, m) self.assertEqual(mod1.__name__, '_testimportmultiple_foo') self.assertEqual(mod2.__name__, '_testimportmultiple_bar') with self.assertRaises(ImportError): imp.load_dynamic('nonexistent', pathname) @requires_load_dynamic def test_load_dynamic_ImportError_path(self): # Issue #1559549 added `name` and `path` attributes to ImportError # in order to provide better detail. Issue #10854 implemented those # attributes on import failures of extensions on Windows. path = 'bogus file path' name = 'extension' with self.assertRaises(ImportError) as err: imp.load_dynamic(name, path) self.assertIn(path, err.exception.path) self.assertEqual(name, err.exception.name) @requires_load_dynamic def test_load_module_extension_file_is_None(self): # When loading an extension module and the file is None, open one # on the behalf of imp.load_dynamic(). # Issue #15902 name = '_testimportmultiple' found = imp.find_module(name) if found[0] is not None: found[0].close() if found[2][2] != imp.C_EXTENSION: self.skipTest("found module doesn't appear to be a C extension") imp.load_module(name, None, *found[1:]) @requires_load_dynamic def test_issue24748_load_module_skips_sys_modules_check(self): name = 'x' try: del sys.modules[name] except KeyError: pass try: spec = importlib.util.find_spec('_testmultiphase') module = imp.load_dynamic(name, spec.origin) self.assertEqual(module.__name__, name) self.assertEqual(module.__spec__.name, name) self.assertEqual(module.__spec__.origin, spec.origin) self.assertRaises(AttributeError, getattr, module, 'x') self.assertEqual(module.int_const, 1969) self.assertIs(sys.modules[name], module) finally: try: del sys.modules[name] except KeyError: pass @unittest.skipIf(sys.dont_write_bytecode, "test meaningful only when writing bytecode") def test_bug7732(self): with os_helper.temp_cwd(): source = os_helper.TESTFN + '.py' os.mkdir(source) self.assertRaisesRegex(ImportError, '^No module', imp.find_module, os_helper.TESTFN, ["."]) def test_multiple_calls_to_get_data(self): # Issue #18755: make sure multiple calls to get_data() can succeed. loader = imp._LoadSourceCompatibility('imp', imp.__file__, open(imp.__file__, encoding="utf-8")) loader.get_data(imp.__file__) # File should be closed loader.get_data(imp.__file__) # Will need to create a newly opened file def test_load_source(self): # Create a temporary module since load_source(name) modifies # sys.modules[name] attributes like __loader___ modname = f"tmp{__name__}" mod = type(sys.modules[__name__])(modname) with support.swap_item(sys.modules, modname, mod): with self.assertRaisesRegex(ValueError, 'embedded null'): imp.load_source(modname, orig__file__ + "\0") @support.cpython_only def test_issue31315(self): # There shouldn't be an assertion failure in imp.create_dynamic(), # when spec.name is not a string. create_dynamic = support.get_attribute(imp, 'create_dynamic') class BadSpec: name = None origin = 'foo' with self.assertRaises(TypeError): create_dynamic(BadSpec()) @unittest.skipIf(sys.version_info < (3, 8), "missing bug fix") def test_issue_35321(self): # Both _frozen_importlib and _frozen_importlib_external # should have a spec origin of "frozen" and # no need to clean up imports in this case. import _frozen_importlib_external self.assertEqual(_frozen_importlib_external.__spec__.origin, "frozen") import _frozen_importlib self.assertEqual(_frozen_importlib.__spec__.origin, "frozen") def test_source_hash(self): if sys.version_info < (3, 11): self.assertEqual(_imp.source_hash(42, b'hi'), b'\xc6\xe7Z\r\x03:}\xab') self.assertEqual(_imp.source_hash(43, b'hi'), b'\x85\x9765\xf8\x9a\x8b9') else: self.assertEqual(_imp.source_hash(42, b'hi'), b'\xfb\xd9G\x05\xaf$\x9b~') self.assertEqual(_imp.source_hash(43, b'hi'), b'\xd0/\x87C\xccC\xff\xe2') def test_pyc_invalidation_mode_from_cmdline(self): cases = [ ([], "default"), (["--check-hash-based-pycs", "default"], "default"), (["--check-hash-based-pycs", "always"], "always"), (["--check-hash-based-pycs", "never"], "never"), ] for interp_args, expected in cases: args = interp_args + [ "-c", "import _imp; print(_imp.check_hash_based_pycs)", ] res = script_helper.assert_python_ok(*args) self.assertEqual(res.out.strip().decode('utf-8'), expected) def test_find_and_load_checked_pyc(self): # issue 34056 with os_helper.temp_cwd(): with open('mymod.py', 'wb') as fp: fp.write(b'x = 42\n') py_compile.compile( 'mymod.py', doraise=True, invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH, ) file, path, description = imp.find_module('mymod', path=['.']) mod = imp.load_module('mymod', file, path, description) self.assertEqual(mod.x, 42) def test_issue98354(self): # _imp.create_builtin should raise TypeError # if 'name' attribute of 'spec' argument is not a 'str' instance create_builtin = support.get_attribute(_imp, "create_builtin") class FakeSpec: def __init__(self, name): self.name = self spec = FakeSpec("time") if sys.version_info >= (3, 12): with self.assertRaises(TypeError): create_builtin(spec) class FakeSpec2: name = [1, 2, 3, 4] spec = FakeSpec2() with self.assertRaises(TypeError): create_builtin(spec) import builtins class UnicodeSubclass(str): pass class GoodSpec: name = UnicodeSubclass("builtins") spec = GoodSpec() bltin = create_builtin(spec) self.assertEqual(bltin, builtins) class UnicodeSubclassFakeSpec(str): def __init__(self, name): self.name = self spec = UnicodeSubclassFakeSpec("builtins") bltin = create_builtin(spec) self.assertEqual(bltin, builtins) @support.cpython_only @unittest.skipIf(sys.version_info < (3, 11), "missing bug fix") def test_create_builtin_subinterp(self): # gh-99578: create_builtin() behavior changes after the creation of the # first sub-interpreter. Test both code paths, before and after the # creation of a sub-interpreter. Previously, create_builtin() had # a reference leak after the creation of the first sub-interpreter. import builtins create_builtin = support.get_attribute(_imp, "create_builtin") class Spec: name = "builtins" spec = Spec() def check_get_builtins(): refcnt = sys.getrefcount(builtins) mod = _imp.create_builtin(spec) self.assertIs(mod, builtins) self.assertEqual(sys.getrefcount(builtins), refcnt + 1) # Check that a GC collection doesn't crash gc.collect() check_get_builtins() ret = support.run_in_subinterp("import builtins") self.assertEqual(ret, 0) check_get_builtins() class ReloadTests(unittest.TestCase): """Very basic tests to make sure that imp.reload() operates just like reload().""" def test_source(self): # XXX (ncoghlan): It would be nice to use test.import_helper.CleanImport # here, but that breaks because the os module registers some # handlers in copy_reg on import. Since CleanImport doesn't # revert that registration, the module is left in a broken # state after reversion. Reinitialising the module contents # and just reverting os.environ to its previous state is an OK # workaround with os_helper.EnvironmentVarGuard(): import os imp.reload(os) def test_extension(self): with import_helper.CleanImport('time'): import time imp.reload(time) def test_builtin(self): with import_helper.CleanImport('marshal'): import marshal imp.reload(marshal) def test_with_deleted_parent(self): # see #18681 from html import parser html = sys.modules.pop('html') def cleanup(): sys.modules['html'] = html self.addCleanup(cleanup) with self.assertRaisesRegex(ImportError, 'html'): imp.reload(parser) class PEP3147Tests(unittest.TestCase): """Tests of PEP 3147.""" tag = imp.get_tag() @unittest.skipUnless(sys.implementation.cache_tag is not None, 'requires sys.implementation.cache_tag not be None') def test_cache_from_source(self): # Given the path to a .py file, return the path to its PEP 3147 # defined .pyc file (i.e. under __pycache__). path = os.path.join('foo', 'bar', 'baz', 'qux.py') expect = os.path.join('foo', 'bar', 'baz', '__pycache__', 'qux.{}.pyc'.format(self.tag)) self.assertEqual(imp.cache_from_source(path, True), expect) @unittest.skipUnless(sys.implementation.cache_tag is not None, 'requires sys.implementation.cache_tag to not be ' 'None') def test_source_from_cache(self): # Given the path to a PEP 3147 defined .pyc file, return the path to # its source. This tests the good path. path = os.path.join('foo', 'bar', 'baz', '__pycache__', 'qux.{}.pyc'.format(self.tag)) expect = os.path.join('foo', 'bar', 'baz', 'qux.py') self.assertEqual(imp.source_from_cache(path), expect) class NullImporterTests(unittest.TestCase): @unittest.skipIf(os_helper.TESTFN_UNENCODABLE is None, "Need an undecodeable filename") def test_unencodeable(self): name = os_helper.TESTFN_UNENCODABLE os.mkdir(name) try: self.assertRaises(ImportError, imp.NullImporter, name) finally: os.rmdir(name) if __name__ == "__main__": unittest.main() ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1687858679.0 zombie-imp-0.0.2/tox.ini0000644000175000017500000000024214446526767015457 0ustar00pviktoripviktori[tox] min_version = 4.0 env_list = py37 py38 py39 py310 py311 py312 [testenv] commands = python -m unittest discover -v test_zombie_imp/ ././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1687859820.7157187 zombie-imp-0.0.2/zombie_imp/0000755000175000017500000000000014446531155016263 5ustar00pviktoripviktori././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1687858107.0 zombie-imp-0.0.2/zombie_imp/__init__.py0000644000175000017500000000045414446525673020407 0ustar00pviktoripviktorifrom .imp_3_11 import * from .imp_3_11 import _fix_co_filename try: from .imp_3_11 import _frozen_module_names except ImportError: pass from .imp_3_11 import _ERR_MSG, _exec, _load, _builtin_from_name from .imp_3_11 import _HackedGetData, _LoadSourceCompatibility, _LoadCompiledCompatibility ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1687858107.0 zombie-imp-0.0.2/zombie_imp/imp_3_11.py0000644000175000017500000002456014446525673020164 0ustar00pviktoripviktori"""This module provides the components needed to build your own __import__ function. Undocumented functions are obsolete. In most cases it is preferred you consider using the importlib module's functionality over this module. """ # (Probably) need to stay in _imp from _imp import (lock_held, acquire_lock, release_lock, get_frozen_object, is_frozen_package, init_frozen, is_builtin, is_frozen, _fix_co_filename) try: from _imp import _frozen_module_names except ImportError: pass try: from _imp import create_dynamic except ImportError: # Platform doesn't support dynamic loading. create_dynamic = None from importlib._bootstrap import _ERR_MSG, _exec, _load, _builtin_from_name from importlib._bootstrap_external import SourcelessFileLoader from importlib import machinery from importlib import util import importlib import os import sys import tokenize import types import warnings warnings.warn("the imp module was removed in favour of importlib. " "Someone brought it back, but it's not a good idea to use it.", DeprecationWarning, stacklevel=2) # DEPRECATED SEARCH_ERROR = 0 PY_SOURCE = 1 PY_COMPILED = 2 C_EXTENSION = 3 PY_RESOURCE = 4 PKG_DIRECTORY = 5 C_BUILTIN = 6 PY_FROZEN = 7 PY_CODERESOURCE = 8 IMP_HOOK = 9 def new_module(name): """**DEPRECATED** Create a new module. The module is not entered into sys.modules. """ return types.ModuleType(name) def get_magic(): """**DEPRECATED** Return the magic number for .pyc files. """ return util.MAGIC_NUMBER def get_tag(): """Return the magic tag for .pyc files.""" return sys.implementation.cache_tag def cache_from_source(path, debug_override=None): """**DEPRECATED** Given the path to a .py file, return the path to its .pyc file. The .py file does not need to exist; this simply returns the path to the .pyc file calculated as if the .py file were imported. If debug_override is not None, then it must be a boolean and is used in place of sys.flags.optimize. If sys.implementation.cache_tag is None then NotImplementedError is raised. """ with warnings.catch_warnings(): warnings.simplefilter('ignore') return util.cache_from_source(path, debug_override) def source_from_cache(path): """**DEPRECATED** Given the path to a .pyc. file, return the path to its .py file. The .pyc file does not need to exist; this simply returns the path to the .py file calculated to correspond to the .pyc file. If path does not conform to PEP 3147 format, ValueError will be raised. If sys.implementation.cache_tag is None then NotImplementedError is raised. """ return util.source_from_cache(path) def get_suffixes(): """**DEPRECATED**""" extensions = [(s, 'rb', C_EXTENSION) for s in machinery.EXTENSION_SUFFIXES] source = [(s, 'r', PY_SOURCE) for s in machinery.SOURCE_SUFFIXES] bytecode = [(s, 'rb', PY_COMPILED) for s in machinery.BYTECODE_SUFFIXES] return extensions + source + bytecode class NullImporter: """**DEPRECATED** Null import object. """ def __init__(self, path): if path == '': raise ImportError('empty pathname', path='') elif os.path.isdir(path): raise ImportError('existing directory', path=path) def find_module(self, fullname): """Always returns None.""" return None class _HackedGetData: """Compatibility support for 'file' arguments of various load_*() functions.""" def __init__(self, fullname, path, file=None): super().__init__(fullname, path) self.file = file def get_data(self, path): """Gross hack to contort loader to deal w/ load_*()'s bad API.""" if self.file and path == self.path: # The contract of get_data() requires us to return bytes. Reopen the # file in binary mode if needed. if not self.file.closed: file = self.file if 'b' not in file.mode: file.close() if self.file.closed: self.file = file = open(self.path, 'rb') with file: return file.read() else: return super().get_data(path) class _LoadSourceCompatibility(_HackedGetData, machinery.SourceFileLoader): """Compatibility support for implementing load_source().""" def load_source(name, pathname, file=None): loader = _LoadSourceCompatibility(name, pathname, file) spec = util.spec_from_file_location(name, pathname, loader=loader) if name in sys.modules: module = _exec(spec, sys.modules[name]) else: module = _load(spec) # To allow reloading to potentially work, use a non-hacked loader which # won't rely on a now-closed file object. module.__loader__ = machinery.SourceFileLoader(name, pathname) module.__spec__.loader = module.__loader__ return module class _LoadCompiledCompatibility(_HackedGetData, SourcelessFileLoader): """Compatibility support for implementing load_compiled().""" def load_compiled(name, pathname, file=None): """**DEPRECATED**""" loader = _LoadCompiledCompatibility(name, pathname, file) spec = util.spec_from_file_location(name, pathname, loader=loader) if name in sys.modules: module = _exec(spec, sys.modules[name]) else: module = _load(spec) # To allow reloading to potentially work, use a non-hacked loader which # won't rely on a now-closed file object. module.__loader__ = SourcelessFileLoader(name, pathname) module.__spec__.loader = module.__loader__ return module def load_package(name, path): """**DEPRECATED**""" if os.path.isdir(path): extensions = (machinery.SOURCE_SUFFIXES[:] + machinery.BYTECODE_SUFFIXES[:]) for extension in extensions: init_path = os.path.join(path, '__init__' + extension) if os.path.exists(init_path): path = init_path break else: raise ValueError('{!r} is not a package'.format(path)) spec = util.spec_from_file_location(name, path, submodule_search_locations=[]) if name in sys.modules: return _exec(spec, sys.modules[name]) else: return _load(spec) def load_module(name, file, filename, details): """**DEPRECATED** Load a module, given information returned by find_module(). The module name must include the full package name, if any. """ suffix, mode, type_ = details if mode and (not mode.startswith('r') or '+' in mode): raise ValueError('invalid file open mode {!r}'.format(mode)) elif file is None and type_ in {PY_SOURCE, PY_COMPILED}: msg = 'file object required for import (type code {})'.format(type_) raise ValueError(msg) elif type_ == PY_SOURCE: return load_source(name, filename, file) elif type_ == PY_COMPILED: return load_compiled(name, filename, file) elif type_ == C_EXTENSION and load_dynamic is not None: if file is None: with open(filename, 'rb') as opened_file: return load_dynamic(name, filename, opened_file) else: return load_dynamic(name, filename, file) elif type_ == PKG_DIRECTORY: return load_package(name, filename) elif type_ == C_BUILTIN: return init_builtin(name) elif type_ == PY_FROZEN: return init_frozen(name) else: msg = "Don't know how to import {} (type code {})".format(name, type_) raise ImportError(msg, name=name) def find_module(name, path=None): """**DEPRECATED** Search for a module. If path is omitted or None, search for a built-in, frozen or special module and continue search in sys.path. The module name cannot contain '.'; to search for a submodule of a package, pass the submodule name and the package's __path__. """ if not isinstance(name, str): raise TypeError("'name' must be a str, not {}".format(type(name))) elif not isinstance(path, (type(None), list)): # Backwards-compatibility raise RuntimeError("'path' must be None or a list, " "not {}".format(type(path))) if path is None: if is_builtin(name): return None, None, ('', '', C_BUILTIN) elif is_frozen(name): return None, None, ('', '', PY_FROZEN) else: path = sys.path for entry in path: package_directory = os.path.join(entry, name) for suffix in ['.py', machinery.BYTECODE_SUFFIXES[0]]: package_file_name = '__init__' + suffix file_path = os.path.join(package_directory, package_file_name) if os.path.isfile(file_path): return None, package_directory, ('', '', PKG_DIRECTORY) for suffix, mode, type_ in get_suffixes(): file_name = name + suffix file_path = os.path.join(entry, file_name) if os.path.isfile(file_path): break else: continue break # Break out of outer loop when breaking out of inner loop. else: raise ImportError(_ERR_MSG.format(name), name=name) encoding = None if 'b' not in mode: with open(file_path, 'rb') as file: encoding = tokenize.detect_encoding(file.readline)[0] file = open(file_path, mode, encoding=encoding) return file, file_path, (suffix, mode, type_) def reload(module): """**DEPRECATED** Reload the module and return it. The module must have been successfully imported before. """ return importlib.reload(module) def init_builtin(name): """**DEPRECATED** Load and return a built-in module by name, or None is such module doesn't exist """ try: return _builtin_from_name(name) except ImportError: return None if create_dynamic: def load_dynamic(name, path, file=None): """**DEPRECATED** Load an extension module. """ import importlib.machinery loader = importlib.machinery.ExtensionFileLoader(name, path) # Issue #24748: Skip the sys.modules check in _load_module_shim; # always load new extension spec = importlib.util.spec_from_file_location( name, path, loader=loader) return _load(spec) else: load_dynamic = None ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1687766556.0 zombie-imp-0.0.2/zombie_imp/pkgutil.py0000644000175000017500000001443514446243034020317 0ustar00pviktoripviktorifrom pkgutil import * import warnings import importlib def _import_imp(): global imp with warnings.catch_warnings(): warnings.simplefilter('ignore', DeprecationWarning) imp = importlib.import_module('imp') class ImpImporter: """PEP 302 Finder that wraps Python's "classic" import algorithm ImpImporter(dirname) produces a PEP 302 finder that searches that directory. ImpImporter(None) produces a PEP 302 finder that searches the current sys.path, plus any modules that are frozen or built-in. Note that ImpImporter does not currently support being used by placement on sys.meta_path. """ def __init__(self, path=None): global imp warnings.warn("This emulation is deprecated and slated for removal " "in Python 3.12; use 'importlib' instead", DeprecationWarning) _import_imp() self.path = path def find_module(self, fullname, path=None): # Note: we ignore 'path' argument since it is only used via meta_path subname = fullname.split(".")[-1] if subname != fullname and self.path is None: return None if self.path is None: path = None else: path = [os.path.realpath(self.path)] try: file, filename, etc = imp.find_module(subname, path) except ImportError: return None return ImpLoader(fullname, file, filename, etc) def iter_modules(self, prefix=''): if self.path is None or not os.path.isdir(self.path): return yielded = {} import inspect try: filenames = os.listdir(self.path) except OSError: # ignore unreadable directories like import does filenames = [] filenames.sort() # handle packages before same-named modules for fn in filenames: modname = inspect.getmodulename(fn) if modname=='__init__' or modname in yielded: continue path = os.path.join(self.path, fn) ispkg = False if not modname and os.path.isdir(path) and '.' not in fn: modname = fn try: dircontents = os.listdir(path) except OSError: # ignore unreadable directories like import does dircontents = [] for fn in dircontents: subname = inspect.getmodulename(fn) if subname=='__init__': ispkg = True break else: continue # not a package if modname and '.' not in modname: yielded[modname] = 1 yield prefix + modname, ispkg class ImpLoader: """PEP 302 Loader that wraps Python's "classic" import algorithm """ code = source = None def __init__(self, fullname, file, filename, etc): warnings.warn("This emulation is deprecated and slated for removal in " "Python 3.12; use 'importlib' instead", DeprecationWarning) _import_imp() self.file = file self.filename = filename self.fullname = fullname self.etc = etc def load_module(self, fullname): self._reopen() try: mod = imp.load_module(fullname, self.file, self.filename, self.etc) finally: if self.file: self.file.close() # Note: we don't set __loader__ because we want the module to look # normal; i.e. this is just a wrapper for standard import machinery return mod def get_data(self, pathname): with open(pathname, "rb") as file: return file.read() def _reopen(self): if self.file and self.file.closed: mod_type = self.etc[2] if mod_type==imp.PY_SOURCE: self.file = open(self.filename, 'r') elif mod_type in (imp.PY_COMPILED, imp.C_EXTENSION): self.file = open(self.filename, 'rb') def _fix_name(self, fullname): if fullname is None: fullname = self.fullname elif fullname != self.fullname: raise ImportError("Loader for module %s cannot handle " "module %s" % (self.fullname, fullname)) return fullname def is_package(self, fullname): fullname = self._fix_name(fullname) return self.etc[2]==imp.PKG_DIRECTORY def get_code(self, fullname=None): fullname = self._fix_name(fullname) if self.code is None: mod_type = self.etc[2] if mod_type==imp.PY_SOURCE: source = self.get_source(fullname) self.code = compile(source, self.filename, 'exec') elif mod_type==imp.PY_COMPILED: self._reopen() try: self.code = read_code(self.file) finally: self.file.close() elif mod_type==imp.PKG_DIRECTORY: self.code = self._get_delegate().get_code() return self.code def get_source(self, fullname=None): fullname = self._fix_name(fullname) if self.source is None: mod_type = self.etc[2] if mod_type==imp.PY_SOURCE: self._reopen() try: self.source = self.file.read() finally: self.file.close() elif mod_type==imp.PY_COMPILED: if os.path.exists(self.filename[:-1]): with open(self.filename[:-1], 'r') as f: self.source = f.read() elif mod_type==imp.PKG_DIRECTORY: self.source = self._get_delegate().get_source() return self.source def _get_delegate(self): finder = ImpImporter(self.filename) spec = _get_spec(finder, '__init__') return spec.loader def get_filename(self, fullname=None): fullname = self._fix_name(fullname) mod_type = self.etc[2] if mod_type==imp.PKG_DIRECTORY: return self._get_delegate().get_filename() elif mod_type in (imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION): return self.filename return None ././@PaxHeader0000000000000000000000000000003400000000000010212 xustar0028 mtime=1687859820.7157187 zombie-imp-0.0.2/zombie_imp.egg-info/0000755000175000017500000000000014446531155017755 5ustar00pviktoripviktori././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1687859820.0 zombie-imp-0.0.2/zombie_imp.egg-info/PKG-INFO0000644000175000017500000000253114446531154021052 0ustar00pviktoripviktoriMetadata-Version: 2.1 Name: zombie-imp Version: 0.0.2 Summary: A copy of the `imp` module that was removed in Python 3.12 Maintainer-email: Petr Viktorin Project-URL: Homepage, https://github.com/encukou/zombie-imp Project-URL: Bug Tracker, https://github.com/encukou/zombie-imp/issues Classifier: Programming Language :: Python :: 3 Classifier: License :: OSI Approved :: Python Software Foundation License Classifier: Operating System :: OS Independent Requires-Python: >=3.7 Description-Content-Type: text/markdown License-File: LICENSE # zombie-imp ~~A particularly mischevious act of necromancy. That is,~~ A copy of the `imp` module that was removed in Python 3.12. Don't use this, it'll probably trick and bite you. # Usage Can be summoned by `import zombie_imp`. On Python versions where `imp` was banished, reanimate it using `import imp`. It promises (with a sneer) to be the same as before. Some functionality that was severed from `pkgutil` is interred in `zombie_imp.pkgutil`, ready for reattachment: - `ImpImporter` - `ImpLoader` # Development You want to help it? Think you'll be rewarded? Great! It loves gullible brains. Seriously, **run!** Find a project that needs this and port *that* to `importlib`. ## License The code was snatched from CPython, and is available under CPython's license (SPDX: `Python-2.0.1`). ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1687859820.0 zombie-imp-0.0.2/zombie_imp.egg-info/SOURCES.txt0000644000175000017500000000062614446531154021644 0ustar00pviktoripviktoriLICENSE MANIFEST.in README.md imp.py pyproject.toml tox.ini test_zombie_imp/test_imp.py test_zombie_imp/test_pkgutil_reanimations.py test_zombie_imp/test_same_files.py test_zombie_imp/test_zombie_imp.py zombie_imp/__init__.py zombie_imp/imp_3_11.py zombie_imp/pkgutil.py zombie_imp.egg-info/PKG-INFO zombie_imp.egg-info/SOURCES.txt zombie_imp.egg-info/dependency_links.txt zombie_imp.egg-info/top_level.txt././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1687859820.0 zombie-imp-0.0.2/zombie_imp.egg-info/dependency_links.txt0000644000175000017500000000000114446531154024022 0ustar00pviktoripviktori ././@PaxHeader0000000000000000000000000000002600000000000010213 xustar0022 mtime=1687859820.0 zombie-imp-0.0.2/zombie_imp.egg-info/top_level.txt0000644000175000017500000000001714446531154022504 0ustar00pviktoripviktoriimp zombie_imp