Ticket #6240: merge-py3utils.2.patch

File merge-py3utils.2.patch, 12.8 KB (added by Josh Schneier, 9 years ago)

NativeStringIO was having a method called on it, extra periods I didn't delete it

  • 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
    1012from twisted.python.runtime import platform
    1113from twisted.trial import unittest
    1214from twisted.internet import error, reactor, utils, interfaces
     15from twisted.internet.defer import Deferred
     16from twisted.python.test.test_util import SuppressedWarningsTests
    1317
    1418
    1519class ProcessUtilsTests(unittest.TestCase):
     
    115119            "sys.exit(1)"
    116120            ])
    117121
    118         def gotOutputAndValue((out, err, code)):
     122        def gotOutputAndValue(out_err_code):
     123            out, err, code = out_err_code
    119124            self.assertEqual(out, "hello world!\n")
    120125            self.assertEqual(err, "goodbye world!" + os.linesep)
    121126            self.assertEqual(code, 1)
     
    141146            "sys.stderr.flush()",
    142147            "os.kill(os.getpid(), signal.SIGKILL)"])
    143148
    144         def gotOutputAndValue((out, err, sig)):
     149        def gotOutputAndValue(out_err_sig):
     150            out, err, sig = out_err_sig
    145151            self.assertEqual(out, "stdout bytes\n")
    146152            self.assertEqual(err, "stderr bytes\n")
    147153            self.assertEqual(sig, signal.SIGKILL)
     
    188194        L{getProcessOutputAndValue} runs the given command with the working
    189195        directory given by the C{path} parameter.
    190196        """
    191         def check((out, err, status), dir):
     197        def check(out_err_status, dir):
     198            out, err, status = out_err_status
    192199            self.assertEqual(out, dir)
    193200            self.assertEqual(status, 0)
    194201        return self._pathTest(utils.getProcessOutputAndValue, check)
     
    252259        directory as the parent process and succeeds even if the current
    253260        working directory is not accessible.
    254261        """
    255         def check((out, err, status), dir):
     262        def check(out_err_status, dir):
     263            out, err, status = out_err_status
    256264            self.assertEqual(out, dir)
    257265            self.assertEqual(status, 0)
    258266        return self._defaultPathTest(
    259267            utils.getProcessOutputAndValue, check)
     268
     269
     270
     271class SuppressWarningsTests(unittest.SynchronousTestCase):
     272    """
     273    Tests for L{utils.suppressWarnings}.
     274    """
     275    def test_suppressWarnings(self):
     276        """
     277        L{utils.suppressWarnings} decorates a function so that the given
     278        warnings are suppressed.
     279        """
     280        result = []
     281        def showwarning(self, *a, **kw):
     282            result.append((a, kw))
     283        self.patch(warnings, "showwarning", showwarning)
     284
     285        def f(msg):
     286            warnings.warn(msg)
     287        g = utils.suppressWarnings(f, (('ignore',), dict(message="This is message")))
     288
     289        # Start off with a sanity check - calling the original function
     290        # should emit the warning.
     291        f("Sanity check message")
     292        self.assertEqual(len(result), 1)
     293
     294        # Now that that's out of the way, call the wrapped function, and
     295        # make sure no new warnings show up.
     296        g("This is message")
     297        self.assertEqual(len(result), 1)
     298
     299        # Finally, emit another warning which should not be ignored, and
     300        # make sure it is not.
     301        g("Unignored message")
     302        self.assertEqual(len(result), 2)
     303
     304
     305
     306class DeferredSuppressedWarningsTests(SuppressedWarningsTests):
     307    """
     308    Tests for L{utils.runWithWarningsSuppressed}, the version that supports
     309    Deferreds.
     310    """
     311    # Override the non-Deferred-supporting function from the base class with
     312    # the function we are testing in this class:
     313    runWithWarningsSuppressed = staticmethod(utils.runWithWarningsSuppressed)
     314
     315    def test_deferredCallback(self):
     316        """
     317        If the function called by L{utils.runWithWarningsSuppressed} returns a
     318        C{Deferred}, the warning filters aren't removed until the Deferred
     319        fires.
     320        """
     321        filters = [(("ignore", ".*foo.*"), {}),
     322                   (("ignore", ".*bar.*"), {})]
     323        result = Deferred()
     324        self.runWithWarningsSuppressed(filters, lambda: result)
     325        warnings.warn("ignore foo")
     326        result.callback(3)
     327        warnings.warn("ignore foo 2")
     328        self.assertEqual(
     329            ["ignore foo 2"], [w['message'] for w in self.flushWarnings()])
     330
     331    def test_deferredErrback(self):
     332        """
     333        If the function called by L{utils.runWithWarningsSuppressed} returns a
     334        C{Deferred}, the warning filters aren't removed until the Deferred
     335        fires with an errback.
     336        """
     337        filters = [(("ignore", ".*foo.*"), {}),
     338                   (("ignore", ".*bar.*"), {})]
     339        result = Deferred()
     340        d = self.runWithWarningsSuppressed(filters, lambda: result)
     341        warnings.warn("ignore foo")
     342        result.errback(ZeroDivisionError())
     343        d.addErrback(lambda f: f.trap(ZeroDivisionError))
     344        warnings.warn("ignore foo 2")
     345        self.assertEqual(
     346            ["ignore foo 2"], [w['message'] for w in self.flushWarnings()])
  • 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 NativeStringIO, reraise
    1517
    16 try:
    17     import cStringIO as StringIO
    18 except ImportError:
    19     import StringIO
    2018
    2119def _callProtocolWithDeferred(protocol, executable, args, env, path, reactor=None):
    2220    if reactor is None:
     
    2826    return d
    2927
    3028
    31 
    3229class _UnexpectedErrorOutput(IOError):
    3330    """
    3431    Standard error data was received where it was not expected.  This is a
     
    3936        produced the data on stderr has ended (exited and all file descriptors
    4037        closed).
    4138    """
     39
    4240    def __init__(self, text, processEnded):
    4341        IOError.__init__(self, "got stderr: %r" % (text,))
    4442        self.processEnded = processEnded
     
    6664
    6765    def __init__(self, deferred, errortoo=0):
    6866        self.deferred = deferred
    69         self.s = StringIO.StringIO()
     67        self.s = NativeStringIO()
    7068        if errortoo:
    7169            self.errReceived = self.errReceivedIsGood
    7270        else:
     
    145143
    146144    def __init__(self, deferred):
    147145        self.deferred = deferred
    148         self.outBuf = StringIO.StringIO()
    149         self.errBuf = StringIO.StringIO()
     146        self.outBuf = NativeStringIO()
     147        self.errBuf = NativeStringIO()
    150148        self.outReceived = self.outBuf.write
    151149        self.errReceived = self.errBuf.write
    152150
     
    160158        else:
    161159            self.deferred.callback((out, err, code))
    162160
     161
    163162def getProcessOutputAndValue(executable, args=(), env={}, path=None,
    164163                             reactor=None):
    165164    """Spawn a process and returns a Deferred that will be called back with
     
    171170                                    reactor)
    172171
    173172
     173def _resetWarningFilters(passthrough, addedFilters):
     174    for f in addedFilters:
     175        try:
     176            warnings.filters.remove(f)
     177        except ValueError:
     178            pass
     179    return passthrough
     180
     181
     182def runWithWarningsSuppressed(suppressedWarnings, f, *a, **kw):
     183    """Run the function C{f}, but with some warnings suppressed.
     184
     185    @param suppressedWarnings: A list of arguments to pass to filterwarnings.
     186                               Must be a sequence of 2-tuples (args, kwargs).
     187    @param f: A callable, followed by its arguments and keyword arguments
     188    """
     189    for args, kwargs in suppressedWarnings:
     190        warnings.filterwarnings(*args, **kwargs)
     191    addedFilters = warnings.filters[:len(suppressedWarnings)]
     192    try:
     193        result = f(*a, **kw)
     194    except:
     195        exc_info = sys.exc_info()
     196        _resetWarningFilters(None, addedFilters)
     197        reraise(exc_info[1], exc_info[2])
     198    else:
     199        if isinstance(result, defer.Deferred):
     200            result.addBoth(_resetWarningFilters, addedFilters)
     201        else:
     202            _resetWarningFilters(None, addedFilters)
     203        return result
     204
     205
     206def suppressWarnings(f, *suppressedWarnings):
     207    """
     208    Wrap C{f} in a callable which suppresses the indicated warnings before
     209    invoking C{f} and unsuppresses them afterwards.  If f returns a Deferred,
     210    warnings will remain suppressed until the Deferred fires.
     211    """
     212    @wraps(f)
     213    def warningSuppressingWrapper(*a, **kw):
     214        return runWithWarningsSuppressed(suppressedWarnings, f, *a, **kw)
     215    return warningSuppressingWrapper
     216
     217
    174218__all__ = [
    175219    "runWithWarningsSuppressed", "suppressWarnings",
    176 
    177220    "getProcessOutput", "getProcessValue", "getProcessOutputAndValue",
    178221    ]
  • 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
  • admin/_twistedpython3.py

     
    5252    "twisted.internet.test.reactormixins",
    5353    "twisted.internet.threads",
    5454    "twisted.internet.udp",
    55     "twisted.internet._utilspy3",
     55    "twisted.internet.utils",
    5656    "twisted.names",
    5757    "twisted.names.cache",
    5858    "twisted.names.client",
     
    140140    "twisted.internet.test.test_tls",
    141141    "twisted.internet.test.test_udp",
    142142    "twisted.internet.test.test_udp_internals",
    143     "twisted.internet.test.test_utilspy3",
    144143    "twisted.names.test.test_cache",
    145144    "twisted.names.test.test_client",
    146145    "twisted.names.test.test_common",
     
    165164    "twisted.test.test_failure",
    166165    "twisted.test.test_fdesc",
    167166    "twisted.test.test_internet",
     167    "twisted.test.test_iutils",
    168168    "twisted.test.test_log",
    169169    "twisted.test.test_loopback",
    170170    "twisted.test.test_monkey",