Skip to content

Commit

Permalink
improve support for py2
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotan committed Jan 13, 2025
1 parent bfe1736 commit 50d6c89
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
9 changes: 6 additions & 3 deletions nxswriter/FElement.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def _findShape(self, rank, lengths=None, extraD=False,
if sys.version_info > (3,):
val = ("".join(self.content)).strip()
else:
val = ("".join(self.content)).strip().encode()
val = ("".join(self.content)).strip().encode("utf8")
found = False
if checkData and self.source and self.source.isValid():
data = self.source.getData()
Expand Down Expand Up @@ -497,9 +497,12 @@ def _setAttributes(self, excluded=None):
ekey, "string", overwrite=True)[...] \
= self._tagAttrs[key].strip()
else:
try:
vl = self._tagAttrs[key].strip().encode()
except Exception:
vl = self._tagAttrs[key].strip().encode("utf8")
self.h5Object.attributes.create(
ekey, "string", overwrite=True)[...] \
= self._tagAttrs[key].strip().encode()
ekey, "string", overwrite=True)[...] = vl

def h5Attribute(self, name):
""" provides attribute h5 object
Expand Down
19 changes: 17 additions & 2 deletions nxswriter/TangoDataWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
#: (:obj:`bool`) tango bug #213 flag related to EncodedAttributes in python3
PYTG_BUG_213 = False
if sys.version_info > (3,):
unicode = str
try:
try:
import tango
Expand Down Expand Up @@ -314,7 +315,13 @@ def __setXML(self, xmlset):
:type xmlset: :obj:`str`
"""
self.__fetcher = FetchNameHandler(streams=self._streams)
sax.parseString(xmlset, self.__fetcher)
if sys.version_info > (3,):
sax.parseString(xmlset, self.__fetcher)
else:
if isinstance(xmlset, unicode):
sax.parseString(xmlset.encode('utf-8'), self.__fetcher)
else:
sax.parseString(xmlset, self.__fetcher)
self.__xmlsettings = xmlset

def __delXML(self):
Expand Down Expand Up @@ -501,7 +508,15 @@ def openEntry(self):
parser.setContentHandler(handler)
parser.setErrorHandler(errorHandler)
inpsrc = sax.InputSource()
inpsrc.setByteStream(StringIO(self.xmlsettings))
if sys.version_info > (3,):
inpsrc.setByteStream(StringIO(self.xmlsettings))
else:
if isinstance(self.xmlsettings, unicode):
inpsrc.setByteStream(
StringIO(self.xmlsettings.encode('utf-8')))
else:
inpsrc.setByteStream(StringIO(self.xmlsettings))

parser.parse(inpsrc)

self.__initPool = handler.initPool
Expand Down
5 changes: 1 addition & 4 deletions test/TangoDataWriterH5Cpp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1683,10 +1683,7 @@ def test_scanRecord_nexuspath(self):
self.assertEqual(at.shape, ())
self.assertEqual(at.dtype, "string")
self.assertEqual(at.name, "units")
if sys.version_info > (3, ):
self.assertEqual(at[...], u"\u03bcm")
else:
self.assertEqual(at[...], b"\xce\xbcm")
self.assertEqual(at[...], u"\u03bcm")

at = cnt.attributes["nexdatas_source"]
self.assertTrue(at.is_valid)
Expand Down
5 changes: 1 addition & 4 deletions test/TangoDataWriterH5PY_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1681,10 +1681,7 @@ def test_scanRecord_nexuspath(self):
self.assertEqual(at.shape, ())
self.assertEqual(at.dtype, "string")
self.assertEqual(at.name, "units")
if sys.version_info > (3, ):
self.assertEqual(at[...], u"\u03bcm")
else:
self.assertEqual(at[...], b"\xce\xbcm")
self.assertEqual(at[...], u"\u03bcm")

at = cnt.attributes["nexdatas_source"]
self.assertTrue(at.is_valid)
Expand Down

0 comments on commit 50d6c89

Please sign in to comment.