SF.net SVN: docutils:[10267 ] trunk/docutils

milde--- via Docutils-checkins <[email protected]> Mon, 01 Dec 2025 22:43:33 +0000
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 10267
          http://sourceforge.net/p/docutils/code/10267
Author:   milde
Date:     2025-12-01 22:43:32 +0000 (Mon, 01 Dec 2025)
Log Message:
-----------
docutils.io.FileInput: ensure read() returns a `str`.

Decode data read from a file-like object, if it is a `bytes` instance.

Fixes bug #514

Modified Paths:
--------------
    trunk/docutils/docs/api/publisher.rst
    trunk/docutils/docutils/core.py
    trunk/docutils/docutils/io.py

Modified: trunk/docutils/docs/api/publisher.rst
===================================================================
--- trunk/docutils/docs/api/publisher.rst	2025-11-28 13:52:09 UTC (rev 10266)
+++ trunk/docutils/docs/api/publisher.rst	2025-12-01 22:43:32 UTC (rev 10267)
@@ -525,9 +525,9 @@
   A file-like object holding the document source
   (must have `read()` and `close()` methods).
 
-  Default: None (open `source_path <source_path (file I/O)_>`__
-  or use `sys.stdin`).
+  Default: None (open `source_path`__ or use `sys.stdin`).
 
+  __
   .. _source_path (file I/O):
 
 source_path : str | pathlib.Path
@@ -534,7 +534,7 @@
   Path to the source file,
   opened if `source <source (file I/O)_>`__ is None.
 
-  Default: None (use `source <source (file I/O)_>`__).
+  Default: None (use `source <source (file I/O)_>`__ or `sys.stdin`).
 
 _`destination` : file-like
   A file-like object that will receive the output document
@@ -548,7 +548,7 @@
 destination_path : str | pathlib.Path
   Path to the destination file, opened if destination_ is None.
 
-  Default: None (use destination_).
+  Default: None (use destination_ or `sys.stdout`).
 
 
 .. _string input:
@@ -594,6 +594,7 @@
 
   Default: None.
 
+
 Component Specification
 -----------------------
 

Modified: trunk/docutils/docutils/core.py
===================================================================
--- trunk/docutils/docutils/core.py	2025-11-28 13:52:09 UTC (rev 10266)
+++ trunk/docutils/docutils/core.py	2025-12-01 22:43:32 UTC (rev 10267)
@@ -719,7 +719,7 @@
       - `io.FileInput`: Path to the input file, opened if no `source`
         supplied.
 
-      - `io.StringInput`: Optional.  Path to the file or name of the
+      - `io.StringInput`: Optional.  Path to the file or description of the
         object that produced `source`.  Only used for diagnostic output.
 
     * `destination_class` **required**: The class for dynamically created

Modified: trunk/docutils/docutils/io.py
===================================================================
--- trunk/docutils/docutils/io.py	2025-11-28 13:52:09 UTC (rev 10266)
+++ trunk/docutils/docutils/io.py	2025-12-01 22:43:32 UTC (rev 10267)
@@ -141,15 +141,13 @@
 
         Provisional: encoding detection will be removed in Docutils 1.0.
         """
-        if self.encoding and self.encoding.lower() == 'unicode':
-            assert isinstance(data, str), ('input encoding is "unicode" '
-                                           'but `data` is no `str` instance')
         if isinstance(data, str):
-            # nothing to decode
-            return data
+            return data  # nothing to decode
         if self.encoding:
             # We believe the user/application when the encoding is
             # explicitly given.
+            assert self.encoding.lower() != 'unicode', (
+                'input encoding is "unicode" but `data` is no `str` instance')
             encoding_candidates = [self.encoding]
         else:
             with warnings.catch_warnings():
@@ -419,15 +417,15 @@
     ) -> None:
         """
         :Parameters:
-            - `source`: either a file-like object (which is read directly), or
-              `None` (which implies `sys.stdin` if no `source_path` given).
-            - `source_path`: a path to a file, which is opened for reading.
-            - `encoding`: the expected text encoding of the input file.
+            - `source`: either a file-like object (with `read()` and `close()`
+              methods) or None (use source indicated by `source_path`).
+            - `source_path`: a path to a file (which is opened for reading
+              if `source` is None) or `None` (implies `sys.stdin`).
+            - `encoding`: the text encoding of the input file.
             - `error_handler`: the encoding error handler to use.
             - `autoclose`: close automatically after read (except when
-              `sys.stdin` is the source).
-            - `mode`: how the file is to be opened (see standard function
-              `open`). The default is read only ('r').
+              the source is `sys.stdin`).
+            - `mode`: how the file is to be opened. Default is read only ('r').
         """
         super().__init__(source, source_path, encoding, error_handler)
         self.autoclose = autoclose
@@ -467,7 +465,7 @@
                 # normalize newlines
                 data = '\n'.join(data.splitlines()+[''])
             else:
-                data = self.source.read()
+                data = self.decode(self.source.read())
         finally:
             if self.autoclose:
                 self.close()

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.