Skip to content

Commit e1a8968

Browse files
port ctypes
1 parent e9ac890 commit e1a8968

File tree

4 files changed

+28
-60
lines changed

4 files changed

+28
-60
lines changed

Modules/_ctypes/_ctypes.c

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ bytes(cdata)
101101
#ifndef Py_BUILD_CORE_BUILTIN
102102
# define Py_BUILD_CORE_MODULE 1
103103
#endif
104-
#define NEEDS_PY_IDENTIFIER
105-
106104
#define PY_SSIZE_T_CLEAN
107105

108106
#include "Python.h"
@@ -498,8 +496,6 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt
498496
PyTypeObject *result;
499497
PyObject *fields;
500498
StgDictObject *dict;
501-
_Py_IDENTIFIER(_abstract_);
502-
_Py_IDENTIFIER(_fields_);
503499

504500
/* create the new instance (which is a class,
505501
since we are a metatype!) */
@@ -508,7 +504,7 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt
508504
return NULL;
509505

510506
/* keep this for bw compatibility */
511-
int r = _PyDict_ContainsId(result->tp_dict, &PyId__abstract_);
507+
int r = PyDict_Contains(result->tp_dict, &_Py_ID(_abstract_));
512508
if (r > 0)
513509
return (PyObject *)result;
514510
if (r < 0) {
@@ -540,9 +536,9 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt
540536

541537
dict->paramfunc = StructUnionType_paramfunc;
542538

543-
fields = _PyDict_GetItemIdWithError((PyObject *)dict, &PyId__fields_);
539+
fields = PyDict_GetItemWithError((PyObject *)dict, &_Py_ID(_fields_));
544540
if (fields) {
545-
if (_PyObject_SetAttrId((PyObject *)result, &PyId__fields_, fields) < 0) {
541+
if (PyObject_SetAttr((PyObject *)result, &_Py_ID(_fields_), fields) < 0) {
546542
Py_DECREF(result);
547543
return NULL;
548544
}
@@ -797,7 +793,6 @@ PyDoc_STRVAR(from_param_doc,
797793
static PyObject *
798794
CDataType_from_param(PyObject *type, PyObject *value)
799795
{
800-
_Py_IDENTIFIER(_as_parameter_);
801796
PyObject *as_parameter;
802797
int res = PyObject_IsInstance(value, type);
803798
if (res == -1)
@@ -831,7 +826,7 @@ CDataType_from_param(PyObject *type, PyObject *value)
831826
return NULL;
832827
}
833828

834-
if (_PyObject_LookupAttrId(value, &PyId__as_parameter_, &as_parameter) < 0) {
829+
if (_PyObject_LookupAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
835830
return NULL;
836831
}
837832
if (as_parameter) {
@@ -1068,7 +1063,7 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
10681063
StgDictObject *stgdict;
10691064
PyObject *proto;
10701065
PyObject *typedict;
1071-
_Py_IDENTIFIER(_type_);
1066+
10721067

10731068
typedict = PyTuple_GetItem(args, 2);
10741069
if (!typedict)
@@ -1088,7 +1083,7 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
10881083
stgdict->paramfunc = PyCPointerType_paramfunc;
10891084
stgdict->flags |= TYPEFLAG_ISPOINTER;
10901085

1091-
proto = _PyDict_GetItemIdWithError(typedict, &PyId__type_); /* Borrowed ref */
1086+
proto = PyDict_GetItemWithError(typedict, &_Py_ID(_type_)); /* Borrowed ref */
10921087
if (proto) {
10931088
StgDictObject *itemdict;
10941089
const char *current_format;
@@ -1146,7 +1141,7 @@ static PyObject *
11461141
PyCPointerType_set_type(PyTypeObject *self, PyObject *type)
11471142
{
11481143
StgDictObject *dict;
1149-
_Py_IDENTIFIER(_type_);
1144+
11501145

11511146
dict = PyType_stgdict((PyObject *)self);
11521147
if (!dict) {
@@ -1158,7 +1153,7 @@ PyCPointerType_set_type(PyTypeObject *self, PyObject *type)
11581153
if (-1 == PyCPointerType_SetProto(dict, type))
11591154
return NULL;
11601155

1161-
if (-1 == _PyDict_SetItemId((PyObject *)dict, &PyId__type_, type))
1156+
if (-1 == PyDict_SetItem((PyObject *)dict, &_Py_ID(_type_), type))
11621157
return NULL;
11631158

11641159
Py_RETURN_NONE;
@@ -1461,8 +1456,6 @@ PyCArrayType_paramfunc(CDataObject *self)
14611456
static PyObject *
14621457
PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
14631458
{
1464-
_Py_IDENTIFIER(_length_);
1465-
_Py_IDENTIFIER(_type_);
14661459
PyTypeObject *result;
14671460
StgDictObject *stgdict;
14681461
StgDictObject *itemdict;
@@ -1481,7 +1474,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
14811474
stgdict = NULL;
14821475
type_attr = NULL;
14831476

1484-
if (_PyObject_LookupAttrId((PyObject *)result, &PyId__length_, &length_attr) < 0) {
1477+
if (_PyObject_LookupAttr((PyObject *)result, &_Py_ID(_length_), &length_attr) < 0) {
14851478
goto error;
14861479
}
14871480
if (!length_attr) {
@@ -1514,7 +1507,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
15141507
goto error;
15151508
}
15161509

1517-
if (_PyObject_LookupAttrId((PyObject *)result, &PyId__type_, &type_attr) < 0) {
1510+
if (_PyObject_LookupAttr((PyObject *)result, &_Py_ID(_type_), &type_attr) < 0) {
15181511
goto error;
15191512
}
15201513
if (!type_attr) {
@@ -1659,7 +1652,6 @@ static const char SIMPLE_TYPE_CHARS[] = "cbBhHiIlLdfuzZqQPXOv?g";
16591652
static PyObject *
16601653
c_wchar_p_from_param(PyObject *type, PyObject *value)
16611654
{
1662-
_Py_IDENTIFIER(_as_parameter_);
16631655
PyObject *as_parameter;
16641656
int res;
16651657
if (value == Py_None) {
@@ -1709,7 +1701,7 @@ c_wchar_p_from_param(PyObject *type, PyObject *value)
17091701
}
17101702
}
17111703

1712-
if (_PyObject_LookupAttrId(value, &PyId__as_parameter_, &as_parameter) < 0) {
1704+
if (_PyObject_LookupAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
17131705
return NULL;
17141706
}
17151707
if (as_parameter) {
@@ -1726,7 +1718,6 @@ c_wchar_p_from_param(PyObject *type, PyObject *value)
17261718
static PyObject *
17271719
c_char_p_from_param(PyObject *type, PyObject *value)
17281720
{
1729-
_Py_IDENTIFIER(_as_parameter_);
17301721
PyObject *as_parameter;
17311722
int res;
17321723
if (value == Py_None) {
@@ -1776,7 +1767,7 @@ c_char_p_from_param(PyObject *type, PyObject *value)
17761767
}
17771768
}
17781769

1779-
if (_PyObject_LookupAttrId(value, &PyId__as_parameter_, &as_parameter) < 0) {
1770+
if (_PyObject_LookupAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
17801771
return NULL;
17811772
}
17821773
if (as_parameter) {
@@ -1793,7 +1784,6 @@ c_char_p_from_param(PyObject *type, PyObject *value)
17931784
static PyObject *
17941785
c_void_p_from_param(PyObject *type, PyObject *value)
17951786
{
1796-
_Py_IDENTIFIER(_as_parameter_);
17971787
StgDictObject *stgd;
17981788
PyObject *as_parameter;
17991789
int res;
@@ -1915,7 +1905,7 @@ c_void_p_from_param(PyObject *type, PyObject *value)
19151905
}
19161906
}
19171907

1918-
if (_PyObject_LookupAttrId(value, &PyId__as_parameter_, &as_parameter) < 0) {
1908+
if (_PyObject_LookupAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
19191909
return NULL;
19201910
}
19211911
if (as_parameter) {
@@ -2038,7 +2028,6 @@ PyCSimpleType_paramfunc(CDataObject *self)
20382028
static PyObject *
20392029
PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
20402030
{
2041-
_Py_IDENTIFIER(_type_);
20422031
PyTypeObject *result;
20432032
StgDictObject *stgdict;
20442033
PyObject *proto;
@@ -2053,7 +2042,7 @@ PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
20532042
if (result == NULL)
20542043
return NULL;
20552044

2056-
if (_PyObject_LookupAttrId((PyObject *)result, &PyId__type_, &proto) < 0) {
2045+
if (_PyObject_LookupAttr((PyObject *)result, &_Py_ID(_type_), &proto) < 0) {
20572046
return NULL;
20582047
}
20592048
if (!proto) {
@@ -2223,7 +2212,6 @@ PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
22232212
static PyObject *
22242213
PyCSimpleType_from_param(PyObject *type, PyObject *value)
22252214
{
2226-
_Py_IDENTIFIER(_as_parameter_);
22272215
StgDictObject *dict;
22282216
const char *fmt;
22292217
PyCArgObject *parg;
@@ -2267,7 +2255,7 @@ PyCSimpleType_from_param(PyObject *type, PyObject *value)
22672255
PyErr_Clear();
22682256
Py_DECREF(parg);
22692257

2270-
if (_PyObject_LookupAttrId(value, &PyId__as_parameter_, &as_parameter) < 0) {
2258+
if (_PyObject_LookupAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
22712259
return NULL;
22722260
}
22732261
if (as_parameter) {
@@ -2344,7 +2332,6 @@ PyTypeObject PyCSimpleType_Type = {
23442332
static PyObject *
23452333
converters_from_argtypes(PyObject *ob)
23462334
{
2347-
_Py_IDENTIFIER(from_param);
23482335
PyObject *converters;
23492336
Py_ssize_t i;
23502337

@@ -2424,7 +2411,7 @@ converters_from_argtypes(PyObject *ob)
24242411
}
24252412
*/
24262413

2427-
if (_PyObject_LookupAttrId(tp, &PyId_from_param, &cnv) <= 0) {
2414+
if (_PyObject_LookupAttr(tp, &_Py_ID(from_param), &cnv) <= 0) {
24282415
Py_DECREF(converters);
24292416
Py_DECREF(ob);
24302417
if (!PyErr_Occurred()) {
@@ -2445,10 +2432,6 @@ make_funcptrtype_dict(StgDictObject *stgdict)
24452432
{
24462433
PyObject *ob;
24472434
PyObject *converters = NULL;
2448-
_Py_IDENTIFIER(_flags_);
2449-
_Py_IDENTIFIER(_argtypes_);
2450-
_Py_IDENTIFIER(_restype_);
2451-
_Py_IDENTIFIER(_check_retval_);
24522435

24532436
stgdict->align = _ctypes_get_fielddesc("P")->pffi_type->alignment;
24542437
stgdict->length = 1;
@@ -2457,7 +2440,7 @@ make_funcptrtype_dict(StgDictObject *stgdict)
24572440
stgdict->getfunc = NULL;
24582441
stgdict->ffi_type_pointer = ffi_type_pointer;
24592442

2460-
ob = _PyDict_GetItemIdWithError((PyObject *)stgdict, &PyId__flags_);
2443+
ob = PyDict_GetItemWithError((PyObject *)stgdict, &_Py_ID(_flags_));
24612444
if (!ob || !PyLong_Check(ob)) {
24622445
if (!PyErr_Occurred()) {
24632446
PyErr_SetString(PyExc_TypeError,
@@ -2468,7 +2451,7 @@ make_funcptrtype_dict(StgDictObject *stgdict)
24682451
stgdict->flags = PyLong_AsUnsignedLongMask(ob) | TYPEFLAG_ISPOINTER;
24692452

24702453
/* _argtypes_ is optional... */
2471-
ob = _PyDict_GetItemIdWithError((PyObject *)stgdict, &PyId__argtypes_);
2454+
ob = PyDict_GetItemWithError((PyObject *)stgdict, &_Py_ID(_argtypes_));
24722455
if (ob) {
24732456
converters = converters_from_argtypes(ob);
24742457
if (!converters)
@@ -2481,7 +2464,7 @@ make_funcptrtype_dict(StgDictObject *stgdict)
24812464
return -1;
24822465
}
24832466

2484-
ob = _PyDict_GetItemIdWithError((PyObject *)stgdict, &PyId__restype_);
2467+
ob = PyDict_GetItemWithError((PyObject *)stgdict, &_Py_ID(_restype_));
24852468
if (ob) {
24862469
if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) {
24872470
PyErr_SetString(PyExc_TypeError,
@@ -2490,7 +2473,7 @@ make_funcptrtype_dict(StgDictObject *stgdict)
24902473
}
24912474
Py_INCREF(ob);
24922475
stgdict->restype = ob;
2493-
if (_PyObject_LookupAttrId(ob, &PyId__check_retval_,
2476+
if (_PyObject_LookupAttr(ob, &_Py_ID(_check_retval_),
24942477
&stgdict->checker) < 0)
24952478
{
24962479
return -1;
@@ -3254,7 +3237,6 @@ PyCFuncPtr_get_errcheck(PyCFuncPtrObject *self, void *Py_UNUSED(ignored))
32543237
static int
32553238
PyCFuncPtr_set_restype(PyCFuncPtrObject *self, PyObject *ob, void *Py_UNUSED(ignored))
32563239
{
3257-
_Py_IDENTIFIER(_check_retval_);
32583240
PyObject *checker, *oldchecker;
32593241
if (ob == NULL) {
32603242
oldchecker = self->checker;
@@ -3268,7 +3250,7 @@ PyCFuncPtr_set_restype(PyCFuncPtrObject *self, PyObject *ob, void *Py_UNUSED(ign
32683250
"restype must be a type, a callable, or None");
32693251
return -1;
32703252
}
3271-
if (_PyObject_LookupAttrId(ob, &PyId__check_retval_, &checker) < 0) {
3253+
if (_PyObject_LookupAttr(ob, &_Py_ID(_check_retval_), &checker) < 0) {
32723254
return -1;
32733255
}
32743256
oldchecker = self->checker;
@@ -4062,10 +4044,9 @@ _build_result(PyObject *result, PyObject *callargs,
40624044
PyTuple_SET_ITEM(tup, index, v);
40634045
index++;
40644046
} else if (bit & outmask) {
4065-
_Py_IDENTIFIER(__ctypes_from_outparam__);
40664047

40674048
v = PyTuple_GET_ITEM(callargs, i);
4068-
v = _PyObject_CallMethodIdNoArgs(v, &PyId___ctypes_from_outparam__);
4049+
v = PyObject_CallMethodNoArgs(v, &_Py_ID(__ctypes_from_outparam__));
40694050
if (v == NULL || numretvals == 1) {
40704051
Py_DECREF(callargs);
40714052
return v;
@@ -4348,7 +4329,6 @@ _init_pos_args(PyObject *self, PyTypeObject *type,
43484329
StgDictObject *dict;
43494330
PyObject *fields;
43504331
Py_ssize_t i;
4351-
_Py_IDENTIFIER(_fields_);
43524332

43534333
if (PyType_stgdict((PyObject *)type->tp_base)) {
43544334
index = _init_pos_args(self, type->tp_base,
@@ -4359,7 +4339,7 @@ _init_pos_args(PyObject *self, PyTypeObject *type,
43594339
}
43604340

43614341
dict = PyType_stgdict((PyObject *)type);
4362-
fields = _PyDict_GetItemIdWithError((PyObject *)dict, &PyId__fields_);
4342+
fields = PyDict_GetItemWithError((PyObject *)dict, &_Py_ID(_fields_));
43634343
if (fields == NULL) {
43644344
if (PyErr_Occurred()) {
43654345
return -1;

Modules/_ctypes/callbacks.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef Py_BUILD_CORE_BUILTIN
22
# define Py_BUILD_CORE_MODULE 1
33
#endif
4-
#define NEEDS_PY_IDENTIFIER
54

65
#include "Python.h"
76
// windows.h must be included before pycore internal headers
@@ -125,9 +124,7 @@ static void
125124
TryAddRef(StgDictObject *dict, CDataObject *obj)
126125
{
127126
IUnknown *punk;
128-
_Py_IDENTIFIER(_needs_com_addref_);
129-
130-
int r = _PyDict_ContainsId((PyObject *)dict, &PyId__needs_com_addref_);
127+
int r = PyDict_Contains((PyObject *)dict, &_Py_ID(_needs_com_addref_));
131128
if (r <= 0) {
132129
if (r < 0) {
133130
PrintError("getting _needs_com_addref_");

Modules/_ctypes/callproc.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
#ifndef Py_BUILD_CORE_BUILTIN
5858
# define Py_BUILD_CORE_MODULE 1
5959
#endif
60-
#define NEEDS_PY_IDENTIFIER
6160

6261
#include "Python.h"
6362
#include "structmember.h" // PyMemberDef
@@ -719,9 +718,8 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
719718
}
720719

721720
{
722-
_Py_IDENTIFIER(_as_parameter_);
723721
PyObject *arg;
724-
if (_PyObject_LookupAttrId(obj, &PyId__as_parameter_, &arg) < 0) {
722+
if (_PyObject_LookupAttr(obj, &_Py_ID(_as_parameter_), &arg) < 0) {
725723
return -1;
726724
}
727725
/* Which types should we exactly allow here?
@@ -1848,16 +1846,14 @@ static PyObject *
18481846
unpickle(PyObject *self, PyObject *args)
18491847
{
18501848
PyObject *typ, *state, *meth, *obj, *result;
1851-
_Py_IDENTIFIER(__new__);
1852-
_Py_IDENTIFIER(__setstate__);
18531849

18541850
if (!PyArg_ParseTuple(args, "OO!", &typ, &PyTuple_Type, &state))
18551851
return NULL;
18561852
obj = _PyObject_CallMethodIdOneArg(typ, &PyId___new__, typ);
18571853
if (obj == NULL)
18581854
return NULL;
18591855

1860-
meth = _PyObject_GetAttrId(obj, &PyId___setstate__);
1856+
meth = PyObject_GetAttr(obj, &_Py_ID(__setstate__));
18611857
if (meth == NULL) {
18621858
goto error;
18631859
}

0 commit comments

Comments
 (0)