Skip to content

Commit 91ab3c4

Browse files
drakes00oroulet
authored andcommitted
Importing bytesting objects from XML in Python3 (#387)
* Fixup: replace on encoded string. Replace methode was called on an encoded string (e.g.: when parsing bytestring objects). Inverted call to replace and string encoding. * Fixup: bytestring tested against not encoded strings.
1 parent dbd1249 commit 91ab3c4

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

opcua/common/xmlparser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,12 @@ def _parse_value(self, val_el, obj):
230230
if obj.value.tzinfo is None or obj.value.tzinfo.utcoffset(obj.value) is None:
231231
utc.localize(obj.value) # FIXME Forcing to UTC if unaware, maybe should raise?
232232
elif ntag in ("ByteString", "String"):
233-
mytext = ua_type_to_python(val_el.text, ntag)
233+
mytext = val_el.text
234234
if mytext is None:
235235
# Support importing null strings.
236236
mytext = ""
237237
mytext = mytext.replace('\n', '').replace('\r', '')
238-
obj.value = mytext
238+
obj.value = ua_type_to_python(mytext, ntag)
239239
elif ntag == "Guid":
240240
self._parse_contained_value(val_el, obj)
241241
# Override parsed string type to guid.

tests/tests_xml.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@ def test_xml_datetime_array(self):
243243
# o = self.opc.nodes.objects.add_variable(2, "xmlltext_array", [ua.QualifiedName("erert", 5), ua.QualifiedName("erert33", 6)])
244244
# self._test_xml_var_type(o, "qualified_name_array")
245245

246+
def test_xml_bytestring(self):
247+
o = self.opc.nodes.objects.add_variable(2, "xmlltext", "mytext".encode("utf8"), ua.VariantType.ByteString)
248+
self._test_xml_var_type(o, "bytestring")
249+
250+
def test_xml_bytestring_array(self):
251+
o = self.opc.nodes.objects.add_variable(2, "xmlltext_array", ["mytext".encode("utf8"), "errsadf".encode("utf8")], ua.VariantType.ByteString)
252+
self._test_xml_var_type(o, "bytestring_array")
253+
246254
def test_xml_localizedtext(self):
247255
o = self.opc.nodes.objects.add_variable(2, "xmlltext", ua.LocalizedText("mytext"))
248256
self._test_xml_var_type(o, "localized_text")

0 commit comments

Comments
 (0)