Skip to content

Commit 558339b

Browse files
committed
Python3 fixes
1 parent 5562494 commit 558339b

File tree

2 files changed

+45
-38
lines changed

2 files changed

+45
-38
lines changed

roundup/rest.py

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
and/or modify under the same terms as Python.
66
"""
77

8-
import urlparse
8+
from __future__ import print_function
9+
10+
try:
11+
from urllib.parse import urlparse
12+
except ImportError:
13+
from urlparse import urlparse
914
import os
1015
import json
1116
import pprint
@@ -20,29 +25,35 @@
2025
from roundup.exceptions import *
2126
from roundup.cgi.exceptions import *
2227

28+
# Py3 compatible basestring
29+
try:
30+
basestring
31+
except NameError:
32+
basestring = str
33+
unicode = str
2334

2435
def _data_decorator(func):
2536
"""Wrap the returned data into an object."""
2637
def format_object(self, *args, **kwargs):
2738
# get the data / error from function
2839
try:
2940
code, data = func(self, *args, **kwargs)
30-
except NotFound, msg:
41+
except NotFound as msg:
3142
code = 404
3243
data = msg
33-
except IndexError, msg:
44+
except IndexError as msg:
3445
code = 404
3546
data = msg
36-
except Unauthorised, msg:
47+
except Unauthorised as msg:
3748
code = 403
3849
data = msg
39-
except UsageError, msg:
50+
except UsageError as msg:
4051
code = 400
4152
data = msg
42-
except (AttributeError, Reject), msg:
53+
except (AttributeError, Reject) as msg:
4354
code = 405
4455
data = msg
45-
except ValueError, msg:
56+
except ValueError as msg:
4657
code = 409
4758
data = msg
4859
except NotImplementedError:
@@ -52,13 +63,13 @@ def format_object(self, *args, **kwargs):
5263
exc, val, tb = sys.exc_info()
5364
code = 400
5465
ts = time.ctime()
55-
if self.client.request.DEBUG_MODE:
66+
if getattr (self.client.request, 'DEBUG_MODE', None):
5667
data = val
5768
else:
5869
data = '%s: An error occurred. Please check the server log' \
5970
' for more information.' % ts
6071
# out to the logfile
61-
print 'EXCEPTION AT', ts
72+
print ('EXCEPTION AT', ts)
6273
traceback.print_exc()
6374

6475
# decorate it
@@ -284,19 +295,15 @@ def prop_from_arg(self, cl, key, value, itemid=None):
284295
prop = None
285296
if isinstance(key, unicode):
286297
try:
287-
key = key.encode('ascii')
298+
x = key.encode('ascii')
288299
except UnicodeEncodeError:
289300
raise UsageError(
290301
'argument %r is no valid ascii keyword' % key
291302
)
292-
if isinstance(value, unicode):
293-
value = value.encode('utf-8')
294303
if value:
295304
try:
296-
prop = hyperdb.rawToHyperdb(
297-
self.db, cl, itemid, key, value
298-
)
299-
except hyperdb.HyperdbValueError, msg:
305+
prop = hyperdb.rawToHyperdb(self.db, cl, itemid, key, value)
306+
except hyperdb.HyperdbValueError as msg:
300307
raise UsageError(msg)
301308

302309
return prop
@@ -478,9 +485,7 @@ def get_element(self, class_name, item_id, input):
478485
props = value.split(",")
479486

480487
if props is None:
481-
props = class_obj.properties.keys()
482-
483-
props.sort() # sort properties
488+
props = list(sorted(class_obj.properties.keys()))
484489

485490
try:
486491
result = [
@@ -490,7 +495,7 @@ def get_element(self, class_name, item_id, input):
490495
'View', self.db.getuid(), class_name, prop_name,
491496
)
492497
]
493-
except KeyError, msg:
498+
except KeyError as msg:
494499
raise UsageError("%s field not valid" % msg)
495500
result = {
496501
'id': item_id,
@@ -592,9 +597,9 @@ def post_collection(self, class_name, input):
592597
try:
593598
item_id = class_obj.create(**props)
594599
self.db.commit()
595-
except (TypeError, IndexError, ValueError), message:
600+
except (TypeError, IndexError, ValueError) as message:
596601
raise ValueError(message)
597-
except KeyError, msg:
602+
except KeyError as msg:
598603
raise UsageError("Must provide the %s property." % msg)
599604

600605
# set the header Location
@@ -634,7 +639,7 @@ def put_element(self, class_name, item_id, input):
634639
class_obj = self.db.getclass(class_name)
635640

636641
props = self.props_from_args(class_obj, input.value, item_id)
637-
for p in props.iterkeys():
642+
for p in props:
638643
if not self.db.security.hasPermission(
639644
'Edit', self.db.getuid(), class_name, p, item_id
640645
):
@@ -645,7 +650,7 @@ def put_element(self, class_name, item_id, input):
645650
try:
646651
result = class_obj.set(item_id, **props)
647652
self.db.commit()
648-
except (TypeError, IndexError, ValueError), message:
653+
except (TypeError, IndexError, ValueError) as message:
649654
raise ValueError(message)
650655

651656
result = {
@@ -696,7 +701,7 @@ def put_attribute(self, class_name, item_id, attr_name, input):
696701
try:
697702
result = class_obj.set(item_id, **props)
698703
self.db.commit()
699-
except (TypeError, IndexError, ValueError), message:
704+
except (TypeError, IndexError, ValueError) as message:
700705
raise ValueError(message)
701706

702707
result = {
@@ -820,7 +825,7 @@ def delete_attribute(self, class_name, item_id, attr_name, input):
820825
try:
821826
class_obj.set(item_id, **props)
822827
self.db.commit()
823-
except (TypeError, IndexError, ValueError), message:
828+
except (TypeError, IndexError, ValueError) as message:
824829
raise ValueError(message)
825830

826831
result = {
@@ -893,7 +898,7 @@ def patch_element(self, class_name, item_id, input):
893898
# else patch operation is processing data
894899
props = self.props_from_args(class_obj, input.value, item_id)
895900

896-
for prop, value in props.iteritems():
901+
for prop in props:
897902
if not self.db.security.hasPermission(
898903
'Edit', self.db.getuid(), class_name, prop, item_id
899904
):
@@ -909,7 +914,7 @@ def patch_element(self, class_name, item_id, input):
909914
try:
910915
result = class_obj.set(item_id, **props)
911916
self.db.commit()
912-
except (TypeError, IndexError, ValueError), message:
917+
except (TypeError, IndexError, ValueError) as message:
913918
raise ValueError(message)
914919

915920
result = {
@@ -975,7 +980,7 @@ def patch_attribute(self, class_name, item_id, attr_name, input):
975980
try:
976981
result = class_obj.set(item_id, **props)
977982
self.db.commit()
978-
except (TypeError, IndexError, ValueError), message:
983+
except (TypeError, IndexError, ValueError) as message:
979984
raise ValueError(message)
980985

981986
result = {
@@ -1113,7 +1118,7 @@ def dispatch(self, method, uri, input):
11131118
# priority : extension from uri (/rest/issue.json),
11141119
# header (Accept: application/json, application/xml)
11151120
# default (application/json)
1116-
ext_type = os.path.splitext(urlparse.urlparse(uri).path)[1][1:]
1121+
ext_type = os.path.splitext(urlparse(uri).path)[1][1:]
11171122
data_type = ext_type or accept_type or self.__default_accept_type
11181123

11191124
# check for pretty print
@@ -1140,9 +1145,9 @@ def dispatch(self, method, uri, input):
11401145
# Call the appropriate method
11411146
try:
11421147
output = Routing.execute(self, uri, method, input)
1143-
except NotFound, msg:
1148+
except NotFound as msg:
11441149
output = self.error_obj(404, msg)
1145-
except Reject, msg:
1150+
except Reject as msg:
11461151
output = self.error_obj(405, msg)
11471152

11481153
# Format the content type

test/rest_common.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from roundup.cgi import client
1111
import random
1212

13-
import db_test_base
13+
from .db_test_base import setupTracker
1414

1515
NEEDS_INSTANCE = 1
1616

@@ -22,7 +22,7 @@ class TestCase():
2222
def setUp(self):
2323
self.dirname = '_test_rest'
2424
# set up and open a tracker
25-
self.instance = db_test_base.setupTracker(self.dirname, self.backend)
25+
self.instance = setupTracker(self.dirname, self.backend)
2626

2727
# open the database
2828
self.db = self.instance.open('admin')
@@ -49,7 +49,9 @@ def setUp(self):
4949

5050
thisdir = os.path.dirname(__file__)
5151
vars = {}
52-
execfile(os.path.join(thisdir, "tx_Source_detector.py"), vars)
52+
with open(os.path.join(thisdir, "tx_Source_detector.py")) as f:
53+
code = compile(f.read(), "tx_Source_detector.py", "exec")
54+
exec(code, vars)
5355
vars['init'](self.db)
5456

5557
env = {
@@ -66,7 +68,7 @@ def tearDown(self):
6668
self.db.close()
6769
try:
6870
shutil.rmtree(self.dirname)
69-
except OSError, error:
71+
except OSError as error:
7072
if error.errno not in (errno.ENOENT, errno.ESRCH):
7173
raise
7274

@@ -341,7 +343,7 @@ def testAuthAllowedPut(self):
341343
]
342344
try:
343345
self.server.put_element('user', '2', form)
344-
except Unauthorised, err:
346+
except Unauthorised as err:
345347
self.fail('raised %s' % err)
346348
finally:
347349
self.db.setCurrentUser('joe')
@@ -357,7 +359,7 @@ def testAuthAllowedPost(self):
357359
]
358360
try:
359361
self.server.post_collection('user', form)
360-
except Unauthorised, err:
362+
except Unauthorised as err:
361363
self.fail('raised %s' % err)
362364
finally:
363365
self.db.setCurrentUser('joe')

0 commit comments

Comments
 (0)