Ticket #5129: compat3k.execfile.diff

File compat3k.execfile.diff, 5.7 KB (added by allenap, 4 years ago)

Patch to create t.p.compat3k.execfile and use it everywhere.

  • setup.py

    === modified file 'setup.py'
     
    2020    """
    2121    Get all extensions from core and all subprojects.
    2222    """
     23    from twisted.python.compat3k import execfile
     24
    2325    extensions = []
    2426
    2527    if not sys.platform.startswith('java'):
  • twisted/names/authority.py

    === modified file 'twisted/names/authority.py'
     
    1212from twisted.names import dns
    1313from twisted.internet import defer
    1414from twisted.python import failure
     15from twisted.python.compat3k import execfile
    1516
    1617import common
    1718
  • twisted/python/_release.py

    === modified file 'twisted/python/_release.py'
     
    2525from twisted.python.versions import Version
    2626from twisted.python.filepath import FilePath
    2727from twisted.python.dist import twisted_subprojects
     28from twisted.python.compat3k import execfile
    2829
    2930# This import is an example of why you shouldn't use this module unless you're
    3031# radix
  • twisted/python/compat.py

    === modified file 'twisted/python/compat.py'
     
    139139                  'set_connect_state', 'set_accept_state',
    140140                  'connect_ex', 'sendall'):
    141141
    142             exec """def %s(self, *args):
     142            exec("""def %s(self, *args):
    143143                self._lock.acquire()
    144144                try:
    145145                    return apply(self._ssl_conn.%s, args)
    146146                finally:
    147                     self._lock.release()\n""" % (f, f)
     147                    self._lock.release()\n""" % (f, f))
    148148sys.modules['OpenSSL.tsafe'] = tsafe
    149149
    150150import operator
     
    175175    from functools import reduce
    176176except ImportError:
    177177    reduce = reduce
     178
     179
     180__all__ = [
     181    "frozenset",
     182    "reduce",
     183    "set",
     184    ]
  • twisted/python/compat3k.py

    === added file 'twisted/python/compat3k.py'
     
     1# -*- test-case-name: twisted.python.test.test_compat3k -*-
     2#
     3# Copyright (c) Twisted Matrix Laboratories.
     4# See LICENSE for details.
     5
     6
     7"""
     8Python 3.x compatibility module to provide backwards compatibility for useful
     9Python 2.x features, chiefly to aid porting.
     10
     11This is mainly for use of internal Twisted code. We encourage you to use
     12the latest version of Python directly from your code, if possible.
     13"""
     14
     15try:
     16    _execfile = execfile
     17except NameError:
     18    _execfile = None
     19
     20
     21def execfile(filename, globals, locals=None):
     22    """Execute a Python script in the given namespaces.
     23
     24    Similar to the execfile builtin, but a namespace is mandatory, partly
     25    because that's a sensible thing to require, and because otherwise we'd
     26    have to do some frame hacking.
     27
     28    This is a compatibility wrapper for Python 3 porting.
     29    """
     30    if locals is None:
     31        locals = globals
     32    if _execfile is None:
     33        fin = open(filename, "rb")
     34        try:
     35            source = fin.read()
     36        finally:
     37            fin.close()
     38        code = compile(source, filename, "exec")
     39        exec(code, globals, locals)
     40    else:
     41        _execfile(filename, globals, locals)
     42
     43
     44__all__ = [
     45    "execfile",
     46    ]
  • twisted/python/dist.py

    === modified file 'twisted/python/dist.py'
     
    1515import platform
    1616import sys
    1717
     18from twisted.python.compat3k import execfile
     19
    1820
    1921twisted_subprojects = ["conch", "lore", "mail", "names",
    2022                       "news", "pair", "runner", "web",
  • twisted/python/test/test_compat3k.py

    === added file 'twisted/python/test/test_compat3k.py'
     
     1# Copyright (c) Twisted Matrix Laboratories.
     2# See LICENSE for details.
     3
     4
     5"""
     6Tests for L{twisted.python.py3compat}.
     7"""
     8
     9import os, tempfile, unittest
     10
     11from twisted.python.compat3k import execfile
     12
     13
     14class ExecfileCompatTestCase(unittest.TestCase):
     15
     16    def setUp(self):
     17        super(ExecfileCompatTestCase, self).setUp()
     18        fd, self.script = tempfile.mkstemp(".py")
     19        fout = os.fdopen(fd, "wb")
     20        try:
     21            fout.write("foo += 1\n".encode("ascii"))
     22        finally:
     23            fout.close()
     24
     25    def tearDown(self):
     26        super(ExecfileCompatTestCase, self).tearDown()
     27        os.unlink(self.script)
     28
     29    def test_execfile_globals(self):
     30        ns_global = {"foo": 1}
     31        execfile(self.script, ns_global)
     32        self.assertEqual(2, ns_global["foo"])
     33
     34    def test_execfile_globals_and_locals(self):
     35        ns_global, ns_local = {"foo": 10}, {"foo": 20}
     36        execfile(self.script, ns_global, ns_local)
     37        self.assertEqual(10, ns_global["foo"])
     38        self.assertEqual(21, ns_local["foo"])
  • twisted/python/test/test_release.py

    === modified file 'twisted/python/test/test_release.py'
     
    2121from twisted.trial.unittest import TestCase
    2222
    2323from twisted.python.compat import set
     24from twisted.python.compat3k import execfile
    2425from twisted.python.procutils import which
    2526from twisted.python import release
    2627from twisted.python.filepath import FilePath
  • twisted/web/script.py

    === modified file 'twisted/web/script.py'
     
    1414    import StringIO
    1515
    1616from twisted import copyright
     17from twisted.python.compat3k import execfile
    1718from twisted.web import http, server, static, resource, html
    1819
    1920