Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7775c71

Browse files
author
Thomas Heller
committed
Accept bytes as parameter to foreign functions without prototype.
These are passed as byte strings (unicode strings are passed as wide character strings).
1 parent 6083131 commit 7775c71

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

Lib/ctypes/test/test_slicing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_setslice_cint(self):
3737
from ctypes.test import is_resource_enabled
3838
if is_resource_enabled("struni-crash"):
3939
def test_char_ptr(self):
40-
s = "abcdefghijklmnopqrstuvwxyz"
40+
s = b"abcdefghijklmnopqrstuvwxyz"
4141

4242
dll = CDLL(_ctypes_test.__file__)
4343
dll.my_strdup.restype = POINTER(c_char)
@@ -57,7 +57,7 @@ def test_char_ptr(self):
5757

5858
def test_char_ptr_with_free(self):
5959
dll = CDLL(_ctypes_test.__file__)
60-
s = "abcdefghijklmnopqrstuvwxyz"
60+
s = b"abcdefghijklmnopqrstuvwxyz"
6161

6262
class allocated_c_char_p(c_char_p):
6363
pass

Modules/_ctypes/callproc.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
512512
return 0;
513513
}
514514

515+
/* XXX struni remove later */
515516
if (PyString_Check(obj)) {
516517
pa->ffi_type = &ffi_type_pointer;
517518
pa->value.p = PyString_AS_STRING(obj);
@@ -520,6 +521,14 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
520521
return 0;
521522
}
522523

524+
if (PyBytes_Check(obj)) {
525+
pa->ffi_type = &ffi_type_pointer;
526+
pa->value.p = PyBytes_AsString(obj);
527+
Py_INCREF(obj);
528+
pa->keep = obj;
529+
return 0;
530+
}
531+
523532
#ifdef CTYPES_UNICODE
524533
if (PyUnicode_Check(obj)) {
525534
#ifdef HAVE_USABLE_WCHAR_T

0 commit comments

Comments
 (0)