Skip to content

Commit

Permalink
Fixed more python 2/3 unicode issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
mlampert committed Oct 23, 2017
1 parent dce9c0e commit 7448d47
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Mod/Path/PathScripts/PathUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ def clearExpressionEngine(obj):

def toUnicode(string):
'''toUnicode(string) ... returns a unicode version of string regardless of the python version.'''
if sys.version_info[0] < 3:
if sys.version_info.major < 3:
return unicode(string)
return string

def isString(string):
'''isString(string) ... return True if string is a string, regardless of string type and python version.'''
if type(string) == str:
return True
if sys.version_info[0] < 3 and type(string) == unicode:
if sys.version_info.major < 3 and type(string) == unicode:
return True
return False

Expand Down
39 changes: 23 additions & 16 deletions src/Mod/Path/PathTests/TestPathSetupSheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@
import Path
import PathScripts.PathSetupSheet as PathSetupSheet
import PathScripts.PathLog as PathLog
import sys

PathLog.setLevel(PathLog.Level.DEBUG, PathLog.thisModule())

from PathTests.PathTestUtils import PathTestBase

def refstring(string):
if sys.version_info.major < 3:
return string
return string.replace(" u'", " '")


class TestPathSetupSheet(PathTestBase):

def setUp(self):
Expand Down Expand Up @@ -145,25 +152,25 @@ def test11(self):
'''Verify template attributes encoding/decoding of strings.'''
ss = PathSetupSheet.Create().Proxy

self.assertEqual(str(ss.encodeTemplateAttributes({'00': 'hugo'})), "{'00': u'hugo'}")
self.assertEqual(str(ss.encodeTemplateAttributes({'00': 'SetupSheet'})), "{'00': u'${SetupSheet}'}")
self.assertEqual(str(ss.encodeTemplateAttributes({'00': 'SetupSheet.y'})), "{'00': u'${SetupSheet}.y'}")
self.assertEqual(str(ss.encodeTemplateAttributes({'00': '${hugo}'})), "{'00': u'${hugo}'}")
self.assertEqual(str(ss.encodeTemplateAttributes({'00': 'hugo'})), refstring("{'00': u'hugo'}"))
self.assertEqual(str(ss.encodeTemplateAttributes({'00': 'SetupSheet'})), refstring("{'00': u'${SetupSheet}'}"))
self.assertEqual(str(ss.encodeTemplateAttributes({'00': 'SetupSheet.y'})), refstring("{'00': u'${SetupSheet}.y'}"))
self.assertEqual(str(ss.encodeTemplateAttributes({'00': '${hugo}'})), refstring("{'00': u'${hugo}'}"))

self.assertEqual(str(ss.decodeTemplateAttributes({'00': 'hugo'})), "{'00': u'hugo'}")
self.assertEqual(str(ss.decodeTemplateAttributes({'00': '${SetupSheet}'})), "{'00': u'SetupSheet'}")
self.assertEqual(str(ss.decodeTemplateAttributes({'00': '${SetupSheet}.y'})), "{'00': u'SetupSheet.y'}")
self.assertEqual(str(ss.decodeTemplateAttributes({'00': '${SetupSheet}.y - ${SetupSheet}.z'})), "{'00': u'SetupSheet.y - SetupSheet.z'}")
self.assertEqual(str(ss.decodeTemplateAttributes({'00': 'hugo'})), refstring("{'00': u'hugo'}"))
self.assertEqual(str(ss.decodeTemplateAttributes({'00': '${SetupSheet}'})), refstring("{'00': u'SetupSheet'}"))
self.assertEqual(str(ss.decodeTemplateAttributes({'00': '${SetupSheet}.y'})), refstring("{'00': u'SetupSheet.y'}"))
self.assertEqual(str(ss.decodeTemplateAttributes({'00': '${SetupSheet}.y - ${SetupSheet}.z'})), refstring("{'00': u'SetupSheet.y - SetupSheet.z'}"))

def test12(self):
'''Verify template attributes encoding/decoding of dictionaries.'''
ss = PathSetupSheet.Create().Proxy

self.assertEqual(str(ss.encodeTemplateAttributes({'00': {'01': 'hugo'}})), "{'00': {'01': u'hugo'}}")
self.assertEqual(str(ss.encodeTemplateAttributes({'00': {'01': 'SetupSheet.y - SetupSheet.z'}})), "{'00': {'01': u'${SetupSheet}.y - ${SetupSheet}.z'}}")
self.assertEqual(str(ss.encodeTemplateAttributes({'00': {'01': 'hugo'}})), refstring("{'00': {'01': u'hugo'}}"))
self.assertEqual(str(ss.encodeTemplateAttributes({'00': {'01': 'SetupSheet.y - SetupSheet.z'}})), refstring("{'00': {'01': u'${SetupSheet}.y - ${SetupSheet}.z'}}"))

