Ticket #2491: no-module-globals.patch

File no-module-globals.patch, 6.4 KB (added by Michael, 6 years ago)
  • twisted/python/deprecate.py

    diff --git twisted/python/deprecate.py twisted/python/deprecate.py
    index 02c107c..0400286 100644
    def warnAboutFunction(offender, warningString): 
    470470    filename = inspect.getabsfile(offenderModule)
    471471    lineStarts = list(findlinestarts(offender.func_code))
    472472    lastLineNo = lineStarts[-1][1]
     473    globals = offender.func_globals
    473474
    474475    kwargs = dict(
    475476        category=DeprecationWarning,
    476477        filename=filename,
    477478        lineno=lastLineNo,
    478         module='', # offenderModule.__name__,
    479         registry={}, # getattr(offenderModule, "__warningregistry__", {}),
    480         module_globals={}, # offenderModule.__dict__
     479        module=offenderModule.__name__,
     480        registry=globals.setdefault("__warningregistry__", {}),
     481        module_globals=None,
    481482        )
    482483
    483     if sys.version < (2, 5):
     484    if sys.version_info[:2] < (2, 5):
    484485        kwargs.pop('module_globals')
     486
    485487    warn_explicit(warningString, **kwargs)
  • twisted/python/test/test_deprecate.py

    diff --git twisted/python/test/test_deprecate.py twisted/python/test/test_deprecate.py
    index cc4b157..733d8e1 100644
    Tests for Twisted's deprecation framework, L{twisted.python.deprecate}. 
    66"""
    77
    88import sys, types
     9import warnings
     10import zipfile
     11import linecache
    912
    1013from twisted.trial.unittest import TestCase
    1114
    deprecatedModuleAttribute( 
    513516        # make sure it's the right module.
    514517        self.assertEquals(module.__file__.rsplit(".", 1)[0],
    515518                          package.child('module.py').path.rsplit(".", 1)[0])
    516         warnings = self.flushWarnings([self.test_deprecatedModule])
    517         self.assertEquals(len(warnings), 1)
     519        warningsShown = self.flushWarnings([self.test_deprecatedModule])
     520        self.assertEquals(len(warningsShown), 1)
    518521
    519522
    520523
    def callTestFunction(): 
    558561        def aFunc():
    559562            pass
    560563        deprecate.warnAboutFunction(aFunc, 'A Warning Message')
    561         warnings = self.flushWarnings()
     564        warningsShown = self.flushWarnings()
    562565        filename = __file__
    563566        if filename.lower().endswith('.pyc'):
    564567            filename = filename[:-1]
    565         self.assertEquals(warnings[0]["filename"], filename)
    566         self.assertEquals(warnings[0]["message"], "A Warning Message")
     568        self.assertEquals(warningsShown[0]["filename"], filename)
     569        self.assertEquals(warningsShown[0]["message"], "A Warning Message")
    567570
    568571
    569572    def test_warningLineNumber(self):
    def callTestFunction(): 
    573576        """
    574577        from twisted_private_helper import module
    575578        module.callTestFunction()
    576         warnings = self.flushWarnings()
     579        warningsShown = self.flushWarnings()
    577580        self.assertEquals(
    578             warnings[0]["filename"],
     581            warningsShown[0]["filename"],
    579582            self.package.dirname() + '/twisted_private_helper/module.py')
    580583        # Line number 9 is the last line in the testFunction in the helper
    581584        # module.
    582         self.assertEquals(warnings[0]["lineno"], 9)
    583         self.assertEquals(warnings[0]["message"], "A Warning String")
    584         self.assertEquals(len(warnings), 1)
     585        self.assertEquals(warningsShown[0]["lineno"], 9)
     586        self.assertEquals(warningsShown[0]["message"], "A Warning String")
     587        self.assertEquals(len(warningsShown), 1)
    585588
    586589
    587590    def test_renamedFile(self):
    def callTestFunction(): 
    606609        self.addCleanup(sys.modules.pop, module.__name__)
    607610
    608611        module.callTestFunction()
    609         warnings = self.flushWarnings()
     612        warningsShown = self.flushWarnings()
    610613        self.assertEquals(
    611             warnings[0]["filename"],
     614            warningsShown[0]["filename"],
    612615            self.package.dirname() + '/twisted_renamed_helper/module.py')
    613         self.assertEquals(warnings[0]["lineno"], 9)
    614         self.assertEquals(warnings[0]["message"], "A Warning String")
    615         self.assertEquals(len(warnings), 1)
     616        self.assertEquals(warningsShown[0]["lineno"], 9)
     617        self.assertEquals(warningsShown[0]["message"], "A Warning String")
     618        self.assertEquals(len(warningsShown), 1)
     619
     620
     621    def test_filtered_warning(self):
     622        """
     623        L{deprecate.warnAboutFunction} emits a warning that will be
     624        filtered if L{warnings.filterwarning} is called with the
     625        module name of the deprecated function.
     626        """
     627       
     628        # must remove the warnings.simplefilter("always") that
     629        # unittest._collectWarnings adds otherwise we can't filter
     630        # a warning
     631        always_simplefilter = warnings.filters.pop(0) 
     632        self.assertEquals(always_simplefilter, ("always", None, Warning, None, 0))
     633        self.addCleanup(warnings.filters.insert, 0, always_simplefilter)
     634
     635        warnings.filterwarnings(action="ignore", module="twisted_private_helper", append=True)
     636        self.addCleanup(warnings.filters.pop)
     637
     638        from twisted_private_helper import module
     639        module.callTestFunction()
     640       
     641        warningsShown = self.flushWarnings()
     642        self.assertEquals(len(warningsShown), 0)
     643
     644
     645    def test_filtered_once_warning(self):
     646        """
     647        L{deprecate.warnAboutFunction} emits a warning that will be
     648        filtered once if L{warnings.filterwarning} is called with the
     649        module name of the deprecated function and an action of once.
     650        """
     651       
     652        # must remove the warnings.simplefilter("always") that
     653        # unittest._collectWarnings adds otherwise we can't filter
     654        # a warning
     655        always_simplefilter = warnings.filters.pop(0) 
     656        self.assertEquals(always_simplefilter, ("always", None, Warning, None, 0))
     657        self.addCleanup(warnings.filters.insert, 0, always_simplefilter)
     658
     659        warnings.filterwarnings(action="module", module="twisted_private_helper", append=True)
     660        self.addCleanup(warnings.filters.pop)
     661
     662        from twisted_private_helper import module
     663        module.callTestFunction()
     664        module.callTestFunction()
     665       
     666        warningsShown = self.flushWarnings()
     667        self.assertEquals(len(warningsShown), 1)
     668        message = warningsShown[0]['message']
     669        category = warningsShown[0]['category']
     670        filename = warningsShown[0]['filename']
     671        lineno = warningsShown[0]['lineno']
     672        msg = warnings.formatwarning(message, category, filename, lineno)
     673