From 58a658b26d1c95b31d02050dcccd648d2e4ce27b Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Mon, 20 Jun 2011 22:55:16 +0100 Subject: Changes to support 2.x and 3.x in the same codebase. --HG-- branch : distribute extra : rebase_source : 7d3608edee54a43789f0574d702fb839628b5071 --- setuptools/compat.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 setuptools/compat.py (limited to 'setuptools/compat.py') diff --git a/setuptools/compat.py b/setuptools/compat.py new file mode 100644 index 00000000..dfbb314d --- /dev/null +++ b/setuptools/compat.py @@ -0,0 +1,73 @@ +import sys + +if sys.version_info[0] < 3: + PY3 = False + + basestring = basestring + import __builtin__ as builtins + import ConfigParser + from cStringIO import StringIO + BytesIO = StringIO + execfile = execfile + func_code = lambda o: o.func_code + func_globals = lambda o: o.func_globals + im_func = lambda o: o.im_func + from htmlentitydefs import name2codepoint + import httplib + from BaseHTTPServer import HTTPServer + from SimpleHTTPServer import SimpleHTTPRequestHandler + iteritems = lambda o: o.iteritems + long_type = long + maxsize = sys.maxint + next = lambda o: o.next() + numeric_types = (int, long, float) + reduce = reduce + unichr = unichr + unicode = unicode + from urllib import url2pathname + import urllib2 + from urllib2 import urlopen, HTTPError, URLError, unquote, splituser + from urlparse import urlparse, urlunparse, urljoin + xrange = xrange + + def exec_(code, globs=None, locs=None): + if globs is None: + frame = sys._getframe(1) + globs = frame.f_globals + if locs is None: + locs = frame.f_locals + del frame + elif locs is None: + locs = globs + exec("""exec code in globs, locs""") + +else: + PY3 = True + + basestring = str + import builtins + import configparser as ConfigParser + exec_ = eval('exec') + from io import StringIO, BytesIO + func_code = lambda o: o.__code__ + func_globals = lambda o: o.__globals__ + im_func = lambda o: o.__func__ + from html.entities import name2codepoint + import http.client as httplib + from http.server import HTTPServer, SimpleHTTPRequestHandler + iteritems = lambda o: o.items + long_type = int + maxsize = sys.maxsize + next = next + numeric_types = (int, float) + from functools import reduce + unichr = chr + unicode = str + from urllib.error import HTTPError, URLError + import urllib.request as urllib2 + from urllib.request import urlopen, url2pathname + from urllib.parse import urlparse, urlunparse, unquote, splituser, urljoin + xrange = range + + def execfile(fn, globs, locs): + exec_(compile(open(fn).read(), fn, 'exec'), globs, locs) -- cgit v1.2.1 From e65d621e1661512a4bcfb7d9a5320d3faa96aebe Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Tue, 21 Jun 2011 08:11:40 +0100 Subject: Fixed some bugs - tests now all pass under Python 3.3. --HG-- branch : distribute extra : rebase_source : 3498bfdc0d4c15e4276673b52e924c461ca353f0 --- setuptools/compat.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'setuptools/compat.py') diff --git a/setuptools/compat.py b/setuptools/compat.py index dfbb314d..72581866 100644 --- a/setuptools/compat.py +++ b/setuptools/compat.py @@ -16,7 +16,7 @@ if sys.version_info[0] < 3: import httplib from BaseHTTPServer import HTTPServer from SimpleHTTPServer import SimpleHTTPRequestHandler - iteritems = lambda o: o.iteritems + iteritems = lambda o: o.iteritems() long_type = long maxsize = sys.maxint next = lambda o: o.next() @@ -55,7 +55,7 @@ else: from html.entities import name2codepoint import http.client as httplib from http.server import HTTPServer, SimpleHTTPRequestHandler - iteritems = lambda o: o.items + iteritems = lambda o: o.items() long_type = int maxsize = sys.maxsize next = next -- cgit v1.2.1 From 5861e0443331531fda2525fc4a714101968cb537 Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Tue, 21 Jun 2011 09:32:50 +0100 Subject: Fixed some bugs, tests now also all pass on 2.7 and 3.2. --HG-- branch : distribute extra : rebase_source : 25c6042a716c49e3576605c3cf4e2878d5b85c18 --- setuptools/compat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setuptools/compat.py') diff --git a/setuptools/compat.py b/setuptools/compat.py index 72581866..7427c17f 100644 --- a/setuptools/compat.py +++ b/setuptools/compat.py @@ -6,7 +6,7 @@ if sys.version_info[0] < 3: basestring = basestring import __builtin__ as builtins import ConfigParser - from cStringIO import StringIO + from StringIO import StringIO BytesIO = StringIO execfile = execfile func_code = lambda o: o.func_code -- cgit v1.2.1 From 6e67978d8e96d4e60462389ec71bfd2d51ac9109 Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Tue, 21 Jun 2011 13:16:40 +0100 Subject: Fixed execfile in compat.py. --HG-- branch : distribute extra : rebase_source : cf31870fee8b718e14e209d957905c8d7573beba --- setuptools/compat.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'setuptools/compat.py') diff --git a/setuptools/compat.py b/setuptools/compat.py index 7427c17f..c5d28be5 100644 --- a/setuptools/compat.py +++ b/setuptools/compat.py @@ -69,5 +69,10 @@ else: from urllib.parse import urlparse, urlunparse, unquote, splituser, urljoin xrange = range - def execfile(fn, globs, locs): + def execfile(fn, globs=None, locs=None): + if globs is None: + globs = globals() + if locs is None: + locs = globs exec_(compile(open(fn).read(), fn, 'exec'), globs, locs) + -- cgit v1.2.1 From ac3ba239c54965e464e6047fd872f02ca1c0cb99 Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Wed, 10 Oct 2012 09:39:21 +0100 Subject: Post-merge fixes for Python 3. --HG-- branch : distribute extra : source : 6b9041dea7b9197f6ea1fb993d7a05dd4f7c580d --- setuptools/compat.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'setuptools/compat.py') diff --git a/setuptools/compat.py b/setuptools/compat.py index c5d28be5..6d4ea539 100644 --- a/setuptools/compat.py +++ b/setuptools/compat.py @@ -41,6 +41,8 @@ if sys.version_info[0] < 3: locs = globs exec("""exec code in globs, locs""") + exec_("""def reraise(tp, value, tb=None): + raise tp, value, tb""") else: PY3 = True @@ -76,3 +78,8 @@ else: locs = globs exec_(compile(open(fn).read(), fn, 'exec'), globs, locs) + def reraise(tp, value, tb=None): + if value.__traceback__ is not tb: + raise value.with_traceback(tb) + raise value + -- cgit v1.2.1 From 7cf9cd93c2fddb7f7f5fe6c1fcc140e9d6c93232 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Tue, 18 Jun 2013 13:54:35 -0500 Subject: Minor fix previously fixed in a merge --HG-- branch : distribute --- setuptools/compat.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'setuptools/compat.py') diff --git a/setuptools/compat.py b/setuptools/compat.py index 6d4ea539..27d472b5 100644 --- a/setuptools/compat.py +++ b/setuptools/compat.py @@ -16,6 +16,7 @@ if sys.version_info[0] < 3: import httplib from BaseHTTPServer import HTTPServer from SimpleHTTPServer import SimpleHTTPRequestHandler + from BaseHTTPServer import BaseHTTPRequestHandler iteritems = lambda o: o.iteritems() long_type = long maxsize = sys.maxint @@ -57,6 +58,7 @@ else: from html.entities import name2codepoint import http.client as httplib from http.server import HTTPServer, SimpleHTTPRequestHandler + from http.server import BaseHTTPRequestHandler iteritems = lambda o: o.items() long_type = int maxsize = sys.maxsize -- cgit v1.2.1 From 94fc39cb62df19e85b07658f2fa5d0b4a7bf9303 Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Wed, 10 Oct 2012 10:49:54 +0100 Subject: Fixed some resource leaks. --HG-- branch : distribute extra : source : 98c929e25fee11a99eb125dd9a13521321d68dd3 --- setuptools/compat.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'setuptools/compat.py') diff --git a/setuptools/compat.py b/setuptools/compat.py index 27d472b5..05417c6e 100644 --- a/setuptools/compat.py +++ b/setuptools/compat.py @@ -78,10 +78,14 @@ else: globs = globals() if locs is None: locs = globs - exec_(compile(open(fn).read(), fn, 'exec'), globs, locs) + f = open(fn) + try: + source = f.read() + finally: + f.close() + exec_(compile(source, fn, 'exec'), globs, locs) def reraise(tp, value, tb=None): if value.__traceback__ is not tb: raise value.with_traceback(tb) raise value - -- cgit v1.2.1 From 3c86c861ab9665df0e502f250a7471d09240e413 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 17 Jun 2013 10:55:09 -0500 Subject: Fix Python3 compatibility issue in filterfalse --HG-- branch : distribute extra : rebase_source : 71e3e89efe6483924a07e7f9819ee86f08dbf1f2 extra : histedit_source : fa75c7bc174b248c74da7b4efd5f6f05c36b6ae4 --- setuptools/compat.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'setuptools/compat.py') diff --git a/setuptools/compat.py b/setuptools/compat.py index 05417c6e..e2f64de2 100644 --- a/setuptools/compat.py +++ b/setuptools/compat.py @@ -1,4 +1,5 @@ import sys +import itertools if sys.version_info[0] < 3: PY3 = False @@ -30,6 +31,7 @@ if sys.version_info[0] < 3: from urllib2 import urlopen, HTTPError, URLError, unquote, splituser from urlparse import urlparse, urlunparse, urljoin xrange = xrange + filterfalse = itertools.ifilterfalse def exec_(code, globs=None, locs=None): if globs is None: @@ -72,6 +74,7 @@ else: from urllib.request import urlopen, url2pathname from urllib.parse import urlparse, urlunparse, unquote, splituser, urljoin xrange = range + filterfalse = itertools.filterfalse def execfile(fn, globs=None, locs=None): if globs is None: -- cgit v1.2.1