Skip to content

Commit

Permalink
fix parsing localized float strings (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paweł Pałucha committed Jan 11, 2021
1 parent e1270eb commit 47a1596
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: 3.x
- name: Setup locales
run: |
sudo locale-gen en_US.UTF-8
sudo update-locale
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
5 changes: 4 additions & 1 deletion junitparser/junitparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
except NameError:
unicode = str

from locale import atof, setlocale, LC_ALL

setlocale(LC_ALL, '')

This comment has been minimized.

Copy link
@arichardson

arichardson Apr 23, 2021

Contributor

Changing the locale when importing a package does not seem like a good idea to me.

This is fine in __main__.py but not in a file that can be imported by external consumers.


def write_xml(obj, filepath=None, pretty=False):
tree = etree.ElementTree(obj._elem)
Expand Down Expand Up @@ -115,7 +118,7 @@ def __get__(self, instance, cls):
if result is None and isinstance(instance, (JUnitXml, TestSuite)):
instance.update_statistics()
result = super(FloatAttr, self).__get__(instance, cls)
return float(result) if result else None
return atof(result) if result else None

def __set__(self, instance, value):
if not (isinstance(value, float) or isinstance(value, int)):
Expand Down
9 changes: 8 additions & 1 deletion tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
except ImportError:
pass

import locale
try:
locale.setlocale(locale.LC_NUMERIC, 'en_US.UTF-8')
has_us_locale = True
except locale.Error:
has_us_locale = False


class Test_MergeSuiteCounts(unittest.TestCase):
def test_merge_test_count(self):
Expand Down Expand Up @@ -94,7 +101,7 @@ def test_fromstring_no_testsuites(self):
def test_fromstring_multiple_fails(self):
text = """<testsuites>
<testsuite errors="1" failures="0" hostname="hooch" name="pytest" skipped="1" tests="3" time="0.025" timestamp="2020-02-05T10:52:33.843536">
<testcase classname="test_x" file="test_x.py" line="7" name="test_comp_1" time="0.000"/>
<testcase classname="test_x" file="test_x.py" line="7" name="test_comp_1" time="1""" + ("," if has_us_locale else "") + """000.000"/>
<testcase classname="test_x" file="test_x.py" line="10" name="test_comp_2" time="0.000">
<skipped message="unconditional skip" type="pytest.skip">test_x.py:11: unconditional skip</skipped>
<error message="test teardown failure">
Expand Down

0 comments on commit 47a1596

Please sign in to comment.