Ticket #5129: compat3k.execfile.2.diff

File compat3k.execfile.2.diff, 7.1 KB (added by allenap, 3 years ago)

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

  • setup.py

    === modified file 'setup.py'
     
    1010try: 
    1111    # Load setuptools, to build a specific source package 
    1212    import setuptools 
     13    setuptools  # Silence lint. 
    1314except ImportError: 
    1415    pass 
    1516 
     
    2021    """ 
    2122    Get all extensions from core and all subprojects. 
    2223    """ 
     24    from twisted.python.compat3k import execfile 
     25 
    2326    extensions = [] 
    2427 
    2528    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 
     
    945946 
    946947    This knows how to build tarballs for Twisted and all of its subprojects. 
    947948    """ 
    948     from twisted.python.dist import twisted_subprojects as subprojects 
    949949 
    950950    def __init__(self, rootDirectory, outputDirectory, apiBaseURL=None): 
    951951        """ 
  • twisted/python/compat.py

    === modified file 'twisted/python/compat.py'
     
    6868            raise ValueError("address length incorrect") 
    6969        parts = struct.unpack('!8H', addr) 
    7070        curBase = bestBase = None 
     71        bestLen = 0 
    7172        for i in range(8): 
    7273            if not parts[i]: 
    7374                if curBase is None: 
     
    139140                  'set_connect_state', 'set_accept_state', 
    140141                  'connect_ex', 'sendall'): 
    141142 
    142             exec """def %s(self, *args): 
     143            exec("""def %s(self, *args): 
    143144                self._lock.acquire() 
    144145                try: 
    145146                    return apply(self._ssl_conn.%s, args) 
    146147                finally: 
    147                     self._lock.release()\n""" % (f, f) 
     148                    self._lock.release()\n""" % (f, f)) 
    148149sys.modules['OpenSSL.tsafe'] = tsafe 
    149150 
    150151import operator 
     
    175176    from functools import reduce 
    176177except ImportError: 
    177178    reduce = reduce 
     179 
     180 
     181__all__ = [ 
     182    "frozenset", 
     183    "reduce", 
     184    "set", 
     185    ] 
  • 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 
     
    5051# Check a bunch of dependencies to skip tests if necessary. 
    5152try: 
    5253    from twisted.lore.scripts import lore 
     54    lore  # Silence lint. 
    5355except ImportError: 
    5456    loreSkip = "Lore is not present." 
    5557else: 
  • twisted/topfiles/5129.misc

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

    === modified file 'twisted/web/script.py'
     
    99import os, traceback 
    1010 
    1111try: 
    12     import cStringIO as StringIO 
     12    import cStringIO 
     13    StringIO = cStringIO  # Silence lint. 
    1314except ImportError: 
    1415    import StringIO 
    1516 
    1617from twisted import copyright 
     18from twisted.python.compat3k import execfile 
    1719from twisted.web import http, server, static, resource, html 
    1820 
    1921