From c0c935b68586f2d4e024352a54b1463482fdbf0b Mon Sep 17 00:00:00 2001 From: cclauss Date: Thu, 17 Jan 2019 15:42:05 +0100 Subject: [PATCH] Old style exceptions --> new style for Python 3 --- cms/djangoapps/contentstore/views/tabs.py | 2 +- .../course_modes/tests/test_views.py | 2 +- common/djangoapps/track/tests/test_util.py | 4 ++-- common/lib/capa/capa/xqueue_interface.py | 6 ++--- .../sandbox-packages/loncapa/loncapa_check.py | 2 +- common/lib/symmath/symmath/formula.py | 4 ++-- common/lib/symmath/symmath/symmath_check.py | 22 +++++++++---------- .../xmodule/xmodule/contentstore/content.py | 2 +- .../xmodule/modulestore/tests/factories.py | 2 +- lms/djangoapps/commerce/api/v0/views.py | 2 +- .../commands/tests/test_dump_course.py | 2 +- lms/djangoapps/courseware/module_render.py | 2 +- lms/djangoapps/discussion_api/tests/utils.py | 2 +- .../django_comment_client/base/views.py | 4 ++-- lms/djangoapps/edxnotes/tests.py | 8 +++---- lms/djangoapps/instructor/views/tools.py | 2 +- .../mobile_api/tests/test_middleware.py | 8 +++---- lms/djangoapps/mobile_api/tests/test_model.py | 10 ++++----- lms/djangoapps/shoppingcart/pdf.py | 2 +- .../external_auth/tests/test_ssl.py | 20 +++++++++-------- .../profile_images/tests/test_views.py | 4 ++-- .../accounts/tests/test_image_helpers.py | 2 +- pavelib/prereqs.py | 2 +- pavelib/quality.py | 2 +- 24 files changed, 60 insertions(+), 58 deletions(-) diff --git a/cms/djangoapps/contentstore/views/tabs.py b/cms/djangoapps/contentstore/views/tabs.py index ed75d0cdf9..edfb438165 100644 --- a/cms/djangoapps/contentstore/views/tabs.py +++ b/cms/djangoapps/contentstore/views/tabs.py @@ -109,7 +109,7 @@ def reorder_tabs_handler(course_item, request): # validate the tabs to make sure everything is Ok (e.g., did the client try to reorder unmovable tabs?) try: CourseTabList.validate_tabs(new_tab_list) - except InvalidTabsException, exception: + except InvalidTabsException as exception: return JsonResponse( {"error": u"New list of tabs is not valid: {0}.".format(str(exception))}, status=400 ) diff --git a/common/djangoapps/course_modes/tests/test_views.py b/common/djangoapps/course_modes/tests/test_views.py index 2ac25b4b32..fed9623f8f 100644 --- a/common/djangoapps/course_modes/tests/test_views.py +++ b/common/djangoapps/course_modes/tests/test_views.py @@ -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)]) diff --git a/common/djangoapps/track/tests/test_util.py b/common/djangoapps/track/tests/test_util.py index 865c1a4d06..bcbc6efc34 100644 --- a/common/djangoapps/track/tests/test_util.py +++ b/common/djangoapps/track/tests/test_util.py @@ -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' diff --git a/common/lib/capa/capa/xqueue_interface.py b/common/lib/capa/capa/xqueue_interface.py index b2eba0965e..7e34fa6791 100644 --- a/common/lib/capa/capa/xqueue_interface.py +++ b/common/lib/capa/capa/xqueue_interface.py @@ -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') diff --git a/common/lib/sandbox-packages/loncapa/loncapa_check.py b/common/lib/sandbox-packages/loncapa/loncapa_check.py index d7d3ad44ea..7bde74e3eb 100644 --- a/common/lib/sandbox-packages/loncapa/loncapa_check.py +++ b/common/lib/sandbox-packages/loncapa/loncapa_check.py @@ -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] diff --git a/common/lib/symmath/symmath/formula.py b/common/lib/symmath/symmath/formula.py index 45d92a7190..7ee7a03d31 100644 --- a/common/lib/symmath/symmath/formula.py +++ b/common/lib/symmath/symmath/formula.py @@ -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)) diff --git a/common/lib/symmath/symmath/symmath_check.py b/common/lib/symmath/symmath/symmath_check.py index 85afc3d879..5e83a70300 100644 --- a/common/lib/symmath/symmath/symmath_check.py +++ b/common/lib/symmath/symmath/symmath_check.py @@ -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
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
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
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
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
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 += '

