import sys, os.path, tempfile, shutil from unittest import TestCase class BrokenTests(TestCase): def mktemp(self): MAX_FILENAME = 32 # some platforms limit lengths of filenames base = os.path.join(self.__class__.__module__[:MAX_FILENAME], self.__class__.__name__[:MAX_FILENAME], self._testMethodName[:MAX_FILENAME]) if not os.path.exists(base): os.makedirs(base) dirname = tempfile.mkdtemp('', '', base) return os.path.join(dirname, 'temp') def test_renamedSource(self): packagePath = os.path.join(self.mktemp().encode('ascii'), b'twisted_private_helper') os.makedirs(packagePath) with open(os.path.join(packagePath, b'__init__.py'), 'wb') as f: f.write(b'') with open(os.path.join(packagePath, b'module.py'), 'wb') as f: f.write(b''' import warnings def foo(): warnings.warn("oh no") ''') pathEntry = os.path.dirname(packagePath.decode('ascii')) sys.path.insert(0, 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__] # Rename the source directory shutil.move(packagePath, os.path.join(os.path.split(packagePath)[0], b'twisted_renamed_helper')) # Import the newly renamed version path = sys.path[:] contents = list(os.walk(path[0])) modules = sys.modules.copy() try: from twisted_renamed_helper import module except ImportError as e: import pdb; pdb.set_trace() print(e) raise