PyDict_GetItemProxy(PyObject *dict, PyObject *key)
{
PyObject *result;
- PyObject *item = PyDict_GetItem(dict, key);
+ PyObject *item = PyDict_GetItemWithError(dict, key);
if (item == NULL)
return NULL;
PyTypeObject *result;
PyObject *fields;
StgDictObject *dict;
+ _Py_IDENTIFIER(_abstract_);
+ _Py_IDENTIFIER(_fields_);
/* create the new instance (which is a class,
since we are a metatype!) */
return NULL;
/* keep this for bw compatibility */
- if (PyDict_GetItemString(result->tp_dict, "_abstract_"))
+ if (_PyDict_GetItemIdWithError(result->tp_dict, &PyId__abstract_))
return (PyObject *)result;
+ if (PyErr_Occurred()) {
+ Py_DECREF(result);
+ return NULL;
+ }
dict = (StgDictObject *)_PyObject_CallNoArg((PyObject *)&PyCStgDict_Type);
if (!dict) {
dict->paramfunc = StructUnionType_paramfunc;
- fields = PyDict_GetItemString((PyObject *)dict, "_fields_");
- if (!fields) {
+ fields = _PyDict_GetItemIdWithError((PyObject *)dict, &PyId__fields_);
+ if (fields) {
+ if (_PyObject_SetAttrId((PyObject *)result, &PyId__fields_, fields) < 0) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ return (PyObject *)result;
+ }
+ else if (PyErr_Occurred()) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ else {
StgDictObject *basedict = PyType_stgdict((PyObject *)result->tp_base);
if (basedict == NULL)
basedict->flags |= DICTFLAG_FINAL; /* set the 'final' flag in the baseclass dict */
return (PyObject *)result;
}
-
- if (-1 == PyObject_SetAttrString((PyObject *)result, "_fields_", fields)) {
- Py_DECREF(result);
- return NULL;
- }
- return (PyObject *)result;
}
static PyObject *
static PyObject *
CDataType_from_param(PyObject *type, PyObject *value)
{
+ _Py_IDENTIFIER(_as_parameter_);
PyObject *as_parameter;
int res = PyObject_IsInstance(value, type);
if (res == -1)
return NULL;
}
- as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
+ if (_PyObject_LookupAttrId(value, &PyId__as_parameter_, &as_parameter) < 0) {
+ return NULL;
+ }
if (as_parameter) {
value = CDataType_from_param(type, as_parameter);
Py_DECREF(as_parameter);
StgDictObject *stgdict;
PyObject *proto;
PyObject *typedict;
+ _Py_IDENTIFIER(_type_);
typedict = PyTuple_GetItem(args, 2);
if (!typedict)
stgdict->paramfunc = PyCPointerType_paramfunc;
stgdict->flags |= TYPEFLAG_ISPOINTER;
- proto = PyDict_GetItemString(typedict, "_type_"); /* Borrowed ref */
- if (proto && -1 == PyCPointerType_SetProto(stgdict, proto)) {
- Py_DECREF((PyObject *)stgdict);
- return NULL;
- }
-
+ proto = _PyDict_GetItemIdWithError(typedict, &PyId__type_); /* Borrowed ref */
if (proto) {
- StgDictObject *itemdict = PyType_stgdict(proto);
+ StgDictObject *itemdict;
const char *current_format;
+ if (-1 == PyCPointerType_SetProto(stgdict, proto)) {
+ Py_DECREF((PyObject *)stgdict);
+ return NULL;
+ }
+ itemdict = PyType_stgdict(proto);
/* PyCPointerType_SetProto has verified proto has a stgdict. */
assert(itemdict);
/* If itemdict->format is NULL, then this is a pointer to an
return NULL;
}
}
+ else if (PyErr_Occurred()) {
+ Py_DECREF((PyObject *)stgdict);
+ return NULL;
+ }
/* create the new instance (which is a class,
since we are a metatype!) */
PyCPointerType_set_type(PyTypeObject *self, PyObject *type)
{
StgDictObject *dict;
+ _Py_IDENTIFIER(_type_);
dict = PyType_stgdict((PyObject *)self);
if (!dict) {
if (-1 == PyCPointerType_SetProto(dict, type))
return NULL;
- if (-1 == PyDict_SetItemString((PyObject *)dict, "_type_", type))
+ if (-1 == _PyDict_SetItemId((PyObject *)dict, &PyId__type_, type))
return NULL;
Py_RETURN_NONE;
static PyObject *
PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
+ _Py_IDENTIFIER(_length_);
+ _Py_IDENTIFIER(_type_);
PyTypeObject *result;
StgDictObject *stgdict;
StgDictObject *itemdict;
stgdict = NULL;
type_attr = NULL;
- length_attr = PyObject_GetAttrString((PyObject *)result, "_length_");
+ if (_PyObject_LookupAttrId((PyObject *)result, &PyId__length_, &length_attr) < 0) {
+ goto error;
+ }
if (!length_attr) {
- if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
- PyErr_SetString(PyExc_AttributeError,
- "class must define a '_length_' attribute");
- }
+ PyErr_SetString(PyExc_AttributeError,
+ "class must define a '_length_' attribute");
goto error;
}
goto error;
}
- type_attr = PyObject_GetAttrString((PyObject *)result, "_type_");
+ if (_PyObject_LookupAttrId((PyObject *)result, &PyId__type_, &type_attr) < 0) {
+ goto error;
+ }
if (!type_attr) {
PyErr_SetString(PyExc_AttributeError,
"class must define a '_type_' attribute");
static PyObject *
c_wchar_p_from_param(PyObject *type, PyObject *value)
{
+ _Py_IDENTIFIER(_as_parameter_);
PyObject *as_parameter;
int res;
if (value == Py_None) {
}
}
- as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
+ if (_PyObject_LookupAttrId(value, &PyId__as_parameter_, &as_parameter) < 0) {
+ return NULL;
+ }
if (as_parameter) {
value = c_wchar_p_from_param(type, as_parameter);
Py_DECREF(as_parameter);
static PyObject *
c_char_p_from_param(PyObject *type, PyObject *value)
{
+ _Py_IDENTIFIER(_as_parameter_);
PyObject *as_parameter;
int res;
if (value == Py_None) {
}
}
- as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
+ if (_PyObject_LookupAttrId(value, &PyId__as_parameter_, &as_parameter) < 0) {
+ return NULL;
+ }
if (as_parameter) {
value = c_char_p_from_param(type, as_parameter);
Py_DECREF(as_parameter);
static PyObject *
c_void_p_from_param(PyObject *type, PyObject *value)
{
+ _Py_IDENTIFIER(_as_parameter_);
StgDictObject *stgd;
PyObject *as_parameter;
int res;
}
}
- as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
+ if (_PyObject_LookupAttrId(value, &PyId__as_parameter_, &as_parameter) < 0) {
+ return NULL;
+ }
if (as_parameter) {
value = c_void_p_from_param(type, as_parameter);
Py_DECREF(as_parameter);
static PyObject *
PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
+ _Py_IDENTIFIER(_type_);
PyTypeObject *result;
StgDictObject *stgdict;
PyObject *proto;
if (result == NULL)
return NULL;
- proto = PyObject_GetAttrString((PyObject *)result, "_type_"); /* new ref */
+ if (_PyObject_LookupAttrId((PyObject *)result, &PyId__type_, &proto) < 0) {
+ return NULL;
+ }
if (!proto) {
PyErr_SetString(PyExc_AttributeError,
"class must define a '_type_' attribute");
error:
Py_XDECREF(proto);
- Py_XDECREF(result);
+ Py_DECREF(result);
return NULL;
}
if (PyUnicode_Check(proto)) {
static PyObject *
PyCSimpleType_from_param(PyObject *type, PyObject *value)
{
+ _Py_IDENTIFIER(_as_parameter_);
StgDictObject *dict;
const char *fmt;
PyCArgObject *parg;
PyErr_Clear();
Py_DECREF(parg);
- as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
+ if (_PyObject_LookupAttrId(value, &PyId__as_parameter_, &as_parameter) < 0) {
+ return NULL;
+ }
if (as_parameter) {
if (Py_EnterRecursiveCall("while processing _as_parameter_")) {
Py_DECREF(as_parameter);
static PyObject *
converters_from_argtypes(PyObject *ob)
{
+ _Py_IDENTIFIER(from_param);
PyObject *converters;
Py_ssize_t i;
Py_ssize_t nArgs;
*/
for (i = 0; i < nArgs; ++i) {
+ PyObject *cnv;
PyObject *tp = PyTuple_GET_ITEM(ob, i);
- PyObject *cnv = PyObject_GetAttrString(tp, "from_param");
- if (!cnv)
- goto argtypes_error_1;
+ if (_PyObject_LookupAttrId(tp, &PyId_from_param, &cnv) <= 0) {
+ Py_DECREF(converters);
+ Py_DECREF(ob);
+ if (!PyErr_Occurred()) {
+ PyErr_Format(PyExc_TypeError,
+ "item %zd in _argtypes_ has no from_param method",
+ i+1);
+ }
+ return NULL;
+ }
PyTuple_SET_ITEM(converters, i, cnv);
}
Py_DECREF(ob);
return converters;
-
- argtypes_error_1:
- Py_XDECREF(converters);
- Py_DECREF(ob);
- PyErr_Format(PyExc_TypeError,
- "item %zd in _argtypes_ has no from_param method",
- i+1);
- return NULL;
}
static int
{
PyObject *ob;
PyObject *converters = NULL;
+ _Py_IDENTIFIER(_flags_);
+ _Py_IDENTIFIER(_argtypes_);
+ _Py_IDENTIFIER(_restype_);
+ _Py_IDENTIFIER(_check_retval_);
stgdict->align = _ctypes_get_fielddesc("P")->pffi_type->alignment;
stgdict->length = 1;
stgdict->getfunc = NULL;
stgdict->ffi_type_pointer = ffi_type_pointer;
- ob = PyDict_GetItemString((PyObject *)stgdict, "_flags_");
+ ob = _PyDict_GetItemIdWithError((PyObject *)stgdict, &PyId__flags_);
if (!ob || !PyLong_Check(ob)) {
- PyErr_SetString(PyExc_TypeError,
- "class must define _flags_ which must be an integer");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_TypeError,
+ "class must define _flags_ which must be an integer");
+ }
return -1;
}
- stgdict->flags = PyLong_AS_LONG(ob) | TYPEFLAG_ISPOINTER;
+ stgdict->flags = PyLong_AsUnsignedLongMask(ob) | TYPEFLAG_ISPOINTER;
/* _argtypes_ is optional... */
- ob = PyDict_GetItemString((PyObject *)stgdict, "_argtypes_");
+ ob = _PyDict_GetItemIdWithError((PyObject *)stgdict, &PyId__argtypes_);
if (ob) {
converters = converters_from_argtypes(ob);
if (!converters)
- goto error;
+ return -1;
Py_INCREF(ob);
stgdict->argtypes = ob;
stgdict->converters = converters;
}
+ else if (PyErr_Occurred()) {
+ return -1;
+ }
- ob = PyDict_GetItemString((PyObject *)stgdict, "_restype_");
+ ob = _PyDict_GetItemIdWithError((PyObject *)stgdict, &PyId__restype_);
if (ob) {
if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) {
PyErr_SetString(PyExc_TypeError,
}
Py_INCREF(ob);
stgdict->restype = ob;
- stgdict->checker = PyObject_GetAttrString(ob, "_check_retval_");
- if (stgdict->checker == NULL)
- PyErr_Clear();
+ if (_PyObject_LookupAttrId(ob, &PyId__check_retval_,
+ &stgdict->checker) < 0)
+ {
+ return -1;
+ }
+ }
+ else if (PyErr_Occurred()) {
+ return -1;
}
/* XXX later, maybe.
- ob = PyDict_GetItemString((PyObject *)stgdict, "_errcheck_");
+ ob = _PyDict_GetItemIdWithError((PyObject *)stgdict, &PyId__errcheck_);
if (ob) {
if (!PyCallable_Check(ob)) {
PyErr_SetString(PyExc_TypeError,
Py_INCREF(ob);
stgdict->errcheck = ob;
}
+ else if (PyErr_Occurred()) {
+ return -1;
+ }
*/
return 0;
-
- error:
- Py_XDECREF(converters);
- return -1;
-
}
static PyCArgObject *
static int
PyCFuncPtr_set_restype(PyCFuncPtrObject *self, PyObject *ob, void *Py_UNUSED(ignored))
{
+ _Py_IDENTIFIER(_check_retval_);
+ PyObject *checker, *oldchecker;
if (ob == NULL) {
+ oldchecker = self->checker;
+ self->checker = NULL;
Py_CLEAR(self->restype);
- Py_CLEAR(self->checker);
+ Py_XDECREF(oldchecker);
return 0;
}
if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) {
"restype must be a type, a callable, or None");
return -1;
}
+ if (_PyObject_LookupAttrId(ob, &PyId__check_retval_, &checker) < 0) {
+ return -1;
+ }
+ oldchecker = self->checker;
+ self->checker = checker;
Py_INCREF(ob);
Py_XSETREF(self->restype, ob);
- Py_XSETREF(self->checker, PyObject_GetAttrString(ob, "_check_retval_"));
- if (self->checker == NULL)
- PyErr_Clear();
+ Py_XDECREF(oldchecker);
return 0;
}
like that.
*/
/*
- if (kwds && PyDict_GetItemString(kwds, "options")) {
+ if (kwds && _PyDict_GetItemIdWithError(kwds, &PyId_options)) {
...
}
+ else if (PyErr_Occurred()) {
+ return NULL;
+ }
*/
dict = PyType_stgdict((PyObject *)type);
Py_INCREF(v);
return v;
}
- if (kwds && name && (v = PyDict_GetItem(kwds, name))) {
- ++*pindex;
- Py_INCREF(v);
- return v;
+ if (kwds && name) {
+ v = PyDict_GetItemWithError(kwds, name);
+ if (v) {
+ ++*pindex;
+ Py_INCREF(v);
+ return v;
+ }
+ else if (PyErr_Occurred()) {
+ return NULL;
+ }
}
if (defval) {
Py_INCREF(defval);
for (i = 0; i < len; ++i) {
PyObject *item = PyTuple_GET_ITEM(paramflags, i);
PyObject *ob;
- int flag;
+ unsigned int flag;
PyObject *name = NULL;
PyObject *defval = NULL;
calls below. */
/* We HAVE already checked that the tuple can be parsed with "i|ZO", so... */
Py_ssize_t tsize = PyTuple_GET_SIZE(item);
- flag = PyLong_AS_LONG(PyTuple_GET_ITEM(item, 0));
+ flag = PyLong_AsUnsignedLongMask(PyTuple_GET_ITEM(item, 0));
name = tsize > 1 ? PyTuple_GET_ITEM(item, 1) : NULL;
defval = tsize > 2 ? PyTuple_GET_ITEM(item, 2) : NULL;
break;
default:
PyErr_Format(PyExc_ValueError,
- "paramflag %d not yet implemented", flag);
+ "paramflag %u not yet implemented", flag);
goto error;
break;
}
StgDictObject *dict;
PyObject *fields;
Py_ssize_t i;
+ _Py_IDENTIFIER(_fields_);
if (PyType_stgdict((PyObject *)type->tp_base)) {
index = _init_pos_args(self, type->tp_base,
}
dict = PyType_stgdict((PyObject *)type);
- fields = PyDict_GetItemString((PyObject *)dict, "_fields_");
- if (fields == NULL)
+ fields = _PyDict_GetItemIdWithError((PyObject *)dict, &PyId__fields_);
+ if (fields == NULL) {
+ if (PyErr_Occurred()) {
+ return -1;
+ }
return index;
+ }
for (i = 0;
i < dict->length && (i+index) < PyTuple_GET_SIZE(args);
return -1;
}
val = PyTuple_GET_ITEM(args, i + index);
- if (kwds && PyDict_GetItem(kwds, name)) {
- PyErr_Format(PyExc_TypeError,
- "duplicate values for field %R",
- name);
- Py_DECREF(pair);
- Py_DECREF(name);
- return -1;
+ if (kwds) {
+ if (PyDict_GetItemWithError(kwds, name)) {
+ PyErr_Format(PyExc_TypeError,
+ "duplicate values for field %R",
+ name);
+ Py_DECREF(pair);
+ Py_DECREF(name);
+ return -1;
+ }
+ else if (PyErr_Occurred()) {
+ Py_DECREF(pair);
+ Py_DECREF(name);
+ return -1;
+ }
}
res = PyObject_SetAttr(self, name, val);
Py_DECREF(key);
return result;
}
+ else if (PyErr_Occurred()) {
+ Py_DECREF(key);
+ return NULL;
+ }
if (!PyType_Check(itemtype)) {
PyErr_SetString(PyExc_TypeError,
static int
MakeAnonFields(PyObject *type)
{
+ _Py_IDENTIFIER(_anonymous_);
PyObject *anon;
PyObject *anon_names;
Py_ssize_t i;
- anon = PyObject_GetAttrString(type, "_anonymous_");
+ if (_PyObject_LookupAttrId(type, &PyId__anonymous_, &anon) < 0) {
+ return -1;
+ }
if (anon == NULL) {
- PyErr_Clear();
return 0;
}
anon_names = PySequence_Fast(anon, "_anonymous_ must be a sequence");
int
PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct)
{
+ _Py_IDENTIFIER(_swappedbytes_);
+ _Py_IDENTIFIER(_use_broken_old_ctypes_structure_semantics_);
+ _Py_IDENTIFIER(_pack_);
StgDictObject *stgdict, *basedict;
Py_ssize_t len, offset, size, align, i;
Py_ssize_t union_size, total_align;
Py_ssize_t field_size = 0;
int bitofs;
- PyObject *isPacked;
- int pack = 0;
+ PyObject *tmp;
+ int isPacked;
+ int pack;
Py_ssize_t ffi_ofs;
int big_endian;
if (fields == NULL)
return 0;
-#ifdef WORDS_BIGENDIAN
- big_endian = PyObject_HasAttrString(type, "_swappedbytes_") ? 0 : 1;
-#else
- big_endian = PyObject_HasAttrString(type, "_swappedbytes_") ? 1 : 0;
-#endif
+ if (_PyObject_LookupAttrId(type, &PyId__swappedbytes_, &tmp) < 0) {
+ return -1;
+ }
+ if (tmp) {
+ Py_DECREF(tmp);
+ big_endian = !PY_BIG_ENDIAN;
+ }
+ else {
+ big_endian = PY_BIG_ENDIAN;
+ }
- use_broken_old_ctypes_semantics = \
- PyObject_HasAttrString(type, "_use_broken_old_ctypes_structure_semantics_");
+ if (_PyObject_LookupAttrId(type,
+ &PyId__use_broken_old_ctypes_structure_semantics_, &tmp) < 0)
+ {
+ return -1;
+ }
+ if (tmp) {
+ Py_DECREF(tmp);
+ use_broken_old_ctypes_semantics = 1;
+ }
+ else {
+ use_broken_old_ctypes_semantics = 0;
+ }
- isPacked = PyObject_GetAttrString(type, "_pack_");
- if (isPacked) {
- pack = _PyLong_AsInt(isPacked);
- if (pack < 0 || PyErr_Occurred()) {
- Py_XDECREF(isPacked);
- PyErr_SetString(PyExc_ValueError,
- "_pack_ must be a non-negative integer");
+ if (_PyObject_LookupAttrId(type, &PyId__pack_, &tmp) < 0) {
+ return -1;
+ }
+ if (tmp) {
+ isPacked = 1;
+ pack = _PyLong_AsInt(tmp);
+ Py_DECREF(tmp);
+ if (pack < 0) {
+ if (!PyErr_Occurred() ||
+ PyErr_ExceptionMatches(PyExc_TypeError) ||
+ PyErr_ExceptionMatches(PyExc_OverflowError))
+ {
+ PyErr_SetString(PyExc_ValueError,
+ "_pack_ must be a non-negative integer");
+ }
return -1;
}
- Py_DECREF(isPacked);
- } else
- PyErr_Clear();
+ }
+ else {
+ isPacked = 0;
+ pack = 0;
+ }
- len = PySequence_Length(fields);
+ len = PySequence_Size(fields);
if (len == -1) {
- PyErr_SetString(PyExc_TypeError,
- "'_fields_' must be a sequence of pairs");
+ if (PyErr_ExceptionMatches(PyExc_TypeError)) {
+ PyErr_SetString(PyExc_TypeError,
+ "'_fields_' must be a sequence of pairs");
+ }
return -1;
}