]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix text failures when ctypes is not available
authorAntoine Pitrou <solipsis@pitrou.net>
Wed, 5 Oct 2011 11:01:41 +0000 (13:01 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Wed, 5 Oct 2011 11:01:41 +0000 (13:01 +0200)
(followup to Victor's 85d11cf67aa8 and 7a50e549bd11)

Lib/test/test_codeccallbacks.py
Lib/test/test_codecs.py

index 81af47db628839a8d028ac1a42a30fad8673efbf..caa1b96e71c81cac59d2a71547cd3e9f64fa0c36 100644 (file)
@@ -1,8 +1,13 @@
 import test.support, unittest
 import sys, codecs, html.entities, unicodedata
-import ctypes
 
-SIZEOF_WCHAR_T = ctypes.sizeof(ctypes.c_wchar)
+try:
+    import ctypes
+except ImportError:
+    ctypes = None
+    SIZEOF_WCHAR_T = -1
+else:
+    SIZEOF_WCHAR_T = ctypes.sizeof(ctypes.c_wchar)
 
 class PosReturn:
     # this can be used for configurable callbacks
@@ -572,33 +577,34 @@ class CodecCallbackTest(unittest.TestCase):
                 UnicodeEncodeError("ascii", "\uffff", 0, 1, "ouch")),
             ("\\uffff", 1)
         )
-        if ctypes.sizeof(ctypes.c_wchar) == 2:
+        if SIZEOF_WCHAR_T == 2:
             len_wide = 2
         else:
             len_wide = 1
-        self.assertEqual(
-            codecs.backslashreplace_errors(
-                UnicodeEncodeError("ascii", "\U00010000",
-                                   0, len_wide, "ouch")),
-            ("\\U00010000", len_wide)
-        )
-        self.assertEqual(
-            codecs.backslashreplace_errors(
-                UnicodeEncodeError("ascii", "\U0010ffff",
-                                   0, len_wide, "ouch")),
-            ("\\U0010ffff", len_wide)
-        )
-        # Lone surrogates (regardless of unicode width)
-        self.assertEqual(
-            codecs.backslashreplace_errors(
-                UnicodeEncodeError("ascii", "\ud800", 0, 1, "ouch")),
-            ("\\ud800", 1)
-        )
-        self.assertEqual(
-            codecs.backslashreplace_errors(
-                UnicodeEncodeError("ascii", "\udfff", 0, 1, "ouch")),
-            ("\\udfff", 1)
-        )
+        if SIZEOF_WCHAR_T > 0:
+            self.assertEqual(
+                codecs.backslashreplace_errors(
+                    UnicodeEncodeError("ascii", "\U00010000",
+                                       0, len_wide, "ouch")),
+                ("\\U00010000", len_wide)
+            )
+            self.assertEqual(
+                codecs.backslashreplace_errors(
+                    UnicodeEncodeError("ascii", "\U0010ffff",
+                                       0, len_wide, "ouch")),
+                ("\\U0010ffff", len_wide)
+            )
+            # Lone surrogates (regardless of unicode width)
+            self.assertEqual(
+                codecs.backslashreplace_errors(
+                    UnicodeEncodeError("ascii", "\ud800", 0, 1, "ouch")),
+                ("\\ud800", 1)
+            )
+            self.assertEqual(
+                codecs.backslashreplace_errors(
+                    UnicodeEncodeError("ascii", "\udfff", 0, 1, "ouch")),
+                ("\\udfff", 1)
+            )
 
     def test_badhandlerresults(self):
         results = ( 42, "foo", (1,2,3), ("foo", 1, 3), ("foo", None), ("foo",), ("foo", 1, 3), ("foo", None), ("foo",) )
index f70ae3364a193f30b315019fbe97800d9f7d7ea4..e9ce95a866672735735074c77eacea3182ab0bf9 100644 (file)
@@ -3,9 +3,14 @@ import unittest
 import codecs
 import locale
 import sys, _testcapi, io
-import ctypes
 
-SIZEOF_WCHAR_T = ctypes.sizeof(ctypes.c_wchar)
+try:
+    import ctypes
+except ImportError:
+    ctypes = None
+    SIZEOF_WCHAR_T = -1
+else:
+    SIZEOF_WCHAR_T = ctypes.sizeof(ctypes.c_wchar)
 
 class Queue(object):
     """