Ticket #6177: web.static-py3k-wip.patch

File web.static-py3k-wip.patch, 22.1 KB (added by ihrachyshka, 3 years ago)

First draft, not completed

  • setup3.py

    diff --git setup3.py setup3.py
    index cc54546..f01b521 100644
    modules = [ 
    110110    "twisted.web.test",
    111111    "twisted.web.test.requesthelper",
    112112    "twisted.web._version",
     113    "twisted.web.static",
    113114]
    114115
    115116
    testModules = [ 
    197198    "twisted.web.test.test_http_headers",
    198199    "twisted.web.test.test_resource",
    199200    "twisted.web.test.test_web",
     201    "twisted.web.test.test_static",
    200202]
    201203
    202204
  • twisted/persisted/styles.py

    diff --git twisted/persisted/styles.py twisted/persisted/styles.py
    index e3ca39b..93c06ba 100644
    Different styles of persisted objects. 
    1010
    1111# System Imports
    1212import types
    13 import copy_reg
     13try:
     14    import copyreg
     15except ImportError:
     16    import copy_reg as copyreg
    1417import copy
    1518import inspect
    1619import sys
    1720
    18 try:
    19     import cStringIO as StringIO
    20 except ImportError:
    21     import StringIO
    22 
    2321# Twisted Imports
    2422from twisted.python import log
    2523from twisted.python import reflect
     24from twisted.python.compat import NativeStringIO as StringIO
    2625
    2726oldModules = {}
    2827
    oldModules = {} 
    3029## be registerable...
    3130
    3231def pickleMethod(method):
    33     'support function for copy_reg to pickle method refs'
     32    'support function for copyreg to pickle method refs'
    3433    return unpickleMethod, (method.im_func.__name__,
    3534                             method.im_self,
    3635                             method.im_class)
    def pickleMethod(method): 
    3837def unpickleMethod(im_name,
    3938                    im_self,
    4039                    im_class):
    41     'support function for copy_reg to unpickle method refs'
     40    'support function for copyreg to unpickle method refs'
    4241    try:
    4342        unbound = getattr(im_class,im_name)
    4443        if im_self is None:
    def unpickleMethod(im_name, 
    5857        bound = types.MethodType(unbound.im_func, im_self, im_self.__class__)
    5958        return bound
    6059
    61 copy_reg.pickle(types.MethodType,
     60copyreg.pickle(types.MethodType,
    6261                pickleMethod,
    6362                unpickleMethod)
    6463
    6564def pickleModule(module):
    66     'support function for copy_reg to pickle module refs'
     65    'support function for copyreg to pickle module refs'
    6766    return unpickleModule, (module.__name__,)
    6867
    6968def unpickleModule(name):
    70     'support function for copy_reg to unpickle module refs'
     69    'support function for copyreg to unpickle module refs'
    7170    if name in oldModules:
    7271        log.msg("Module has moved: %s" % name)
    7372        name = oldModules[name]
    def unpickleModule(name): 
    7574    return __import__(name,{},{},'x')
    7675
    7776
    78 copy_reg.pickle(types.ModuleType,
     77copyreg.pickle(types.ModuleType,
    7978                pickleModule,
    8079                unpickleModule)
    8180
    8281def pickleStringO(stringo):
    83     'support function for copy_reg to pickle StringIO.OutputTypes'
     82    'support function for copyreg to pickle StringIO.OutputTypes'
    8483    return unpickleStringO, (stringo.getvalue(), stringo.tell())
    8584
    8685def unpickleStringO(val, sek):
    def unpickleStringO(val, sek): 
    9089    return x
    9190
    9291if hasattr(StringIO, 'OutputType'):
    93     copy_reg.pickle(StringIO.OutputType,
     92    copyreg.pickle(StringIO.OutputType,
    9493                    pickleStringO,
    9594                    unpickleStringO)
    9695
    def unpickleStringI(val, sek): 
    104103
    105104
    106105if hasattr(StringIO, 'InputType'):
    107     copy_reg.pickle(StringIO.InputType,
     106    copyreg.pickle(StringIO.InputType,
    108107                pickleStringI,
    109108                unpickleStringI)
    110109
  • twisted/python/modules.py

    diff --git twisted/python/modules.py twisted/python/modules.py
    index 11455be..421af62 100644
    import sys 
    6262import zipimport
    6363import inspect
    6464import warnings
    65 from zope.interface import Interface, implements
     65from zope.interface import Interface, implementer
    6666
    6767from twisted.python.components import registerAdapter
    6868from twisted.python.filepath import FilePath, UnlistableError
    class IPathImportMapper(Interface): 
    476476        L{ZipPath}, but more might be added later).
    477477        """
    478478
     479@implementer(IPathImportMapper)
    479480class _DefaultMapImpl:
    480481    """ Wrapper for the default importer, i.e. None.  """
    481     implements(IPathImportMapper)
    482482    def mapPath(self, fsPathString):
    483483        return FilePath(fsPathString)
    484484_theDefaultMapper = _DefaultMapImpl()
    485485
     486@implementer(IPathImportMapper)
    486487class _ZipMapImpl:
    487488    """ IPathImportMapper implementation for zipimport.ZipImporter.  """
    488     implements(IPathImportMapper)
    489489    def __init__(self, importer):
    490490        self.importer = importer
    491491
  • twisted/python/reflect.py

    diff --git twisted/python/reflect.py twisted/python/reflect.py
    index 4baf090..f1d4a6b 100644
    from collections import deque 
    1818RegexType = type(re.compile(""))
    1919
    2020
    21 try:
    22     from cStringIO import StringIO
    23 except ImportError:
    24     from StringIO import StringIO
    25 
     21from twisted.python.compat import NativeStringIO as StringIO
    2622from twisted.python.deprecate import deprecated, deprecatedModuleAttribute
    2723from twisted.python.deprecate import _fullyQualifiedName as fullyQualifiedName
    2824from twisted.python.versions import Version
    def objgrep(start, goal, eq=isLike, path='', paths=None, seen=None, showUnknowns 
    509505                                   'member_descriptor', 'getset_descriptor')):
    510506        pass
    511507    elif showUnknowns:
    512         print 'unknown type', type(start), start
     508        print('unknown type', type(start), start)
    513509    return paths
    514510
    515511
  • twisted/python/urlpath.py

    diff --git twisted/python/urlpath.py twisted/python/urlpath.py
    index 1c15f09..2e67926 100644
     
    44
    55#
    66
    7 import urlparse
     7try:
     8    from urlparse import urlsplit
     9except ImportError:
     10    from urllib.parse import urlsplit
    811import urllib
    912
    1013class URLPath:
  • twisted/python/zippath.py

    diff --git twisted/python/zippath.py twisted/python/zippath.py
    index a82f253..c29addd 100644
    else: 
    2727
    2828from twisted.python.filepath import IFilePath, FilePath, AbstractFilePath
    2929
    30 from zope.interface import implements
     30from zope.interface import implementer
    3131
    3232# using FilePath here exclusively rather than os to make sure that we don't do
    3333# anything OS-path-specific here.
    ZIP_PATH_SEP = '/' # In zipfiles, "/" is universally used as the 
    3636                                # path separator, regardless of platform.
    3737
    3838
     39@implementer(IFilePath)
    3940class ZipPath(AbstractFilePath):
    4041    """
    4142    I represent a file or directory contained within a zip file.
    4243    """
    4344
    44     implements(IFilePath)
    45 
    4645    sep = ZIP_PATH_SEP
    4746
    4847    def __init__(self, archive, pathInArchive):
  • twisted/web/_element.py

    diff --git twisted/web/_element.py twisted/web/_element.py
    index 3c15b3b..621d7e9 100644
     
    22# Copyright (c) Twisted Matrix Laboratories.
    33# See LICENSE for details.
    44
    5 from zope.interface import implements
     5from zope.interface import implementer
    66
    77from twisted.web.iweb import IRenderable
    88
    class Expose(object): 
    3131        Add one or more functions to the set of exposed functions.
    3232
    3333        This is a way to declare something about a class definition, similar to
    34         L{zope.interface.implements}.  Use it like this::
     34        L{zope.interface.implementer}.  Use it like this::
    3535
    3636            magic = Expose('perform extra magic')
    3737            class Foo(Bar):
    def renderer(): 
    122122
    123123
    124124
     125@implementer(IRenderable)
    125126class Element(object):
    126127    """
    127128    Base for classes which can render part of a page.
    class Element(object): 
    149150    @ivar loader: The factory which will be used to load documents to
    150151        return from C{render}.
    151152    """
    152     implements(IRenderable)
    153153    loader = None
    154154
    155155    def __init__(self, loader=None):
  • twisted/web/_flatten.py

    diff --git twisted/web/_flatten.py twisted/web/_flatten.py
    index 98d2b46..e8e8269 100644
    Context-free flattener/serializer for rendering Python objects, possibly 
    77complex or arbitrarily nested, as strings.
    88"""
    99
    10 from cStringIO import StringIO
    1110from sys import exc_info
    1211from types import GeneratorType
    1312from traceback import extract_tb
    from twisted.web.iweb import IRenderable 
    1817from twisted.web._stan import (
    1918    Tag, slot, voidElements, Comment, CDATA, CharRef)
    2019
     20from twisted.python.compat import NativeStringIO as StringIO
    2121
    2222
    2323def escapeForContent(data):
    def _flattenTree(request, root): 
    317317            element = stack[-1].next()
    318318        except StopIteration:
    319319            stack.pop()
    320         except Exception, e:
     320        except Exception as e:
    321321            stack.pop()
    322322            roots = []
    323323            for generator in stack:
    def _flattenTree(request, root): 
    328328            if type(element) is str:
    329329                yield element
    330330            elif isinstance(element, Deferred):
    331                 def cbx((original, toFlatten)):
     331                def cbx(arg):
     332                    original, toFlatten = arg
    332333                    stack.append(toFlatten)
    333334                    return original
    334335                yield element.addCallback(cbx)
  • twisted/web/script.py

    diff --git twisted/web/script.py twisted/web/script.py
    index 1b50105..e675ddb 100644
    I contain PythonScript, which is a very simple python script resource. 
    88
    99import os, traceback
    1010
    11 try:
    12     import cStringIO as StringIO
    13 except ImportError:
    14     import StringIO
    15 
    1611from twisted import copyright
    1712from twisted.python.compat import execfile
     13from twisted.python.compat import NativeStringIO as StringIO
    1814from twisted.web import http, server, static, resource, html
    1915
    2016
    def ResourceScript(path, registry): 
    6359            'recache': cs.recache}
    6460    try:
    6561        execfile(path, glob, glob)
    66     except AlreadyCached, ac:
     62    except AlreadyCached as ac:
    6763        return ac.args[0]
    6864    rsrc = glob['resource']
    6965    if cs.doCache and rsrc is not noRsrc:
    def ResourceTemplate(path, registry): 
    7975            'registry': registry}
    8076
    8177    e = ptl_compile.compile_template(open(path), path)
    82     exec e in glob
     78    exec(e, glob)
    8379    return glob['resource']
    8480
    8581
    class PythonScript(resource.Resource): 
    158154                     'registry': self.registry}
    159155        try:
    160156            execfile(self.filename, namespace, namespace)
    161         except IOError, e:
     157        except IOError as e:
    162158            if e.errno == 2: #file not found
    163159                request.setResponseCode(http.NOT_FOUND)
    164160                request.write(resource.NoResource("File not found.").render(request))
  • twisted/web/static.py

    diff --git twisted/web/static.py twisted/web/static.py
    index 05fd7d9..7d75fcd 100644
    import itertools 
    1414import cgi
    1515import time
    1616
    17 from zope.interface import implements
     17from zope.interface import implementer
    1818
    1919from twisted.web import server
    2020from twisted.web import resource
    class File(resource.Resource, styles.Versioned, filepath.FilePath): 
    438438            offset, offset + size - 1, self.getFileSize())
    439439
    440440
    441     def _doSingleRangeRequest(self, request, (start, end)):
     441    def _doSingleRangeRequest(self, request, start, end):
    442442        """
    443443        Set up the response for Range headers that specify a single range.
    444444
    class File(resource.Resource, styles.Versioned, filepath.FilePath): 
    580580
    581581        if len(parsedRanges) == 1:
    582582            offset, size = self._doSingleRangeRequest(
    583                 request, parsedRanges[0])
     583                request, *parsedRanges[0])
    584584            self._setContentHeaders(request, size)
    585585            return SingleRangeStaticProducer(
    586586                request, fileForReading, offset, size)
    class File(resource.Resource, styles.Versioned, filepath.FilePath): 
    613613
    614614        try:
    615615            fileForReading = self.openForReading()
    616         except IOError, e:
     616        except IOError as e:
    617617            import errno
    618618            if e[0] == errno.EACCES:
    619619                return resource.ForbiddenResource().render(request)
    class File(resource.Resource, styles.Versioned, filepath.FilePath): 
    659659        return f
    660660
    661661
    662 
     662@implementer(interfaces.IPullProducer)
    663663class StaticProducer(object):
    664664    """
    665665    Superclass for classes that implement the business of producing.
    class StaticProducer(object): 
    668668    @ivar fileObject: The file the contents of which to write to the request.
    669669    """
    670670
    671     implements(interfaces.IPullProducer)
    672 
    673671    bufferSize = abstract.FileDescriptor.bufferSize
    674672
    675673
  • twisted/web/template.py

    diff --git twisted/web/template.py twisted/web/template.py
    index 224a192..cb195ed 100644
    __all__ = [ 
    2626    ]
    2727
    2828import warnings
    29 from zope.interface import implements
     29from zope.interface import implementer
    3030
    31 from cStringIO import StringIO
    3231from xml.sax import make_parser, handler
    3332
    3433from twisted.web._stan import Tag, slot, Comment, CDATA, CharRef
    3534from twisted.python.filepath import FilePath
     35from twisted.python.compat import NativeStringIO as StringIO
    3636
    3737TEMPLATE_NAMESPACE = 'http://twistedmatrix.com/ns/twisted.web.template/0.1'
    3838
    def _flatsaxParse(fl): 
    353353    return s.document
    354354
    355355
     356@implementer(ITemplateLoader)
    356357class TagLoader(object):
    357358    """
    358359    An L{ITemplateLoader} that loads existing L{IRenderable} providers.
    class TagLoader(object): 
    360361    @ivar tag: The object which will be loaded.
    361362    @type tag: An L{IRenderable} provider.
    362363    """
    363     implements(ITemplateLoader)
    364364
    365365    def __init__(self, tag):
    366366        """
    class TagLoader(object): 
    375375
    376376
    377377
     378@implementer(ITemplateLoader)
    378379class XMLString(object):
    379380    """
    380381    An L{ITemplateLoader} that loads and parses XML from a string.
    class XMLString(object): 
    382383    @ivar _loadedTemplate: The loaded document.
    383384    @type _loadedTemplate: a C{list} of Stan objects.
    384385    """
    385     implements(ITemplateLoader)
    386386
    387387    def __init__(self, s):
    388388        """
    class XMLString(object): 
    405405
    406406
    407407
     408@implementer(ITemplateLoader)
    408409class XMLFile(object):
    409410    """
    410411    An L{ITemplateLoader} that loads and parses XML from a file.
    class XMLFile(object): 
    415416    @ivar _path: The L{FilePath}, file object, or filename that is being
    416417        loaded from.
    417418    """
    418     implements(ITemplateLoader)
    419419
    420420    def __init__(self, path):
    421421        """
  • twisted/web/test/test_static.py

    diff --git twisted/web/test/test_static.py twisted/web/test/test_static.py
    index bc6320f..4d5e10f 100644
     
    55Tests for L{twisted.web.static}.
    66"""
    77
    8 import os, re, StringIO
     8import os
     9import re
    910
    1011from zope.interface.verify import verifyObject
    1112
     13from twisted.python.compat import NativeStringIO as StringIO
    1214from twisted.internet import abstract, interfaces
    1315from twisted.python.runtime import platform
    1416from twisted.python.filepath import FilePath
    from twisted.web.server import UnsupportedMethod 
    1921from twisted.web.test.test_web import DummyRequest
    2022from twisted.web.test._util import _render
    2123
    22 
    2324class StaticDataTests(TestCase):
    2425    """
    2526    Tests for L{Data}.
    class StaticFileTests(TestCase): 
    134135        base = FilePath(self.mktemp())
    135136        base.setContent('')
    136137        # Make sure we can delete the file later.
    137         self.addCleanup(base.chmod, 0700)
     138        self.addCleanup(base.chmod, 420) # 0700 in octal, 'rwx------'
    138139
    139140        # Get rid of our own read permission.
    140141        base.chmod(0)
    class StaticMakeProducerTests(TestCase): 
    340341        start with 'content-'.
    341342        """
    342343        contentHeaders = {}
    343         for k, v in request.outgoingHeaders.iteritems():
     344        for k, v in request.outgoingHeaders.items():
    344345            if k.startswith('content-'):
    345346                contentHeaders[k] = v
    346347        return contentHeaders
    class StaticProducerTests(TestCase): 
    612613        L{StaticProducer.stopProducing} closes the file object the producer is
    613614        producing data from.
    614615        """
    615         fileObject = StringIO.StringIO()
     616        fileObject = StringIO()
    616617        producer = static.StaticProducer(None, fileObject)
    617618        producer.stopProducing()
    618619        self.assertTrue(fileObject.closed)
    class StaticProducerTests(TestCase): 
    624625        None, which indicates to subclasses' resumeProducing methods that no
    625626        more data should be produced.
    626627        """
    627         fileObject = StringIO.StringIO()
     628        fileObject = StringIO()
    628629        producer = static.StaticProducer(DummyRequest([]), fileObject)
    629630        producer.stopProducing()
    630631        self.assertIdentical(None, producer.request)
    class NoRangeStaticProducerTests(TestCase): 
    653654        request = DummyRequest([])
    654655        content = 'abcdef'
    655656        producer = static.NoRangeStaticProducer(
    656             request, StringIO.StringIO(content))
     657            request, StringIO(content))
    657658        # start calls registerProducer on the DummyRequest, which pulls all
    658659        # output from the producer and so we just need this one call.
    659660        producer.start()
    class NoRangeStaticProducerTests(TestCase): 
    670671        bufferSize = abstract.FileDescriptor.bufferSize
    671672        content = 'a' * (2*bufferSize + 1)
    672673        producer = static.NoRangeStaticProducer(
    673             request, StringIO.StringIO(content))
     674            request, StringIO(content))
    674675        # start calls registerProducer on the DummyRequest, which pulls all
    675676        # output from the producer and so we just need this one call.
    676677        producer.start()
    class NoRangeStaticProducerTests(TestCase): 
    692693        callbackList = []
    693694        finishDeferred.addCallback(callbackList.append)
    694695        producer = static.NoRangeStaticProducer(
    695             request, StringIO.StringIO('abcdef'))
     696            request, StringIO('abcdef'))
    696697        # start calls registerProducer on the DummyRequest, which pulls all
    697698        # output from the producer and so we just need this one call.
    698699        producer.start()
    class SingleRangeStaticProducerTests(TestCase): 
    723724        request = DummyRequest([])
    724725        content = 'abcdef'
    725726        producer = static.SingleRangeStaticProducer(
    726             request, StringIO.StringIO(content), 1, 3)
     727            request, StringIO(content), 1, 3)
    727728        # DummyRequest.registerProducer pulls all output from the producer, so
    728729        # we just need to call start.
    729730        producer.start()
    class SingleRangeStaticProducerTests(TestCase): 
    740741        bufferSize = abstract.FileDescriptor.bufferSize
    741742        content = 'abc' * bufferSize
    742743        producer = static.SingleRangeStaticProducer(
    743             request, StringIO.StringIO(content), 1, bufferSize+10)
     744            request, StringIO(content), 1, bufferSize+10)
    744745        # DummyRequest.registerProducer pulls all output from the producer, so
    745746        # we just need to call start.
    746747        producer.start()
    class SingleRangeStaticProducerTests(TestCase): 
    761762        callbackList = []
    762763        finishDeferred.addCallback(callbackList.append)
    763764        producer = static.SingleRangeStaticProducer(
    764             request, StringIO.StringIO('abcdef'), 1, 1)
     765            request, StringIO('abcdef'), 1, 1)
    765766        # start calls registerProducer on the DummyRequest, which pulls all
    766767        # output from the producer and so we just need this one call.
    767768        producer.start()
    class MultipleRangeStaticProducerTests(TestCase): 
    792793        request = DummyRequest([])
    793794        content = 'abcdef'
    794795        producer = static.MultipleRangeStaticProducer(
    795             request, StringIO.StringIO(content), [('1', 1, 3), ('2', 5, 1)])
     796            request, StringIO(content), [('1', 1, 3), ('2', 5, 1)])
    796797        # DummyRequest.registerProducer pulls all output from the producer, so
    797798        # we just need to call start.
    798799        producer.start()
    class MultipleRangeStaticProducerTests(TestCase): 
    816817        request = DummyRequest([])
    817818        content = '0123456789' * 2
    818819        producer = static.MultipleRangeStaticProducer(
    819             request, StringIO.StringIO(content),
     820            request, StringIO(content),
    820821            [('a', 0, 2), ('b', 5, 10), ('c', 0, 0)])
    821822        producer.bufferSize = 10
    822823        # DummyRequest.registerProducer pulls all output from the producer, so
    class MultipleRangeStaticProducerTests(TestCase): 
    839840        callbackList = []
    840841        finishDeferred.addCallback(callbackList.append)
    841842        producer = static.MultipleRangeStaticProducer(
    842             request, StringIO.StringIO('abcdef'), [('', 1, 2)])
     843            request, StringIO('abcdef'), [('', 1, 2)])
    843844        # start calls registerProducer on the DummyRequest, which pulls all
    844845        # output from the producer and so we just need this one call.
    845846        producer.start()
  • twisted/web/util.py

    diff --git twisted/web/util.py twisted/web/util.py
    index 0c6cdb6..09a0be4 100644
    __all__ = [ 
    1010    "redirectTo", "Redirect", "ChildRedirector", "ParentRedirect",
    1111    "DeferredResource", "htmlIndent", "FailureElement", "formatFailure"]
    1212
    13 from cStringIO import StringIO
    1413import linecache
    1514import string
    1615import types
    1716
     17from twisted.python.compat import NativeStringIO as StringIO
    1818from twisted.python.filepath import FilePath
    1919from twisted.python.reflect import fullyQualifiedName
    2020from twisted.python.deprecate import deprecatedModuleAttribute
    def htmlFunc(f): 
    191191                         f.func_code.co_firstlineno))+
    192192            '</div>')
    193193
    194 htmlReprTypes = {types.DictType: htmlDict,
    195                  types.ListType: htmlList,
    196                  types.InstanceType: htmlInst,
    197                  types.StringType: htmlString,
     194htmlReprTypes = {dict: htmlDict,
     195                 list: htmlList,
     196                 str: htmlString,
     197                 object: htmlInst,
    198198                 types.FunctionType: htmlFunc}
    199199
    200200
    class FailureElement(Element): 
    369369
    370370    @since: 12.1
    371371    """
    372     loader = XMLFile(getModule(__name__).filePath.sibling("failure.xhtml"))
     372    loader = XMLFile(getModule(__name__).filePath.sibling(b"failure.xhtml"))
    373373
    374374    def __init__(self, failure, loader=None):
    375375        Element.__init__(self, loader)