def test_renamedSource(self): """ Warnings emitted by a function defined in a file which has been renamed since it was initially compiled can still be flushed. This is testing the code which specifically supports working around the unfortunate behavior of CPython to write a .py source file name into the .pyc files it generates and then trust that it is correct in various places. If source files are renamed, .pyc files may not be regenerated, but they will contain incorrect filenames. """ package = FilePath(self.mktemp().encode("utf-8")).child( b"twisted_private_helper" ) package.makedirs() package.child(b"__init__.py").setContent(b"") package.child(b"module.py").setContent( b""" import warnings def foo(): warnings.warn("oh no") """ ) pathEntry = package.parent().path.decode("utf-8") sys.path.insert(0, pathEntry) self.addCleanup(sys.path.remove, pathEntry) # Import it to cause pycs to be generated from twisted_private_helper import module # Clean up the state resulting from that import; we're not going to use # this module, so it should go away. del sys.modules["twisted_private_helper"] del sys.modules[module.__name__] # Some Python versions have extra state related to the just # imported/renamed package. Clean it up too. See also # http://bugs.python.org/issue15912 try: from importlib import invalidate_caches except ImportError: pass else: invalidate_caches() # Rename the source directory package.moveTo(package.sibling(b"twisted_renamed_helper")) # Import the newly renamed version from twisted_renamed_helper import module # type: ignore[import] self.addCleanup(sys.modules.pop, "twisted_renamed_helper") self.addCleanup(sys.modules.pop, module.__name__) # Generate the warning module.foo() # Flush it self.assertEqual(len(self.flushWarnings([module.foo])), 1)
def test_renamedSource(self): """ Warnings emitted by a function defined in a file which has been renamed since it was initially compiled can still be flushed. This is testing the code which specifically supports working around the unfortunate behavior of CPython to write a .py source file name into the .pyc files it generates and then trust that it is correct in various places. If source files are renamed, .pyc files may not be regenerated, but they will contain incorrect filenames. """ package = FilePath(self.mktemp().encode('utf-8')).child(b'twisted_private_helper') package.makedirs() package.child(b'__init__.py').setContent(b'') package.child(b'module.py').setContent(b''' import warnings def foo(): warnings.warn("oh no") ''') pathEntry = package.parent().path.decode('utf-8') sys.path.insert(0, pathEntry) self.addCleanup(sys.path.remove, pathEntry) # Import it to cause pycs to be generated from twisted_private_helper import module # Clean up the state resulting from that import; we're not going to use # this module, so it should go away. del sys.modules['twisted_private_helper'] del sys.modules[module.__name__] # Some Python versions have extra state related to the just # imported/renamed package. Clean it up too. See also # http://bugs.python.org/issue15912 try: from importlib import invalidate_caches except ImportError: pass else: invalidate_caches() # Rename the source directory package.moveTo(package.sibling(b'twisted_renamed_helper')) # Import the newly renamed version from twisted_renamed_helper import module self.addCleanup(sys.modules.pop, 'twisted_renamed_helper') self.addCleanup(sys.modules.pop, module.__name__) # Generate the warning module.foo() # Flush it self.assertEqual(len(self.flushWarnings([module.foo])), 1)
def test_renamedSource(self): """ Warnings emitted by a function defined in a file which has been renamed since it was initially compiled can still be flushed. This is testing the code which specifically supports working around the unfortunate behavior of CPython to write a .py source file name into the .pyc files it generates and then trust that it is correct in various places. If source files are renamed, .pyc files may not be regenerated, but they will contain incorrect filenames. """ package = FilePath(self.mktemp()).child('twisted_private_helper') package.makedirs() package.child('__init__.py').setContent('') package.child('module.py').setContent(''' import warnings def foo(): warnings.warn("oh no") ''') sys.path.insert(0, package.parent().path) self.addCleanup(sys.path.remove, package.parent().path) # Import it to cause pycs to be generated from twisted_private_helper import module # Clean up the state resulting from that import; we're not going to use # this module, so it should go away. del sys.modules['twisted_private_helper'] del sys.modules[module.__name__] # Rename the source directory package.moveTo(package.sibling('twisted_renamed_helper')) # Import the newly renamed version from twisted_renamed_helper import module self.addCleanup(sys.modules.pop, 'twisted_renamed_helper') self.addCleanup(sys.modules.pop, module.__name__) # Generate the warning module.foo() # Flush it self.assertEqual(len(self.flushWarnings([module.foo])), 1)