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

milde--- via Docutils-checkins <[email protected]>
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 9712
          http://sourceforge.net/p/docutils/code/9712
Author:   milde
Date:     2024-05-18 08:44:56 +0000 (Sat, 18 May 2024)
Log Message:
-----------
Prevent accidental drop of first child when initializing `nodes.Element`.

Raise ValueError if the "rawsource" argument in `nodes.Element.__init__()`
is an `Element` instance.
Prevents surprises when initializing an element with child element(s)
but forgetting about the mandatory "rawsource" argument.

Modified Paths:
--------------
    trunk/docutils/HISTORY.txt
    trunk/docutils/docutils/nodes.py

Modified: trunk/docutils/HISTORY.txt
===================================================================
--- trunk/docutils/HISTORY.txt	2024-05-17 07:15:06 UTC (rev 9711)
+++ trunk/docutils/HISTORY.txt	2024-05-18 08:44:56 UTC (rev 9712)
@@ -24,6 +24,9 @@
 
 * docutils/nodes.py
 
+  - Raise ValueError if the "rawsource" argument in `Element.__init__()`
+    is an `Element` instance.
+    Catches errors like ``nodes.hint(nodes.paragraph())``.
   - New element category classes `SubStructural` and `PureTextElement`.
   - Fix element categories.
   - New method `Element.validate()` (work in progress).

Modified: trunk/docutils/docutils/nodes.py
===================================================================
--- trunk/docutils/docutils/nodes.py	2024-05-17 07:15:06 UTC (rev 9711)
+++ trunk/docutils/docutils/nodes.py	2024-05-18 08:44:56 UTC (rev 9712)
@@ -514,6 +514,8 @@
 
         NOTE: some elements do not set this value (default '').
         """
+        if isinstance(rawsource, Element):
+            raise ValueError('First argument "rawsource" must be a string.')
 
         self.children = []
         """List of child nodes (elements and/or `Text`)."""
@@ -1318,7 +1320,7 @@
     """Separator for child nodes, used by `astext()` method."""
 
     def __init__(self, rawsource='', text='', *children, **attributes):
-        if text != '':
+        if text:
             textnode = Text(text)
             Element.__init__(self, rawsource, textnode, *children,
                              **attributes)

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.