diff options
author | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2012-10-11 22:13:35 +0000 |
---|---|---|
committer | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2012-10-11 22:13:35 +0000 |
commit | 9b87c114965783e658d3fcd6d511194639c31780 (patch) | |
tree | 3eb4cd916095f06e893ae50f9d511c925cf500f1 /docutils | |
parent | 1e36558b9837ccb5b186988039e363f88cd73278 (diff) | |
download | docutils-9b87c114965783e658d3fcd6d511194639c31780.tar.gz |
Fix Py3k error writing to stdout with encoding differing from default.
Cleanup of the workarounds for Py3k encoding problems.
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@7525 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'docutils')
-rw-r--r-- | docutils/io.py | 47 |
1 files changed, 22 insertions, 25 deletions
diff --git a/docutils/io.py b/docutils/io.py index 60a398708..b5d2be3a4 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -191,7 +191,7 @@ class Output(TransformSpec): 'a Unicode string') return data if not isinstance(data, unicode): - # Non-unicode (e.g. binary) output. + # Non-unicode (e.g. bytes) output. return data else: return data.encode(self.encoding, self.error_handler) @@ -335,7 +335,7 @@ class FileOutput(Output): elif (# destination is file-type object -> check mode: mode and hasattr(self.destination, 'mode') and mode != self.destination.mode): - print >>self._stderr, ('Destination mode "%s" ' + print >>self._stderr, ('Warning: Destination mode "%s" ' 'differs from specified mode "%s"' % (self.destination.mode, mode)) if not destination_path: @@ -343,21 +343,6 @@ class FileOutput(Output): self.destination_path = self.destination.name except AttributeError: pass - # Special cases under Python 3: different encoding or binary output - if sys.version_info >= (3,0): - if ('b' in self.mode - and self.destination in (sys.stdout, sys.stderr) - ): - self.destination = self.destination.buffer - if check_encoding(self.destination, self.encoding) is False: - if self.destination in (sys.stdout, sys.stderr): - self.destination = self.destination.buffer - else: # TODO: try the `write to .buffer` scheme instead? - raise ValueError('Encoding of %s (%s) differs \n' - ' from specified encoding (%s)' % - (self.destination_path or 'destination', - destination.encoding, encoding)) - def open(self): # Specify encoding in Python 3. @@ -381,17 +366,29 @@ class FileOutput(Output): """ if not self.opened: self.open() + if ('b' not in self.mode and sys.version_info < (3,0) + or check_encoding(self.destination, self.encoding) is False + ): + data = self.encode(data) + if sys.version_info >= (3,0) and os.linesep != '\n': + data = data.replace('\n', os.linesep) # fix endings + try: # In Python < 2.5, try...except has to be nested in try...finally. try: - if 'b' not in self.mode and (sys.version_info < (3,0) or - check_encoding(self.destination, self.encoding) is False): - data = self.encode(data) - if sys.version_info >= (3,0) and os.linesep != '\n': - # writing as binary data -> fix endings - data = data.replace('\n', os.linesep) - self.destination.write(data) - + except TypeError, e: + if sys.version_info >= (3,0) and isinstance(data, bytes): + try: + self.destination.buffer.write(data) + except AttributeError: + if check_encoding(self.destination, + self.encoding) is False: + raise ValueError('Encoding of %s (%s) differs \n' + ' from specified encoding (%s)' % + (self.destination_path or 'destination', + self.destination.encoding, self.encoding)) + else: + raise e except (UnicodeError, LookupError), err: raise UnicodeError( 'Unable to encode output data. output-encoding is: ' |