self.assertEqual(str(ss.decodeTemplateAttributes({'00': {'01': 'hugo'}})), "{'00': {'01': u'hugo'}}")
self.assertEqual(str(ss.decodeTemplateAttributes({'00': {'01': '${SetupSheet}.y - ${SetupSheet}.z'}})), "{'00': {'01': u'SetupSheet.y - SetupSheet.z'}}")
self.assertEqual(str(ss.decodeTemplateAttributes({'00': {'01': 'hugo'}})), refstring("{'00': {'01': u'hugo'}}"))
self.assertEqual(str(ss.decodeTemplateAttributes({'00': {'01': '${SetupSheet}.y - ${SetupSheet}.z'}})), refstring("{'00': {'01': u'SetupSheet.y - SetupSheet.z'}}"))

def test13(self):
'''Verify template attributes encoding/decoding of lists.'''
Expand All @@ -179,13 +186,13 @@ def test13(self):
self.assertEqual(len(encoded['01']), 2)
self.assertEqual(encoded['01'][0]['10'], '${SetupSheet}')
self.assertEqual(encoded['01'][0]['11'], '${SetupSheet}.y')
self.assertEqual(str(encoded['01'][1]), "{'20': u'${SetupSheet}'}")
self.assertEqual(str(encoded['01'][1]), refstring("{'20': u'${SetupSheet}'}"))
self.assertEqual(len(encoded['02']), 1)
self.assertEqual(len(encoded['02'][0]['a']), 2)
self.assertEqual(str(encoded['02'][0]['a'][0]), "{'b': u'${SetupSheet}'}")
self.assertEqual(str(encoded['02'][0]['a'][1]), "{'c': u'${SetupSheet}'}")
self.assertEqual(str(encoded['02'][0]['a'][0]), refstring("{'b': u'${SetupSheet}'}"))
self.assertEqual(str(encoded['02'][0]['a'][1]), refstring("{'c': u'${SetupSheet}'}"))
self.assertEqual(len(encoded['02'][0]['b']), 1)
self.assertEqual(str(encoded['02'][0]['b'][0]), "{'b': u'${SetupSheet}'}")
self.assertEqual(str(encoded['02'][0]['b'][0]), refstring("{'b': u'${SetupSheet}'}"))

decoded = ss.decodeTemplateAttributes(encoded)
self.assertEqual(len(decoded), len(attrs))
Expand Down

9 comments on commit 7448d47

@plgarcia
Copy link
Contributor

@plgarcia plgarcia commented on 7448d47 Oct 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I beleave that this is one of these last changes that causes the error while testing

FAIL: test00 (PathTests.TestPathSetupSheet.TestPathSetupSheet)
Verify SetupSheet templateAttributes

Traceback (most recent call last):
File "/home/pagarcia/FreeCAD/FreeCAD/Mod/Path/PathTests/TestPathSetupSheet.py", line 56, in test00
self.assertEqual(attrs[PathSetupSheet.Template.HorizRapid], '0.00 mm/s')
AssertionError: u'0,00 mm/s' != '0.00 mm/s'

The difference is the comma that is the decimal separator in french instead of the dot.
But this did not happen yesterday evening, before the last commits.

@mlampert
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's nothing in this commit (or the previous) that changed locale handling. In fact test00 wasn't changed at all, nor anything test00 tests. Locale handling is an interesting point though, I did not consider that when I wrote the test.

@plgarcia
Copy link
Contributor

@plgarcia plgarcia commented on 7448d47 Oct 28, 2017 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mlampert
Copy link
Contributor Author

@mlampert mlampert commented on 7448d47 Oct 28, 2017 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plgarcia
Copy link
Contributor

@plgarcia plgarcia commented on 7448d47 Oct 28, 2017 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mlampert
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FC sets LC_NUMERIC="C" explicitly in order to get the '.' as a comma separator. There are some comments in the code and according to wmayer that must not be changed. There's something else going on here.

@plgarcia
Copy link
Contributor

@plgarcia plgarcia commented on 7448d47 Oct 29, 2017 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plgarcia
Copy link
Contributor

@plgarcia plgarcia commented on 7448d47 Nov 1, 2017 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mlampert
Copy link
Contributor Author

@mlampert mlampert commented on 7448d47 Nov 1, 2017 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.