Ticket #6437: 6437.patch

File 6437.patch, 2.9 KB (added by eddie, 5 years ago)
  • twisted/trial/test/test_warning.py

     
    359359        self.assertEqual(len(self.flushWarnings([module.foo])), 1)
    360360
    361361
     362    def test_removedSource(self):
     363        '''
     364        Warnings emitted by a function defined in a file which has been removed
     365        since it was initially compiled can still be flushed.
     366        '''
     367        package = FilePath(self.mktemp().encode('utf-8')).child(
     368            b'twisted_removed_helper')
     369        package.makedirs()
     370        package.child(b'__init__.py').setContent(b'')
     371        sourceFile = package.child(b'module.py')
     372        sourceFile.setContent(b'''
     373import warnings
     374def foo():
     375    warnings.warn("oh no")
     376''')
     377        pathEntry = package.parent().path.decode('utf-8')
     378        sys.path.insert(0, pathEntry)
     379        self.addCleanup(sys.path.remove, pathEntry)
    362380
     381        # Import it to cause pycs to be generated
     382        from twisted_removed_helper import module
     383
     384        # Clean up the state resulting from that import; we're not going to
     385        #  immediately use this module, so it should go away.
     386        del sys.modules['twisted_removed_helper']
     387        del sys.modules[module.__name__]
     388
     389        # Some Python versions have extra state related to the just
     390        # imported/renamed package.  Clean it up too.  See also
     391        # http://bugs.python.org/issue15912
     392        try:
     393            from importlib import invalidate_caches
     394        except ImportError:
     395            pass
     396        else:
     397            invalidate_caches()
     398
     399        # Remove the source file
     400        sourceFile.remove()
     401
     402        # Import the pyc based version
     403        from twisted_removed_helper import module
     404        self.addCleanup(sys.modules.pop, 'twisted_removed_helper')
     405        self.addCleanup(sys.modules.pop, module.__name__)
     406
     407        # Generate the warning
     408        module.foo()
     409
     410        # Flush it
     411        self.assertEqual(len(self.flushWarnings([module.foo])), 1)
     412
     413
     414
    363415class FakeWarning(Warning):
    364416    pass
    365417
  • twisted/trial/_synctest.py

     
    10641064                    # somewhat broken in Python < 2.6.  See Python bug 4845.
    10651065                    aModule = sys.modules[aFunction.__module__]
    10661066                    filename = inspect.getabsfile(aModule)
    1067 
     1067                    if filename.lower()[-4:] in ('.pyc', '.pyo'):
     1068                        filename = filename[:-4] + '.py'
    10681069                    if filename != os.path.normcase(aWarning.filename):
    10691070                        continue
    10701071                    lineStarts = list(_findlinestarts(aFunction.__code__))