Ticket #6437: 6437_2.patch

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

     
    280280
    281281    def test_missingSource(self):
    282282        """
    283         Warnings emitted by a function the source code of which is not
    284         available can still be flushed.
     283        Warnings emitted by a function the source code of which is
     284        removed after import can still be flushed.
    285285        """
    286286        package = FilePath(self.mktemp().encode('utf-8')).child(b'twisted_private_helper')
    287287        package.makedirs()
     
    359359        self.assertEqual(len(self.flushWarnings([module.foo])), 1)
    360360
    361361
    362     def test_removedSource(self):
     362    def test_importFromPYC(self):
    363363        '''
    364         Warnings emitted by a function defined in a file which has been removed
    365         since it was initially compiled can still be flushed.
     364        Warnings emitted by a function defined in a .pyc file whose source file
     365        is removed before import can still be flushed.
    366366        '''
    367367        package = FilePath(self.mktemp().encode('utf-8')).child(
    368368            b'twisted_removed_helper')
     
    382382        from twisted_removed_helper import module
    383383
    384384        # Clean up the state resulting from that import; we're not going to
    385         #  immediately use this module, so it should go away.
     385        # immediately use this module, so it should go away.
    386386        del sys.modules['twisted_removed_helper']
    387387        del sys.modules[module.__name__]
    388388
    389389        # Some Python versions have extra state related to the just
    390         # imported/renamed package.  Clean it up too. See also
     390        # imported/renamed package. Clean it up too. See also
    391391        # http://bugs.python.org/issue15912
    392392        try:
    393393            from importlib import invalidate_caches
     
    399399        # Remove the source file
    400400        sourceFile.remove()
    401401
    402         # Import the pyc based version
     402        # .pyc file location has been changed in py3 according to PEP3147
     403        if sys.version_info[0] == 3:
     404            filename = ("module.cpython-" +
     405                        str(sys.version_info[0]) +
     406                        str(sys.version_info[1]) +
     407                        ".pyc").encode("ascii")
     408            FilePath(package.path + b'/__pycache__/' + filename).moveTo(
     409                FilePath(package.path + b'/module.pyc'))
     410
     411        # At this point, The source file has already been deleted
     412        # with only the .pyc file remaining
     413        # We now import the pyc based version
    403414        from twisted_removed_helper import module
    404415        self.addCleanup(sys.modules.pop, 'twisted_removed_helper')
    405416        self.addCleanup(sys.modules.pop, module.__name__)