mirror of
https://github.com/python/cpython.git
synced 2025-12-15 21:44:50 +00:00
Patch #1318 by Amaury Forgeot d'Arc.
Updates to ctypes for python 3.0 to make the tests pass.
Notable changes are:
- return bytes instead of str8
- integers in range(256) are accepted as "one char string":
libc.strchr("abcdef", 98) is now valid.
- directly use the wide-char version of the win32 function LoadLibrary.
This commit is contained in:
parent
b98cda44b7
commit
97f9d4f312
14 changed files with 106 additions and 108 deletions
|
|
@ -763,7 +763,7 @@ CharArray_set_raw(CDataObject *self, PyObject *value)
|
|||
static PyObject *
|
||||
CharArray_get_raw(CDataObject *self)
|
||||
{
|
||||
return PyString_FromStringAndSize(self->b_ptr, self->b_size);
|
||||
return PyBytes_FromStringAndSize(self->b_ptr, self->b_size);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
|
|
@ -774,7 +774,7 @@ CharArray_get_value(CDataObject *self)
|
|||
for (i = 0; i < self->b_size; ++i)
|
||||
if (*ptr++ == '\0')
|
||||
break;
|
||||
return PyString_FromStringAndSize(self->b_ptr, i);
|
||||
return PyBytes_FromStringAndSize(self->b_ptr, i);
|
||||
}
|
||||
|
||||
static int
|
||||
|
|
@ -1251,7 +1251,7 @@ c_void_p_from_param(PyObject *type, PyObject *value)
|
|||
}
|
||||
/* XXX struni: remove later */
|
||||
/* string */
|
||||
if (PyString_Check(value)) {
|
||||
if (PyBytes_Check(value)) {
|
||||
PyCArgObject *parg;
|
||||
struct fielddesc *fd = getentry("z");
|
||||
|
||||
|
|
@ -1452,7 +1452,7 @@ SimpleType_paramfunc(CDataObject *self)
|
|||
|
||||
dict = PyObject_stgdict((PyObject *)self);
|
||||
assert(dict); /* Cannot be NULL for CDataObject instances */
|
||||
fmt = PyString_AsString(dict->proto);
|
||||
fmt = PyUnicode_AsString(dict->proto);
|
||||
assert(fmt);
|
||||
|
||||
fd = getentry(fmt);
|
||||
|
|
@ -1644,7 +1644,7 @@ SimpleType_from_param(PyObject *type, PyObject *value)
|
|||
assert(dict);
|
||||
|
||||
/* I think we can rely on this being a one-character string */
|
||||
fmt = PyString_AsString(dict->proto);
|
||||
fmt = PyUnicode_AsString(dict->proto);
|
||||
assert(fmt);
|
||||
|
||||
fd = getentry(fmt);
|
||||
|
|
@ -2667,7 +2667,7 @@ _validate_paramflags(PyTypeObject *type, PyObject *paramflags)
|
|||
char *name;
|
||||
PyObject *defval;
|
||||
PyObject *typ;
|
||||
if (!PyArg_ParseTuple(item, "i|zO", &flag, &name, &defval)) {
|
||||
if (!PyArg_ParseTuple(item, "i|ZO", &flag, &name, &defval)) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"paramflags must be a sequence of (int [,string [,value]]) tuples");
|
||||
return 0;
|
||||
|
|
@ -2705,8 +2705,12 @@ _get_name(PyObject *obj, char **pname)
|
|||
return 1;
|
||||
}
|
||||
#endif
|
||||
if (PyString_Check(obj) || PyUnicode_Check(obj)) {
|
||||
*pname = PyString_AsString(obj);
|
||||
if (PyBytes_Check(obj)) {
|
||||
*pname = PyBytes_AS_STRING(obj);
|
||||
return *pname ? 1 : 0;
|
||||
}
|
||||
if (PyUnicode_Check(obj)) {
|
||||
*pname = PyUnicode_AsString(obj);
|
||||
return *pname ? 1 : 0;
|
||||
}
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
|
|
@ -2966,7 +2970,7 @@ _byref(PyObject *obj)
|
|||
}
|
||||
|
||||
static PyObject *
|
||||
_get_arg(int *pindex, char *name, PyObject *defval, PyObject *inargs, PyObject *kwds)
|
||||
_get_arg(int *pindex, PyObject *name, PyObject *defval, PyObject *inargs, PyObject *kwds)
|
||||
{
|
||||
PyObject *v;
|
||||
|
||||
|
|
@ -2976,7 +2980,7 @@ _get_arg(int *pindex, char *name, PyObject *defval, PyObject *inargs, PyObject *
|
|||
Py_INCREF(v);
|
||||
return v;
|
||||
}
|
||||
if (kwds && (v = PyDict_GetItemString(kwds, name))) {
|
||||
if (kwds && (v = PyDict_GetItem(kwds, name))) {
|
||||
++*pindex;
|
||||
Py_INCREF(v);
|
||||
return v;
|
||||
|
|
@ -3057,15 +3061,15 @@ _build_callargs(CFuncPtrObject *self, PyObject *argtypes,
|
|||
PyObject *item = PyTuple_GET_ITEM(paramflags, i);
|
||||
PyObject *ob;
|
||||
int flag;
|
||||
char *name = NULL;
|
||||
PyObject *name = NULL;
|
||||
PyObject *defval = NULL;
|
||||
|
||||
/* This way seems to be ~2 us faster than the PyArg_ParseTuple
|
||||
calls below. */
|
||||
/* We HAVE already checked that the tuple can be parsed with "i|zO", so... */
|
||||
/* We HAVE already checked that the tuple can be parsed with "i|ZO", so... */
|
||||
Py_ssize_t tsize = PyTuple_GET_SIZE(item);
|
||||
flag = PyInt_AS_LONG(PyTuple_GET_ITEM(item, 0));
|
||||
name = tsize > 1 ? PyString_AS_STRING(PyTuple_GET_ITEM(item, 1)) : NULL;
|
||||
name = tsize > 1 ? PyTuple_GET_ITEM(item, 1) : NULL;
|
||||
defval = tsize > 2 ? PyTuple_GET_ITEM(item, 2) : NULL;
|
||||
|
||||
switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) {
|
||||
|
|
@ -3730,10 +3734,10 @@ Array_subscript(PyObject *_self, PyObject *item)
|
|||
char *dest;
|
||||
|
||||
if (slicelen <= 0)
|
||||
return PyString_FromString("");
|
||||
return PyBytes_FromStringAndSize("", 0);
|
||||
if (step == 1) {
|
||||
return PyString_FromStringAndSize(ptr + start,
|
||||
slicelen);
|
||||
return PyBytes_FromStringAndSize(ptr + start,
|
||||
slicelen);
|
||||
}
|
||||
dest = (char *)PyMem_Malloc(slicelen);
|
||||
|
||||
|
|
@ -3745,7 +3749,7 @@ Array_subscript(PyObject *_self, PyObject *item)
|
|||
dest[i] = ptr[cur];
|
||||
}
|
||||
|
||||
np = PyString_FromStringAndSize(dest, slicelen);
|
||||
np = PyBytes_FromStringAndSize(dest, slicelen);
|
||||
PyMem_Free(dest);
|
||||
return np;
|
||||
}
|
||||
|
|
@ -4407,10 +4411,10 @@ Pointer_subscript(PyObject *_self, PyObject *item)
|
|||
char *dest;
|
||||
|
||||
if (len <= 0)
|
||||
return PyString_FromString("");
|
||||
return PyBytes_FromStringAndSize("", 0);
|
||||
if (step == 1) {
|
||||
return PyString_FromStringAndSize(ptr + start,
|
||||
len);
|
||||
return PyBytes_FromStringAndSize(ptr + start,
|
||||
len);
|
||||
}
|
||||
dest = (char *)PyMem_Malloc(len);
|
||||
if (dest == NULL)
|
||||
|
|
@ -4418,7 +4422,7 @@ Pointer_subscript(PyObject *_self, PyObject *item)
|
|||
for (cur = start, i = 0; i < len; cur += step, i++) {
|
||||
dest[i] = ptr[cur];
|
||||
}
|
||||
np = PyString_FromStringAndSize(dest, len);
|
||||
np = PyBytes_FromStringAndSize(dest, len);
|
||||
PyMem_Free(dest);
|
||||
return np;
|
||||
}
|
||||
|
|
@ -4629,7 +4633,7 @@ create_comerror(void)
|
|||
++methods;
|
||||
}
|
||||
|
||||
s = PyString_FromString(comerror_doc);
|
||||
s = PyUnicode_FromString(comerror_doc);
|
||||
if (s == NULL)
|
||||
return -1;
|
||||
status = PyDict_SetItemString(dict, "__doc__", s);
|
||||
|
|
@ -4654,8 +4658,8 @@ static PyObject *
|
|||
string_at(const char *ptr, int size)
|
||||
{
|
||||
if (size == -1)
|
||||
return PyString_FromString(ptr);
|
||||
return PyString_FromStringAndSize(ptr, size);
|
||||
return PyBytes_FromStringAndSize(ptr, strlen(ptr));
|
||||
return PyBytes_FromStringAndSize(ptr, size);
|
||||
}
|
||||
|
||||
static int
|
||||
|
|
|
|||
|
|
@ -365,7 +365,7 @@ long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
|
|||
static PyObject *context;
|
||||
|
||||
if (context == NULL)
|
||||
context = PyString_FromString("_ctypes.DllGetClassObject");
|
||||
context = PyUnicode_FromString("_ctypes.DllGetClassObject");
|
||||
|
||||
mod = PyImport_ImportModule("ctypes");
|
||||
if (!mod) {
|
||||
|
|
@ -444,7 +444,7 @@ long Call_CanUnloadNow(void)
|
|||
static PyObject *context;
|
||||
|
||||
if (context == NULL)
|
||||
context = PyString_FromString("_ctypes.DllCanUnloadNow");
|
||||
context = PyUnicode_FromString("_ctypes.DllCanUnloadNow");
|
||||
|
||||
mod = PyImport_ImportModule("ctypes");
|
||||
if (!mod) {
|
||||
|
|
|
|||
|
|
@ -770,7 +770,7 @@ void Extend_Error_Info(PyObject *exc_class, char *fmt, ...)
|
|||
PyObject *tp, *v, *tb, *s, *cls_str, *msg_str;
|
||||
|
||||
va_start(vargs, fmt);
|
||||
s = PyString_FromFormatV(fmt, vargs);
|
||||
s = PyUnicode_FromFormatV(fmt, vargs);
|
||||
va_end(vargs);
|
||||
if (!s)
|
||||
return;
|
||||
|
|
@ -779,18 +779,18 @@ void Extend_Error_Info(PyObject *exc_class, char *fmt, ...)
|
|||
PyErr_NormalizeException(&tp, &v, &tb);
|
||||
cls_str = PyObject_Str(tp);
|
||||
if (cls_str) {
|
||||
PyString_ConcatAndDel(&s, cls_str);
|
||||
PyString_ConcatAndDel(&s, PyString_FromString(": "));
|
||||
PyUnicode_AppendAndDel(&s, cls_str);
|
||||
PyUnicode_AppendAndDel(&s, PyUnicode_FromString(": "));
|
||||
if (s == NULL)
|
||||
goto error;
|
||||
} else
|
||||
PyErr_Clear();
|
||||
msg_str = PyObject_Str(v);
|
||||
msg_str = PyObject_Unicode(v);
|
||||
if (msg_str)
|
||||
PyString_ConcatAndDel(&s, msg_str);
|
||||
PyUnicode_AppendAndDel(&s, msg_str);
|
||||
else {
|
||||
PyErr_Clear();
|
||||
PyString_ConcatAndDel(&s, PyString_FromString("???"));
|
||||
PyUnicode_AppendAndDel(&s, PyUnicode_FromString("???"));
|
||||
if (s == NULL)
|
||||
goto error;
|
||||
}
|
||||
|
|
@ -1087,34 +1087,18 @@ The handle may be used to locate exported functions in this\n\
|
|||
module.\n";
|
||||
static PyObject *load_library(PyObject *self, PyObject *args)
|
||||
{
|
||||
TCHAR *name;
|
||||
WCHAR *name;
|
||||
PyObject *nameobj;
|
||||
PyObject *ignored;
|
||||
HMODULE hMod;
|
||||
if (!PyArg_ParseTuple(args, "O|O:LoadLibrary", &nameobj, &ignored))
|
||||
return NULL;
|
||||
#ifdef _UNICODE
|
||||
name = alloca((PyString_Size(nameobj) + 1) * sizeof(WCHAR));
|
||||
if (!name) {
|
||||
PyErr_NoMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
{
|
||||
int r;
|
||||
char *aname = PyString_AsString(nameobj);
|
||||
if(!aname)
|
||||
return NULL;
|
||||
r = MultiByteToWideChar(CP_ACP, 0, aname, -1, name, PyString_Size(nameobj) + 1);
|
||||
name[r] = 0;
|
||||
}
|
||||
#else
|
||||
name = PyString_AsString(nameobj);
|
||||
if(!name)
|
||||
name = PyUnicode_AsUnicode(nameobj);
|
||||
if (!name)
|
||||
return NULL;
|
||||
#endif
|
||||
|
||||
hMod = LoadLibrary(name);
|
||||
hMod = LoadLibraryW(name);
|
||||
if (!hMod)
|
||||
return PyErr_SetFromWindowsErr(GetLastError());
|
||||
#ifdef _WIN64
|
||||
|
|
|
|||
|
|
@ -1169,6 +1169,14 @@ c_set(void *ptr, PyObject *value, Py_ssize_t size)
|
|||
*(char *)ptr = PyBytes_AsString(value)[0];
|
||||
_RET(value);
|
||||
}
|
||||
if (PyInt_Check(value))
|
||||
{
|
||||
long longval = PyInt_AS_LONG(value);
|
||||
if (longval < 0 || longval >= 256)
|
||||
goto error;
|
||||
*(char *)ptr = (char)longval;
|
||||
_RET(value);
|
||||
}
|
||||
error:
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"one character string expected");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue