Ticket #6240: merge-py3utils-redux.patch

File merge-py3utils-redux.patch, 18.7 KB (added by Josh Schneier, 9 years ago)
  • twisted/trial/test/test_reporter.py

     
    289289    def test_exception(self):
    290290        """
    291291        C{_trimFrames} removes traces of C{runWithWarningsSuppressed} from
    292         C{_utilspy3} when a synchronous exception happens in a C{TestCase}
     292        C{utils} when a synchronous exception happens in a C{TestCase}
    293293        based test.
    294294        """
    295295        test = erroneous.TestAsynchronousFail('test_exception')
  • twisted/trial/reporter.py

     
    492492        When a C{TestCase} method fails synchronously, the stack looks like
    493493        this:
    494494         - [0]: C{defer.maybeDeferred}
    495          - [1]: C{_utilspy3.runWithWarningsSuppressed}
    496          - [2]: C{_utilspy3.runWithWarningsSuppressed}
     495         - [1]: C{utils.runWithWarningsSuppressed}
     496         - [2]: C{utils.runWithWarningsSuppressed}
    497497         - [3:-2]: code in the test method which failed
    498498         - [-1]: C{_synctest.fail}
    499499
     
    532532        syncCase = (("_run", "_synctest"),
    533533                    ("runWithWarningsSuppressed", "util"))
    534534        asyncCase = (("maybeDeferred", "defer"),
    535                      ("runWithWarningsSuppressed", "_utilspy3"))
     535                     ("runWithWarningsSuppressed", "utils"))
    536536
    537537        twoFrames = ((firstMethod, firstFile), (secondMethod, secondFile))
    538538        if twoFrames == syncCase:
  • twisted/trial/_asynctest.py

     
    1717# We can't import reactor at module-level because this code runs before trial
    1818# installs a user-specified reactor, installing the default reactor and
    1919# breaking reactor installation. See also #6047.
    20 from twisted.internet import defer, _utilspy3 as utils
     20from twisted.internet import defer, utils
    2121from twisted.python import failure
    2222
    2323from twisted.trial import itrial, util
  • twisted/trial/util.py

     
    2424from random import randrange
    2525
    2626from twisted.python.compat import _PY3
    27 from twisted.internet import defer, _utilspy3 as utils, interfaces
     27from twisted.internet import defer, utils, interfaces
    2828from twisted.python.failure import Failure
    2929from twisted.python import deprecate, versions
    3030from twisted.python.filepath import FilePath
  • twisted/test/test_iutils.py

     
    55Test running processes with the APIs in L{twisted.internet.utils}.
    66"""
    77
     8from __future__ import division, absolute_import
     9
    810import warnings, os, stat, sys, signal
    911
     12from twisted.python.compat import _PY3
    1013from twisted.python.runtime import platform
    1114from twisted.trial import unittest
    1215from twisted.internet import error, reactor, utils, interfaces
     16from twisted.internet.defer import Deferred
     17from twisted.python.test.test_util import SuppressedWarningsTests
    1318
    1419
    1520class ProcessUtilsTests(unittest.TestCase):
     
    115120            "sys.exit(1)"
    116121            ])
    117122
    118         def gotOutputAndValue((out, err, code)):
     123        def gotOutputAndValue(out_err_code):
     124            out, err, code = out_err_code
    119125            self.assertEqual(out, "hello world!\n")
    120126            self.assertEqual(err, "goodbye world!" + os.linesep)
    121127            self.assertEqual(code, 1)
     
    141147            "sys.stderr.flush()",
    142148            "os.kill(os.getpid(), signal.SIGKILL)"])
    143149
    144         def gotOutputAndValue((out, err, sig)):
     150        def gotOutputAndValue(out_err_sig):
     151            out, err, sig = out_err_sig
    145152            self.assertEqual(out, "stdout bytes\n")
    146153            self.assertEqual(err, "stderr bytes\n")
    147154            self.assertEqual(sig, signal.SIGKILL)
     
    188195        L{getProcessOutputAndValue} runs the given command with the working
    189196        directory given by the C{path} parameter.
    190197        """
    191         def check((out, err, status), dir):
     198        def check(out_err_status, dir):
     199            out, err, status = out_err_status
    192200            self.assertEqual(out, dir)
    193201            self.assertEqual(status, 0)
    194202        return self._pathTest(utils.getProcessOutputAndValue, check)
     
    252260        directory as the parent process and succeeds even if the current
    253261        working directory is not accessible.
    254262        """
    255         def check((out, err, status), dir):
     263        def check(out_err_status, dir):
     264            out, err, status = out_err_status
    256265            self.assertEqual(out, dir)
    257266            self.assertEqual(status, 0)
    258267        return self._defaultPathTest(
    259268            utils.getProcessOutputAndValue, check)
     269
     270
     271
     272class SuppressWarningsTests(unittest.SynchronousTestCase):
     273    """
     274    Tests for L{utils.suppressWarnings}.
     275    """
     276    def test_suppressWarnings(self):
     277        """
     278        L{utils.suppressWarnings} decorates a function so that the given
     279        warnings are suppressed.
     280        """
     281        result = []
     282        def showwarning(self, *a, **kw):
     283            result.append((a, kw))
     284        self.patch(warnings, "showwarning", showwarning)
     285
     286        def f(msg):
     287            warnings.warn(msg)
     288        g = utils.suppressWarnings(f, (('ignore',), dict(message="This is message")))
     289
     290        # Start off with a sanity check - calling the original function
     291        # should emit the warning.
     292        f("Sanity check message")
     293        self.assertEqual(len(result), 1)
     294
     295        # Now that that's out of the way, call the wrapped function, and
     296        # make sure no new warnings show up.
     297        g("This is message")
     298        self.assertEqual(len(result), 1)
     299
     300        # Finally, emit another warning which should not be ignored, and
     301        # make sure it is not.
     302        g("Unignored message")
     303        self.assertEqual(len(result), 2)
     304
     305
     306
     307class DeferredSuppressedWarningsTests(SuppressedWarningsTests):
     308    """
     309    Tests for L{utils.runWithWarningsSuppressed}, the version that supports
     310    Deferreds.
     311    """
     312    # Override the non-Deferred-supporting function from the base class with
     313    # the function we are testing in this class:
     314    runWithWarningsSuppressed = staticmethod(utils.runWithWarningsSuppressed)
     315
     316    def test_deferredCallback(self):
     317        """
     318        If the function called by L{utils.runWithWarningsSuppressed} returns a
     319        C{Deferred}, the warning filters aren't removed until the Deferred
     320        fires.
     321        """
     322        filters = [(("ignore", ".*foo.*"), {}),
     323                   (("ignore", ".*bar.*"), {})]
     324        result = Deferred()
     325        self.runWithWarningsSuppressed(filters, lambda: result)
     326        warnings.warn("ignore foo")
     327        result.callback(3)
     328        warnings.warn("ignore foo 2")
     329        self.assertEqual(
     330            ["ignore foo 2"], [w['message'] for w in self.flushWarnings()])
     331
     332    def test_deferredErrback(self):
     333        """
     334        If the function called by L{utils.runWithWarningsSuppressed} returns a
     335        C{Deferred}, the warning filters aren't removed until the Deferred
     336        fires with an errback.
     337        """
     338        filters = [(("ignore", ".*foo.*"), {}),
     339                   (("ignore", ".*bar.*"), {})]
     340        result = Deferred()
     341        d = self.runWithWarningsSuppressed(filters, lambda: result)
     342        warnings.warn("ignore foo")
     343        result.errback(ZeroDivisionError())
     344        d.addErrback(lambda f: f.trap(ZeroDivisionError))
     345        warnings.warn("ignore foo 2")
     346        self.assertEqual(
     347            ["ignore foo 2"], [w['message'] for w in self.flushWarnings()])
     348
     349if _PY3:
     350    del ProcessUtilsTest
  • twisted/internet/test/test_utilspy3.py

     
    1 # Copyright (c) Twisted Matrix Laboratories.
    2 # See LICENSE for details.
    3 
    4 """
    5 Tests for L{twisted.internet._utilspy3}.
    6 """
    7 
    8 from __future__ import division, absolute_import
    9 
    10 import warnings
    11 
    12 from twisted.trial import unittest
    13 from twisted.internet import _utilspy3 as utils
    14 from twisted.internet.defer import Deferred
    15 from twisted.python.test.test_util import SuppressedWarningsTests
    16 
    17 class SuppressWarningsTests(unittest.SynchronousTestCase):
    18     """
    19     Tests for L{utils.suppressWarnings}.
    20     """
    21     def test_suppressWarnings(self):
    22         """
    23         L{utils.suppressWarnings} decorates a function so that the given
    24         warnings are suppressed.
    25         """
    26         result = []
    27         def showwarning(self, *a, **kw):
    28             result.append((a, kw))
    29         self.patch(warnings, "showwarning", showwarning)
    30 
    31         def f(msg):
    32             warnings.warn(msg)
    33         g = utils.suppressWarnings(f, (('ignore',), dict(message="This is message")))
    34 
    35         # Start off with a sanity check - calling the original function
    36         # should emit the warning.
    37         f("Sanity check message")
    38         self.assertEqual(len(result), 1)
    39 
    40         # Now that that's out of the way, call the wrapped function, and
    41         # make sure no new warnings show up.
    42         g("This is message")
    43         self.assertEqual(len(result), 1)
    44 
    45         # Finally, emit another warning which should not be ignored, and
    46         # make sure it is not.
    47         g("Unignored message")
    48         self.assertEqual(len(result), 2)
    49 
    50 
    51 
    52 class DeferredSuppressedWarningsTests(SuppressedWarningsTests):
    53     """
    54     Tests for L{utils.runWithWarningsSuppressed}, the version that supports
    55     Deferreds.
    56     """
    57     # Override the non-Deferred-supporting function from the base class with
    58     # the function we are testing in this class:
    59     runWithWarningsSuppressed = staticmethod(utils.runWithWarningsSuppressed)
    60 
    61     def test_deferredCallback(self):
    62         """
    63         If the function called by L{utils.runWithWarningsSuppressed} returns a
    64         C{Deferred}, the warning filters aren't removed until the Deferred
    65         fires.
    66         """
    67         filters = [(("ignore", ".*foo.*"), {}),
    68                    (("ignore", ".*bar.*"), {})]
    69         result = Deferred()
    70         self.runWithWarningsSuppressed(filters, lambda: result)
    71         warnings.warn("ignore foo")
    72         result.callback(3)
    73         warnings.warn("ignore foo 2")
    74         self.assertEqual(
    75             ["ignore foo 2"], [w['message'] for w in self.flushWarnings()])
    76 
    77     def test_deferredErrback(self):
    78         """
    79         If the function called by L{utils.runWithWarningsSuppressed} returns a
    80         C{Deferred}, the warning filters aren't removed until the Deferred
    81         fires with an errback.
    82         """
    83         filters = [(("ignore", ".*foo.*"), {}),
    84                    (("ignore", ".*bar.*"), {})]
    85         result = Deferred()
    86         d = self.runWithWarningsSuppressed(filters, lambda: result)
    87         warnings.warn("ignore foo")
    88         result.errback(ZeroDivisionError())
    89         d.addErrback(lambda f: f.trap(ZeroDivisionError))
    90         warnings.warn("ignore foo 2")
    91         self.assertEqual(
    92             ["ignore foo 2"], [w['message'] for w in self.flushWarnings()])
  • twisted/internet/_utilspy3.py

     
    1 # -*- test-case-name: twisted.internet.test.test_utilspy3 -*-
    2 # Copyright (c) Twisted Matrix Laboratories.
    3 # See LICENSE for details.
    4 
    5 """
    6 Utility methods, ported to Python 3.
    7 """
    8 
    9 from __future__ import division, absolute_import
    10 
    11 import sys, warnings
    12 from functools import wraps
    13 
    14 from twisted.python.compat import reraise
    15 from twisted.internet import defer
    16 
    17 def _resetWarningFilters(passthrough, addedFilters):
    18     for f in addedFilters:
    19         try:
    20             warnings.filters.remove(f)
    21         except ValueError:
    22             pass
    23     return passthrough
    24 
    25 
    26 def runWithWarningsSuppressed(suppressedWarnings, f, *a, **kw):
    27     """Run the function C{f}, but with some warnings suppressed.
    28 
    29     @param suppressedWarnings: A list of arguments to pass to filterwarnings.
    30                                Must be a sequence of 2-tuples (args, kwargs).
    31     @param f: A callable, followed by its arguments and keyword arguments
    32     """
    33     for args, kwargs in suppressedWarnings:
    34         warnings.filterwarnings(*args, **kwargs)
    35     addedFilters = warnings.filters[:len(suppressedWarnings)]
    36     try:
    37         result = f(*a, **kw)
    38     except:
    39         exc_info = sys.exc_info()
    40         _resetWarningFilters(None, addedFilters)
    41         reraise(exc_info[1], exc_info[2])
    42     else:
    43         if isinstance(result, defer.Deferred):
    44             result.addBoth(_resetWarningFilters, addedFilters)
    45         else:
    46             _resetWarningFilters(None, addedFilters)
    47         return result
    48 
    49 
    50 def suppressWarnings(f, *suppressedWarnings):
    51     """
    52     Wrap C{f} in a callable which suppresses the indicated warnings before
    53     invoking C{f} and unsuppresses them afterwards.  If f returns a Deferred,
    54     warnings will remain suppressed until the Deferred fires.
    55     """
    56     @wraps(f)
    57     def warningSuppressingWrapper(*a, **kw):
    58         return runWithWarningsSuppressed(suppressedWarnings, f, *a, **kw)
    59     return warningSuppressingWrapper
  • twisted/internet/utils.py

     
    66Utility methods.
    77"""
    88
     9from __future__ import division, absolute_import
     10
    911import sys, warnings
     12from functools import wraps
    1013
    1114from twisted.internet import protocol, defer
    12 from twisted.internet._utilspy3 import runWithWarningsSuppressed
    13 from twisted.internet._utilspy3 import suppressWarnings
    1415from twisted.python import failure
     16from twisted.python.compat import _PY3
    1517
    16 try:
    17     import cStringIO as StringIO
    18 except ImportError:
    19     import StringIO
     18if _PY3:
     19    from twisted.python.compat import reraise
     20    from twisted.python.compat import NativeStringIO as StringIO
     21else:
     22    try:
     23        from cStringIO import StringIO
     24    except ImportError:
     25        from StringIO import StringIO
    2026
     27
    2128def _callProtocolWithDeferred(protocol, executable, args, env, path, reactor=None):
    2229    if reactor is None:
    2330        from twisted.internet import reactor
     
    2835    return d
    2936
    3037
    31 
    3238class _UnexpectedErrorOutput(IOError):
    3339    """
    3440    Standard error data was received where it was not expected.  This is a
     
    3945        produced the data on stderr has ended (exited and all file descriptors
    4046        closed).
    4147    """
     48
    4249    def __init__(self, text, processEnded):
    4350        IOError.__init__(self, "got stderr: %r" % (text,))
    4451        self.processEnded = processEnded
     
    6673
    6774    def __init__(self, deferred, errortoo=0):
    6875        self.deferred = deferred
    69         self.s = StringIO.StringIO()
     76        self.s = StringIO()
    7077        if errortoo:
    7178            self.errReceived = self.errReceivedIsGood
    7279        else:
     
    145152
    146153    def __init__(self, deferred):
    147154        self.deferred = deferred
    148         self.outBuf = StringIO.StringIO()
    149         self.errBuf = StringIO.StringIO()
     155        self.outBuf = StringIO()
     156        self.errBuf = StringIO()
    150157        self.outReceived = self.outBuf.write
    151158        self.errReceived = self.errBuf.write
    152159
     
    160167        else:
    161168            self.deferred.callback((out, err, code))
    162169
     170
    163171def getProcessOutputAndValue(executable, args=(), env={}, path=None,
    164172                             reactor=None):
    165173    """Spawn a process and returns a Deferred that will be called back with
     
    171179                                    reactor)
    172180
    173181
     182def _resetWarningFilters(passthrough, addedFilters):
     183    for f in addedFilters:
     184        try:
     185            warnings.filters.remove(f)
     186        except ValueError:
     187            pass
     188    return passthrough
     189
     190
     191def runWithWarningsSuppressed(suppressedWarnings, f, *a, **kw):
     192    """Run the function C{f}, but with some warnings suppressed.
     193
     194    @param suppressedWarnings: A list of arguments to pass to filterwarnings.
     195                               Must be a sequence of 2-tuples (args, kwargs).
     196    @param f: A callable, followed by its arguments and keyword arguments
     197    """
     198    for args, kwargs in suppressedWarnings:
     199        warnings.filterwarnings(*args, **kwargs)
     200    addedFilters = warnings.filters[:len(suppressedWarnings)]
     201    try:
     202        result = f(*a, **kw)
     203    except:
     204        exc_info = sys.exc_info()
     205        _resetWarningFilters(None, addedFilters)
     206        reraise(exc_info[1], exc_info[2])
     207    else:
     208        if isinstance(result, defer.Deferred):
     209            result.addBoth(_resetWarningFilters, addedFilters)
     210        else:
     211            _resetWarningFilters(None, addedFilters)
     212        return result
     213
     214
     215def suppressWarnings(f, *suppressedWarnings):
     216    """
     217    Wrap C{f} in a callable which suppresses the indicated warnings before
     218    invoking C{f} and unsuppresses them afterwards.  If f returns a Deferred,
     219    warnings will remain suppressed until the Deferred fires.
     220    """
     221    @wraps(f)
     222    def warningSuppressingWrapper(*a, **kw):
     223        return runWithWarningsSuppressed(suppressedWarnings, f, *a, **kw)
     224    return warningSuppressingWrapper
     225
     226
    174227__all__ = [
    175228    "runWithWarningsSuppressed", "suppressWarnings",
    176 
    177229    "getProcessOutput", "getProcessValue", "getProcessOutputAndValue",
    178230    ]
     231
     232if _PY3:
     233    __all3__ = ["runWithWarningsSuppressed", "suppressWarnings"]
     234    for name in __all__[:]:
     235        if name not in __all3__:
     236            __all__.remove(name)
     237            del globals()[name]
     238    del name, __all3__
  • admin/_twistedpython3.py

     
    5252    "twisted.internet.test.reactormixins",
    5353    "twisted.internet.threads",
    5454    "twisted.internet.udp",
    55     "twisted.internet._utilspy3",
    5655    "twisted.names",
    5756    "twisted.names.cache",
    5857    "twisted.names.client",
     
    140139    "twisted.internet.test.test_tls",
    141140    "twisted.internet.test.test_udp",
    142141    "twisted.internet.test.test_udp_internals",
    143     "twisted.internet.test.test_utilspy3",
    144142    "twisted.names.test.test_cache",
    145143    "twisted.names.test.test_client",
    146144    "twisted.names.test.test_common",