import sys
import unittest
import test.support
+import ctypes
from ctypes import (CDLL, PyDLL, ArgumentError,
Structure, Array, Union,
_Pointer, _SimpleCData, _CFuncPtr,
self.assertRegex(repr(c_char_p.from_param(b'hihi')), r"^<cparam 'z' \(0x[A-Fa-f0-9]+\)>$")
self.assertRegex(repr(c_wchar_p.from_param('hihi')), r"^<cparam 'Z' \(0x[A-Fa-f0-9]+\)>$")
self.assertRegex(repr(c_void_p.from_param(0x12)), r"^<cparam 'P' \(0x0*12\)>$")
+ if hasattr(ctypes, 'c_double_complex'):
+ self.assertRegex(repr(ctypes.c_double_complex.from_param(0)),
+ r"^<cparam 'Zd' at 0x[A-Fa-f0-9]+>$")
+ self.assertRegex(repr(ctypes.c_float_complex.from_param(0)),
+ r"^<cparam 'Zf' at 0x[A-Fa-f0-9]+>$")
+ self.assertRegex(repr(ctypes.c_longdouble_complex.from_param(0)),
+ r"^<cparam 'Zg' at 0x[A-Fa-f0-9]+>$")
@test.support.cpython_only
def test_from_param_result_refcount(self):
}
assert(stginfo); /* Cannot be NULL for structure/union instances */
- parg->tag = 'V';
+ parg->tag = "V";
parg->pffi_type = &stginfo->ffi_type_pointer;
parg->value.p = ptr;
parg->size = self->b_size;
if (parg == NULL)
return NULL;
- parg->tag = 'P';
+ parg->tag = "P";
parg->pffi_type = &ffi_type_pointer;
parg->obj = Py_NewRef(self);
parg->value.p = *(void **)self->b_ptr;
PyCArgObject *p = PyCArgObject_new(st);
if (p == NULL)
return NULL;
- p->tag = 'P';
+ p->tag = "P";
p->pffi_type = &ffi_type_pointer;
p->value.p = (char *)self->b_ptr;
p->obj = Py_NewRef(self);
if (parg == NULL)
return NULL;
parg->pffi_type = &ffi_type_pointer;
- parg->tag = 'Z';
+ parg->tag = "Z";
parg->obj = fd->setfunc(&parg->value, value, 0);
if (parg->obj == NULL) {
Py_DECREF(parg);
if (parg == NULL)
return NULL;
parg->pffi_type = &ffi_type_pointer;
- parg->tag = 'z';
+ parg->tag = "z";
parg->obj = fd->setfunc(&parg->value, value, 0);
if (parg->obj == NULL) {
Py_DECREF(parg);
if (parg == NULL)
return NULL;
parg->pffi_type = &ffi_type_pointer;
- parg->tag = 'P';
+ parg->tag = "P";
parg->obj = fd->setfunc(&parg->value, value, sizeof(void*));
if (parg->obj == NULL) {
Py_DECREF(parg);
if (parg == NULL)
return NULL;
parg->pffi_type = &ffi_type_pointer;
- parg->tag = 'z';
+ parg->tag = "z";
parg->obj = fd->setfunc(&parg->value, value, 0);
if (parg->obj == NULL) {
Py_DECREF(parg);
if (parg == NULL)
return NULL;
parg->pffi_type = &ffi_type_pointer;
- parg->tag = 'Z';
+ parg->tag = "Z";
parg->obj = fd->setfunc(&parg->value, value, 0);
if (parg->obj == NULL) {
Py_DECREF(parg);
if (PyCArg_CheckExact(st, value)) {
/* byref(c_xxx()) */
PyCArgObject *a = (PyCArgObject *)value;
- if (a->tag == 'P') {
+ if (strcmp(a->tag, "P") == 0) {
return Py_NewRef(value);
}
}
if (parg == NULL)
return NULL;
parg->pffi_type = &ffi_type_pointer;
- parg->tag = 'P';
+ parg->tag = "P";
Py_INCREF(value);
// Function pointers don't change their contents, no need to lock
parg->value.p = *(void **)func->b_ptr;
if (parg == NULL)
return NULL;
parg->pffi_type = &ffi_type_pointer;
- parg->tag = 'Z';
+ parg->tag = "Z";
parg->obj = Py_NewRef(value);
/* Remember: b_ptr points to where the pointer is stored! */
Py_BEGIN_CRITICAL_SECTION(value);
if (parg == NULL)
return NULL;
- parg->tag = fmt[0];
+ assert(strcmp(fd->code, fmt) == 0);
+ parg->tag = fd->code;
parg->pffi_type = fd->pffi_type;
parg->obj = Py_NewRef(self);
memcpy(&parg->value, self->b_ptr, self->b_size);
if (parg == NULL)
return NULL;
- parg->tag = fmt[0];
+ assert(strcmp(fd->code, fmt) == 0);
+ parg->tag = fd->code;
parg->pffi_type = fd->pffi_type;
parg->obj = fd->setfunc(&parg->value, value, info->size);
if (parg->obj)
if (parg == NULL)
return NULL;
- parg->tag = 'P';
+ parg->tag = "P";
parg->pffi_type = &ffi_type_pointer;
parg->obj = Py_NewRef(self);
parg->value.p = *(void **)self->b_ptr;
return NULL;
}
- parg->tag = 'P';
+ parg->tag = "P";
parg->pffi_type = &ffi_type_pointer;
parg->obj = obj;
parg->value.p = ((CDataObject *)obj)->b_ptr;
if (p == NULL)
return NULL;
p->pffi_type = NULL;
- p->tag = '\0';
+ p->tag = "";
p->obj = NULL;
memset(&p->value, 0, sizeof(p->value));
PyObject_GC_Track(p);
PyCArg_repr(PyObject *op)
{
PyCArgObject *self = _PyCArgObject_CAST(op);
- switch(self->tag) {
+
+ if (strlen(self->tag) != 1) {
+ goto generic;
+ }
+
+ switch(self->tag[0]) {
case 'b':
case 'B':
- return PyUnicode_FromFormat("<cparam '%c' (%d)>",
+ return PyUnicode_FromFormat("<cparam '%s' (%d)>",
self->tag, self->value.b);
case 'h':
case 'H':
- return PyUnicode_FromFormat("<cparam '%c' (%d)>",
+ return PyUnicode_FromFormat("<cparam '%s' (%d)>",
self->tag, self->value.h);
case 'i':
case 'I':
- return PyUnicode_FromFormat("<cparam '%c' (%d)>",
+ return PyUnicode_FromFormat("<cparam '%s' (%d)>",
self->tag, self->value.i);
case 'l':
case 'L':
- return PyUnicode_FromFormat("<cparam '%c' (%ld)>",
+ return PyUnicode_FromFormat("<cparam '%s' (%ld)>",
self->tag, self->value.l);
case 'q':
case 'Q':
- return PyUnicode_FromFormat("<cparam '%c' (%lld)>",
+ return PyUnicode_FromFormat("<cparam '%s' (%lld)>",
self->tag, self->value.q);
case 'd':
case 'f': {
- PyObject *f = PyFloat_FromDouble((self->tag == 'f') ? self->value.f : self->value.d);
+ PyObject *f = PyFloat_FromDouble((strcmp(self->tag, "f") == 0) ? self->value.f : self->value.d);
if (f == NULL) {
return NULL;
}
- PyObject *result = PyUnicode_FromFormat("<cparam '%c' (%R)>", self->tag, f);
+ PyObject *result = PyUnicode_FromFormat("<cparam '%s' (%R)>", self->tag, f);
Py_DECREF(f);
return result;
}
case 'c':
if (is_literal_char((unsigned char)self->value.c)) {
- return PyUnicode_FromFormat("<cparam '%c' ('%c')>",
+ return PyUnicode_FromFormat("<cparam '%s' ('%c')>",
self->tag, self->value.c);
}
else {
- return PyUnicode_FromFormat("<cparam '%c' ('\\x%02x')>",
+ return PyUnicode_FromFormat("<cparam '%s' ('\\x%02x')>",
self->tag, (unsigned char)self->value.c);
}
case 'z':
case 'Z':
case 'P':
- return PyUnicode_FromFormat("<cparam '%c' (%p)>",
+ return PyUnicode_FromFormat("<cparam '%s' (%p)>",
self->tag, self->value.p);
- break;
default:
- if (is_literal_char((unsigned char)self->tag)) {
- return PyUnicode_FromFormat("<cparam '%c' at %p>",
- (unsigned char)self->tag, (void *)self);
- }
- else {
- return PyUnicode_FromFormat("<cparam 0x%02x at %p>",
- (unsigned char)self->tag, (void *)self);
- }
+ break;
}
+
+generic:
+ return PyUnicode_FromFormat("<cparam '%s' at %p>",
+ self->tag, (void *)self);
}
static PyMemberDef PyCArgType_members[] = {
if (parg == NULL)
return NULL;
- parg->tag = 'P';
+ parg->tag = "P";
parg->pffi_type = &ffi_type_pointer;
parg->obj = Py_NewRef(obj);
parg->value.p = (char *)((CDataObject *)obj)->b_ptr + offset;
struct tagPyCArgObject {
PyObject_HEAD
ffi_type *pffi_type;
- char tag;
+ const char *tag;
union {
char c;
char b;
long double G[2];
} value;
PyObject *obj;
- Py_ssize_t size; /* for the 'V' tag */
+ Py_ssize_t size; /* for the "V" tag */
};
#define _PyCArgObject_CAST(op) ((PyCArgObject *)(op))