Ticket #5129: compat3k.execfile.3.diff

File compat3k.execfile.3.diff, 6.5 KB (added by allenap, 5 years ago)

Patch to create t.p.compat3k.execfile and use it everywhere (with review fixes).

  • setup.py

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

    === modified file 'twisted/lore/htmlbook.py'
     
    11# Copyright (c) Twisted Matrix Laboratories.
    22# See LICENSE for details.
    33
     4from twisted.python.compat import execfile
     5
    46
    57def getNumber(filename):
    68    return None
     
    2426        Index = self.Index
    2527
    2628        if filename:
    27             execfile(filename)
     29            execfile(filename, globals())
    2830
    2931    def getFiles(self):
    3032        return [c[0] for c in self.chapters]
  • 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.compat 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.compat 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
     180try:
     181    _execfile = execfile
     182except NameError:
     183    _execfile = None
     184
     185
     186def execfile(filename, globals, locals=None):
     187    """Execute a Python script in the given namespaces.
     188
     189    Similar to the execfile builtin, but a namespace is mandatory, partly
     190    because that's a sensible thing to require, and because otherwise we'd
     191    have to do some frame hacking.
     192
     193    This is a compatibility wrapper for Python 3 porting.
     194    """
     195    if locals is None:
     196        locals = globals
     197    if _execfile is None:
     198        fin = open(filename, "rb")
     199        try:
     200            source = fin.read()
     201        finally:
     202            fin.close()
     203        code = compile(source, filename, "exec")
     204        exec(code, globals, locals)
     205    else:
     206        _execfile(filename, globals, locals)
     207
     208
     209__all__ = [
     210    "execfile",
     211    "frozenset",
     212    "reduce",
     213    "set",
     214    ]
  • twisted/python/dist.py

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

    === modified file 'twisted/python/test/test_release.py'
     
    2020
    2121from twisted.trial.unittest import TestCase
    2222
    23 from twisted.python.compat import set
     23from twisted.python.compat import execfile, set
    2424from twisted.python.procutils import which
    2525from twisted.python import release
    2626from twisted.python.filepath import FilePath
  • twisted/test/test_compat.py

    === modified file 'twisted/test/test_compat.py'
     
    66Tests for L{twisted.python.compat}.
    77"""
    88
    9 import types, socket
     9import os, tempfile, types, socket
    1010
    1111from twisted.trial import unittest
    1212
    13 from twisted.python.compat import set, frozenset, reduce
     13from twisted.python.compat import set, frozenset, reduce, execfile
    1414
    1515
    1616
     
    197197        """
    198198        self.assertEqual(15, reduce(lambda x, y: x + y, [1, 2, 3, 4, 5]))
    199199        self.assertEqual(16, reduce(lambda x, y: x + y, [1, 2, 3, 4, 5], 1))
     200
     201
     202class ExecfileCompatTestCase(unittest.TestCase):
     203    """Tests for the L{execfile} compatibility wrapper."""
     204
     205    def setUp(self):
     206        super(ExecfileCompatTestCase, self).setUp()
     207        fd, self.script = tempfile.mkstemp(".py")
     208        fout = os.fdopen(fd, "wb")
     209        try:
     210            fout.write("foo += 1\n".encode("ascii"))
     211        finally:
     212            fout.close()
     213
     214    def tearDown(self):
     215        super(ExecfileCompatTestCase, self).tearDown()
     216        os.unlink(self.script)
     217
     218    def test_execfileGlobals(self):
     219        """
     220        L{execfile} executes the specified file in the given global namespace.
     221        """
     222        ns_global = {"foo": 1}
     223        execfile(self.script, ns_global)
     224        self.assertEqual(2, ns_global["foo"])
     225
     226    def test_execfileGlobalsAndLocals(self):
     227        """
     228        L{execfile} executes the specified file in the given global and local
     229        namespaces.
     230        """
     231        ns_global, ns_local = {"foo": 10}, {"foo": 20}
     232        execfile(self.script, ns_global, ns_local)
     233        self.assertEqual(10, ns_global["foo"])
     234        self.assertEqual(21, ns_local["foo"])
  • twisted/topfiles/5129.misc

    === added file 'twisted/topfiles/5129.misc'
     
     1Replace usage of execfile() with t.p.compat.execfile(), a new function
     2that wraps execfile() on Python 2.x and provides equivalent
     3functionality on Python 3.x.
  • twisted/web/script.py

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