[Python-checkins] cpython: Fix text failures when ctypes is not available

antoine.pitrou python-checkins at python.org
Wed Oct 5 13:05:18 CEST 2011


http://hg.python.org/cpython/rev/da3bfa2e9daa
changeset:   72683:da3bfa2e9daa
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Wed Oct 05 13:01:41 2011 +0200
summary:
  Fix text failures when ctypes is not available
(followup to Victor's 85d11cf67aa8 and 7a50e549bd11)

files:
  Lib/test/test_codeccallbacks.py |  58 +++++++++++---------
  Lib/test/test_codecs.py         |   9 ++-
  2 files changed, 39 insertions(+), 28 deletions(-)


diff --git a/Lib/test/test_codeccallbacks.py b/Lib/test/test_codeccallbacks.py
--- a/Lib/test/test_codeccallbacks.py
+++ b/Lib/test/test_codeccallbacks.py
@@ -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 @@
                 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",) )
diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -3,9 +3,14 @@
 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):
     """

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list