]> granicus.if.org Git - python/commitdiff
Accept bytes as parameter to foreign functions without prototype.
authorThomas Heller <theller@ctypes.org>
Thu, 12 Jul 2007 19:19:43 +0000 (19:19 +0000)
committerThomas Heller <theller@ctypes.org>
Thu, 12 Jul 2007 19:19:43 +0000 (19:19 +0000)
These are passed as byte strings (unicode strings are passed as wide
character strings).

Lib/ctypes/test/test_slicing.py
Modules/_ctypes/callproc.c

index 3db82c3aeceddf1bbfbe188957b77fc32ba17058..ab01c1802839a7049151515af20c2ff3df7bc045 100644 (file)
@@ -37,7 +37,7 @@ class SlicesTestCase(unittest.TestCase):
     from ctypes.test import is_resource_enabled
     if is_resource_enabled("struni-crash"):
         def test_char_ptr(self):
-            s = "abcdefghijklmnopqrstuvwxyz"
+            s = b"abcdefghijklmnopqrstuvwxyz"
 
             dll = CDLL(_ctypes_test.__file__)
             dll.my_strdup.restype = POINTER(c_char)
@@ -57,7 +57,7 @@ class SlicesTestCase(unittest.TestCase):
 
         def test_char_ptr_with_free(self):
             dll = CDLL(_ctypes_test.__file__)
-            s = "abcdefghijklmnopqrstuvwxyz"
+            s = b"abcdefghijklmnopqrstuvwxyz"
 
             class allocated_c_char_p(c_char_p):
                 pass
index 23150b3f5789375d4abb43c02dfaad3e74d6747f..6380b1ab5fb4adc6e27cfeb7354807eb8ea2af61 100644 (file)
@@ -512,6 +512,7 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
                return 0;
        }
 
+       /* XXX struni remove later */
        if (PyString_Check(obj)) {
                pa->ffi_type = &ffi_type_pointer;
                pa->value.p = PyString_AS_STRING(obj);
@@ -520,6 +521,14 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
                return 0;
        }
 
+       if (PyBytes_Check(obj)) {
+               pa->ffi_type = &ffi_type_pointer;
+               pa->value.p = PyBytes_AsString(obj);
+               Py_INCREF(obj);
+               pa->keep = obj;
+               return 0;
+       }
+
 #ifdef CTYPES_UNICODE
        if (PyUnicode_Check(obj)) {
 #ifdef HAVE_USABLE_WCHAR_T