Skip to content

Commit

Permalink
more python 3 compatibility fixes, especially to tests (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
dirk-thomas committed Dec 12, 2013
1 parent fa4bd33 commit 60fd165
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
4 changes: 1 addition & 3 deletions python/catkin/environment_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ def generate_environment_script(env_script):
# fetch environment after calling setup
python_code = 'import os; print(dict(os.environ))'
output = subprocess.check_output([env_script, sys.executable, '-c', python_code])
if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
output = output.decode(sys.stdout.encoding)
env_after = ast.literal_eval(output)
env_after = ast.literal_eval(output.decode('utf8'))

# calculate added and modified environment variables
added = {}
Expand Down
2 changes: 1 addition & 1 deletion python/catkin/tidy_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ def tidy_xml(filename):
data = data[:match.start()] + '?' + data[match.end():]

with open(filename, 'w') as fhand:
fhand.write(data.encode('utf-8'))
fhand.write(data)
return True
2 changes: 1 addition & 1 deletion test/unit_tests/test_environment_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_generate_environment_script(self):
catkin.environment_cache.os.environ = fake_environ
rootdir = tempfile.mkdtemp()
env_file = os.path.join(rootdir, 'env.sh')
with open(env_file, 'ab') as fhand:
with open(env_file, 'a') as fhand:
fhand.write('''\
#! /usr/bin/env sh
export FOO=/foo:/bar
Expand Down
9 changes: 7 additions & 2 deletions test/unit_tests/test_terminal_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
'Please adjust your pythonpath before running this test: %s' % str(e)
)

try:
char = unichr
except NameError:
char = chr


class TerminalColorTest(unittest.TestCase):

Expand All @@ -18,7 +23,7 @@ def test_terminal_colors(self):
test = sanitize(test)
rslt = 'This has bad stuff @! @/ @_ @| OK!\033[0m'
assert fmt(test) == rslt
test = u'\u2018@'
test = char(2018)
test = sanitize(test)
rslt = u'\u2018@@'
rslt = char(2018)
assert test == rslt
11 changes: 8 additions & 3 deletions test/unit_tests/test_tidy_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
raise ImportError(
'Please adjust your pythonpath before running this test: %s' % str(impe))

try:
char = unichr
except NameError:
char = chr


class TidyXmlTest(unittest.TestCase):

def test_safe_xml_regex(self):
for data in [u'\u0000', u'\u000e']:
for data in [char(0), char(14)]:
self.assertIsNotNone(_SAFE_XML_REGEX.match(data))

def test_tiny_xml(self):
Expand All @@ -24,15 +29,15 @@ def test_tiny_xml(self):

utf8_file = os.path.join(rootdir, 'utf8.xml')
with open(utf8_file, 'ab') as fhand:
fhand.write(u'\u0000')
fhand.write(char(0).encode('utf8)'))
tidy_xml(utf8_file)
with open(utf8_file, 'r') as fhand:
contents = fhand.read()
self.assertEqual('?', contents)

iso_file = os.path.join(rootdir, 'iso.xml')
with open(iso_file, 'ab') as fhand:
fhand.write(u'\u0000')
fhand.write(char(0).encode('ascii'))
tidy_xml(iso_file)
with open(iso_file, 'r') as fhand:
contents = fhand.read()
Expand Down

0 comments on commit 60fd165

Please sign in to comment.