Vinay Sajip avatar Vinay Sajip committed 6015632

Initial commit. All tests pass on 2.7, 3.2 and 3.3, though there are some atexit errors in the multiprocessing module in 2.7/3.2 (seemingly unrelated to setuptools).

Comments (0)

Files changed (34)

 *.swp
 .coverage
 distribute.egg-info
+setuptools.egg-info
 build
 dist
 lib
 method.
 """
 
-import sys, os, time, re, imp, types, zipfile, zipimport
-from urlparse import urlparse, urlunparse
+import sys, os, zipfile, zipimport, time, re, imp, types
+try:
+    from urlparse import urlparse, urlunparse
+except ImportError:
+    from urllib.parse import urlparse, urlunparse
 
 try:
     frozenset
 except NameError:
     from sets import ImmutableSet as frozenset
+try:
+    basestring
+    next = lambda o: o.next()
+    from cStringIO import StringIO
+    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""")
+except NameError:
+    basestring = str
+    from io import StringIO
+    from functools import reduce
+    exec_ = eval("exec")
+    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)
 
 # capture these to bypass sandboxing
 from os import utime
 except ImportError:
     pass
 
-def _bypass_ensure_directory(name, mode=0777):
+def _bypass_ensure_directory(name, mode=0x1FF):  # 0777
     # Sandbox-bypassing version of ensure_directory()
     if not WRITE_SUPPORT:
         raise IOError('"os.mkdir" not supported on this platform.')
 
 def _declare_state(vartype, **kw):
     g = globals()
-    for name, val in kw.iteritems():
+    for name, val in kw.items():
         g[name] = val
         _state_vars[name] = vartype
 
 def __getstate__():
     state = {}
     g = globals()
-    for k, v in _state_vars.iteritems():
+    for k, v in _state_vars.items():
         state[k] = g['_sget_'+v](g[k])
     return state
 
 def __setstate__(state):
     g = globals()
-    for k, v in state.iteritems():
+    for k, v in state.items():
         g['_sset_'+_state_vars[k]](k, g[k], v)
     return state
 
             env = full_env + plugin_env
 
         shadow_set = self.__class__([])
-        map(shadow_set.add, self)   # put all our entries in shadow_set
+        list(map(shadow_set.add, self))   # put all our entries in shadow_set
 
         for project_name in plugin_projects:
 
                 try:
                     resolvees = shadow_set.resolve(req, env, installer)
 
-                except ResolutionError,v:
+                except ResolutionError:
+                    v = sys.exc_info()[1]
                     error_info[dist] = v    # save error info
                     if fallback:
                         continue    # try the next older version of project
                         break       # give up on this project, keep going
 
                 else:
-                    map(shadow_set.add, resolvees)
+                    list(map(shadow_set.add, resolvees))
                     distributions.update(dict.fromkeys(resolvees))
 
                     # success, no need to try any more versions of this project
             self.callbacks[:]
         )
 
-    def __setstate__(self, (entries, keys, by_key, callbacks)):
+    def __setstate__(self, e_k_b_c):
+        entries, keys, by_key, callbacks = e_k_b_c
         self.entries = entries[:]
         self.entry_keys = keys.copy()
         self.by_key = by_key.copy()
 
         if os.name == 'posix':
             # Make the resource executable
-            mode = ((os.stat(tempname).st_mode) | 0555) & 07777
+            mode = ((os.stat(tempname).st_mode) | 0x16D) & 0xFFF # 0555, 07777
             os.chmod(tempname, mode)
 
 
                 len(script_text), 0, script_text.split('\n'), script_filename
             )
             script_code = compile(script_text,script_filename,'exec')
