From efe0c9a75c5b2a8d094840927e93e0e873e77ece Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Tue, 19 Nov 2019 23:52:58 +0700 Subject: [PATCH 06/26] py3: Replace deprecated form of raising exception Signed-off-by: Stephen Finucane git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@8353 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 Signed-off-by: Doan Tran Cong Danh --- docutils/frontend.py | 25 ++++++++++--------------- docutils/nodes.py | 12 ++++++------ docutils/transforms/frontmatter.py | 4 ++-- docutils/utils/roman.py | 9 +++++---- docutils/writers/manpage.py | 4 ++-- test/DocutilsTestSupport.py | 21 ++++----------------- test/package_unittest.py | 2 +- 7 files changed, 30 insertions(+), 47 deletions(-) diff --git a/docutils/frontend.py b/docutils/frontend.py index ebdbd6a..4b389b0 100644 --- a/docutils/frontend.py +++ b/docutils/frontend.py @@ -74,9 +74,8 @@ def validate_encoding(setting, value, option_parser, try: codecs.lookup(value) except LookupError: - raise (LookupError('setting "%s": unknown encoding: "%s"' - % (setting, value)), - None, sys.exc_info()[2]) + raise LookupError('setting "%s": unknown encoding: "%s"' + % (setting, value)) return value def validate_encoding_error_handler(setting, value, option_parser, @@ -84,12 +83,11 @@ def validate_encoding_error_handler(setting, value, option_parser, try: codecs.lookup_error(value) except LookupError: - raise (LookupError( + raise LookupError( 'unknown encoding error handler: "%s" (choices: ' '"strict", "ignore", "replace", "backslashreplace", ' '"xmlcharrefreplace", and possibly others; see documentation for ' - 'the Python ``codecs`` module)' % value), - None, sys.exc_info()[2]) + 'the Python ``codecs`` module)' % value) return value def validate_encoding_and_error_handler( @@ -125,8 +123,7 @@ def validate_boolean(setting, value, option_parser, try: return option_parser.booleans[value.strip().lower()] except KeyError: - raise (LookupError('unknown boolean value: "%s"' % value), - None, sys.exc_info()[2]) + raise LookupError('unknown boolean value: "%s"' % value) def validate_ternary(setting, value, option_parser, config_parser=None, config_section=None): @@ -157,8 +154,7 @@ def validate_threshold(setting, value, option_parser, try: return option_parser.thresholds[value.lower()] except (KeyError, AttributeError): - raise (LookupError('unknown threshold: %r.' % value), - None, sys.exc_info[2]) + raise LookupError('unknown threshold: %r.' % value) def validate_colon_separated_string_list( setting, value, option_parser, config_parser=None, config_section=None): @@ -350,10 +346,9 @@ class Option(optparse.Option): try: new_value = self.validator(setting, value, parser) except Exception as error: - raise (optparse.OptionValueError( + raise optparse.OptionValueError( 'Error in option "%s":\n %s' - % (opt, ErrorString(error))), - None, sys.exc_info()[2]) + % (opt, ErrorString(error))) setattr(values, setting, new_value) if self.overrides: setattr(values, self.overrides, None) @@ -830,12 +825,12 @@ Skipping "%s" configuration file. setting, value, option_parser, config_parser=self, config_section=section) except Exception as error: - raise (ValueError( + raise ValueError( 'Error in config file "%s", section "[%s]":\n' ' %s\n' ' %s = %s' % (filename, section, ErrorString(error), - setting, value)), None, sys.exc_info()[2]) + setting, value)) self.set(section, setting, new_value) if option.overrides: self.set(section, option.overrides, None) diff --git a/docutils/nodes.py b/docutils/nodes.py index fa02c6e..71189ab 100644 --- a/docutils/nodes.py +++ b/docutils/nodes.py @@ -594,8 +594,8 @@ class Element(Node): assert key.step in (None, 1), 'cannot handle slice with stride' return self.children[key.start:key.stop] else: - raise TypeError, ('element index must be an integer, a slice, or ' - 'an attribute name string') + raise TypeError('element index must be an integer, a slice, or ' + 'an attribute name string') def __setitem__(self, key, item): if isinstance(key, basestring): @@ -609,8 +609,8 @@ class Element(Node): self.setup_child(node) self.children[key.start:key.stop] = item else: - raise TypeError, ('element index must be an integer, a slice, or ' - 'an attribute name string') + raise TypeError('element index must be an integer, a slice, or ' + 'an attribute name string') def __delitem__(self, key): if isinstance(key, basestring): @@ -621,8 +621,8 @@ class Element(Node): assert key.step in (None, 1), 'cannot handle slice with stride' del self.children[key.start:key.stop] else: - raise TypeError, ('element index must be an integer, a simple ' - 'slice, or an attribute name string') + raise TypeError('element index must be an integer, a simple ' + 'slice, or an attribute name string') def __add__(self, other): return self.children + other diff --git a/docutils/transforms/frontmatter.py b/docutils/transforms/frontmatter.py index 1279f50..23b9c95 100644 --- a/docutils/transforms/frontmatter.py +++ b/docutils/transforms/frontmatter.py @@ -57,7 +57,7 @@ class TitlePromoter(Transform): """ # Type check if not isinstance(node, nodes.Element): - raise TypeError, 'node must be of Element-derived type.' + raise TypeError('node must be of Element-derived type.') # `node` must not have a title yet. assert not (len(node) and isinstance(node[0], nodes.title)) @@ -100,7 +100,7 @@ class TitlePromoter(Transform): """ # Type check if not isinstance(node, nodes.Element): - raise TypeError, 'node must be of Element-derived type.' + raise TypeError('node must be of Element-derived type.') subsection, index = self.candidate_index(node) if index is None: diff --git a/docutils/utils/roman.py b/docutils/utils/roman.py index 0335f29..fc4680d 100644 --- a/docutils/utils/roman.py +++ b/docutils/utils/roman.py @@ -40,9 +40,9 @@ romanNumeralMap = (('M', 1000), def toRoman(n): """convert integer to Roman numeral""" if not (0 < n < 5000): - raise OutOfRangeError, "number out of range (must be 1..4999)" + raise OutOfRangeError("number out of range (must be 1..4999)") if int(n) != n: - raise NotIntegerError, "decimals can not be converted" + raise NotIntegerError("decimals can not be converted") result = "" for numeral, integer in romanNumeralMap: @@ -67,9 +67,10 @@ romanNumeralPattern = re.compile(""" def fromRoman(s): """convert Roman numeral to integer""" if not s: - raise InvalidRomanNumeralError, 'Input can not be blank' + raise InvalidRomanNumeralError('Input can not be blank') + if not romanNumeralPattern.search(s): - raise InvalidRomanNumeralError, 'Invalid Roman numeral: %s' % s + raise InvalidRomanNumeralError('Invalid Roman numeral: %s' % s) result = 0 index = 0 diff --git a/docutils/writers/manpage.py b/docutils/writers/manpage.py index 9c887c7..cbb8648 100644 --- a/docutils/writers/manpage.py +++ b/docutils/writers/manpage.py @@ -715,7 +715,7 @@ class Translator(nodes.NodeVisitor): pass def visit_header(self, node): - raise NotImplementedError, node.astext() + raise NotImplementedError(node.astext()) def depart_header(self, node): pass @@ -855,7 +855,7 @@ class Translator(nodes.NodeVisitor): self.depart_literal_block(node) def visit_meta(self, node): - raise NotImplementedError, node.astext() + raise NotImplementedError(node.astext()) def depart_meta(self, node): pass diff --git a/test/DocutilsTestSupport.py b/test/DocutilsTestSupport.py index c6f5b9f..a1fec25 100644 --- a/test/DocutilsTestSupport.py +++ b/test/DocutilsTestSupport.py @@ -123,30 +123,17 @@ class StandardTestCase(unittest.TestCase): operator. """ if not first == second: - raise self.failureException, ( - msg or '%s != %s' % _format_str(first, second)) + raise self.failureException( + msg or '%s != %s' % _format_str(first, second)) def assertNotEqual(self, first, second, msg=None): """Fail if the two objects are equal as determined by the '==' operator. """ if first == second: - raise self.failureException, ( - msg or '%s == %s' % _format_str(first, second)) + raise self.failureException( + msg or '%s == %s' % _format_str(first, second)) - # assertIn and assertNotIn: new in Python 2.7: - if sys.version_info < (2,7): - - def assertIn(self, a, b, msg=None): - if a not in b: - raise self.failureException, ( - msg or '%s not in %s' % _format_str(a, b)) - - def assertNotIn(self, a, b, msg=None): - if a in b: - raise self.failureException, ( - msg or '%s in %s' % _format_str(a, b)) - # aliases for assertion methods, deprecated since Python 2.7 failUnlessEqual = assertEquals = assertEqual diff --git a/test/package_unittest.py b/test/package_unittest.py index 16b00dc..4db826b 100644 --- a/test/package_unittest.py +++ b/test/package_unittest.py @@ -120,7 +120,7 @@ def loadTestModules(path, name='', packages=None): elif isinstance(suite, unittest.TestSuite): testSuite.addTest(suite) else: - raise AssertionError, "don't understand suite (%s)" % mod + raise AssertionError("don't understand suite (%s)" % mod) sys.path.pop(0) return testSuite -- 2.24.0.375.geb5ae68d41