changeset 5602:c40d04915e23 REST-rebased

Python3 fixes
author Ralf Schlatterbeck <rsc@runtux.com>
date Wed, 30 Jan 2019 13:58:18 +0100
parents fcbeff272828
children 79da1ca2f94b
files roundup/rest.py test/rest_common.py
diffstat 2 files changed, 45 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/roundup/rest.py	Wed Jan 30 13:58:14 2019 +0100
+++ b/roundup/rest.py	Wed Jan 30 13:58:18 2019 +0100
@@ -5,7 +5,12 @@
 and/or modify under the same terms as Python.
 """
 
-import urlparse
+from __future__ import print_function
+
+try:
+    from urllib.parse import urlparse
+except ImportError:
+    from urlparse import urlparse
 import os
 import json
 import pprint
@@ -20,6 +25,12 @@
 from roundup.exceptions import *
 from roundup.cgi.exceptions import *
 
+# Py3 compatible basestring
+try:
+    basestring
+except NameError:
+    basestring = str
+    unicode = str
 
 def _data_decorator(func):
     """Wrap the returned data into an object."""
@@ -27,22 +38,22 @@
         # get the data / error from function
         try:
             code, data = func(self, *args, **kwargs)
-        except NotFound, msg:
+        except NotFound as msg:
             code = 404
             data = msg
-        except IndexError, msg:
+        except IndexError as msg:
             code = 404
             data = msg
-        except Unauthorised, msg:
+        except Unauthorised as msg:
             code = 403
             data = msg
-        except UsageError, msg:
+        except UsageError as msg:
             code = 400
             data = msg
-        except (AttributeError, Reject), msg:
+        except (AttributeError, Reject) as msg:
             code = 405
             data = msg
-        except ValueError, msg:
+        except ValueError as msg:
             code = 409
             data = msg
         except NotImplementedError:
@@ -52,13 +63,13 @@
             exc, val, tb = sys.exc_info()
             code = 400
             ts = time.ctime()
-            if self.client.request.DEBUG_MODE:
+            if getattr (self.client.request, 'DEBUG_MODE', None):
                 data = val
             else:
                 data = '%s: An error occurred. Please check the server log' \
                        ' for more information.' % ts
             # out to the logfile
-            print 'EXCEPTION AT', ts
+            print ('EXCEPTION AT', ts)
             traceback.print_exc()
 
         # decorate it
@@ -284,19 +295,15 @@
         prop = None
         if isinstance(key, unicode):
             try:
-                key = key.encode('ascii')
+                x = key.encode('ascii')
             except UnicodeEncodeError:
                 raise UsageError(
                     'argument %r is no valid ascii keyword' % key
                 )
-        if isinstance(value, unicode):
-            value = value.encode('utf-8')
         if value:
             try:
-                prop = hyperdb.rawToHyperdb(
-                    self.db, cl, itemid, key, value
-                )
-            except hyperdb.HyperdbValueError, msg:
+                prop = hyperdb.rawToHyperdb(self.db, cl, itemid, key, value)
+            except hyperdb.HyperdbValueError as msg:
                 raise UsageError(msg)
 
         return prop
@@ -478,9 +485,7 @@
                 props = value.split(",")
 
         if props is None:
-            props = class_obj.properties.keys()
-
-        props.sort()  # sort properties
+            props = list(sorted(class_obj.properties.keys()))
 
         try:
             result = [
@@ -490,7 +495,7 @@
                     'View', self.db.getuid(), class_name, prop_name,
                 )
             ]
-        except KeyError, msg:
+        except KeyError as msg:
             raise UsageError("%s field not valid" % msg)
         result = {
             'id': item_id,
@@ -592,9 +597,9 @@
         try:
             item_id = class_obj.create(**props)
             self.db.commit()
-        except (TypeError, IndexError, ValueError), message:
+        except (TypeError, IndexError, ValueError) as message:
             raise ValueError(message)
-        except KeyError, msg:
+        except KeyError as msg:
             raise UsageError("Must provide the %s property." % msg)
 
         # set the header Location
@@ -634,7 +639,7 @@
         class_obj = self.db.getclass(class_name)
 
         props = self.props_from_args(class_obj, input.value, item_id)
-        for p in props.iterkeys():
+        for p in props:
             if not self.db.security.hasPermission(
                 'Edit', self.db.getuid(), class_name, p, item_id
             ):
@@ -645,7 +650,7 @@
         try:
             result = class_obj.set(item_id, **props)
             self.db.commit()
-        except (TypeError, IndexError, ValueError), message:
+        except (TypeError, IndexError, ValueError) as message:
             raise ValueError(message)
 
         result = {
@@ -696,7 +701,7 @@
         try:
             result = class_obj.set(item_id, **props)
             self.db.commit()
-        except (TypeError, IndexError, ValueError), message:
+        except (TypeError, IndexError, ValueError) as message:
             raise ValueError(message)
 
         result = {
@@ -820,7 +825,7 @@
         try:
             class_obj.set(item_id, **props)
             self.db.commit()
-        except (TypeError, IndexError, ValueError), message:
+        except (TypeError, IndexError, ValueError) as message:
             raise ValueError(message)
 
         result = {
@@ -893,7 +898,7 @@
             # else patch operation is processing data
             props = self.props_from_args(class_obj, input.value, item_id)
 
-            for prop, value in props.iteritems():
+            for prop in props:
                 if not self.db.security.hasPermission(
                     'Edit', self.db.getuid(), class_name, prop, item_id
                 ):
@@ -909,7 +914,7 @@
             try:
                 result = class_obj.set(item_id, **props)
                 self.db.commit()
-            except (TypeError, IndexError, ValueError), message:
+            except (TypeError, IndexError, ValueError) as message:
                 raise ValueError(message)
 
             result = {
@@ -975,7 +980,7 @@
         try:
             result = class_obj.set(item_id, **props)
             self.db.commit()
-        except (TypeError, IndexError, ValueError), message:
+        except (TypeError, IndexError, ValueError) as message:
             raise ValueError(message)
 
         result = {
@@ -1113,7 +1118,7 @@
         # priority : extension from uri (/rest/issue.json),
         #            header (Accept: application/json, application/xml)
         #            default (application/json)
-        ext_type = os.path.splitext(urlparse.urlparse(uri).path)[1][1:]
+        ext_type = os.path.splitext(urlparse(uri).path)[1][1:]
         data_type = ext_type or accept_type or self.__default_accept_type
 
         # check for pretty print
@@ -1140,9 +1145,9 @@
         # Call the appropriate method
         try:
             output = Routing.execute(self, uri, method, input)
-        except NotFound, msg:
+        except NotFound as msg:
             output = self.error_obj(404, msg)
-        except Reject, msg:
+        except Reject as msg:
             output = self.error_obj(405, msg)
 
         # Format the content type
--- a/test/rest_common.py	Wed Jan 30 13:58:14 2019 +0100
+++ b/test/rest_common.py	Wed Jan 30 13:58:18 2019 +0100
@@ -10,7 +10,7 @@
 from roundup.cgi import client
 import random
 
-import db_test_base
+from .db_test_base import setupTracker
 
 NEEDS_INSTANCE = 1
 
@@ -22,7 +22,7 @@
     def setUp(self):
         self.dirname = '_test_rest'
         # set up and open a tracker
-        self.instance = db_test_base.setupTracker(self.dirname, self.backend)
+        self.instance = setupTracker(self.dirname, self.backend)
 
         # open the database
         self.db = self.instance.open('admin')
@@ -49,7 +49,9 @@
 
         thisdir = os.path.dirname(__file__)
         vars = {}
-        execfile(os.path.join(thisdir, "tx_Source_detector.py"), vars)
+        with open(os.path.join(thisdir, "tx_Source_detector.py")) as f:
+            code = compile(f.read(), "tx_Source_detector.py", "exec")
+            exec(code, vars)
         vars['init'](self.db)
 
         env = {
@@ -66,7 +68,7 @@
         self.db.close()
         try:
             shutil.rmtree(self.dirname)
-        except OSError, error:
+        except OSError as error:
             if error.errno not in (errno.ENOENT, errno.ESRCH):
                 raise
 
@@ -341,7 +343,7 @@
         ]
         try:
             self.server.put_element('user', '2', form)
-        except Unauthorised, err:
+        except Unauthorised as err:
             self.fail('raised %s' % err)
         finally:
             self.db.setCurrentUser('joe')
@@ -357,7 +359,7 @@
         ]
         try:
             self.server.post_collection('user', form)
-        except Unauthorised, err:
+        except Unauthorised as err:
             self.fail('raised %s' % err)
         finally:
             self.db.setCurrentUser('joe')

Roundup Issue Tracker: http://roundup-tracker.org/