Error %s in parsing OUR expected answer "%s"

' % (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 += '

You entered: %s

' % to_latex(f.sympy) - except Exception, err: + except Exception as err: log.exception("Error evaluating expression '%s' as a valid equation", ans) msg += "

Error in evaluating your expression '%s' as a valid equation

" % (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 += "

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 += "

Error %s in comparing expected (a list) and your answer

" % str(err).replace('<', '<') if DEBUG: msg += "

%s
" % 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: diff --git a/common/lib/xmodule/xmodule/contentstore/content.py b/common/lib/xmodule/xmodule/contentstore/content.py index 69deac9626..6e34ed41a3 100644 --- a/common/lib/xmodule/xmodule/contentstore/content.py +++ b/common/lib/xmodule/xmodule/contentstore/content.py @@ -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)) diff --git a/common/lib/xmodule/xmodule/modulestore/tests/factories.py b/common/lib/xmodule/xmodule/modulestore/tests/factories.py index 0e757e575e..e68f79c542 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/factories.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/factories.py @@ -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': [ { diff --git a/lms/djangoapps/commerce/api/v0/views.py b/lms/djangoapps/commerce/api/v0/views.py index ed5565240b..4dc28a2491 100644 --- a/lms/djangoapps/commerce/api/v0/views.py +++ b/lms/djangoapps/commerce/api/v0/views.py @@ -56,7 +56,7 @@ class BasketsView(APIView): try: course_key = CourseKey.from_string(course_id) courses.get_course(course_key) - except (InvalidKeyError, ValueError)as ex: + except (InvalidKeyError, ValueError) as ex: log.exception(u'Unable to locate course matching %s.', course_id) return False, None, text_type(ex) diff --git a/lms/djangoapps/courseware/management/commands/tests/test_dump_course.py b/lms/djangoapps/courseware/management/commands/tests/test_dump_course.py index 182368b948..42c44e0b9b 100644 --- a/lms/djangoapps/courseware/management/commands/tests/test_dump_course.py +++ b/lms/djangoapps/courseware/management/commands/tests/test_dump_course.py @@ -97,7 +97,7 @@ class CommandsTestBase(SharedModuleStoreTestCase): try: output = self.call_command('dump_course_structure', *args, **kwargs) - except TypeError, exception: + except TypeError as exception: self.fail(exception) dump = json.loads(output) diff --git a/lms/djangoapps/courseware/module_render.py b/lms/djangoapps/courseware/module_render.py index 111221f21d..83e0e3118b 100644 --- a/lms/djangoapps/courseware/module_render.py +++ b/lms/djangoapps/courseware/module_render.py @@ -285,7 +285,7 @@ def _add_timed_exam_info(user, course, section, section_context): unicode(course.id), unicode(section.location) ) - except Exception, ex: # pylint: disable=broad-except + except Exception as ex: # pylint: disable=broad-except # safety net in case something blows up in edx_proctoring # as this is just informational descriptions, it is better # to log and continue (which is safe) than to have it be an diff --git a/lms/djangoapps/discussion_api/tests/utils.py b/lms/djangoapps/discussion_api/tests/utils.py index 6a0b43764f..39902d2669 100644 --- a/lms/djangoapps/discussion_api/tests/utils.py +++ b/lms/djangoapps/discussion_api/tests/utils.py @@ -489,7 +489,7 @@ class ProfileImageTestMixin(object): Mixin with utility methods for user profile image """ - TEST_PROFILE_IMAGE_UPLOADED_AT = datetime(2002, 1, 9, 15, 43, 01, tzinfo=UTC) + TEST_PROFILE_IMAGE_UPLOADED_AT = datetime(2002, 1, 9, 15, 43, 1, tzinfo=UTC) def create_profile_image(self, user, storage): """ diff --git a/lms/djangoapps/django_comment_client/base/views.py b/lms/djangoapps/django_comment_client/base/views.py index 58bc28b4b5..bcb26950b1 100644 --- a/lms/djangoapps/django_comment_client/base/views.py +++ b/lms/djangoapps/django_comment_client/base/views.py @@ -770,9 +770,9 @@ def upload(request, course_id): # ajax upload file to a question or answer max_file_size=cc_settings.MAX_UPLOAD_FILE_SIZE ) - except exceptions.PermissionDenied, err: + except exceptions.PermissionDenied as err: error = unicode(err) - except Exception, err: # pylint: disable=broad-except + except Exception as err: # pylint: disable=broad-except print(err) logging.critical(unicode(err)) error = _('Error uploading file. Please contact the site administrator. Thank you.') diff --git a/lms/djangoapps/edxnotes/tests.py b/lms/djangoapps/edxnotes/tests.py index 8b31d8aa06..d63770b28c 100644 --- a/lms/djangoapps/edxnotes/tests.py +++ b/lms/djangoapps/edxnotes/tests.py @@ -671,13 +671,13 @@ class EdxNotesHelpersTest(ModuleStoreTestCase): u"quote": u"quote text1", u"text": u"text1", u"usage_id": unicode(self.html_module_1.location), - u"updated": datetime(2016, 01, 26, 8, 5, 16, 00000).isoformat(), + u"updated": datetime(2016, 1, 26, 8, 5, 16, 00000).isoformat(), }, { u"quote": u"quote text2", u"text": u"text2", u"usage_id": unicode(self.html_module_2.location), - u"updated": datetime(2016, 01, 26, 9, 6, 17, 00000).isoformat(), + u"updated": datetime(2016, 1, 26, 9, 6, 17, 00000).isoformat(), }, ] @@ -695,7 +695,7 @@ class EdxNotesHelpersTest(ModuleStoreTestCase): u'text': u'text1', u'quote': u'quote text1', u'usage_id': unicode(self.html_module_1.location), - u'updated': datetime(2016, 01, 26, 8, 5, 16) + u'updated': datetime(2016, 1, 26, 8, 5, 16) }, { 'section': {}, @@ -708,7 +708,7 @@ class EdxNotesHelpersTest(ModuleStoreTestCase): u'text': u'text2', u'quote': u'quote text2', u'usage_id': unicode(self.html_module_2.location), - u'updated': datetime(2016, 01, 26, 9, 6, 17) + u'updated': datetime(2016, 1, 26, 9, 6, 17) } ], helpers.preprocess_collection(self.user, self.course, initial_collection) diff --git a/lms/djangoapps/instructor/views/tools.py b/lms/djangoapps/instructor/views/tools.py index 71f8e424c0..e9109ff4a6 100644 --- a/lms/djangoapps/instructor/views/tools.py +++ b/lms/djangoapps/instructor/views/tools.py @@ -48,7 +48,7 @@ def handle_dashboard_error(view): """ try: return view(request, course_id=course_id) - except DashboardError, error: + except DashboardError as error: return error.response() return wrapper diff --git a/lms/djangoapps/mobile_api/tests/test_middleware.py b/lms/djangoapps/mobile_api/tests/test_middleware.py index b2547f6fc0..0d67c889e0 100644 --- a/lms/djangoapps/mobile_api/tests/test_middleware.py +++ b/lms/djangoapps/mobile_api/tests/test_middleware.py @@ -33,13 +33,13 @@ class TestAppVersionUpgradeMiddleware(CacheIsolationTestCase): AppVersionConfig( platform="iOS", version="2.2.2", - expire_at=datetime(2014, 01, 01, tzinfo=UTC), + expire_at=datetime(2014, 1, 1, tzinfo=UTC), enabled=True ).save() AppVersionConfig( platform="iOS", version="4.4.4", - expire_at=datetime(9000, 01, 01, tzinfo=UTC), + expire_at=datetime(9000, 1, 1, tzinfo=UTC), enabled=True ).save() AppVersionConfig(platform="iOS", version="6.6.6", expire_at=None, enabled=True).save() @@ -48,13 +48,13 @@ class TestAppVersionUpgradeMiddleware(CacheIsolationTestCase): AppVersionConfig( platform="Android", version="2.2.2", - expire_at=datetime(2014, 01, 01, tzinfo=UTC), + expire_at=datetime(2014, 1, 1, tzinfo=UTC), enabled=True ).save() AppVersionConfig( platform="Android", version="4.4.4", - expire_at=datetime(5000, 01, 01, tzinfo=UTC), + expire_at=datetime(5000, 1, 1, tzinfo=UTC), enabled=True ).save() AppVersionConfig(platform="Android", version="8.8.8", expire_at=None, enabled=True).save() diff --git a/lms/djangoapps/mobile_api/tests/test_model.py b/lms/djangoapps/mobile_api/tests/test_model.py index e1bb94414a..f293f08809 100644 --- a/lms/djangoapps/mobile_api/tests/test_model.py +++ b/lms/djangoapps/mobile_api/tests/test_model.py @@ -22,19 +22,19 @@ class TestAppVersionConfigModel(TestCase): AppVersionConfig( platform="ios", version="2.2.2", - expire_at=datetime(2014, 01, 01, tzinfo=UTC), + expire_at=datetime(2014, 1, 1, tzinfo=UTC), enabled=True ).save() AppVersionConfig( platform="ios", version="4.1.1", - expire_at=datetime(5000, 01, 01, tzinfo=UTC), + expire_at=datetime(5000, 1, 1, tzinfo=UTC), enabled=False ).save() AppVersionConfig( platform="ios", version="4.4.4", - expire_at=datetime(9000, 01, 01, tzinfo=UTC), + expire_at=datetime(9000, 1, 1, tzinfo=UTC), enabled=True ).save() AppVersionConfig(platform="ios", version="6.6.6", expire_at=None, enabled=True).save() @@ -44,13 +44,13 @@ class TestAppVersionConfigModel(TestCase): AppVersionConfig( platform="android", version="2.2.2", - expire_at=datetime(2014, 01, 01, tzinfo=UTC), + expire_at=datetime(2014, 1, 1, tzinfo=UTC), enabled=True ).save() AppVersionConfig( platform="android", version="4.4.4", - expire_at=datetime(9000, 01, 01, tzinfo=UTC), + expire_at=datetime(9000, 1, 1, tzinfo=UTC), enabled=True ).save() AppVersionConfig(platform="android", version="8.8.8", expire_at=None, enabled=True).save() diff --git a/lms/djangoapps/shoppingcart/pdf.py b/lms/djangoapps/shoppingcart/pdf.py index eb2f5bc626..1fa4c3e26f 100644 --- a/lms/djangoapps/shoppingcart/pdf.py +++ b/lms/djangoapps/shoppingcart/pdf.py @@ -180,7 +180,7 @@ class PDFInvoice(object): """ try: img = Image.open(img_path) - except IOError, ex: + except IOError as ex: log.exception(u'Pdf unable to open the image file: %s', str(ex)) img = None diff --git a/openedx/core/djangoapps/external_auth/tests/test_ssl.py b/openedx/core/djangoapps/external_auth/tests/test_ssl.py index 1885d7ee4f..a91d4390ec 100644 --- a/openedx/core/djangoapps/external_auth/tests/test_ssl.py +++ b/openedx/core/djangoapps/external_auth/tests/test_ssl.py @@ -7,6 +7,8 @@ import copy from unittest import skip from mock import Mock, patch +from six import text_type + from django.conf import settings from django.contrib.auth import SESSION_KEY from django.contrib.auth.models import AnonymousUser, User @@ -97,7 +99,7 @@ class SSLClientTest(ModuleStoreTestCase): self.assertIn('