When publishing from a file-like object that lacks a name attribute (e.g., an io.StringIO instance), the resulting document object will have its "source" attribute set to the string "None" instead of to an actual None.
You can test this for yourself by running the following code:
from io import StringIO
from docutils.core import publish_doctree
from docutils.io import FileInput
document = publish_doctree(
source=StringIO('This is test text.'),
source_path=None,
source_class=FileInput,
)
print(repr(document["source"]))
This behavior can be fixed by wrapping the line source_path = decode_path(source_path) in new_document() in docutils/utils/__init__.py (line 444) inside an if source_path is not None: check.
Why this matters to me: I'm writing a command & library for rendering reStructuredText and splitting the result into a more powerful set of fields than offered by Docutils' built-in templating. One of these fields is the name of the document source, and having that field be a string "None" when it should be None is just wrong.
Although the tests pass with the suggested change, there are a couple more edits that will need to be made to fully support it:
visit_document()functions indocutils/writers/_html_base.pyanddocutils/writers/html5_polyglot/__init__.pywill need to guard againstnode['source']beingNonebefore passing it toos.path.basename().self.encode(node['source'])call invisit_system_message()indocutils/writers/latex2e/__init__.pywill need to be edited.The following patch changes the representation of missing source from
"None" to the empty string "":
This keeps the promise that utils.decode_path() returns a
nodes.reprunicode object and would work with the HTML writer's
visit_document().
Alternatively, one may consider making '' a node's "source" default value.
That works too, thanks!
Fixed in r8527. Thank you for reporting and analysis.
Fixed in Docutils 0.17.
Thanks again for the report.