summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormilde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2023-04-26 11:40:15 +0000
committermilde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2023-04-26 11:40:15 +0000
commit1ccdac51aa271128140fdfa3333470bd0f3548c5 (patch)
treefa2b6f66231e8a5890236b094a98facfcd29138c
parentacb44ff53095c134afa0938eef2a486c161bb71a (diff)
downloaddocutils-1ccdac51aa271128140fdfa3333470bd0f3548c5.tar.gz
Make `io.FileOutput.write()` work with bytes also on Windows.
Don't attempt to change line endings to the OS default, if the `data` to write is a `bytes` instance. This fixes an error with the `core.rst2odt()` "script entry point" function under Windows. Mark current behaviour as provisional. git-svn-id: https://svn.code.sf.net/p/docutils/code/trunk@9364 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
-rw-r--r--docutils/docutils/io.py17
1 files changed, 13 insertions, 4 deletions
diff --git a/docutils/docutils/io.py b/docutils/docutils/io.py
index a622503bc..99f8b121a 100644
--- a/docutils/docutils/io.py
+++ b/docutils/docutils/io.py
@@ -580,16 +580,25 @@ class FileOutput(Output):
def write(self, data):
"""Write `data` to a single file, also return it.
- `data` is encoded and returned as `bytes` instance
- if `self.encoding` and `self.destination.encoding` differ.
+ `data` can be a `str` or `bytes` instance.
+ If writing `bytes` fails, an attempt is made to write to
+ the low-level interface ``self.destination.buffer``.
+
+ If `data` is a `str` instance and `self.encoding` and
+ `self.destination.encoding` are set to different values, `data`
+ is encoded to a `bytes` instance using `self.encoding`.
+
+ Provisional: future versions may raise an error if `self.encoding`
+ and `self.destination.encoding` are set to different values.
"""
if not self.opened:
self.open()
- if ('b' not in self.mode
+ if (isinstance(data, str)
and check_encoding(self.destination, self.encoding) is False):
if os.linesep != '\n':
data = data.replace('\n', os.linesep) # fix endings
data = self.encode(data)
+
try:
self.destination.write(data)
except TypeError as err:
@@ -625,7 +634,7 @@ class BinaryFileOutput(FileOutput):
A version of docutils.io.FileOutput which writes to a binary file.
"""
# Used by core.publish_cmdline_to_binary() which in turn is used by
- # rst2odt (OpenOffice writer)
+ # tools/rst2odt.py but not by core.rst2odt().
mode = 'wb'