Skip to content
This repository has been archived by the owner on Nov 17, 2022. It is now read-only.

Commit

Permalink
[#24] Add logic to only convert strings to bytes in python3
Browse files Browse the repository at this point in the history
Adds additional logic statement to check python version. This is
necessary as I couldn't find a way to make the underlaying
functionality compatible with python2 and 3 due to the differences
in the way both versions handle strings - i.e. strings in python2
are treated as bytes objects by default, so converting to bytes is
not necessary in the first place.
  • Loading branch information
dalepotter committed Jul 11, 2017
1 parent 5157343 commit e64fe57
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion iati/core/data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""A module containing a core representation of an IATI Dataset."""
import sys
from lxml import etree
import iati.core.exceptions
import iati.core.utilities
Expand Down Expand Up @@ -79,7 +80,8 @@ def xml_str(self, value):
value_stripped = value.strip()

# Convert the input to bytes, as etree.fromstring works most consistently with bytes objects, especially if an XML encoding declaration has been used.
if isinstance(value_stripped, str):
if (isinstance(value_stripped, str)
and sys.version_info.major > 2): # Python v2 treats strings as byte objects by default
value_stripped_bytes = value_stripped.encode()
else:
value_stripped_bytes = value_stripped
Expand Down

0 comments on commit e64fe57

Please sign in to comment.