[pypy-commit] cffi default: Clarify the error we get if we try to use TCHAR & friends before calling

arigo noreply at buildbot.pypy.org
Wed Jan 7 22:14:44 CET 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r1623:138fea1cf4bd
Date: 2015-01-07 22:15 +0100
http://bitbucket.org/cffi/cffi/changeset/138fea1cf4bd/

Log:	Clarify the error we get if we try to use TCHAR & friends before
	calling ffi.set_unicode().

diff --git a/cffi/api.py b/cffi/api.py
--- a/cffi/api.py
+++ b/cffi/api.py
@@ -420,6 +420,7 @@
         """
         if self._windows_unicode is not None:
             raise ValueError("set_unicode() can only be called once")
+        enabled_flag = bool(enabled_flag)
         if enabled_flag:
             self.cdef("typedef wchar_t TBYTE;"
                       "typedef wchar_t TCHAR;"
diff --git a/cffi/commontypes.py b/cffi/commontypes.py
--- a/cffi/commontypes.py
+++ b/cffi/commontypes.py
@@ -29,6 +29,9 @@
                 result = model.PointerType(resolve_common_type(result[:-2]))
         elif result in model.PrimitiveType.ALL_PRIMITIVE_TYPES:
             result = model.PrimitiveType(result)
+        elif result == 'set-unicode-needed':
+            raise api.FFIError("The Windows type %r is only available after "
+                               "you call ffi.set_unicode()" % (commontype,))
         else:
             if commontype == result:
                 raise api.FFIError("Unsupported type: %r.  Please file a bug "
@@ -232,6 +235,15 @@
         "USN": "LONGLONG",
         "VOID": model.void_type,
         "WPARAM": "UINT_PTR",
+
+        "TBYTE": "set-unicode-needed",
+        "TCHAR": "set-unicode-needed",
+        "LPCTSTR": "set-unicode-needed",
+        "PCTSTR": "set-unicode-needed",
+        "LPTSTR": "set-unicode-needed",
+        "PTSTR": "set-unicode-needed",
+        "PTBYTE": "set-unicode-needed",
+        "PTCHAR;": "set-unicode-needed",
         })
     return result
 
diff --git a/doc/source/index.rst b/doc/source/index.rst
--- a/doc/source/index.rst
+++ b/doc/source/index.rst
@@ -1381,7 +1381,8 @@
 declare the types ``TBYTE TCHAR LPCTSTR PCTSTR LPTSTR PTSTR PTBYTE
 PTCHAR`` to be (pointers to) ``wchar_t``.  If ``enabled_flag`` is
 False, declare these types to be (pointers to) plain 8-bit characters.
-*New in version 0.9.*
+(These types are not predeclared at all if you don't call
+``set_unicode()``.)  *New in version 0.9.*
 
 The reason behind this method is that a lot of standard functions have
 two versions, like ``MessageBoxA()`` and ``MessageBoxW()``.  The
diff --git a/testing/test_verify.py b/testing/test_verify.py
--- a/testing/test_verify.py
+++ b/testing/test_verify.py
@@ -1,6 +1,6 @@
 import py, re
 import sys, os, math, weakref
-from cffi import FFI, VerificationError, VerificationMissing, model
+from cffi import FFI, VerificationError, VerificationMissing, model, FFIError
 from testing.support import *
 
 
@@ -2096,6 +2096,10 @@
 def test_implicit_unicode_on_windows():
     if sys.platform != 'win32':
         py.test.skip("win32-only test")
+    ffi = FFI()
+    e = py.test.raises(FFIError, ffi.cdef, "int foo(LPTSTR);")
+    assert str(e.value) == ("The Windows type 'LPTSTR' is only available after"
+                            " you call ffi.set_unicode()")
     for with_unicode in [True, False]:
         ffi = FFI()
         ffi.set_unicode(with_unicode)


More information about the pypy-commit mailing list