Ticket #5129: compat3k.execfile.4.diff
| File compat3k.execfile.4.diff, 11.1 KB (added by allenap, 16 months ago) |
|---|
-
setup.py
=== modified file 'setup.py'
16 16 import sys, os 17 17 18 18 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 extensions36 37 38 19 def main(args): 39 20 """ 40 21 Invoke twisted.python.dist with the appropriate metadata about the … … 43 24 if os.path.exists('twisted'): 44 25 sys.path.insert(0, '.') 45 26 from twisted import copyright 46 from twisted.python.dist import getDataFiles, get Scripts, getPackages, \47 setup, twisted_subprojects27 from twisted.python.dist import getDataFiles, getExtensions, getScripts, \ 28 getPackages, setup, twisted_subprojects 48 29 49 30 # "" is included because core scripts are directly in bin/ 50 31 projects = [''] + [x for x in os.listdir('bin') -
twisted/lore/htmlbook.py
=== modified file 'twisted/lore/htmlbook.py'
1 1 # Copyright (c) Twisted Matrix Laboratories. 2 2 # See LICENSE for details. 3 3 4 from twisted.python.compat import execfile 5 4 6 5 7 def getNumber(filename): 6 8 return None … … 24 26 Index = self.Index 25 27 26 28 if filename: 27 execfile(filename )29 execfile(filename, globals()) 28 30 29 31 def getFiles(self): 30 32 return [c[0] for c in self.chapters] -
twisted/names/authority.py
=== modified file 'twisted/names/authority.py'
12 12 from twisted.names import dns 13 13 from twisted.internet import defer 14 14 from twisted.python import failure 15 from twisted.python.compat import execfile 15 16 16 17 import common 17 18 -
twisted/python/_release.py
=== modified file 'twisted/python/_release.py'
25 25 from twisted.python.versions import Version 26 26 from twisted.python.filepath import FilePath 27 27 from twisted.python.dist import twisted_subprojects 28 from twisted.python.compat import execfile 28 29 29 30 # This import is an example of why you shouldn't use this module unless you're 30 31 # radix -
twisted/python/compat.py
=== modified file 'twisted/python/compat.py'
139 139 'set_connect_state', 'set_accept_state', 140 140 'connect_ex', 'sendall'): 141 141 142 exec """def %s(self, *args):142 exec("""def %s(self, *args): 143 143 self._lock.acquire() 144 144 try: 145 145 return apply(self._ssl_conn.%s, args) 146 146 finally: 147 self._lock.release()\n""" % (f, f) 147 self._lock.release()\n""" % (f, f)) 148 148 sys.modules['OpenSSL.tsafe'] = tsafe 149 149 150 150 import operator … … 175 175 from functools import reduce 176 176 except ImportError: 177 177 reduce = reduce 178 179 180 try: 181 _execfile = execfile 182 except NameError: 183 _execfile = None 184 185 186 def 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'
15 15 import platform 16 16 import sys 17 17 18 from twisted.python.compat import execfile 19 18 20 19 21 twisted_subprojects = ["conch", "lore", "mail", "names", 20 22 "news", "pair", "runner", "web", … … 208 210 return result 209 211 210 212 213 def 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 211 232 def getPackages(dname, pkgname=None, results=None, ignore=None, parent=None): 212 233 """ 213 234 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'
7 7 8 8 9 9 import os 10 import shutil 10 11 11 12 from distutils.core import Distribution 12 13 … … 55 56 self.assertEqual(ext.define_macros, [("whatever", 2), ("WIN32", 1)]) 56 57 57 58 59 class 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 58 126 59 127 class GetVersionTest(TestCase): 60 128 """ -
twisted/python/test/test_release.py
=== modified file 'twisted/python/test/test_release.py'
20 20 21 21 from twisted.trial.unittest import TestCase 22 22 23 from twisted.python.compat import set23 from twisted.python.compat import execfile, set 24 24 from twisted.python.procutils import which 25 25 from twisted.python import release 26 26 from twisted.python.filepath import FilePath -
twisted/test/test_compat.py
=== modified file 'twisted/test/test_compat.py'
6 6 Tests for L{twisted.python.compat}. 7 7 """ 8 8 9 import types, socket9 import os, tempfile, types, socket 10 10 11 11 from twisted.trial import unittest 12 12 13 from twisted.python.compat import set, frozenset, reduce 13 from twisted.python.compat import set, frozenset, reduce, execfile 14 14 15 15 16 16 … … 197 197 """ 198 198 self.assertEqual(15, reduce(lambda x, y: x + y, [1, 2, 3, 4, 5])) 199 199 self.assertEqual(16, reduce(lambda x, y: x + y, [1, 2, 3, 4, 5], 1)) 200 201 202 class 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'
1 Replace usage of execfile() with t.p.compat.execfile(), a new function 2 that wraps execfile() on Python 2.x and provides equivalent 3 functionality on Python 3.x. -
twisted/web/script.py
=== modified file 'twisted/web/script.py'
14 14 import StringIO 15 15 16 16 from twisted import copyright 17 from twisted.python.compat import execfile 17 18 from twisted.web import http, server, static, resource, html 18 19 19 20
