-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1761 from peternewman/0.10-c11-compat
Fix vast majority of outstanding Python 3 issues
- Loading branch information
Showing
23 changed files
with
453 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 2.1 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
# | ||
# StringUtils.py | ||
# Copyright (C) 2022 Peter Newman | ||
|
||
"""Common utils for OLA Python string handling""" | ||
|
||
import sys | ||
|
||
if sys.version_info >= (3, 0): | ||
try: | ||
unicode | ||
except NameError: | ||
unicode = str | ||
|
||
__author__ = '[email protected] (Simon Newton)' | ||
|
||
|
||
def StringEscape(s): | ||
"""Escape unprintable characters in a string.""" | ||
# TODO(Peter): How does this interact with the E1.20 Unicode flag? | ||
# We don't use sys.version_info.major to support Python 2.6. | ||
if sys.version_info[0] == 2 and type(s) == str: | ||
return s.encode('string-escape') | ||
elif sys.version_info[0] == 2 and type(s) == unicode: | ||
return s.encode('unicode-escape') | ||
elif type(s) == str: | ||
# All strings in Python 3 are unicode | ||
# This encode/decode pair gets us an escaped string | ||
return s.encode('unicode-escape').decode(encoding="ascii", | ||
errors="backslashreplace") | ||
else: | ||
raise TypeError('Only strings are supported not %s' % type(s)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/usr/bin/env python | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 2.1 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
# | ||
# StringUtilsTest.py | ||
# Copyright (C) 2022 Peter Newman | ||
|
||
import unittest | ||
|
||
from ola.StringUtils import StringEscape | ||
|
||
"""Test cases for StringUtils utilities.""" | ||
|
||
__author__ = '[email protected] (Simon Newton)' | ||
|
||
|
||
class StringUtilsTest(unittest.TestCase): | ||
def testStringEscape(self): | ||
# Test we escape properly | ||
self.assertEqual('foo', StringEscape("foo")) | ||
self.assertEqual('bar', StringEscape("bar")) | ||
self.assertEqual('bar[]', StringEscape("bar[]")) | ||
self.assertEqual('foo-bar', StringEscape(u'foo-bar')) | ||
self.assertEqual('foo\\x00bar', StringEscape("foo\x00bar")) | ||
# TODO(Peter): How does this interact with the E1.20 Unicode flag? | ||
self.assertEqual('caf\\xe9', StringEscape(u'caf\xe9')) | ||
self.assertEqual('foo\\u2014bar', StringEscape(u'foo\u2014bar')) | ||
|
||
# Test that we display nicely in a string | ||
self.assertEqual('foo', ("%s" % StringEscape("foo"))) | ||
self.assertEqual('bar[]', ("%s" % StringEscape("bar[]"))) | ||
self.assertEqual('foo-bar', ("%s" % StringEscape(u'foo-bar'))) | ||
self.assertEqual('foo\\x00bar', ("%s" % StringEscape("foo\x00bar"))) | ||
# TODO(Peter): How does this interact with the E1.20 Unicode flag? | ||
self.assertEqual('caf\\xe9', ("%s" % StringEscape(u'caf\xe9'))) | ||
self.assertEqual('foo\\u2014bar', ("%s" % StringEscape(u'foo\u2014bar'))) | ||
|
||
# Confirm we throw an exception if we pass in a number or something else | ||
# that's not a string | ||
with self.assertRaises(TypeError): | ||
result = StringEscape(42) | ||
self.assertNone(result) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.