Convert two tests to work with both Python 2 and 3

Now these two tests work without having to use 2to3
This commit is contained in:
William S Fulton 2020-08-13 23:34:28 +01:00
commit 982b08dced
2 changed files with 21 additions and 8 deletions

View file

@ -1,7 +1,11 @@
import python_destructor_exception import python_destructor_exception
from StringIO import StringIO
import sys import sys
if sys.version_info[0:2] < (3, 0):
import StringIO as io
else:
import io
def error_function(): def error_function():
python_destructor_exception.ClassWithThrowingDestructor().GetBlah() python_destructor_exception.ClassWithThrowingDestructor().GetBlah()
@ -9,13 +13,13 @@ def runtest():
attributeErrorOccurred = False attributeErrorOccurred = False
try: try:
error_function() error_function()
except AttributeError, e: except AttributeError:
attributeErrorOccurred = True attributeErrorOccurred = True
return attributeErrorOccurred return attributeErrorOccurred
def test1(): def test1():
stderr_saved = sys.stderr stderr_saved = sys.stderr
buffer = StringIO() buffer = io.StringIO()
attributeErrorOccurred = False attributeErrorOccurred = False
try: try:
# Suppress stderr while making this call to suppress the output shown by PyErr_WriteUnraisable # Suppress stderr while making this call to suppress the output shown by PyErr_WriteUnraisable

View file

@ -1,8 +1,17 @@
import python_strict_unicode import python_strict_unicode
import sys
test_bytes = b"hello \x01world\x99" test_bytes = b"hello \x01world\x99"
BYTES = b"BYTES" BYTES = b"BYTES"
if sys.version_info[0:2] < (3, 0):
test_unicode = u"h\udce9llo w\u00f6rld" test_unicode = u"h\udce9llo w\u00f6rld"
UNICODE = u"UNICODE"
type_unicode_string = type(u"")
else:
test_unicode = "h\udce9llo w\u00f6rld"
UNICODE = "UNICODE"
type_unicode_string = type("")
# Test that byte string inputs and outputs work as expected # Test that byte string inputs and outputs work as expected
bdbl = python_strict_unicode.double_str(test_bytes) bdbl = python_strict_unicode.double_str(test_bytes)
@ -20,12 +29,12 @@ if type(bout) != type(BYTES):
udbl = python_strict_unicode.double_wstr(test_unicode) udbl = python_strict_unicode.double_wstr(test_unicode)
if udbl != test_unicode + test_unicode: if udbl != test_unicode + test_unicode:
raise RuntimeError("Failed to double wide string") raise RuntimeError("Failed to double wide string")
if type(udbl) != type(u""): if type(udbl) != type_unicode_string:
raise RuntimeError("Wrong type output for wide string") raise RuntimeError("Wrong type output for wide string")
uout = python_strict_unicode.same_wstr(test_unicode) uout = python_strict_unicode.same_wstr(test_unicode)
if uout != test_unicode: if uout != test_unicode:
raise RuntimeError("Failed to copy wchar_t*") raise RuntimeError("Failed to copy wchar_t*")
if type(uout) != type(u""): if type(uout) != type_unicode_string:
raise RuntimeError("Wrong type output for wchar_t*") raise RuntimeError("Wrong type output for wchar_t*")
# Test that overloading is handled properly # Test that overloading is handled properly
@ -35,9 +44,9 @@ if bovr != BYTES:
if type(bovr) != type(BYTES): if type(bovr) != type(BYTES):
raise RuntimeError("Wrong type output from overload") raise RuntimeError("Wrong type output from overload")
uovr = python_strict_unicode.overload(test_unicode) uovr = python_strict_unicode.overload(test_unicode)
if uovr != u"UNICODE": if uovr != UNICODE:
raise RuntimeError("Failed to return unicode from overload") raise RuntimeError("Failed to return unicode from overload")
if type(uovr) != type(u""): if type(uovr) != type_unicode_string:
raise RuntimeERror("Wrong type output from overload") raise RuntimeERror("Wrong type output from overload")
# Test that bytes aren't accepted as wide strings and unicode isn't accepted as narrow strings # Test that bytes aren't accepted as wide strings and unicode isn't accepted as narrow strings