Old style exceptions --> new style for Python 3

This commit is contained in:
cclauss
2019-01-17 15:42:05 +01:00
parent 9a5b2108b8
commit c0c935b685
24 changed files with 60 additions and 58 deletions

View File

@@ -442,7 +442,7 @@ class CourseModeViewTest(CatalogIntegrationMixin, UrlResetMixin, ModuleStoreTest
for mode in ["honor", "verified"]:
CourseModeFactory(mode_slug=mode, course_id=self.course.id)
self.course.enrollment_end = datetime(2015, 01, 01)
self.course.enrollment_end = datetime(2015, 1, 1)
modulestore().update_item(self.course, self.user.id)
url = reverse('course_modes_choose', args=[unicode(self.course.id)])

View File

@@ -9,8 +9,8 @@ from track.utils import DateTimeJSONEncoder
class TestDateTimeJSONEncoder(TestCase):
def test_datetime_encoding(self):
a_naive_datetime = datetime(2012, 05, 01, 07, 27, 10, 20000)
a_tz_datetime = datetime(2012, 05, 01, 07, 27, 10, 20000, tzinfo=UTC)
a_naive_datetime = datetime(2012, 5, 1, 7, 27, 10, 20000)
a_tz_datetime = datetime(2012, 5, 1, 7, 27, 10, 20000, tzinfo=UTC)
a_date = a_naive_datetime.date()
an_iso_datetime = '2012-05-01T07:27:10.020000+00:00'
an_iso_date = '2012-05-01'

View File

@@ -54,7 +54,7 @@ def parse_xreply(xreply):
"""
try:
xreply = json.loads(xreply)
except ValueError, err:
except ValueError as err:
log.error(err)
return (1, 'unexpected reply from server')
@@ -135,11 +135,11 @@ class XQueueInterface(object):
response = self.session.post(
url, data=data, files=files, timeout=(CONNECT_TIMEOUT, READ_TIMEOUT)
)
except requests.exceptions.ConnectionError, err:
except requests.exceptions.ConnectionError as err:
log.error(err)
return (1, 'cannot connect to server')
except requests.exceptions.ReadTimeout, err:
except requests.exceptions.ReadTimeout as err:
log.error(err)
return (1, 'failed to read from the server')

View File

@@ -26,7 +26,7 @@ def lc_choose(index, *args):
'''
try:
return args[int(index) - 1]
except Exception, err:
except Exception as err:
pass
if len(args):
return args[0]

View File

@@ -451,7 +451,7 @@ class formula(object):
try:
cmml = self.cmathml
xml = etree.fromstring(str(cmml))
except Exception, err:
except Exception as err:
if 'conversion from Presentation MathML to Content MathML was not successful' in cmml:
msg = "Illegal math expression"
else:
@@ -538,7 +538,7 @@ class formula(object):
args = [self.make_sympy(expr) for expr in xml[1:]]
try:
res = op(*args)
except Exception, err:
except Exception as err:
self.args = args # pylint: disable=attribute-defined-outside-init
self.op = op # pylint: disable=attribute-defined-outside-init, invalid-name
raise Exception('[formula] error=%s failed to apply %s to args=%s' % (err, opstr, args))

View File

@@ -46,7 +46,7 @@ def symmath_check_simple(expect, ans, adict={}, symtab=None, extra_options=None)
abcsym=options['__ABC__'],
symtab=symtab,
)
except Exception, err:
except Exception as err:
return {'ok': False,
'msg': 'Error %s<br/>Failed in evaluating check(%s,%s)' % (err, expect, ans)
}
@@ -92,22 +92,22 @@ def check(expect, given, numerical=False, matrix=False, normphase=False, abcsym=
try:
xgiven = my_sympify(given, normphase, matrix, do_qubit=do_qubit, abcsym=abcsym, symtab=symtab)
except Exception, err:
except Exception as err:
return {'ok': False, 'msg': 'Error %s<br/> in evaluating your expression "%s"' % (err, given)}
try:
xexpect = my_sympify(expect, normphase, matrix, do_qubit=do_qubit, abcsym=abcsym, symtab=symtab)
except Exception, err:
except Exception as err:
return {'ok': False, 'msg': 'Error %s<br/> in evaluating OUR expression "%s"' % (err, expect)}
if 'autonorm' in flags: # normalize trace of matrices
try:
xgiven /= xgiven.trace()
except Exception, err:
except Exception as err:
return {'ok': False, 'msg': 'Error %s<br/> in normalizing trace of your expression %s' % (err, to_latex(xgiven))}
try:
xexpect /= xexpect.trace()
except Exception, err:
except Exception as err:
return {'ok': False, 'msg': 'Error %s<br/> in normalizing trace of OUR expression %s' % (err, to_latex(xexpect))}
msg = 'Your expression was evaluated as ' + to_latex(xgiven)
@@ -208,7 +208,7 @@ def symmath_check(expect, ans, dynamath=None, options=None, debug=None, xml=None
# parse expected answer
try:
fexpect = my_sympify(str(expect), matrix=do_matrix, do_qubit=do_qubit)
except Exception, err:
except Exception as err:
msg += '<p>Error %s in parsing OUR expected answer "%s"</p>' % (err, expect)
return {'ok': False, 'msg': make_error_message(msg)}
@@ -216,7 +216,7 @@ def symmath_check(expect, ans, dynamath=None, options=None, debug=None, xml=None
# if expected answer is a number, try parsing provided answer as a number also
try:
fans = my_sympify(str(ans), matrix=do_matrix, do_qubit=do_qubit)
except Exception, err:
except Exception as err:
fans = None
# do a numerical comparison if both expected and answer are numbers
@@ -243,7 +243,7 @@ def symmath_check(expect, ans, dynamath=None, options=None, debug=None, xml=None
# convert mathml answer to formula
try:
mmlans = dynamath[0] if dynamath else None
except Exception, err:
except Exception as err:
mmlans = None
if not mmlans:
return {'ok': False, 'msg': '[symmath_check] failed to get MathML for input; dynamath=%s' % dynamath}
@@ -255,7 +255,7 @@ def symmath_check(expect, ans, dynamath=None, options=None, debug=None, xml=None
try:
fsym = f.sympy
msg += '<p>You entered: %s</p>' % to_latex(f.sympy)
except Exception, err:
except Exception as err:
log.exception("Error evaluating expression '%s' as a valid equation", ans)
msg += "<p>Error in evaluating your expression '%s' as a valid equation</p>" % (ans)
if "Illegal math" in str(err):
@@ -298,7 +298,7 @@ def symmath_check(expect, ans, dynamath=None, options=None, debug=None, xml=None
except sympy.ShapeError:
msg += "<p>Error - your input vector or matrix has the wrong dimensions"
return {'ok': False, 'msg': make_error_message(msg)}
except Exception, err:
except Exception as err:
msg += "<p>Error %s in comparing expected (a list) and your answer</p>" % str(err).replace('<', '&lt;')
if DEBUG:
msg += "<p/><pre>%s</pre>" % traceback.format_exc()
@@ -309,7 +309,7 @@ def symmath_check(expect, ans, dynamath=None, options=None, debug=None, xml=None
#fexpect = fexpect.simplify()
try:
diff = (fexpect - fsym)
except Exception, err:
except Exception as err:
diff = None
if DEBUG:

View File

@@ -455,7 +455,7 @@ class ContentStore(object):
self.save(thumbnail_content)
except Exception, exc: # pylint: disable=broad-except
except Exception as exc: # pylint: disable=broad-except
# log and continue as thumbnails are generally considered as optional
logging.exception(
u"Failed to generate thumbnail for {0}. Exception: {1}".format(content.location, str(exc))

View File

@@ -196,7 +196,7 @@ class ToyCourseFactory(SampleCourseFactory):
'graded': True,
'discussion_topics': {"General": {"id": "i4x-edX-toy-course-2012_Fall"}},
'graceperiod': datetime.timedelta(days=2, seconds=21599),
'start': datetime.datetime(2015, 07, 17, 12, tzinfo=pytz.utc),
'start': datetime.datetime(2015, 7, 17, 12, tzinfo=pytz.utc),
'xml_attributes': {"filename": ["course/2012_Fall.xml", "course/2012_Fall.xml"]},
'pdf_textbooks': [
{