[Python-checkins] distutils2: Further work on Py3 support; try blocks for imports mainly

tarek.ziade python-checkins at python.org
Sun Mar 13 19:45:16 CET 2011


http://hg.python.org/distutils2/rev/48c6997fe43c
changeset:   1110:48c6997fe43c
user:        Arc Riley <arcriley at gmail.com>
date:        Sun Mar 13 13:58:54 2011 -0400
summary:
  Further work on Py3 support; try blocks for imports mainly

files:
  distutils2/fancy_getopt.py
  distutils2/tests/pypi_server.py
  setup.py

diff --git a/distutils2/fancy_getopt.py b/distutils2/fancy_getopt.py
--- a/distutils2/fancy_getopt.py
+++ b/distutils2/fancy_getopt.py
@@ -374,8 +374,12 @@
     return parser.getopt(args, object)
 
 
-
-WS_TRANS = string.maketrans(string.whitespace, ' ' * len(string.whitespace))
+if 'maketrans' in str.__dict__ :
+    # Python 3.2+
+    WS_TRANS = str.maketrans(string.whitespace, ' ' * len(string.whitespace))
+else :
+    # Depreciated syntax
+    WS_TRANS = string.maketrans(string.whitespace, ' ' * len(string.whitespace))
 
 
 def wrap_text(text, width):
diff --git a/distutils2/tests/pypi_server.py b/distutils2/tests/pypi_server.py
--- a/distutils2/tests/pypi_server.py
+++ b/distutils2/tests/pypi_server.py
@@ -29,16 +29,23 @@
 implementations (static HTTP and XMLRPC over HTTP).
 """
 
-import Queue
-import SocketServer
 import os.path
 import select
 import socket
 import threading
 
-from BaseHTTPServer import HTTPServer
-from SimpleHTTPServer import SimpleHTTPRequestHandler
-from SimpleXMLRPCServer import SimpleXMLRPCServer
+# several packages had different names in Python 2.x
+try:
+    import queue
+    import socketserver
+    from http.server import HTTPServer, SimpleHTTPRequestHandler
+    from xmlrpc.server import SimpleXMLRPCServer
+except ImportError:
+    import Queue as queue
+    import SocketServer as socketserver
+    from BaseHTTPServer import HTTPServer    
+    from SimpleHTTPServer import SimpleHTTPRequestHandler
+    from SimpleXMLRPCServer import SimpleXMLRPCServer
 
 from distutils2.tests import unittest
 
@@ -109,7 +116,7 @@
             self.server = HTTPServer(('127.0.0.1', 0), PyPIRequestHandler)
             self.server.RequestHandlerClass.pypi_server = self
 
-            self.request_queue = Queue.Queue()
+            self.request_queue = queue.Queue()
             self._requests = []
             self.default_response_status = 200
             self.default_response_headers = [('Content-type', 'text/plain')]
@@ -157,7 +164,7 @@
         while True:
             try:
                 self._requests.append(self.request_queue.get_nowait())
-            except Queue.Empty:
+            except queue.Empty:
                 break
         return self._requests
 
@@ -252,7 +259,7 @@
 class PyPIXMLRPCServer(SimpleXMLRPCServer):
     def server_bind(self):
         """Override server_bind to store the server name."""
-        SocketServer.TCPServer.server_bind(self)
+        socketserver.TCPServer.server_bind(self)
         host, port = self.socket.getsockname()[:2]
         self.server_name = socket.getfqdn(host)
         self.server_port = port
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -13,14 +13,15 @@
 # Python 3.x hook to run 2to3 automatically
 try:
     from distutils.command.build_py import build_py_2to3 as build_py
+    from distutils.core import setup, Extension
 except ImportError:
     # 2.x, try to use setuptools if available
     try :
+        from setuptools.command.build_py import build_py
         from setuptools import setup, Extension
-        from setuptools.command.build_py import build_py
     except ImportError:
+        from distutils.command.build_py import build_py
         from distutils.core import setup, Extension
-        from distutils.command.build_py import build_py
 
 
 f = open('README.txt')

-- 
Repository URL: http://hg.python.org/distutils2


More information about the Python-checkins mailing list