|
From: <mi...@us...> - 2022-01-04 23:34:02
|
Revision: 8931
http://sourceforge.net/p/docutils/code/8931
Author: milde
Date: 2022-01-04 23:33:59 +0000 (Tue, 04 Jan 2022)
Log Message:
-----------
Deprecate `nodes.reprunicode` and `nodes.ensure_str()`.
Drop uses of the deprecated constructs (not required with Python 3).
Modified Paths:
--------------
trunk/docutils/HISTORY.txt
trunk/docutils/RELEASE-NOTES.txt
trunk/docutils/docutils/nodes.py
trunk/docutils/docutils/parsers/rst/directives/misc.py
trunk/docutils/docutils/utils/__init__.py
trunk/docutils/test/test_nodes.py
trunk/docutils/test/test_parsers/test_rst/test_directives/test_images.py
trunk/docutils/test/test_utils.py
Modified: trunk/docutils/HISTORY.txt
===================================================================
--- trunk/docutils/HISTORY.txt 2022-01-04 22:53:36 UTC (rev 8930)
+++ trunk/docutils/HISTORY.txt 2022-01-04 23:33:59 UTC (rev 8931)
@@ -15,9 +15,10 @@
====================
* General
-
- - Dropped support for Python 2.7, 3.5, and 3.6.
+ - Dropped support for Python 2.7, 3.5, and 3.6. and removed compatibility
+ hacks from code and tests.
+
* docutils/parsers/__init__.py
- Alias for the "myst" parser (https://pypi.org/project/myst-docutils).
@@ -42,6 +43,10 @@
- Don't use mutable default values for function arguments. Fixes bug #430.
+* docutils/utils/__init__.py
+
+ - decode_path() returns `str` instance instead of `nodes.reprunicode`.
+
* test/DocutilsTestSupport.py
- exception_data() returns None if no exception was raised.
Modified: trunk/docutils/RELEASE-NOTES.txt
===================================================================
--- trunk/docutils/RELEASE-NOTES.txt 2022-01-04 22:53:36 UTC (rev 8930)
+++ trunk/docutils/RELEASE-NOTES.txt 2022-01-04 23:33:59 UTC (rev 8931)
@@ -54,6 +54,9 @@
* Remove the "rawsource" argument from nodes.Text.__init__()
(deprecated and ignored since Docutils 0.18) in Docutils 1.3.
+
+* Remove the compatibility hacks `nodes.reprunicode` and `nodes.ensure_str()`
+ in Docutils 1.2. They are not required with Python 3.x.
* Move math format conversion from docutils/utils/math (called from
docutils/writers/_html_base.py) to a transform__.
Modified: trunk/docutils/docutils/nodes.py
===================================================================
--- trunk/docutils/docutils/nodes.py 2022-01-04 22:53:36 UTC (rev 8930)
+++ trunk/docutils/docutils/nodes.py 2022-01-04 23:33:59 UTC (rev 8931)
@@ -77,15 +77,6 @@
"""
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
- def __str__(self):
- return unicode(self).encode('raw_unicode_escape')
-
def asdom(self, dom=None):
"""Return a DOM **fragment** representation of this Node."""
if dom is None:
@@ -346,24 +337,25 @@
except (AttributeError, IndexError):
return None
-if sys.version_info < (3, 0):
- class reprunicode(unicode):
- """
- A unicode sub-class that removes the initial u from unicode's repr.
- """
- def __repr__(self):
- return unicode.__repr__(self)[1:]
-else:
- reprunicode = unicode
+class reprunicode(str):
+ """
+ Deprecated backwards compatibility stub. Use the standard `str` instead.
+ """
+ def __init__(self, s):
+ warnings.warn('nodes.reprunicode() is not required with Python 3'
+ ' and will be removed in Docutils 1.2.',
+ DeprecationWarning, stacklevel=2)
+ super().__init__()
def ensure_str(s):
"""
- Failsafe conversion of `unicode` to `str`.
+ Deprecated backwards compatibility stub returning `s`.
"""
- if sys.version_info < (3, 0) and isinstance(s, unicode):
- return s.encode('ascii', 'backslashreplace')
+ warnings.warn('nodes.ensure_str() is not required with Python 3'
+ ' and will be removed in Docutils 1.2.',
+ DeprecationWarning, stacklevel=2)
return s
# definition moved here from `utils` to avoid circular import dependency
@@ -381,12 +373,13 @@
return text
-class Text(Node, reprunicode):
+class Text(Node, str):
"""
Instances are terminal nodes (leaves) containing text only; no child
nodes or attributes. Initialize by passing a string to the constructor.
- Access the text itself with the `astext` method.
+ Access the raw (null-escaped) text with ``str(<instance>)``
+ and unescaped text with the `astext` method.
"""
tagname = '#text'
@@ -394,15 +387,11 @@
children = ()
"""Text nodes have no children, and cannot have children."""
- if sys.version_info > (3, 0):
- def __new__(cls, data, rawsource=None):
- """Assert that `data` is not an array of bytes."""
- if isinstance(data, bytes):
- raise TypeError('expecting str data, not bytes')
- return reprunicode.__new__(cls, data)
- else:
- def __new__(cls, data, rawsource=None):
- return reprunicode.__new__(cls, data)
+ def __new__(cls, data, rawsource=None):
+ """Assert that `data` is not an array of bytes."""
+ if isinstance(data, bytes):
+ raise TypeError('expecting str data, not bytes')
+ return str.__new__(cls, data)
def __init__(self, data, rawsource=None):
"""The `rawsource` argument is ignored and deprecated."""
@@ -415,7 +404,7 @@
data = self
if len(data) > maxlen:
data = data[:maxlen-4] + ' ...'
- return '<%s: %r>' % (self.tagname, reprunicode(data))
+ return '<%s: %r>' % (self.tagname, str(data))
def __repr__(self):
return self.shortrepr(maxlen=68)
@@ -424,7 +413,7 @@
return domroot.createTextNode(unicode(self))
def astext(self):
- return reprunicode(unescape(self))
+ return str(unescape(self))
# Note about __unicode__: The implementation of __unicode__ here,
# and the one raising NotImplemented in the superclass Node had
@@ -436,7 +425,7 @@
# an infinite loop
def copy(self):
- return self.__class__(reprunicode(self))
+ return self.__class__(str(self))
def deepcopy(self):
return self.copy()
@@ -445,7 +434,7 @@
try:
if self.document.settings.detailed:
lines = ['%s%s' % (indent*level, '<#text>')
- ] + [indent*(level+1) + repr(reprunicode(line))
+ ] + [indent*(level+1) + repr(line)
for line in self.splitlines(True)]
return '\n'.join(lines) + '\n'
except AttributeError:
@@ -461,10 +450,10 @@
# taken care of by UserString.
def rstrip(self, chars=None):
- return self.__class__(reprunicode.rstrip(self, chars))
+ return self.__class__(str.rstrip(self, chars))
def lstrip(self, chars=None):
- return self.__class__(reprunicode.lstrip(self, chars))
+ return self.__class__(str.lstrip(self, chars))
class Element(Node):
@@ -589,7 +578,7 @@
break
if self['names']:
return '<%s "%s": %s>' % (self.__class__.__name__,
- '; '.join([ensure_str(n) for n in self['names']]), data)
+ '; '.join(self['names']), data)
else:
return '<%s: %s>' % (self.__class__.__name__, data)
@@ -596,11 +585,11 @@
def shortrepr(self):
if self['names']:
return '<%s "%s"...>' % (self.__class__.__name__,
- '; '.join([ensure_str(n) for n in self['names']]))
+ '; '.join(self['names']))
else:
return '<%s...>' % self.tagname
- def __unicode__(self):
+ def __str__(self):
if self.children:
return u'%s%s%s' % (self.starttag(),
''.join([unicode(c) for c in self.children]),
@@ -608,9 +597,6 @@
else:
return self.emptytag()
- if sys.version_info >= (3, 0):
- __str__ = __unicode__
-
def starttag(self, quoteattr=None):
# the optional arg is used by the docutils_xml writer
if quoteattr is None:
Modified: trunk/docutils/docutils/parsers/rst/directives/misc.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/directives/misc.py 2022-01-04 22:53:36 UTC (rev 8930)
+++ trunk/docutils/docutils/parsers/rst/directives/misc.py 2022-01-04 23:33:59 UTC (rev 8931)
@@ -67,7 +67,6 @@
path = os.path.join(self.standard_include_path, path[1:-1])
path = os.path.normpath(os.path.join(source_dir, path))
path = utils.relative_path(None, path)
- path = nodes.reprunicode(path)
encoding = self.options.get(
'encoding', self.state.document.settings.input_encoding)
e_handler=self.state.document.settings.input_encoding_error_handler
Modified: trunk/docutils/docutils/utils/__init__.py
===================================================================
--- trunk/docutils/docutils/utils/__init__.py 2022-01-04 22:53:36 UTC (rev 8930)
+++ trunk/docutils/docutils/utils/__init__.py 2022-01-04 23:33:59 UTC (rev 8931)
@@ -336,7 +336,7 @@
def decode_path(path):
"""
- Ensure `path` is Unicode. Return `nodes.reprunicode` object.
+ Ensure `path` is Unicode. Return `str` instance.
Decode file/path string in a failsafe manner if not already done.
"""
@@ -348,7 +348,7 @@
path = path.decode(sys.getfilesystemencoding(), 'strict')
except AttributeError: # default value None has no decode method
if not path:
- return nodes.reprunicode('')
+ return ''
raise ValueError('`path` value must be a String or ``None``, not %r'
%path)
except UnicodeDecodeError:
@@ -356,7 +356,7 @@
path = path.decode('utf-8', 'strict')
except UnicodeDecodeError:
path = path.decode('ascii', 'replace')
- return nodes.reprunicode(path)
+ return path
def extract_name_value(line):
Modified: trunk/docutils/test/test_nodes.py
===================================================================
--- trunk/docutils/test/test_nodes.py 2022-01-04 22:53:36 UTC (rev 8930)
+++ trunk/docutils/test/test_nodes.py 2022-01-04 23:33:59 UTC (rev 8931)
@@ -31,7 +31,6 @@
self.assertEqual(repr(self.text), r"<#text: 'Line 1.\nLine 2.'>")
self.assertEqual(self.text.shortrepr(),
r"<#text: 'Line 1.\nLine 2.'>")
- self.assertEqual(nodes.reprunicode('foo'), u'foo')
self.assertEqual(repr(self.unicode_text), u"<#text: 'Möhren'>")
def test_str(self):
@@ -323,19 +322,6 @@
class MiscTests(unittest.TestCase):
- def test_reprunicode(self):
- # return `unicode` instance
- self.assertTrue(isinstance(nodes.reprunicode('foo'), str))
- self.assertEqual(nodes.reprunicode('foo'), u'foo')
- self.assertEqual(nodes.reprunicode(u'Möhre'), u'Möhre')
- # no change to `unicode` under Python 3k
- self.assertEqual(repr(nodes.reprunicode(u'Möhre')), repr(u'Möhre'))
-
- def test_ensure_str(self):
- self.assertTrue(isinstance(nodes.ensure_str(u'über'), str))
- self.assertEqual(nodes.ensure_str('over'), 'over')
- self.assertEqual(nodes.ensure_str(u'über'), r'über')
-
def test_node_class_names(self):
node_class_names = []
for x in dir(nodes):
Modified: trunk/docutils/test/test_parsers/test_rst/test_directives/test_images.py
===================================================================
--- trunk/docutils/test/test_parsers/test_rst/test_directives/test_images.py 2022-01-04 22:53:36 UTC (rev 8930)
+++ trunk/docutils/test/test_parsers/test_rst/test_directives/test_images.py 2022-01-04 23:33:59 UTC (rev 8931)
@@ -12,9 +12,7 @@
import __init__
from test_parsers import DocutilsTestSupport
-from docutils.nodes import reprunicode
-
def suite():
s = DocutilsTestSupport.ParserTestSuite()
s.generateTests(totest)
@@ -417,12 +415,12 @@
<system_message level="3" line="1" source="test data" type="ERROR">
<paragraph>
Error in "image" directive:
- invalid option value: (option: "align"; value: %s)
+ invalid option value: (option: "align"; value: 'ä')
"\xe4" unknown; choose from "top", "middle", "bottom", "left", "center", or "right".
<literal_block xml:space="preserve">
.. image:: picture.png
:align: \xe4
-""" % repr(reprunicode(u'\xe4'))],
+"""],
["""
.. image:: test.png
:target: Uppercase_
Modified: trunk/docutils/test/test_utils.py
===================================================================
--- trunk/docutils/test/test_utils.py 2022-01-04 22:53:36 UTC (rev 8930)
+++ trunk/docutils/test/test_utils.py 2022-01-04 23:33:59 UTC (rev 8931)
@@ -288,7 +288,7 @@
self.assertEqual(bytespath, u'späm')
self.assertEqual(unipath, u'späm')
self.assertEqual(defaultpath, u'')
- self.assertTrue(isinstance(bytespath, nodes.reprunicode))
+ self.assertTrue(isinstance(bytespath, str))
self.assertTrue(isinstance(unipath, str))
self.assertTrue(isinstance(defaultpath, str))
self.assertRaises(ValueError, utils.decode_path, 13)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|