|
From: <aa-...@us...> - 2022-10-21 14:18:08
|
Revision: 9167
http://sourceforge.net/p/docutils/code/9167
Author: aa-turner
Date: 2022-10-21 14:18:05 +0000 (Fri, 21 Oct 2022)
Log Message:
-----------
Simplify ``CustomTestCase.compare_output``
Ensure that the ``core.publish_string`` always returns a string,
introduce ``core.publish_bytes`` for binary output.
Modified Paths:
--------------
trunk/docutils/docutils/core.py
trunk/docutils/docutils/io.py
trunk/docutils/test/DocutilsTestSupport.py
trunk/docutils/test/test_parsers/test_recommonmark/test_misc.py
trunk/docutils/test/test_parsers/test_rst/test_directives/test_code_parsing.py
trunk/docutils/test/test_publisher.py
trunk/docutils/test/test_writers/test_docutils_xml.py
trunk/docutils/test/test_writers/test_html4css1_misc.py
trunk/docutils/test/test_writers/test_html5_polyglot_misc.py
trunk/docutils/test/test_writers/test_odt.py
Modified: trunk/docutils/docutils/core.py
===================================================================
--- trunk/docutils/docutils/core.py 2022-10-21 13:50:56 UTC (rev 9166)
+++ trunk/docutils/docutils/core.py 2022-10-21 14:18:05 UTC (rev 9167)
@@ -429,23 +429,50 @@
enable_exit_status=False):
"""
Set up & run a `Publisher` for programmatic use with string I/O. Return
- the encoded string or Unicode string output.
+ Unicode string output.
- For encoded string output, be sure to set the 'output_encoding' setting to
- the desired encoding. Set it to 'unicode' for unencoded Unicode string
- output. Here's one way::
+ Parameters: see `publish_programmatically`.
+ """
+ output, pub = publish_programmatically(
+ source_class=io.StringInput, source=source, source_path=source_path,
+ destination_class=io.StringOutput,
+ destination=None, destination_path=destination_path,
+ reader=reader, reader_name=reader_name,
+ parser=parser, parser_name=parser_name,
+ writer=writer, writer_name=writer_name,
+ settings=settings, settings_spec=settings_spec,
+ settings_overrides=settings_overrides,
+ config_section=config_section,
+ enable_exit_status=enable_exit_status)
+ return output
- publish_string(..., settings_overrides={'output_encoding': 'unicode'})
- Similarly for Unicode string input (`source`)::
+def publish_bytes(source, source_path=None, destination_path=None,
+ reader=None, reader_name='standalone',
+ parser=None, parser_name='restructuredtext',
+ writer=None, writer_name='pseudoxml',
+ settings=None, settings_spec=None,
+ settings_overrides=None, config_section=None,
+ enable_exit_status=False):
+ """
+ Set up & run a `Publisher` for programmatic use with string I/O. Return
+ the encoded bytes.
- publish_string(..., settings_overrides={'input_encoding': 'unicode'})
+ Be sure to set the 'output_encoding' setting to the desired encoding.::
+ publish_bytes(..., settings_overrides={'output_encoding': 'latin1'})
+
+ Similarly for bytes input (`source`)::
+
+ publish_bytes(..., settings_overrides={'input_encoding': 'latin1'})
+
Parameters: see `publish_programmatically`.
+
+ Provisional.
"""
output, pub = publish_programmatically(
source_class=io.StringInput, source=source, source_path=source_path,
- destination_class=io.StringOutput,
+ destination_class=io.BytesOutput,
destination=None, destination_path=destination_path,
reader=reader, reader_name=reader_name,
parser=parser, parser_name=parser_name,
Modified: trunk/docutils/docutils/io.py
===================================================================
--- trunk/docutils/docutils/io.py 2022-10-21 13:50:56 UTC (rev 9166)
+++ trunk/docutils/docutils/io.py 2022-10-21 14:18:05 UTC (rev 9167)
@@ -555,6 +555,25 @@
mode = 'wb'
+class BytesOutput(Output):
+
+ """
+ Direct string output.
+ """
+
+ default_destination_path = '<bytes>'
+
+ def write(self, data):
+ """Encode `data`, store it in `self.destination`, and return it."""
+ self.destination = self.encode(data)
+ return self.destination
+
+ def encode(self, data):
+ if isinstance(data, bytes):
+ return data
+ return str(data).encode(self.encoding, self.error_handler)
+
+
class StringInput(Input):
"""
@@ -581,7 +600,12 @@
self.destination = self.encode(data)
return self.destination
+ def encode(self, data):
+ if isinstance(data, bytes):
+ return data.decode(self.encoding, self.error_handler)
+ return str(data)
+
class NullInput(Input):
"""
Modified: trunk/docutils/test/DocutilsTestSupport.py
===================================================================
--- trunk/docutils/test/DocutilsTestSupport.py 2022-10-21 13:50:56 UTC (rev 9166)
+++ trunk/docutils/test/DocutilsTestSupport.py 2022-10-21 14:18:05 UTC (rev 9167)
@@ -149,16 +149,13 @@
# empties that dictionary.
roles._roles = {}
- def compare_output(self, _input, output, expected):
- """`output` and `expected` should be strings."""
- if isinstance(expected, bytes):
- expected = expected.decode('utf-8')
- if isinstance(output, bytes):
- output = output.decode('utf-8')
+ def compare_output(self, _input, output: str, expected: str) -> None:
+ """`output` and `expected` are strings."""
# Normalise line endings:
- expected = expected and '\n'.join(expected.splitlines())
- output = output and '\n'.join(output.splitlines())
- self.assertEqual(output, expected)
+ self.assertEqual(
+ '\n'.join(str(output).splitlines()),
+ '\n'.join(str(expected).splitlines())
+ )
class CustomTestSuite(unittest.TestSuite):
Modified: trunk/docutils/test/test_parsers/test_recommonmark/test_misc.py
===================================================================
--- trunk/docutils/test/test_parsers/test_recommonmark/test_misc.py 2022-10-21 13:50:56 UTC (rev 9166)
+++ trunk/docutils/test/test_parsers/test_recommonmark/test_misc.py 2022-10-21 14:18:05 UTC (rev 9167)
@@ -53,9 +53,9 @@
output = publish_string(sample_with_html, parser=parser,
settings_overrides={'warning_stream': '',
'raw_enabled': False})
- self.assertNotIn(b'<raw>', output)
- self.assertIn(b'<system_message', output)
- self.assertIn(b'Raw content disabled.', output)
+ self.assertNotIn('<raw>', output)
+ self.assertIn('<system_message', output)
+ self.assertIn('Raw content disabled.', output)
def test_raw_disabled_inline(self):
output = publish_string('foo <a href="uri">', parser=parser,
@@ -62,9 +62,9 @@
settings_overrides={'warning_stream': '',
'raw_enabled': False,
})
- self.assertNotIn(b'<raw>', output)
- self.assertIn(b'<system_message', output)
- self.assertIn(b'Raw content disabled.', output)
+ self.assertNotIn('<raw>', output)
+ self.assertIn('<system_message', output)
+ self.assertIn('Raw content disabled.', output)
@unittest.skipIf(parser, 'Optional "recommonmark" module found.')
Modified: trunk/docutils/test/test_parsers/test_rst/test_directives/test_code_parsing.py
===================================================================
--- trunk/docutils/test/test_parsers/test_rst/test_directives/test_code_parsing.py 2022-10-21 13:50:56 UTC (rev 9166)
+++ trunk/docutils/test/test_parsers/test_rst/test_directives/test_code_parsing.py 2022-10-21 14:18:05 UTC (rev 9167)
@@ -46,16 +46,16 @@
def test_lexer_error(self):
output = publish_string(unknown_language, settings_overrides=settings)
- self.assertIn(b'<system_message level="2"', output)
- self.assertIn(b'Cannot analyze code. '
- b'No Pygments lexer found for "s-lang".', output)
- self.assertIn(b'<literal_block xml:space="preserve">', output)
+ self.assertIn('<system_message level="2"', output)
+ self.assertIn('Cannot analyze code. '
+ 'No Pygments lexer found for "s-lang".', output)
+ self.assertIn('<literal_block xml:space="preserve">', output)
def test_lexer_error_workaround(self):
output = publish_string(workaround, settings_overrides=settings)
- self.assertNotIn(b'<system_message', output)
- self.assertIn(b'<literal_block classes="code s-lang"', output)
- self.assertIn(b'autoload("abc_mode", "abc");', output)
+ self.assertNotIn('<system_message', output)
+ self.assertIn('<literal_block classes="code s-lang"', output)
+ self.assertIn('autoload("abc_mode", "abc");', output)
if __name__ == '__main__':
Modified: trunk/docutils/test/test_publisher.py
===================================================================
--- trunk/docutils/test/test_publisher.py 2022-10-21 13:50:56 UTC (rev 9166)
+++ trunk/docutils/test/test_publisher.py 2022-10-21 14:18:05 UTC (rev 9167)
@@ -21,7 +21,7 @@
This is a test document with a broken reference: nonexistent_
"""
-pseudoxml_output = b"""\
+pseudoxml_output = """\
<document ids="test-document" names="test\\ document" source="<string>" title="Test Document">
<title>
Test Document
@@ -36,7 +36,7 @@
<paragraph>
Unknown target name: "nonexistent".
"""
-exposed_pseudoxml_output = b"""\
+exposed_pseudoxml_output = """\
<document ids="test-document" internal:refnames="{'nonexistent': [<reference: <#text: 'nonexistent'>>]}" names="test\\ document" source="<string>" title="Test Document">
<title>
Test Document
Modified: trunk/docutils/test/test_writers/test_docutils_xml.py
===================================================================
--- trunk/docutils/test/test_writers/test_docutils_xml.py 2022-10-21 13:50:56 UTC (rev 9166)
+++ trunk/docutils/test/test_writers/test_docutils_xml.py 2022-10-21 14:18:05 UTC (rev 9167)
@@ -124,7 +124,8 @@
return docutils.core.publish_string(source=source.encode('utf-8'),
reader_name='standalone',
writer_name='docutils_xml',
- settings_overrides=settings)
+ settings_overrides=settings
+ ).encode('latin1', 'xmlcharrefreplace')
# XML Test Case
Modified: trunk/docutils/test/test_writers/test_html4css1_misc.py
===================================================================
--- trunk/docutils/test/test_writers/test_html4css1_misc.py 2022-10-21 13:50:56 UTC (rev 9166)
+++ trunk/docutils/test/test_writers/test_html4css1_misc.py 2022-10-21 14:18:05 UTC (rev 9167)
@@ -24,7 +24,7 @@
'stylesheet': '',
'_disable_config': True,
}
- result = core.publish_string(
+ result = core.publish_bytes(
'EUR = \u20ac', writer_name='html4css1',
settings_overrides=settings_overrides)
# Encoding a euro sign with latin1 doesn't work, so the
@@ -53,7 +53,7 @@
"""
result = core.publish_string(data, writer_name='html4css1',
settings_overrides=self.mys)
- self.assertIn(b'<dt class="for the second item">second term:</dt>',
+ self.assertIn('<dt class="for the second item">second term:</dt>',
result)
def test_definition_list_item_name(self):
@@ -70,7 +70,7 @@
"""
result = core.publish_string(data, writer_name='html4css1',
settings_overrides=self.mys)
- self.assertIn(b'<dt id="second-item">second term:</dt>',
+ self.assertIn('<dt id="second-item">second term:</dt>',
result)
Modified: trunk/docutils/test/test_writers/test_html5_polyglot_misc.py
===================================================================
--- trunk/docutils/test/test_writers/test_html5_polyglot_misc.py 2022-10-21 13:50:56 UTC (rev 9166)
+++ trunk/docutils/test/test_writers/test_html5_polyglot_misc.py 2022-10-21 14:18:05 UTC (rev 9167)
@@ -23,7 +23,7 @@
'output_encoding': 'latin1',
'stylesheet': '',
'_disable_config': True}
- result = core.publish_string(
+ result = core.publish_bytes(
'EUR = \u20ac', writer_name='html5_polyglot',
settings_overrides=settings_overrides)
# Encoding a euro sign with latin1 doesn't work, so the
@@ -52,7 +52,7 @@
"""
result = core.publish_string(data, writer_name='html5_polyglot',
settings_overrides=self.mys)
- self.assertIn(b'<dt class="for the second item">second term:</dt>',
+ self.assertIn('<dt class="for the second item">second term:</dt>',
result)
def test_definition_list_item_name(self):
@@ -69,7 +69,7 @@
"""
result = core.publish_string(data, writer_name='html5_polyglot',
settings_overrides=self.mys)
- self.assertIn(b'<dt id="second-item">second term:</dt>',
+ self.assertIn('<dt id="second-item">second term:</dt>',
result)
Modified: trunk/docutils/test/test_writers/test_odt.py
===================================================================
--- trunk/docutils/test/test_writers/test_odt.py 2022-10-21 13:50:56 UTC (rev 9166)
+++ trunk/docutils/test/test_writers/test_odt.py 2022-10-21 14:18:05 UTC (rev 9167)
@@ -63,7 +63,7 @@
settings_overrides['_disable_config'] = True
settings_overrides['language_code'] = 'en-US'
- result = docutils.core.publish_string(
+ result = docutils.core.publish_bytes(
source=input,
reader_name='standalone',
writer_name='odf_odt',
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|