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

Skip to content

Commit 27384da

Browse files
author
Thomas Heller
committed
Do not accept str8 type in function calls any longer.
Accept bytes instead of str8 in the (unexposed in ctypes) BSTR type.
1 parent ace0505 commit 27384da

3 files changed

Lines changed: 11 additions & 10 deletions

File tree

Lib/ctypes/test/test_bytes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Test where byte objects are accepted"""
22
import unittest
3+
import sys
34
from ctypes import *
45

56
class BytesTest(unittest.TestCase):
@@ -37,5 +38,14 @@ class X(Structure):
3738
X("abc")
3839
X(b"abc")
3940

41+
if sys.platform == "win32":
42+
def test_BSTR(self):
43+
from _ctypes import _SimpleCData
44+
class BSTR(_SimpleCData):
45+
_type_ = "X"
46+
47+
BSTR("abc")
48+
BSTR(b"abc")
49+
4050
if __name__ == '__main__':
4151
unittest.main()

Modules/_ctypes/callproc.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -507,15 +507,6 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
507507
return 0;
508508
}
509509

510-
/* XXX struni remove later */
511-
if (PyString_Check(obj)) {
512-
pa->ffi_type = &ffi_type_pointer;
513-
pa->value.p = PyString_AS_STRING(obj);
514-
Py_INCREF(obj);
515-
pa->keep = obj;
516-
return 0;
517-
}
518-
519510
if (PyBytes_Check(obj)) {
520511
pa->ffi_type = &ffi_type_pointer;
521512
pa->value.p = PyBytes_AsString(obj);

Modules/_ctypes/cfield.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,7 @@ BSTR_set(void *ptr, PyObject *value, Py_ssize_t size)
15011501
/* convert value into a PyUnicodeObject or NULL */
15021502
if (Py_None == value) {
15031503
value = NULL;
1504-
} else if (PyString_Check(value)) {
1504+
} else if (PyBytes_Check(value)) {
15051505
value = PyUnicode_FromEncodedObject(value,
15061506
conversion_mode_encoding,
15071507
conversion_mode_errors);

0 commit comments

Comments
 (0)