From 2d058db82d1ab38a8f7aeea0d273b5e5b8d92340 Mon Sep 17 00:00:00 2001 From: milde Date: Tue, 16 Jan 2018 13:30:57 +0000 Subject: [PATCH] Fix [ 251 ] system_message.copy() TypeError. Fix nodes.Element.copy() Avoid clash with multiple values for keyword argument "rawsource". Let nodes.Element.copy() also copy "document", "line", and "source" attributes. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@8212 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/HISTORY.txt | 6 ++++-- docutils/docutils/nodes.py | 20 ++++++++++++++++---- docutils/test/test_nodes.py | 3 +++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/docutils/HISTORY.txt b/docutils/HISTORY.txt index fa5b3020c..e6fa70fba 100644 --- a/docutils/HISTORY.txt +++ b/docutils/HISTORY.txt @@ -29,11 +29,13 @@ Changes Since 0.14 * docutils/nodes.py - `Text.rstrip` and `Text.lstrip` now handle `rawsource` attribute. - + - Fix [ 251 ] system_message.copy() TypeError. + Element.copy() also copies `document`, `line`, and `source` attributes. + * docutils/parsers/rst/states.py: - Allow embedded colons in field list field names. - - Add "rawsource" attribute for text of inline elements and definition + - Add `rawsource` attribute for text of inline elements and definition list terms. * docutils/parsers/rst/directives/html.py: diff --git a/docutils/docutils/nodes.py b/docutils/docutils/nodes.py index 2b31e7771..1490f43fb 100644 --- a/docutils/docutils/nodes.py +++ b/docutils/docutils/nodes.py @@ -1005,7 +1005,11 @@ class Element(Node): for child in self.children]) def copy(self): - return self.__class__(rawsource=self.rawsource, **self.attributes) + obj = self.__class__(rawsource=self.rawsource, **self.attributes) + obj.document = self.document + obj.source = self.source + obj.line = self.line + return obj def deepcopy(self): copy = self.copy() @@ -1472,8 +1476,11 @@ class document(Root, Structural, Element): self.current_line = offset + 1 def copy(self): - return self.__class__(self.settings, self.reporter, + obj = self.__class__(self.settings, self.reporter, **self.attributes) + obj.source = self.source + obj.line = self.line + return obj def get_decoration(self): if not self.decoration: @@ -1672,11 +1679,12 @@ class system_message(Special, BackLinkable, PreBibliographic, Element): """ def __init__(self, message=None, *children, **attributes): + rawsource = attributes.get('rawsource', '') if message: p = paragraph('', message) children = (p,) + children try: - Element.__init__(self, '', *children, **attributes) + Element.__init__(self, rawsource, *children, **attributes) except: print 'system_message: children=%r' % (children,) raise @@ -1752,8 +1760,12 @@ class pending(Special, Invisible, Element): for line in internals])) def copy(self): - return self.__class__(self.transform, self.details, self.rawsource, + obj = self.__class__(self.transform, self.details, self.rawsource, **self.attributes) + obj.document = self.document + obj.source = self.source + obj.line = self.line + return obj class raw(Special, Inline, PreBibliographic, FixedTextElement): diff --git a/docutils/test/test_nodes.py b/docutils/test/test_nodes.py index 557078c77..6541c699e 100755 --- a/docutils/test/test_nodes.py +++ b/docutils/test/test_nodes.py @@ -616,6 +616,9 @@ class MiscTests(unittest.TestCase): self.assertEqual(e.rawsource, 'rawsource') self.assertEqual(e_copy.rawsource, e.rawsource) self.assertEqual(e_copy['att'], 'e') + self.assertEqual(e_copy.document, e.document) + self.assertEqual(e_copy.source, e.source) + self.assertEqual(e_copy.line, e.line) # Children are not copied. self.assertEqual(len(e_copy), 0) # Deep copy: -- 2.11.4.GIT