From 0a68965b7f5880aeb8642a081ff5ebd86a0d1c4e Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Tue, 27 Aug 2019 12:10:52 +0000 Subject: [PATCH 23/26] py3: Fix magic methods Python 3 uses '__bool__' and '__next__', where Python 2 used '__nonzero__' and 'next'. Use the new names but add aliases. Based on patch by Stephen Finucane git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@8371 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 Signed-off-by: Doan Tran Cong Danh --- docutils/nodes.py | 8 +-- docutils/utils/math/math2html.py | 93 +++++++++++++++++++++++++++----- docutils/writers/manpage.py | 6 ++- 3 files changed, 89 insertions(+), 18 deletions(-) diff --git a/docutils/nodes.py b/docutils/nodes.py index dd9c4b6..6fffa56 100644 --- a/docutils/nodes.py +++ b/docutils/nodes.py @@ -53,7 +53,7 @@ class Node(object): line = None """The line number (1-based) of the beginning of this Node in `source`.""" - def __nonzero__(self): + def __bool__(self): """ Node instances are always true, even if they're empty. A node is more than a simple container. Its boolean "truth" does not depend on @@ -64,6 +64,9 @@ class Node(object): """ return True + if sys.version_info < (3, 0): + __nonzero__ = __bool__ + if sys.version_info < (3, 0): # on 2.x, str(node) will be a byte string with Unicode # characters > 255 escaped; on 3.x this is no longer necessary @@ -544,8 +547,7 @@ class Element(Node): else: return self.emptytag() - if sys.version_info > (3, 0): - # 2to3 doesn't convert __unicode__ to __str__ + if sys.version_info >= (3, 0): __str__ = __unicode__ def starttag(self, quoteattr=None): diff --git a/docutils/utils/math/math2html.py b/docutils/utils/math/math2html.py index 757dec0..a7e2aed 100644 --- a/docutils/utils/math/math2html.py +++ b/docutils/utils/math/math2html.py @@ -1313,6 +1313,9 @@ class BranchOptions(object): "String representation" return 'options for ' + self.name + ': ' + unicode(self.options) + if sys.version_info >= (3, 0): + __str__ = __unicode__ + class Cloner(object): "An object used to clone other objects." @@ -1453,6 +1456,10 @@ class Parser(object): "Return a description" return self.__class__.__name__ + ' (' + unicode(self.begin) + ')' + if sys.version_info >= (3, 0): + __str__ = __unicode__ + + class LoneCommand(Parser): "A parser for just one command line" @@ -1986,6 +1993,10 @@ class EndingList(object): string = string[:-1] return string + ']' + if sys.version_info >= (3, 0): + __str__ = __unicode__ + + class PositionEnding(object): "An ending for a parsing position" @@ -2004,6 +2015,8 @@ class PositionEnding(object): string += ' (optional)' return string + if sys.version_info >= (3, 0): + __str__ = __unicode__ class Position(Globable): @@ -2046,11 +2059,14 @@ class Position(Globable): self.skip(current) return current - def next(self): + def __next__(self): "Advance the position and return the next character." self.skipcurrent() return self.current() + if sys.version_info < (3, 0): + next = __next__ + def checkskip(self, string): "Check for a string at the given position; if there, skip it" if not self.checkfor(string): @@ -2312,6 +2328,10 @@ class Container(object): return self.__class__.__name__ return self.__class__.__name__ + '@' + unicode(self.begin) + if sys.version_info >= (3, 0): + __str__ = __unicode__ + + class BlackBox(Container): "A container that does not output anything" @@ -2370,7 +2390,7 @@ class StringContainer(Container): def extracttext(self): "Return all text." return self.string - + def __unicode__(self): "Return a printable representation." result = 'StringContainer' @@ -2381,6 +2401,10 @@ class StringContainer(Container): ellipsis = '' return result + ' (' + self.string.strip()[:15] + ellipsis + ')' + if sys.version_info >= (3, 0): + __str__ = __unicode__ + + class Constant(StringContainer): "A constant string" @@ -2392,6 +2416,10 @@ class Constant(StringContainer): def __unicode__(self): return 'Constant: ' + self.string + if sys.version_info >= (3, 0): + __str__ = __unicode__ + + class TaggedText(Container): "Text inside a tag" @@ -2421,9 +2449,8 @@ class TaggedText(Container): return 'Tagged ' return 'Tagged <' + self.output.tag + '>' - - - + if sys.version_info >= (3, 0): + __str__ = __unicode__ class DocumentParameters(object): @@ -2555,19 +2582,12 @@ class MacroParser(FormulaParser): "See if the formula is inlined" self.begin = reader.linenumber + 1 return ['inline'] - + def parse(self, reader): "Parse the formula until the end" formula = self.parsemultiliner(reader, self.parent.start, self.ending) reader.nextline() return formula - - - - - - - class FormulaBit(Container): @@ -2614,6 +2634,10 @@ class FormulaBit(Container): "Get a string representation" return self.__class__.__name__ + ' read in ' + self.original + if sys.version_info >= (3, 0): + __str__ = __unicode__ + + class TaggedBit(FormulaBit): "A tagged string in a formula" @@ -2656,6 +2680,10 @@ class FormulaConstant(Constant): "Return a printable representation." return 'Formula constant: ' + self.string + if sys.version_info >= (3, 0): + __str__ = __unicode__ + + class RawText(FormulaBit): "A bit of text inside a formula" @@ -2739,6 +2767,10 @@ class WhiteSpace(FormulaBit): "Return a printable representation." return 'Whitespace: *' + self.original + '*' + if sys.version_info >= (3, 0): + __str__ = __unicode__ + + class Bracket(FormulaBit): "A {} bracket inside a formula" @@ -2822,7 +2854,6 @@ class SquareBracket(Bracket): return bracket - class MathsProcessor(object): "A processor for a maths construction inside the FormulaProcessor." @@ -2834,6 +2865,10 @@ class MathsProcessor(object): "Return a printable description." return 'Maths processor ' + self.__class__.__name__ + if sys.version_info >= (3, 0): + __str__ = __unicode__ + + class FormulaProcessor(object): "A processor specifically for formulas." @@ -2997,6 +3032,10 @@ class Formula(Container): return 'Formula (' + self.partkey.number + ')' return 'Unnumbered formula' + if sys.version_info >= (3, 0): + __str__ = __unicode__ + + class WholeFormula(FormulaBit): "Parse a whole formula" @@ -3229,6 +3268,10 @@ class NumberCounter(object): result += ' in mode ' + self.mode return result + if sys.version_info >= (3, 0): + __str__ = __unicode__ + + class DependentCounter(NumberCounter): "A counter which depends on another one (the master)." @@ -3780,6 +3823,10 @@ class Link(Container): result += ' to ' + self.url return result + if sys.version_info >= (3, 0): + __str__ = __unicode__ + + class URL(Link): "A clickable URL" @@ -3948,6 +3995,10 @@ class Label(Link): return 'Unnamed label' return 'Label ' + self.key + if sys.version_info >= (3, 0): + __str__ = __unicode__ + + class Reference(Link): "A reference to a label." @@ -4008,6 +4059,8 @@ class Reference(Link): "Return a printable representation." return 'Reference ' + self.key + if sys.version_info >= (3, 0): + __str__ = __unicode__ class FormulaCommand(FormulaBit): @@ -4630,6 +4683,10 @@ class LimitPreviousCommand(LimitCommand): "Return a printable representation." return 'Limit previous command' + if sys.version_info >= (3, 0): + __str__ = __unicode__ + + class LimitsProcessor(MathsProcessor): "A processor for limits inside an element." @@ -4854,6 +4911,10 @@ class ParameterDefinition(object): result += ' (empty)' return result + if sys.version_info >= (3, 0): + __str__ = __unicode__ + + class ParameterFunction(CommandBit): "A function with a variable number of parameters defined in a template." "The parameters are defined as a parameter definition." @@ -5306,6 +5367,10 @@ class FormulaMacro(Formula): "Return a printable representation." return 'Math macro' + if sys.version_info >= (3, 0): + __str__ = __unicode__ + + FormulaFactory.types += [ MacroParameter ] FormulaCommand.types += [ diff --git a/docutils/writers/manpage.py b/docutils/writers/manpage.py index df4f1a3..9809dd4 100644 --- a/docutils/writers/manpage.py +++ b/docutils/writers/manpage.py @@ -331,7 +331,7 @@ class Translator(nodes.NodeVisitor): elif style.endswith('roman'): self._indent = 5 - def next(self): + def __next__(self): if self._style == 'bullet': return self.enum_style[self._style] elif self._style == 'emdash': @@ -349,6 +349,10 @@ class Translator(nodes.NodeVisitor): return res.lower() else: return "%d." % self._cnt + + if sys.version_info < (3, 0): + next = __next__ + def get_width(self): return self._indent def __repr__(self): -- 2.24.0.375.geb5ae68d41