Skip to content

Commit 15fbe51

Browse files
author
Steve Canny
committed
py3: fix Python 3 compatibility on xmlchemy
1 parent 1b753b3 commit 15fbe51

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

docx/oxml/xmlchemy.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,10 +689,12 @@ def _remove_choice_group_method_name(self):
689689
return '_remove_%s' % self._prop_name
690690

691691

692-
class BaseOxmlElement(etree.ElementBase):
692+
class _OxmlElementBase(etree.ElementBase):
693693
"""
694-
Base class for all custom element classes, to add standardized behavior
695-
to all classes in one place.
694+
Effective base class for all custom element classes, to add standardized
695+
behavior to all classes in one place. Actual inheritance is from
696+
BaseOxmlElement below, needed to manage Python 2-3 metaclass declaration
697+
compatibility.
696698
"""
697699

698700
__metaclass__ = MetaOxmlElement
@@ -752,3 +754,8 @@ def xpath(self, xpath_str):
752754
@property
753755
def _nsptag(self):
754756
return NamespacePrefixedTag.from_clark_name(self.tag)
757+
758+
759+
BaseOxmlElement = MetaOxmlElement(
760+
'BaseOxmlElement', (etree.ElementBase,), dict(_OxmlElementBase.__dict__)
761+
)

tests/oxml/test__init__.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,15 @@ def it_adds_supplied_attributes(self):
3131
assert etree.tostring(element) == (
3232
'<a:foo xmlns:a="http://schemas.openxmlformats.org/drawingml/200'
3333
'6/main" a="b" c="d"/>'
34-
)
34+
).encode('utf-8')
3535

3636
def it_adds_additional_namespace_declarations_when_supplied(self):
37-
element = OxmlElement(
38-
'a:foo', nsdecls={
39-
'a': 'http://schemas.openxmlformats.org/drawingml/2006/main',
40-
'x': 'other'
41-
}
42-
)
43-
assert etree.tostring(element) == (
44-
'<a:foo xmlns:a="http://schemas.openxmlformats.org/drawingml/200'
45-
'6/main" xmlns:x="other"/>'
46-
)
37+
ns1 = 'http://schemas.openxmlformats.org/drawingml/2006/main'
38+
ns2 = 'other'
39+
element = OxmlElement('a:foo', nsdecls={'a': ns1, 'x': ns2})
40+
assert len(element.nsmap.items()) == 2
41+
assert element.nsmap['a'] == ns1
42+
assert element.nsmap['x'] == ns2
4743

4844

4945
class DescribeOxmlParser(object):

0 commit comments

Comments
 (0)