Ticket #5129: compat3k.execfile.5.diff

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

Patch to create t.p.compat.execfile and use it everywhere (with universal newlines).

  • setup.py

    === modified file 'setup.py'
     
    1616import sys, os
    1717
    1818
    19 def getExtensions():
    20     """
    21     Get all extensions from core and all subprojects.
    22     """
    23     extensions = []
    24 
    25     if not sys.platform.startswith('java'):
    26         for dir in os.listdir("twisted") + [""]:
    27             topfiles = os.path.join("twisted", dir, "topfiles")
    28             if os.path.isdir(topfiles):
    29                 ns = {}
    30                 setup_py = os.path.join(topfiles, "setup.py")
    31                 execfile(setup_py, ns, ns)
    32                 if "extensions" in ns:
    33                     extensions.extend(ns["extensions"])
    34 
    35     return extensions
    36 
    37 
    3819def main(args):
    3920    """
    4021    Invoke twisted.python.dist with the appropriate metadata about the
     
    4324    if os.path.exists('twisted'):
    4425        sys.path.insert(0, '.')
    4526    from twisted import copyright
    46     from twisted.python.dist import getDataFiles, getScripts, getPackages, \
    47                                     setup, twisted_subprojects
     27    from twisted.python.dist import getDataFiles, getExtensions, getScripts, \
     28        getPackages, setup, twisted_subprojects
    4829
    4930    # "" is included because core scripts are directly in bin/
    5031    projects = [''] + [x for x in os.listdir('bin')
  • 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, "rbU")
     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",
     
    208210    return result
    209211
    210212
     213def getExtensions():
     214    """
     215    Get all extensions from core and all subprojects.
     216    """
     217    extensions = []
     218
     219    if not sys.platform.startswith('java'):
     220        for dir in os.listdir("twisted") + [""]:
     221            topfiles = os.path.join("twisted", dir, "topfiles")
     222            if os.path.isdir(topfiles):
     223                ns = {}
     224                setup_py = os.path.join(topfiles, "setup.py")
     225                execfile(setup_py, ns, ns)
     226                if "extensions" in ns:
     227                    extensions.extend(ns["extensions"])
     228
     229    return extensions
     230
     231
    211232def getPackages(dname, pkgname=None, results=None, ignore=None, parent=None):
    212233    """
    213234    Get all packages which are under dname. This is necessary for
  • twisted/python/test/test_dist.py

    === modified file 'twisted/python/test/test_dist.py'
     
    77
    88
    99import os
     10import shutil
    1011
    1112from distutils.core import Distribution
    1213
     
    5556        self.assertEqual(ext.define_macros, [("whatever", 2), ("WIN32", 1)])
    5657
    5758
     59class GetExtensionsTest(TestCase):
     60    """
     61    Tests for L{dist.getExtensions}.
     62    """
     63
     64    setup_template = (
     65        "from twisted.python.dist import ConditionalExtension\n"
     66        "extensions = [\n"
     67        "    ConditionalExtension(\n"
     68        "        '%s', ['twisted/some/thing.c'],\n"
     69        "        condition=lambda builder: True)\n"
     70        "    ]\n")
     71
     72    def setUp(self):
     73        super(GetExtensionsTest, self).setUp()
     74        self.basedir = os.path.abspath("twisted")
     75        # The following will fail if this is is executed with a test runner
     76        # that does not chdir to a test-specific temporary working directory.
     77        os.mkdir(self.basedir)
     78        self.addCleanup(shutil.rmtree, self.basedir)
     79
     80    def writeSetup(self, name, *path):
     81        """
     82        Write out a C{setup.py} file to a location determined by
     83        L{self.basedir} and L{path}. L{self.setup_template} is used to
     84        generate its contents.
     85        """
     86        outdir = os.path.join(self.basedir, *path)
     87        os.makedirs(outdir)
     88        open(os.path.join(outdir, "setup.py"), "wb").write(
     89            self.setup_template % name)
     90
     91    def assertExtensions(self, expected):
     92        """
     93        Assert that the given names match the (sorted) names of discovered
     94        extensions.
     95        """
     96        extensions = dist.getExtensions()
     97        extension_names = [extension.name for extension in extensions]
     98        self.assertEqual(sorted(extension_names), expected)
     99
     100    def test_getExtensions(self):
     101        """
     102        Files named C{setup.py} in C{twisted/topfiles} and
     103        C{twisted/*/topfiles} are L{execfile}ed in order to discover the
     104        extensions they declare.
     105        """
     106        self.writeSetup("twisted.transmutate", "topfiles")
     107        self.writeSetup("twisted.tele.port", "tele", "topfiles")
     108        self.assertExtensions(["twisted.tele.port", "twisted.transmutate"])
     109
     110    def test_getExtensions_too_deep(self):
     111        """
     112        Files named C{setup.py} in C{topfiles} directories are not considered
     113        if they are too deep in the directory hierarchy.
     114        """
     115        self.writeSetup("twisted.trans.mog.rify", "trans", "mog", "topfiles")
     116        self.assertExtensions([])
     117
     118    def test_getExtensions_not_topfiles(self):
     119        """
     120        The folder in which C{setup.py} is discovered must be called
     121        C{topfiles} otherwise it is ignored.
     122        """
     123        self.writeSetup("twisted.metamorphosis", "notfiles")
     124        self.assertExtensions([])
     125
    58126
    59127class GetVersionTest(TestCase):
    60128    """
  • 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 writeScript(self, content):
     206        """
     207        Write L{content} to a new temporary file, returning the filename.
     208        """
     209        fd, script = tempfile.mkstemp()
     210        self.addCleanup(os.unlink, script)
     211        fout = os.fdopen(fd, "wb")
     212        try:
     213            fout.write(content.encode("ascii"))
     214        finally:
     215            fout.close()
     216        return script
     217
     218    def test_execfileGlobals(self):
     219        """
     220        L{execfile} executes the specified file in the given global namespace.
     221        """
     222        script = self.writeScript("foo += 1\n")
     223        ns_global = {"foo": 1}
     224        execfile(script, ns_global)
     225        self.assertEqual(2, ns_global["foo"])
     226
     227    def test_execfileGlobalsAndLocals(self):
     228        """
     229        L{execfile} executes the specified file in the given global and local
     230        namespaces.
     231        """
     232        script = self.writeScript("foo += 1\n")
     233        ns_global, ns_local = {"foo": 10}, {"foo": 20}
     234        execfile(script, ns_global, ns_local)
     235        self.assertEqual(10, ns_global["foo"])
     236        self.assertEqual(21, ns_local["foo"])
     237
     238    def test_execfileUniversalNewlines(self):
     239        """
     240        L{execfile} reads in the specified file using universal newlines so
     241        that scripts written on one platform will work on another.
     242        """
     243        for line_ending in "\n", "\r", "\r\n":
     244            script = self.writeScript("foo = 'okay'" + line_ending)
     245            ns_global = {"foo": None}
     246            execfile(script, ns_global)
     247            self.assertEqual("okay", ns_global["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