Ticket #7993: wsgi-python3-7993-6.2.patch

File wsgi-python3-7993-6.2.patch, 5.4 KB (added by Gavin Panella, 4 years ago)

Second incremental patch to the wsgi-py3-7993-6 branch; apply after wsgi-python3-7993-6.1.patch.

  • twisted/web/test/test_wsgi.py

    diff --git a/twisted/web/test/test_wsgi.py b/twisted/web/test/test_wsgi.py
    index 6705572..0ee17ab 100644
    a b class EnvironTests(WSGITestsMixin, TestCase): 
    798798
    799799
    800800    @inlineCallbacks
    801     def test_wsgiErrorsAcceptsOnlyNativeStrings(self):
     801    def test_wsgiErrorsExpectsOnlyNativeStringsInPython2(self):
    802802        """
    803         The C{'wsgi.errors'} file-like object from the C{environ} C{dict} will
    804         permit writes of only native strings in Python 3, and will warn
    805         against the use of non-native strings in Python 2.
     803        The C{'wsgi.errors'} file-like object from the C{environ} C{dict}
     804        expects writes of only native strings in Python 2. Some existing WSGI
     805        applications may write non-native (i.e. C{unicode}) strings so, for
     806        compatibility, these elicit only a warning in Python 2.
    806807        """
     808        if _PY3:
     809            raise SkipTest("Not relevant in Python 3")
     810
    807811        request, result = self.prepareRequest()
    808812        request.requestReceived()
    809813        environ, _ = yield result
    810814        errors = environ["wsgi.errors"]
    811815
    812         if _PY3:
    813             # In Python 3, TypeError is raised.
    814             error = self.assertRaises(TypeError, errors.write, b"fred")
    815             self.assertEqual(
    816                 "write() argument must be str, not b'fred' (bytes)",
    817                 str(error))
    818         else:
    819             # In Python 2, only a warning is issued; existing WSGI
    820             # applications may rely on this non-compliant behaviour.
    821             with warnings.catch_warnings(record=True) as caught:
    822                 errors.write(u"fred")
    823             self.assertEqual(1, len(caught))
    824             self.assertEqual(UnicodeWarning, caught[0].category)
    825             self.assertEqual(
    826                 "write() argument should be str, not u'fred' (unicode)",
    827                 str(caught[0].message))
     816        with warnings.catch_warnings(record=True) as caught:
     817            errors.write(u"fred")
     818        self.assertEqual(1, len(caught))
     819        self.assertEqual(UnicodeWarning, caught[0].category)
     820        self.assertEqual(
     821            "write() argument should be str, not u'fred' (unicode)",
     822            str(caught[0].message))
     823
     824
     825    @inlineCallbacks
     826    def test_wsgiErrorsAcceptsOnlyNativeStringsInPython3(self):
     827        """
     828        The C{'wsgi.errors'} file-like object from the C{environ} C{dict}
     829        permits writes of only native strings in Python 3, and raises
     830        C{TypeError} for writes of non-native strings.
     831        """
     832        if not _PY3:
     833            raise SkipTest("Relevant only in Python 3")
     834
     835        request, result = self.prepareRequest()
     836        request.requestReceived()
     837        environ, _ = yield result
     838        errors = environ["wsgi.errors"]
     839
     840        error = self.assertRaises(TypeError, errors.write, b"fred")
     841        self.assertEqual(
     842            "write() argument must be str, not b'fred' (bytes)",
     843            str(error))
    828844
    829845
    830846
    class StartResponseTests(WSGITestsMixin, TestCase): 
    13141330    @inlineCallbacks
    13151331    def test_headersShouldBePlainList(self):
    13161332        """
    1317         The headers passed to the I{start_response} callable SHOULD be a plain
    1318         list.
     1333        According to PEP-3333, the headers passed to the I{start_response}
     1334        callable MUST be a plain list:
     1335
     1336          The response_headers argument ... must be a Python list; i.e.
     1337          type(response_headers) is ListType
     1338
     1339        However, for bug-compatiblity, any sequence is accepted. In both
     1340        Python 2 and Python 3, only a warning is issued when a sequence other
     1341        than a list is encountered.
    13191342        """
    13201343        def application(environ, startResponse):
    13211344            startResponse("200 OK", (("not", "list"),))
    class StartResponseTests(WSGITestsMixin, TestCase): 
    13231346
    13241347        request, result = self.prepareRequest(application)
    13251348
    1326         # In both Python 2 and Python 3, only a warning is issued; existing
    1327         # WSGI applications may rely on this non-compliant behaviour, and we
    1328         # can actually work with any sequence type.
    13291349        with warnings.catch_warnings(record=True) as caught:
    13301350            request.requestReceived()
    13311351            yield result
    class StartResponseTests(WSGITestsMixin, TestCase): 
    13611381    @inlineCallbacks
    13621382    def test_headersShouldEachBeTuple(self):
    13631383        """
    1364         Each header passed to the I{start_response} callable SHOULD be a
    1365         tuple.
     1384        According to PEP-3333, each header passed to the I{start_response}
     1385        callable should be a tuple:
     1386
     1387          The response_headers argument is a list of (header_name,
     1388          header_value) tuples
     1389
     1390        However, for bug-compatiblity, any 2 element sequence is also
     1391        accepted. In both Python 2 and Python 3, only a warning is issued when
     1392        a sequence other than a tuple is encountered.
    13661393        """
    13671394        def application(environ, startResponse):
    13681395            startResponse("200 OK", [["not", "tuple"]])
    class StartResponseTests(WSGITestsMixin, TestCase): 
    13701397
    13711398        request, result = self.prepareRequest(application)
    13721399
    1373         # In both Python 2 and Python 3, only a warning is issued; existing
    1374         # WSGI applications may rely on this non-compliant behaviour, and we
    1375         # can actually work with any sequence type.
    13761400        with warnings.catch_warnings(record=True) as caught:
    13771401            request.requestReceived()
    13781402            yield result