-            exec script_code in namespace, namespace
+            exec_(script_code, namespace, namespace)
 
     def _has(self, path):
         raise NotImplementedError(
     try:
         from cStringIO import StringIO
     except ImportError:
-        from StringIO import StringIO
+        from io import StringIO
     return StringIO(*args,**kw)
 
 def find_nothing(importer, path_item, only=False):
     def require(self, env=None, installer=None):
         if self.extras and not self.dist:
             raise UnknownExtra("Can't require() without a distribution", self)
-        map(working_set.add,
-            working_set.resolve(self.dist.requires(self.extras),env,installer))
+        list(map(working_set.add,
+            working_set.resolve(self.dist.requires(self.extras),env,installer)))
 
 
 
     def __getattr__(self,attr):
         """Delegate all unrecognized public attributes to .metadata provider"""
         if attr.startswith('_'):
-            raise AttributeError,attr
+            raise AttributeError(attr)
         return getattr(self._provider, attr)
 
 
         # Including any condition expressions
         for req in self._parsed_pkg_info.get_all('Requires-Dist') or []:
             distvers, mark = self._preparse_requirement(req)
-            parsed = parse_requirements(distvers).next()
+            parsed = next(parse_requirements(distvers))
             parsed.marker_fn = compile_marker(mark)
             reqs.append(parsed)
 
         while not TERMINATOR(line,p):
             if CONTINUE(line,p):
                 try:
-                    line = lines.next(); p = 0
+                    line = next(lines); p = 0
                 except StopIteration:
                     raise ValueError(
                         "\\ must not appear on the last nonblank line"
 # all distributions added to the working set in the future (e.g. by
 # calling ``require()``) will get activated as well.
 add_activation_listener(lambda dist: dist.activate())
-working_set.entries=[]; map(working_set.add_entry,sys.path) # match order
-
-
+working_set.entries=[]; list(map(working_set.add_entry,sys.path)) # match order
+
 os.chdir(os.path.dirname(os.path.abspath(__file__)))
 
 src_root = None
-if sys.version_info >= (3,):
+do_2to3 = False
+if sys.version_info >= (3,) and do_2to3:
     tmp_src = os.path.join("build", "src")
     from distutils.filelist import FileList
     from distutils import dir_util, file_util, util, log
                 # previous version doesn't have convert_2to3_doctests)
                 if not hasattr(self.distribution, 'convert_2to3_doctests'):
                     continue
-
+                if not do_2to3:
+                    continue
                 if copied and srcfile in self.distribution.convert_2to3_doctests:
                     self.__doctests_2to3.append(outf)
 

setuptools.egg-info/dependency_links.txt

-http://pypi.python.org/packages/source/c/certifi/certifi-0.0.8.tar.gz#md5=dc5f5e7f0b5fc08d27654b17daa6ecec
-http://pypi.python.org/packages/source/s/ssl/ssl-1.16.tar.gz#md5=fb12d335d56f3c8c7c1fefc1c06c4bfb
-http://pypi.python.org/packages/source/w/wincertstore/wincertstore-0.1.zip#md5=2f9accbebe8f7b4c06ac7aa83879b81c
-http://bitbucket.org/pypa/setuptools/downloads/ctypes-1.0.2.win32-py2.4.exe#md5=9092a0ad5a3d79fa2d980f1ddc5e9dbc
-http://bitbucket.org/pypa/setuptools/downloads/ssl-1.16-py2.4-win32.egg#md5=3cfa2c526dc66e318e8520b6f1aadce5
-http://bitbucket.org/pypa/setuptools/downloads/ssl-1.16-py2.5-win32.egg#md5=85ad1cda806d639743121c0bbcb5f39b
+https://pypi.python.org/packages/source/c/certifi/certifi-0.0.8.tar.gz#md5=dc5f5e7f0b5fc08d27654b17daa6ecec
+https://pypi.python.org/packages/source/s/ssl/ssl-1.16.tar.gz#md5=fb12d335d56f3c8c7c1fefc1c06c4bfb
+https://pypi.python.org/packages/source/w/wincertstore/wincertstore-0.1.zip#md5=2f9accbebe8f7b4c06ac7aa83879b81c
+https://bitbucket.org/pypa/setuptools/downloads/ctypes-1.0.2.win32-py2.4.exe#md5=9092a0ad5a3d79fa2d980f1ddc5e9dbc
+https://bitbucket.org/pypa/setuptools/downloads/ssl-1.16-py2.4-win32.egg#md5=3cfa2c526dc66e318e8520b6f1aadce5
+https://bitbucket.org/pypa/setuptools/downloads/ssl-1.16-py2.5-win32.egg#md5=85ad1cda806d639743121c0bbcb5f39b

setuptools.egg-info/requires.txt

 
 
+[certs]
+certifi==0.0.8
+
 [ssl:sys_platform=='win32']
 wincertstore==0.1
 
+[ssl:python_version in '2.4, 2.5']
+ssl==1.16
+
 [ssl:sys_platform=='win32' and python_version=='2.4']
-ctypes==1.0.2
-
-[certs]
-certifi==0.0.8
-
-[ssl:python_version in '2.4, 2.5']
-ssl==1.16
+ctypes==1.0.2

setuptools/command/alias.py

         aliases = self.distribution.get_option_dict('aliases')
 
         if not self.args:
-            print "Command Aliases"
-            print "---------------"
+            print("Command Aliases")
+            print("---------------")
             for alias in aliases:
-                print "setup.py alias", format_alias(alias, aliases)
+                print("setup.py alias", format_alias(alias, aliases))
             return
 
         elif len(self.args)==1:
             if self.remove:
                 command = None
             elif alias in aliases:
-                print "setup.py alias", format_alias(alias, aliases)
+                print("setup.py alias", format_alias(alias, aliases))
                 return
             else:
-                print "No alias definition found for %r" % alias
+                print("No alias definition found for %r" % alias)
                 return
         else:
             alias = self.args[0]

setuptools/command/bdist_egg.py

 from pkg_resources import get_build_platform, Distribution, ensure_directory
 from pkg_resources import EntryPoint
 from types import CodeType
+from setuptools.compat import basestring, next
 from setuptools.extension import Library
 
 def strip_module(filename):
 def walk_egg(egg_dir):
     """Walk an unpacked egg's contents, skipping the metadata directory"""
     walker = os.walk(egg_dir)
-    base,dirs,files = walker.next()
+    base,dirs,files = next(walker)
     if 'EGG-INFO' in dirs:
         dirs.remove('EGG-INFO')
     yield base,dirs,files

setuptools/command/easy_install.py

 from setuptools.package_index import PackageIndex
 from setuptools.package_index import URL_SCHEME
 from setuptools.command import bdist_egg, egg_info
+from setuptools.compat import (iteritems, maxsize, xrange, basestring, unicode,
+                               reraise)
 from pkg_resources import yield_lines, normalize_path, resource_string, \
         ensure_directory, get_distribution, find_distributions, \
         Environment, Requirement, Distribution, \
          DistributionNotFound, VersionConflict, \
         DEVELOP_DIST
 
-sys_executable = os.path.normpath(sys.executable)
+if '__VENV_LAUNCHER__' in os.environ:
+    sys_executable = os.environ['__VENV_LAUNCHER__']
+else:
+    sys_executable = os.path.normpath(sys.executable)
 
 __all__ = [
     'samefile', 'easy_install', 'PthDistributions', 'extract_wininst_cfg',
 
     def finalize_options(self):
         if self.version:
-            print 'setuptools %s' % get_distribution('setuptools').version
+            print('setuptools %s' % get_distribution('setuptools').version)
             sys.exit()
 
         py_version = sys.version.split()[0]
         try:
             pid = os.getpid()
         except:
-            pid = random.randint(0,sys.maxint)
+            pid = random.randint(0, maxsize)
         return os.path.join(self.install_dir, "test-easy-install-%s" % pid)
 
     def warn_deprecated_options(self):
             distros = WorkingSet([]).resolve(
                 [requirement], self.local_index, self.easy_install
             )
-        except DistributionNotFound, e:
+        except DistributionNotFound:
+            e = sys.exc_info()[1]
             raise DistutilsError(
                 "Could not find required distribution %s" % e.args
             )
-        except VersionConflict, e:
+        except VersionConflict:
+            e = sys.exc_info()[1]
             raise DistutilsError(
                 "Installed distribution %s conflicts with requirement %s"
                 % e.args
             f = open(target,"w"+mode)
             f.write(contents)
             f.close()
-            chmod(target, 0777-mask)
+            chmod(target, 0o777-mask)
 
 
 
         )
         try:
             run_setup(setup_script, args)
-        except SystemExit, v:
+        except SystemExit:
+            v = sys.exc_info()[1]
             raise DistutilsError("Setup script exited with %s" % (v.args[0],))
 
     def build_and_install(self, setup_script, setup_base):
             'site_dirs', 'allow_hosts',
         )
         fetch_options = {}
-        for key, val in ei_opts.iteritems():
+        for key, val in iteritems(ei_opts):
             if key not in fetch_directives: continue
             fetch_options[key.replace('_', '-')] = val[1]
         # create a settings dictionary suitable for `edit_config`
         self.byte_compile(to_compile)
         if not self.dry_run:
             for f in to_chmod:
-                mode = ((os.stat(f)[stat.ST_MODE]) | 0555) & 07755
+                mode = ((os.stat(f)[stat.ST_MODE]) | 0x16D) & 0xFED  # 0555, 07755
                 chmod(f, mode)
 
     def byte_compile(self, to_compile):
         if not self.user:
             return
         home = convert_path(os.path.expanduser("~"))
-        for name, path in self.config_vars.iteritems():
+        for name, path in iteritems(self.config_vars):
             if path.startswith(home) and not os.path.isdir(path):
                 self.debug_print("os.makedirs('%s', 0700)" % path)
-                os.makedirs(path, 0700)
+                os.makedirs(path, 0x1C0)    # 0700
 
 
 
 
 def get_site_dirs():
     # return a list of 'site' dirs
-    sitedirs = filter(None,os.environ.get('PYTHONPATH','').split(os.pathsep))
+    sitedirs = list(filter(None,os.environ.get('PYTHONPATH','').split(os.pathsep)))
     prefixes = [sys.prefix]
     if sys.exec_prefix != sys.prefix:
         prefixes.append(sys.exec_prefix)
     if HAS_USER_SITE:
         sitedirs.append(site.USER_SITE)
 
-    sitedirs = map(normalize_path, sitedirs)
+    sitedirs = list(map(normalize_path, sitedirs))
 
     return sitedirs
 
             return None
         f.seek(prepended-12)
 
-        import struct, StringIO, ConfigParser
+        from setuptools.compat import StringIO, ConfigParser
         tag, cfglen, bmlen = struct.unpack("<iii",f.read(12))
         if tag not in (0x1234567A, 0x1234567B):
             return None     # not a valid tag
     dirty = False
 
     def __init__(self, filename, sitedirs=()):
-        self.filename = filename; self.sitedirs=map(normalize_path, sitedirs)
+        self.filename = filename; self.sitedirs = list(map(normalize_path, sitedirs))
         self.basedir = normalize_path(os.path.dirname(self.filename))
         self._load(); Environment.__init__(self, [], None, None)
         for path in yield_lines(self.paths):
-            map(self.add, find_distributions(path, True))
+            list(map(self.add, find_distributions(path, True)))
 
     def _load(self):
         self.paths = []
     if func is os.remove and os.name=='nt':
         chmod(arg, stat.S_IWRITE)
         return func(arg)
-    exc = sys.exc_info()
-    raise exc[0], (exc[1][0], exc[1][1] + (" %s %s" % (func,arg)))
+    et, ev, _ = sys.exc_info()
+    reraise(et, (ev[0], ev[1] + (" %s %s" % (func,arg))))
 
 def uncache_zipdir(path):
     """Ensure that the importer caches dont have stale info for `path`"""
     log.debug("changing mode of %s to %o", path, mode)
     try:
         _chmod(path, mode)
-    except os.error, e:
+    except os.error:
+        e = sys.exc_info()[1]
         log.debug("chmod failed: %s", e)
 
 def fix_jython_executable(executable, options):
     names = []
     try:
         names = os.listdir(path)
-    except os.error, err:
+    except os.error:
         onerror(os.listdir, path, sys.exc_info())
     for name in names:
         fullname = os.path.join(path, name)
         else:
             try:
                 os.remove(fullname)
-            except os.error, err:
+            except os.error:
                 onerror(os.remove, fullname, sys.exc_info())
     try:
         os.rmdir(path)
         onerror(os.rmdir, path, sys.exc_info())
 
 def current_umask():
-    tmp = os.umask(022)
+    tmp = os.umask(0x12)    # 022
     os.umask(tmp)
     return tmp
 

setuptools/command/egg_info.py

 from distutils.errors import *
 from distutils import log
 from setuptools.command.sdist import sdist
+from setuptools.compat import basestring, PY3
 from distutils.util import convert_path
 from distutils.filelist import FileList as _FileList
 from pkg_resources import parse_requirements, safe_name, parse_version, \
     safe_version, yield_lines, EntryPoint, iter_entry_points, to_filename
-from sdist import walk_revctrl
+from setuptools.command.sdist import walk_revctrl
 
 class egg_info(Command):
     description = "create a distribution's .egg-info directory"
         self.vtags = None
 
     def save_version_info(self, filename):
-        from setopt import edit_config
+        from setuptools.command.setopt import edit_config
         edit_config(
             filename,
             {'egg_info':
             item = item[:-1]
         path = convert_path(item)
 
-        if sys.version_info >= (3,):
+        if PY3:
             try:
                 if os.path.exists(path) or os.path.exists(path.encode('utf-8')):
                     self.files.append(path)
         named by 'self.manifest'.
         """
         # The manifest must be UTF-8 encodable. See #303.
-        if sys.version_info >= (3,):
+        if PY3:
             files = []
             for file in self.filelist.files:
                 try:
             metadata.name, metadata.version = oldname, oldver
 
         safe = getattr(cmd.distribution,'zip_safe',None)
-        import bdist_egg; bdist_egg.write_safety_flag(cmd.egg_info, safe)
+        from setuptools.command import bdist_egg; bdist_egg.write_safety_flag(cmd.egg_info, safe)
 
 def warn_depends_obsolete(cmd, basename, filename):
     if os.path.exists(filename):

setuptools/command/install_egg_info.py

 from setuptools import Command
 from setuptools.archive_util import unpack_archive
+from setuptools.compat import PY3
 from distutils import log, dir_util
 import os, shutil, pkg_resources
 

setuptools/command/install_scripts.py

             f = open(target,"w"+mode)
             f.write(contents)
             f.close()
-            chmod(target, 0777-mask)
+            chmod(target, 0o777-mask)
 

setuptools/command/rotate.py

 import distutils, os
 from setuptools import Command
+from setuptools.compat import basestring
 from distutils.util import convert_path
 from distutils import log
 from distutils.errors import *

setuptools/command/sdist.py

 
         optional = ['test/test*.py', 'setup.cfg']
         for pattern in optional:
-            files = filter(os.path.isfile, glob(pattern))
+            files = list(filter(os.path.isfile, glob(pattern)))
             if files:
                 self.filelist.extend(files)
 

setuptools/command/setopt.py

     while a dictionary lists settings to be changed or deleted in that section.
     A setting of ``None`` means to delete that setting.
     """
-    from ConfigParser import RawConfigParser
+    from setuptools.compat import ConfigParser
     log.debug("Reading configuration from %s", filename)
-    opts = RawConfigParser()
+    opts = ConfigParser.RawConfigParser()
     opts.read([filename])
     for section, options in settings.items():
         if options is None:

setuptools/command/upload.py

 import os
 import socket
 import platform
-import ConfigParser
-import httplib
 import base64
-import urlparse
-import cStringIO as StringIO
+
+from setuptools.compat import urlparse, StringIO, httplib, ConfigParser
 
 class upload(Command):
 
             raise DistutilsOptionError(
                 "Must use --sign for --identity to have meaning"
             )
-        if os.environ.has_key('HOME'):
+        if 'HOME' in os.environ:
             rc = os.path.join(os.environ['HOME'], '.pypirc')
             if os.path.exists(rc):
                 self.announce('Using PyPI login from %s' % rc)
         # We can't use urllib2 since we need to send the Basic
         # auth right with the first request
         schema, netloc, url, params, query, fragments = \
-            urlparse.urlparse(self.repository)
+            urlparse(self.repository)
         assert not params and not query and not fragments
         if schema == 'http':
             http = httplib.HTTPConnection(netloc)
         elif schema == 'https':
             http = httplib.HTTPSConnection(netloc)
         else:
-            raise AssertionError, "unsupported schema "+schema
+            raise AssertionError("unsupported schema " + schema)
 
         data = ''
         loglevel = log.INFO
             self.announce('Upload failed (%s): %s' % (r.status, r.reason),
                           log.ERROR)
         if self.show_response:
-            print '-'*75, r.read(), '-'*75
-
+            print('-'*75, r.read(), '-'*75)

setuptools/command/upload_docs.py

 import os
 import socket
 import zipfile
-import httplib
-import urlparse
 import tempfile
 import sys
 import shutil
 except ImportError:
     from setuptools.command.upload import upload
 
+from setuptools.compat import httplib, urlparse
+
+if sys.version_info >= (3,):
+    errors = 'surrogateescape'
+else:
+    errors = 'strict'
+
 
 # This is not just a replacement for byte literals
 # but works as a general purpose encoder
 def b(s, encoding='utf-8'):
     if isinstance(s, unicode):
-        return s.encode(encoding)
+        return s.encode(encoding, errors)
     return s
 
 
         # We can't use urllib2 since we need to send the Basic
         # auth right with the first request
         schema, netloc, url, params, query, fragments = \
-            urlparse.urlparse(self.repository)
+            urlparse(self.repository)
         assert not params and not query and not fragments
         if schema == 'http':
             conn = httplib.HTTPConnection(netloc)
             conn.putheader('Authorization', auth)
             conn.endheaders()
             conn.send(body)
-        except socket.error, e:
+        except socket.error:
+            e = sys.exc_info()[1]
             self.announce(str(e), log.ERROR)
             return
 
             self.announce('Upload failed (%s): %s' % (r.status, r.reason),
                           log.ERROR)
         if self.show_response:
-            print '-'*75, r.read(), '-'*75
+            print('-'*75, r.read(), '-'*75)

setuptools/compat.py

+import sys
+
+if sys.version_info[0] < 3:
+    PY3 = False
+
+    basestring = basestring
+    import __builtin__ as builtins
+    import ConfigParser
+    from StringIO 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, BaseHTTPRequestHandler
+    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, quote # Python 2.4 has no quote in urllib2
+    import urllib2
+    from urllib2 import urlopen, HTTPError, URLError, unquote, splituser
+    from urlparse import urlparse, urlunparse, urljoin
+    xrange = xrange
+    from itertools import ifilterfalse
+
+    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""")
+
+    exec_("""def reraise(tp, value, tb=None):
+    raise tp, value, tb""")
+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, BaseHTTPRequestHandler
+    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, quote, unquote, splituser, urljoin
+    xrange = range
+    from itertools import filterfalse as ifilterfalse
+
+    def execfile(fn, globs=None, locs=None):
+        if globs is None:
+            globs = globals()
+        if locs is None:
+            locs = globs
+        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

setuptools/depends.py

             ptr += 3
 
             if op==EXTENDED_ARG:
-                extended_arg = arg * 65536L
+                extended_arg = arg * long_type(65536)
                 continue
 
         else:

setuptools/dist.py

 __all__ = ['Distribution']
 
 import re
+import sys
 from distutils.core import Distribution as _Distribution
 from setuptools.depends import Require
 from setuptools.command.install import install
 from setuptools.command.sdist import sdist
 from setuptools.command.install_lib import install_lib
+from setuptools.compat import numeric_types, basestring
 from distutils.errors import DistutilsOptionError, DistutilsPlatformError
 from distutils.errors import DistutilsSetupError
 import setuptools, pkg_resources, distutils.core, distutils.dist, distutils.cmd
     """Verify that entry_points map is parseable"""
     try:
         pkg_resources.EntryPoint.parse_map(value)
-    except ValueError, e:
+    except ValueError:
+        e = sys.exc_info()[1]
         raise DistutilsSetupError(e)
 
 def check_test_suite(dist, attr, value):
             if not hasattr(self,ep.name):
                 setattr(self,ep.name,None)
         _Distribution.__init__(self,attrs)
-        if isinstance(self.metadata.version, (int,long,float)):
+        if isinstance(self.metadata.version, numeric_types):
             # Some people apparently take "version number" too literally :)
             self.metadata.version = str(self.metadata.version)
 
             raise DistutilsSetupError(
                 "packages: setting must be a list or tuple (%r)" % (packages,)
             )
-        map(self.exclude_package, packages)
+        list(map(self.exclude_package, packages))
 
 
 

setuptools/package_index.py

 """PyPI and direct package downloading"""
-import sys, os.path, re, urlparse, urllib2, shutil, random, socket, cStringIO
-import itertools
-import base64
-import httplib, urllib
-from setuptools import ssl_support
+import sys, os.path, re, shutil, random, socket
 from pkg_resources import *
 from distutils import log
 from distutils.errors import DistutilsError
+
+from setuptools import ssl_support
+from setuptools.compat import (urllib2, httplib, StringIO, HTTPError,
+                               urlparse, urlunparse, unquote, splituser,
+                               url2pathname, name2codepoint, ifilterfalse,
+                               unichr, urljoin)
 try:
     from hashlib import md5
 except ImportError:
 
 
 def egg_info_for_url(url):
-    scheme, server, path, parameters, query, fragment = urlparse.urlparse(url)
+    scheme, server, path, parameters, query, fragment = urlparse(url)
     base = urllib2.unquote(path.split('/')[-1])
     if server=='sourceforge.net' and base=='download':    # XXX Yuck
         base = urllib2.unquote(path.split('/')[-2])
     seen = set()
     seen_add = seen.add
     if key is None:
-        for element in itertools.ifilterfalse(seen.__contains__, iterable):
+        for element in ifilterfalse(seen.__contains__, iterable):
             seen_add(element)
             yield element
     else:
         rels = map(str.strip, rel.lower().split(','))
         if 'homepage' in rels or 'download' in rels:
             for match in HREF.finditer(tag):
-                yield urlparse.urljoin(url, htmldecode(match.group(1)))
+                yield urljoin(url, htmldecode(match.group(1)))
 
     for tag in ("<th>Home Page", "<th>Download URL"):
         pos = page.find(tag)
         if pos!=-1:
             match = HREF.search(page,pos)
             if match:
-                yield urlparse.urljoin(url, htmldecode(match.group(1)))
+                yield urljoin(url, htmldecode(match.group(1)))
 
 user_agent = "Python-urllib/%s setuptools/%s" % (
     sys.version[:3], require('setuptools')[0].version
                 self.debug("Found link: %s", url)
 
         if dists or not retrieve or url in self.fetched_urls:
-            map(self.add, dists)
+            list(map(self.add, dists))
             return  # don't need the actual page
 
         if not self.url_ok(url):
         base = f.url     # handle redirects
         page = f.read()
         if not isinstance(page, str): # We are in Python 3 and got bytes. We want str.
-            if isinstance(f, urllib2.HTTPError):
+            if isinstance(f, HTTPError):
                 # Errors have no charset, assume latin1:
                 charset = 'latin-1'
             else:
             page = page.decode(charset, "ignore")
         f.close()
         for match in HREF.finditer(page):
-            link = urlparse.urljoin(base, htmldecode(match.group(1)))
+            link = urljoin(base, htmldecode(match.group(1)))
             self.process_url(link)
         if url.startswith(self.index_url) and getattr(f,'code',None)!=404:
             page = self.process_index(url, page)
         dists = distros_for_filename(fn)
         if dists:
             self.debug("Found: %s", fn)
-            map(self.add, dists)
+            list(map(self.add, dists))
 
     def url_ok(self, url, fatal=False):
         s = URL_SCHEME(url)
-        if (s and s.group(1).lower()=='file') or self.allows(urlparse.urlparse(url)[1]):
+        if (s and s.group(1).lower()=='file') or self.allows(urlparse(url)[1]):
             return True
         msg = "\nLink to % s ***BLOCKED*** by --allow-hosts\n"
         if fatal:
                         self.scan_egg_link(item, entry)
 
     def scan_egg_link(self, path, entry):
-        lines = filter(None, map(str.strip, open(os.path.join(path, entry))))
+        lines = list(filter(None, map(str.strip, open(os.path.join(path, entry)))))
         if len(lines)==2:
             for dist in find_distributions(os.path.join(path, lines[0])):
                 dist.location = os.path.join(path, *lines)
         def scan(link):
             # Process a URL to see if it's for a package page
             if link.startswith(self.index_url):
-                parts = map(
-                    urllib2.unquote, link[len(self.index_url):].split('/')
-                )
+                parts = list(map(
+                    unquote, link[len(self.index_url):].split('/')
+                ))
                 if len(parts)==2 and '#' not in parts[1]:
                     # it's a package page, sanitize and index it
                     pkg = safe_name(parts[0])
         # process an index page into the package-page index
         for match in HREF.finditer(page):
             try:
-                scan( urlparse.urljoin(url, htmldecode(match.group(1))) )
+                scan( urljoin(url, htmldecode(match.group(1))) )
             except ValueError:
                 pass
 
     def prescan(self):
         """Scan urls scheduled for prescanning (e.g. --find-links)"""
         if self.to_scan:
-            map(self.scan_url, self.to_scan)
+            list(map(self.scan_url, self.to_scan))
         self.to_scan = None     # from now on, go ahead and process immediately
 
     def not_found_in_index(self, requirement):
             if '#' in url:
                 url, info = url.split('#', 1)
             fp = self.open_url(url)
-            if isinstance(fp, urllib2.HTTPError):
+            if isinstance(fp, HTTPError):
                 raise DistutilsError(
                     "Can't download %s: %s %s" % (url, fp.code,fp.msg)
                 )
             return local_open(url)
         try:
             return open_with_auth(url, self.opener)
-        except (ValueError, httplib.InvalidURL), v:
+        except (ValueError, httplib.InvalidURL):
+            v = sys.exc_info()[1]
             msg = ' '.join([str(arg) for arg in v.args])
             if warning:
                 self.warn(warning, msg)
             else:
                 raise DistutilsError('%s %s' % (url, msg))
-        except urllib2.HTTPError, v:
+        except urllib2.HTTPError:
+            v = sys.exc_info()[1]
             return v
-        except urllib2.URLError, v:
+        except urllib2.URLError:
+            v = sys.exc_info()[1]
             if warning:
                 self.warn(warning, v.reason)
             else:
                 raise DistutilsError("Download error for %s: %s"
                                      % (url, v.reason))
-        except httplib.BadStatusLine, v:
+        except httplib.BadStatusLine:
+            v = sys.exc_info()[1]
             if warning:
                 self.warn(warning, v.line)
             else:
                 raise DistutilsError('%s returned a bad status line. '
                                      'The server might be down, %s' % \
                                              (url, v.line))
-        except httplib.HTTPException, v:
+        except httplib.HTTPException:
+            v = sys.exc_info()[1]
             if warning:
                 self.warn(warning, v)
             else:
         elif scheme.startswith('hg+'):
             return self._download_hg(url, filename)
         elif scheme=='file':
-            return urllib.url2pathname(urlparse.urlparse(url)[2])
+            return url2pathname(urlparse(url)[2])
         else:
             self.url_ok(url, True)   # raises error if not allowed
             return self._attempt_download(url, filename)
         url = url.split('#',1)[0]   # remove any fragment for svn's sake
         creds = ''
         if url.lower().startswith('svn:') and '@' in url:
-            scheme, netloc, path, p, q, f = urlparse.urlparse(url)
+            scheme, netloc, path, p, q, f = urlparse(url)
             if not netloc and path.startswith('//') and '/' in path[2:]:
                 netloc, path = path[2:].split('/',1)
                 auth, host = urllib.splituser(netloc)
                     else:
                         creds = " --username="+auth
                     netloc = host
-                    url = urlparse.urlunparse((scheme, netloc, url, p, q, f))
+                    url = urlunparse((scheme, netloc, url, p, q, f))
         self.info("Doing subversion checkout from %s to %s", url, filename)
         os.system("svn checkout%s -q %s %s" % (creds, url, filename))
         return filename
 
     def _vcs_split_rev_from_url(self, url, pop_prefix=False):
-        scheme, netloc, path, query, frag = urlparse.urlsplit(url)
+        scheme, netloc, path, query, frag = urlsplit(url)
 
         scheme = scheme.split('+', 1)[-1]
 
             path, rev = path.rsplit('@', 1)
 
         # Also, discard fragment
-        url = urlparse.urlunsplit((scheme, netloc, path, query, ''))
+        url = urlunsplit((scheme, netloc, path, query, ''))
 
         return url, rev
 
     elif what.startswith('#'):
         what = int(what[1:])
     else:
-        from htmlentitydefs import name2codepoint
         what = name2codepoint.get(what, match.group(0))
     return uchr(what)
 
 def open_with_auth(url, opener=urllib2.urlopen):
     """Open a urllib2 request, handling HTTP authentication"""
 
-    scheme, netloc, path, params, query, frag = urlparse.urlparse(url)
+    scheme, netloc, path, params, query, frag = urlparse(url)
 
     # Double scheme does not raise on Mac OS X as revealed by a
     # failing test. We would expect "nonnumeric port". Refs #20.
         raise httplib.InvalidURL("nonnumeric port: ''")
 
     if scheme in ('http', 'https'):
-        auth, host = urllib.splituser(netloc)
+        auth, host = splituser(netloc)
     else:
         auth = None
 
     if auth:
         auth = "Basic " + _encode_auth(auth)
-        new_url = urlparse.urlunparse((scheme,host,path,params,query,frag))
+        new_url = urlunparse((scheme,host,path,params,query,frag))
         request = urllib2.Request(new_url)
         request.add_header("Authorization", auth)
     else:
     if auth:
         # Put authentication info back into request URL if same host,
         # so that links found on the page will work
-        s2, h2, path2, param2, query2, frag2 = urlparse.urlparse(fp.url)
+        s2, h2, path2, param2, query2, frag2 = urlparse(fp.url)
         if s2==scheme and h2==host:
-            fp.url = urlparse.urlunparse((s2,netloc,path2,param2,query2,frag2))
+            fp.url = urlunparse((s2,netloc,path2,param2,query2,frag2))
 
     return fp
 
 
 def local_open(url):
     """Read a local path, with special support for directories"""
-    scheme, server, path, param, query, frag = urlparse.urlparse(url)
-    filename = urllib.url2pathname(path)
+    scheme, server, path, param, query, frag = urlparse(url)
+    filename = url2pathname(path)
     if os.path.isfile(filename):
         return urllib2.urlopen(url)
     elif path.endswith('/') and os.path.isdir(filename):
     else:
         status, message, body = 404, "Path not found", "Not found"
 
-    return urllib2.HTTPError(url, status, message,
-            {'content-type':'text/html'}, cStringIO.StringIO(body))
+    return HTTPError(url, status, message,
+            {'content-type':'text/html'}, StringIO(body))
 
 
 

setuptools/sandbox.py

-import os, sys, __builtin__, tempfile, operator, pkg_resources
+import os, sys, tempfile, operator, pkg_resources
 if os.name == "java":
     import org.python.modules.posix.PosixModule as _os
 else:
     _file = None
 _open = open
 from distutils.errors import DistutilsError
+from setuptools.compat import builtins, execfile, reduce
 from pkg_resources import working_set
 
 __all__ = [
                     {'__file__':setup_script, '__name__':'__main__'}
                 )
             )
-        except SystemExit, v:
+        except SystemExit:
+            v = sys.exc_info()[1]
             if v.args and v.args[0]:
                 raise
             # Normal exit, just return
         try:
             self._copy(self)
             if _file:
-                __builtin__.file = self._file
-            __builtin__.open = self._open
+                builtins.file = self._file
+            builtins.open = self._open
             self._active = True
             return func()
         finally:
             self._active = False
             if _file:
-                __builtin__.file = _file
-            __builtin__.open = _open
+                builtins.file = _file
+            builtins.open = _open
             self._copy(_os)
 
     def _mk_dual_path_wrapper(name):
             self._violation(operation, src, dst, *args, **kw)
         return (src,dst)
 
-    def open(self, file, flags, mode=0777):
+    def open(self, file, flags, mode=0x1FF):    # 0777
         """Called for low-level os.open()"""
         if flags & WRITE_FLAGS and not self._ok(file):
             self._violation("os.open", file, flags, mode)

setuptools/ssl_support.py

-import sys, os, socket, urllib2, atexit, re
+import sys, os, socket, atexit, re
 import pkg_resources
 from pkg_resources import ResolutionError, ExtractionError
 
 try:
+    import urllib2
+except ImportError:
+    import urllib.request as urllib2
+
+try:
     import ssl
 except ImportError:
     ssl = None

setuptools/tests/__init__.py

 from distutils.core import Extension
 from distutils.version import LooseVersion
 
+from setuptools.compat import func_code
 import setuptools.dist
 import setuptools.depends as dep
 from setuptools import Feature
             x = "test"
             y = z
 
+        fc = func_code(f1)
         # unrecognized name
-        self.assertEqual(dep.extract_constant(f1.func_code,'q', -1), None)
+        self.assertEqual(dep.extract_constant(fc,'q', -1), None)
 
         # constant assigned
-        self.assertEqual(dep.extract_constant(f1.func_code,'x', -1), "test")
+        self.assertEqual(dep.extract_constant(fc,'x', -1), "test")
 
         # expression assigned
-        self.assertEqual(dep.extract_constant(f1.func_code,'y', -1), -1)
+        self.assertEqual(dep.extract_constant(fc,'y', -1), -1)
 
         # recognized name, not assigned
-        self.assertEqual(dep.extract_constant(f1.func_code,'z', -1), None)
+        self.assertEqual(dep.extract_constant(fc,'z', -1), None)
 
     def testFindModule(self):
         self.assertRaises(ImportError, dep.find_module, 'no-such.-thing')

setuptools/tests/doctest.py

 try:
     basestring
 except NameError:
-    basestring = str,unicode
+    basestring = str
 
 try:
     enumerate
 import sys, traceback, inspect, linecache, os, re, types
 import unittest, difflib, pdb, tempfile
 import warnings
-from StringIO import StringIO
+from setuptools.compat import StringIO, execfile, exec_, func_code, im_func
 
 # Don't whine about the deprecated is_private function in this
 # module's tests.
     """
     if inspect.ismodule(module):
         return module
-    elif isinstance(module, (str, unicode)):
+    elif isinstance(module, basestring):
         return __import__(module, globals(), locals(), ["*"])
     elif module is None:
         return sys.modules[sys._getframe(depth).f_globals['__name__']]
 # [XX] Normalize with respect to os.path.pardir?
 def _module_relative_path(module, path):
     if not inspect.ismodule(module):
-        raise TypeError, 'Expected a module: %r' % module
+        raise TypeError('Expected a module: %r' % module)
     if path.startswith('/'):
-        raise ValueError, 'Module-relative files may not have absolute paths'
+        raise ValueError('Module-relative files may not have absolute paths')
 
     # Find the base directory for the path.
     if hasattr(module, '__file__'):
         if module is None:
             return True
         elif inspect.isfunction(object):
-            return module.__dict__ is object.func_globals
+            return module.__dict__ is func_globals(object)
         elif inspect.isclass(object):
             return module.__name__ == object.__module__
         elif inspect.getmodule(object) is not None:
         add them to `tests`.
         """
         if self._verbose:
-            print 'Finding tests in %s' % name
+            print('Finding tests in %s' % name)
 
         # If we've already processed this object, then ignore it.
         if id(obj) in seen:
                 if isinstance(val, staticmethod):
                     val = getattr(obj, valname)
                 if isinstance(val, classmethod):
-                    val = getattr(obj, valname).im_func
+                    val = im_func(getattr(obj, valname))
 
                 # Recurse to methods, properties, and nested classes.
                 if ((inspect.isfunction(val) or inspect.isclass(val) or
                     break
 
         # Find the line number for functions & methods.
-        if inspect.ismethod(obj): obj = obj.im_func
-        if inspect.isfunction(obj): obj = obj.func_code
+        if inspect.ismethod(obj): obj = im_func(obj)
+        if inspect.isfunction(obj): obj = func_code(obj)
         if inspect.istraceback(obj): obj = obj.tb_frame
         if inspect.isframe(obj): obj = obj.f_code
         if inspect.iscode(obj):
             # keyboard interrupts.)
             try:
                 # Don't blink!  This is where the user's code gets run.
-                exec compile(example.source, filename, "single",
-                             compileflags, 1) in test.globs
+                exec_(compile(example.source, filename, "single",
+                              compileflags, 1), test.globs)
                 self.debugger.set_continue() # ==== Example Finished ====
                 exception = None
             except KeyboardInterrupt:
         if m and m.group('name') == self.test.name:
             example = self.test.examples[int(m.group('examplenum'))]
             return example.source.splitlines(True)
-        elif self.save_linecache_getlines.func_code.co_argcount>1:
+        elif func_code(self.save_linecache_getlines).co_argcount > 1:
             return self.save_linecache_getlines(filename, module_globals)
         else:
             return self.save_linecache_getlines(filename)
                 failed.append(x)
         if verbose:
             if notests:
-                print len(notests), "items had no tests:"
+                print(len(notests), "items had no tests:")
                 notests.sort()
                 for thing in notests:
-                    print "   ", thing
+                    print("   ", thing)
             if passed:
-                print len(passed), "items passed all tests:"
+                print(len(passed), "items passed all tests:")
                 passed.sort()
                 for thing, count in passed:
-                    print " %3d tests in %s" % (count, thing)
+                    print(" %3d tests in %s" % (count, thing))
         if failed:
-            print self.DIVIDER
-            print len(failed), "items had failures:"
+            print(self.DIVIDER)
+            print(len(failed), "items had failures:")
             failed.sort()
             for thing, (f, t) in failed:
-                print " %3d of %3d in %s" % (f, t, thing)
+                print(" %3d of %3d in %s" % (f, t, thing))
         if verbose:
-            print totalt, "tests in", len(self._name2ft), "items."
-            print totalt - totalf, "passed and", totalf, "failed."
+            print(totalt, "tests in", len(self._name2ft), "items.")
+            print(totalt - totalf, "passed and", totalf, "failed.")
         if totalf:
-            print "***Test Failed***", totalf, "failures."
+            print("***Test Failed***", totalf, "failures.")
         elif verbose:
-            print "Test passed."
+            print("Test passed.")
         return totalf, totalt
 
     #/////////////////////////////////////////////////////////////////
         d = self._name2ft
         for name, (f, t) in other._name2ft.items():
             if name in d:
-                print "*** DocTestRunner.merge: '" + name + "' in both" \
-                    " testers; summing outcomes."
+                print("*** DocTestRunner.merge: '" + name + "' in both" \
+                    " testers; summing outcomes.")
                 f2, t2 = d[name]
                 f = f + f2
                 t = t + t2
     def runstring(self, s, name):
         test = DocTestParser().get_doctest(s, self.globs, name, None, None)
         if self.verbose:
-            print "Running string", name
+            print("Running string", name)
         (f,t) = self.testrunner.run(test)
         if self.verbose:
-            print f, "of", t, "examples failed in string", name
+            print(f, "of", t, "examples failed in string", name)
         return (f,t)
 
     def rundoc(self, object, name=None, module=None):
             try:
                 execfile(srcfilename, globs, globs)
             except:
-                print sys.exc_info()[1]
+                print(sys.exc_info()[1])
                 pdb.post_mortem(sys.exc_info()[2])
         else:
             # Note that %r is vital here.  '%s' instead can, e.g., cause

setuptools/tests/server.py

 """Basic http server for tests to simulate PyPI or custom indexes
 """
-import urllib2
 import sys
 import time
-import threading
-import BaseHTTPServer
-from BaseHTTPServer import HTTPServer
-from SimpleHTTPServer import SimpleHTTPRequestHandler
+from threading import Thread
+from setuptools.compat import (urllib2, URLError, HTTPServer,
+                               SimpleHTTPRequestHandler,
+                               BaseHTTPRequestHandler)
 
 class IndexServer(HTTPServer):
     """Basic single-threaded http server simulating a package index
             self.handle_request()
 
     def start(self):
-        self.thread = threading.Thread(target=self.serve)
+        self.thread = Thread(target=self.serve)
         self.thread.start()
 
     def stop(self):
             # ignore any errors; all that's important is the request
             pass
         self.thread.join()
+        self.socket.close()
 
     def base_url(self):
         port = self.server_port
         return 'http://127.0.0.1:%s/setuptools/tests/indexes/' % port
 
-class RequestRecorder(BaseHTTPServer.BaseHTTPRequestHandler):
+class RequestRecorder(BaseHTTPRequestHandler):
     def do_GET(self):
         requests = vars(self.server).setdefault('requests', [])
         requests.append(self)
         self.send_response(200, 'OK')
 
-class MockServer(HTTPServer, threading.Thread):
+class MockServer(HTTPServer, Thread):
     """
     A simple HTTP Server that records the requests made to it.
     """
     def __init__(self, server_address=('', 0),
             RequestHandlerClass=RequestRecorder):
         HTTPServer.__init__(self, server_address, RequestHandlerClass)
-        threading.Thread.__init__(self)
+        Thread.__init__(self)
         self.setDaemon(True)
         self.requests = []
 

setuptools/tests/test_bdist_egg.py

 import os, re, shutil, tempfile, unittest
 import tempfile
 import site
-from StringIO import StringIO
 
 from distutils.errors import DistutilsError
+from setuptools.compat import StringIO
 from setuptools.command.bdist_egg import bdist_egg
 from setuptools.command import easy_install as easy_install_pkg
 from setuptools.dist import Distribution

setuptools/tests/test_develop.py

 import os, shutil, tempfile, unittest
 import tempfile
 import site
-from StringIO import StringIO
 
 from distutils.errors import DistutilsError
 from setuptools.command.develop import develop
 from setuptools.command import easy_install as easy_install_pkg
+from setuptools.compat import StringIO
 from setuptools.dist import Distribution
 
 SETUP_PY = """\
         f = open(init, 'w')
         f.write(INIT_PY)
         f.close()
-        
+
         os.chdir(self.dir)
         self.old_base = site.USER_BASE
         site.USER_BASE = tempfile.mkdtemp()
         site.USER_SITE = tempfile.mkdtemp()
 
     def tearDown(self):
-        if sys.version < "2.6" or hasattr(sys, 'real_prefix'):
+        if sys.version < "2.6" or hasattr(sys, 'real_prefix') or (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix):
             return
-        
+
         os.chdir(self.old_cwd)
         shutil.rmtree(self.dir)
         shutil.rmtree(site.USER_BASE)
         try:
             try:
                 dist = Distribution({'setup_requires': ['I_DONT_EXIST']})
-            except DistutilsError, e:
+            except DistutilsError:
+                e = sys.exc_info()[1]
                 error = str(e)
                 if error ==  wanted:
                     pass

setuptools/tests/test_easy_install.py

 import site
 import textwrap
 import tarfile
-import urlparse
-import StringIO
 import distutils.core
 
+from setuptools.compat import StringIO, BytesIO, next, urlparse
 from setuptools.sandbox import run_setup, SandboxViolation
 from setuptools.command.easy_install import easy_install, fix_jython_executable, get_script_args
 from setuptools.command.easy_install import  PthDistributions
 
         old_platform = sys.platform
         try:
-            name, script = [i for i in get_script_args(dist).next()][0:2]
+            name, script = [i for i in next(get_script_args(dist))][0:2]
         finally:
             sys.platform = old_platform
 
         cmd.install_dir = os.path.join(tempfile.mkdtemp(), 'ok')
         cmd.args = ['ok']
         cmd.ensure_finalized()
-        keys = cmd.package_index.scanned_urls.keys()
-        keys.sort()
+        keys = sorted(cmd.package_index.scanned_urls.keys())
         self.assertEqual(keys, ['link1', 'link2'])
 
 
 
         old_stdout = sys.stdout
         old_stderr = sys.stderr
-        sys.stdout = StringIO.StringIO()
-        sys.stderr = StringIO.StringIO()
+        sys.stdout = StringIO()
+        sys.stderr = StringIO()
         try:
             reset_setup_stop_context(
                 lambda: run_setup(test_setup_py, ['install'])
         p_index = setuptools.tests.server.MockServer()
         p_index.start()
         netloc = 1
-        p_index_loc = urlparse.urlparse(p_index.url)[netloc]
+        p_index_loc = urlparse(p_index.url)[netloc]
         if p_index_loc.endswith(':0'):
             # Some platforms (Jython) don't find a port to which to bind,
             #  so skip this test for them.
     setup_py_file = tarfile.TarInfo(name='setup.py')
     try:
         # Python 3 (StringIO gets converted to io module)
-        MemFile = StringIO.BytesIO
+        MemFile = BytesIO
     except AttributeError:
-        MemFile = StringIO.StringIO
+        MemFile = StringIO
     setup_py_bytes = MemFile(setup_py.encode('utf-8'))
     setup_py_file.size = len(setup_py_bytes.getvalue())
     dist = tarfile.open(dist_path, 'w:gz')

setuptools/tests/test_packageindex.py

 """
 import sys
 import unittest
-import urllib2
 import pkg_resources
-import httplib
+from setuptools.compat import urllib2, httplib, HTTPError, unicode
 import distutils.errors
 import setuptools.package_index
-from server import IndexServer
+from setuptools.tests.server import IndexServer
 
 class TestPackageIndex(unittest.TestCase):
 
         url = 'http://127.0.0.1:0/nonesuch/test_package_index'
         try:
             v = index.open_url(url)
-        except Exception, v:
+        except Exception:
+            v = sys.exc_info()[1]
             self.assertTrue(url in str(v))
         else:
-            self.assertTrue(isinstance(v,urllib2.HTTPError))
+            self.assertTrue(isinstance(v, HTTPError))
 
     def test_bad_url_typo(self):
         # issue 16
         url = 'url:%20https://svn.plone.org/svn/collective/inquant.contentmirror.plone/trunk'
         try:
             v = index.open_url(url)
-        except Exception, v:
+        except Exception:
+            v = sys.exc_info()[1]
             self.assertTrue(url in str(v))
         else:
-            self.assertTrue(isinstance(v, urllib2.HTTPError))
+            self.assertTrue(isinstance(v, HTTPError))
 
     def test_bad_url_bad_status_line(self):
         index = setuptools.package_index.PackageIndex(
         )
 
         def _urlopen(*args):
-            import httplib
             raise httplib.BadStatusLine('line')
 
         index.opener = _urlopen
         url = 'http://example.com'
         try:
             v = index.open_url(url)
-        except Exception, v:
+        except Exception:
+            v = sys.exc_info()[1]
             self.assertTrue('line' in str(v))
         else:
             raise AssertionError('Should have raise here!')
         url = 'http://http://svn.pythonpaste.org/Paste/wphp/trunk'
         try:
             index.open_url(url)
-        except distutils.errors.DistutilsError, error:
-            msg = unicode(error)
+        except distutils.errors.DistutilsError:
+            msg = unicode(sys.exc_info()[1])
             assert 'nonnumeric port' in msg or 'getaddrinfo failed' in msg or 'Name or service not known' in msg
             return
         raise RuntimeError("Did not raise")

setuptools/tests/test_resources.py

 # NOTE: the shebang and encoding lines are for ScriptHeaderTests; do not remove
 from unittest import TestCase, makeSuite; from pkg_resources import *
 from setuptools.command.easy_install import get_script_header, is_sh
-import os, pkg_resources, sys, StringIO, tempfile, shutil
+from setuptools.compat import StringIO, iteritems
+import os, pkg_resources, sys, tempfile, shutil
 try: frozenset
 except NameError:
     from sets import ImmutableSet as frozenset
         for i in range(3):
             targets = list(ws.resolve(parse_requirements("Foo"), ad))
             self.assertEqual(targets, [Foo])
-            map(ws.add,targets)
+            list(map(ws.add,targets))
         self.assertRaises(VersionConflict, ws.resolve,
             parse_requirements("Foo==0.9"), ad)
         ws = WorkingSet([]) # reset
 
     def checkSubMap(self, m):
         self.assertEqual(len(m), len(self.submap_expect))
-        for key, ep in self.submap_expect.iteritems():
+        for key, ep in iteritems(self.submap_expect):
             self.assertEqual(repr(m.get(key)), repr(ep))
 
     submap_expect = dict(
     def testParseMap(self):
         m = EntryPoint.parse_map({'xyz':self.submap_str})
         self.checkSubMap(m['xyz'])
-        self.assertEqual(m.keys(),['xyz'])
+        self.assertEqual(list(m.keys()),['xyz'])
         m = EntryPoint.parse_map("[xyz]\n"+self.submap_str)
         self.checkSubMap(m['xyz'])
-        self.assertEqual(m.keys(),['xyz'])
+        self.assertEqual(list(m.keys()),['xyz'])
         self.assertRaises(ValueError, EntryPoint.parse_map, ["[xyz]", "[xyz]"])
         self.assertRaises(ValueError, EntryPoint.parse_map, self.submap_str)
 
 
             # Ensure we generate what is basically a broken shebang line
             # when there's options, with a warning emitted
-            sys.stdout = sys.stderr = StringIO.StringIO()
+            sys.stdout = sys.stderr = StringIO()
             self.assertEqual(get_script_header('#!/usr/bin/python -x',
                                                executable=exe),
                              '#!%s  -x\n' % exe)
             self.assertTrue('Unable to adapt shebang line' in sys.stdout.getvalue())
-            sys.stdout = sys.stderr = StringIO.StringIO()
+            sys.stdout = sys.stderr = StringIO()
             self.assertEqual(get_script_header('#!/usr/bin/python',
                                                executable=self.non_ascii_exe),
                              '#!%s -x\n' % self.non_ascii_exe)
         self._assertIn("pkg1", pkg_resources._namespace_packages.keys())
         try:
             import pkg1.pkg2
-        except ImportError, e:
+        except ImportError:
             self.fail("Setuptools tried to import the parent namespace package")
         # check the _namespace_packages dict
         self._assertIn("pkg1.pkg2", pkg_resources._namespace_packages.keys())

setuptools/tests/test_sdist.py

 import sys
 import tempfile
 import unittest
-import urllib
 import unicodedata
-from StringIO import StringIO
 
 
+from setuptools.compat import StringIO, quote, unicode
 from setuptools.command.sdist import sdist
 from setuptools.command.egg_info import manifest_maker
 from setuptools.dist import Distribution
         # The manifest should be UTF-8 encoded
         try:
             u_contents = contents.decode('UTF-8')
-        except UnicodeDecodeError, e:
+        except UnicodeDecodeError:
+            e = sys.exc_info()[1]
             self.fail(e)
 
         # The manifest should contain the UTF-8 filename
             # The manifest should be UTF-8 encoded
             try:
                 contents.decode('UTF-8')
-            except UnicodeDecodeError, e:
+            except UnicodeDecodeError:
+                e = sys.exc_info()[1]
                 self.fail(e)
 
             # The manifest should contain the UTF-8 filename
             # The manifest should be UTF-8 encoded
             try:
                 contents.decode('UTF-8')
-            except UnicodeDecodeError, e:
+            except UnicodeDecodeError:
+                e = sys.exc_info()[1]
                 self.fail(e)
 
             # The Latin-1 filename should have been skipped
             try:
                 try:
                     cmd.read_manifest()
-                except UnicodeDecodeError, e:
+                except UnicodeDecodeError:
+                    e = sys.exc_info()[1]
                     self.fail(e)
             finally:
                 unquiet()

setuptools/tests/test_test.py

 import os, shutil, tempfile, unittest
 import tempfile
 import site
-from StringIO import StringIO
 
 from distutils.errors import DistutilsError
+from setuptools.compat import StringIO
 from setuptools.command.test import test
 from setuptools.command import easy_install as easy_install_pkg
 from setuptools.dist import Distribution

tests/api_tests.txt

     >>> dist.py_version == sys.version[:3]
     True
 
-    >>> print dist.platform
+    >>> print(dist.platform)
     None
 
 Including various computed attributes::
 You can ask a WorkingSet to ``find()`` a distribution matching a requirement::
 
     >>> from pkg_resources import Requirement
-    >>> print ws.find(Requirement.parse("Foo==1.0"))    # no match, return None
+    >>> print(ws.find(Requirement.parse("Foo==1.0")))   # no match, return None
     None
 
     >>> ws.find(Requirement.parse("Bar==0.9"))  # match, return distribution
 once for each existing distribution in the working set, and then is called
 again for new distributions added thereafter::
 
-    >>> def added(dist): print "Added", dist
+    >>> def added(dist): print("Added %s" % dist)
     >>> ws.subscribe(added)
     Added Bar 0.9
     >>> foo12 = Distribution(project_name="Foo", version="1.2", location="f12")

tests/manual_test.py

 import tempfile
 from distutils.command.install import INSTALL_SCHEMES
 from string import Template
-from urllib2 import urlopen
+from setuptools.compat import urlopen
 
 try:
     import subprocess
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.