Skip to content

Commit d17a693

Browse files
authored
bpo-35199: Add an internal _PyTuple_ITEMS() macro (pythonGH-10434)
* _PyTuple_ITEMS() gives access to the tuple->ob_item field and cast the first argument to PyTupleObject*. This internal macro is only usable if Py_BUILD_CORE is defined. * Replace &PyTuple_GET_ITEM(ob, 0) with _PyTuple_ITEMS(ob). * Replace PyTuple_GET_ITEM(op, 1) with &_PyTuple_ITEMS(ob)[1].
1 parent 130893d commit d17a693

File tree

9 files changed

+27
-23
lines changed

9 files changed

+27
-23
lines changed

Include/tupleobject.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ PyAPI_FUNC(void) _PyTuple_MaybeUntrack(PyObject *);
5858
#define PyTuple_GET_ITEM(op, i) (((PyTupleObject *)(op))->ob_item[i])
5959
#define PyTuple_GET_SIZE(op) (assert(PyTuple_Check(op)),Py_SIZE(op))
6060

61+
#ifdef Py_BUILD_CORE
62+
# define _PyTuple_ITEMS(op) ((((PyTupleObject *)(op))->ob_item))
63+
#endif
64+
6165
/* Macro, *only* to be used to fill in brand new tuples */
6266
#define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = v)
6367
#endif

Modules/_functoolsmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ partial_fastcall(partialobject *pto, PyObject **args, Py_ssize_t nargs,
142142
stack = args;
143143
}
144144
else if (nargs == 0) {
145-
stack = &PyTuple_GET_ITEM(pto->args, 0);
145+
stack = _PyTuple_ITEMS(pto->args);
146146
}
147147
else {
148148
if (nargs2 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
@@ -159,7 +159,7 @@ partial_fastcall(partialobject *pto, PyObject **args, Py_ssize_t nargs,
159159

160160
/* use borrowed references */
161161
memcpy(stack,
162-
&PyTuple_GET_ITEM(pto->args, 0),
162+
_PyTuple_ITEMS(pto->args),
163163
pto_nargs * sizeof(PyObject*));
164164
memcpy(&stack[pto_nargs],
165165
args,
@@ -222,7 +222,7 @@ partial_call(partialobject *pto, PyObject *args, PyObject *kwargs)
222222

223223
if (pto->use_fastcall) {
224224
res = partial_fastcall(pto,
225-
&PyTuple_GET_ITEM(args, 0),
225+
_PyTuple_ITEMS(args),
226226
PyTuple_GET_SIZE(args),
227227
kwargs2);
228228
}

Modules/_testcapimodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4361,7 +4361,7 @@ fastcall_args(PyObject *args, PyObject ***stack, Py_ssize_t *nargs)
43614361
*nargs = 0;
43624362
}
43634363
else if (PyTuple_Check(args)) {
4364-
*stack = &PyTuple_GET_ITEM(args, 0);
4364+
*stack = ((PyTupleObject *)args)->ob_item;
43654365
*nargs = PyTuple_GET_SIZE(args);
43664366
}
43674367
else {

Objects/call.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
224224

225225
if (PyFunction_Check(callable)) {
226226
return _PyFunction_FastCallDict(callable,
227-
&PyTuple_GET_ITEM(args, 0),
227+
_PyTuple_ITEMS(args),
228228
PyTuple_GET_SIZE(args),
229229
kwargs);
230230
}
@@ -325,7 +325,7 @@ _PyFunction_FastCallDict(PyObject *func, PyObject *const *args, Py_ssize_t nargs
325325
&& co->co_argcount == PyTuple_GET_SIZE(argdefs)) {
326326
/* function called with no arguments, but all parameters have
327327
a default value: use default values as arguments .*/
328-
args = &PyTuple_GET_ITEM(argdefs, 0);
328+
args = _PyTuple_ITEMS(argdefs);
329329
return function_code_fastcall(co, args, PyTuple_GET_SIZE(argdefs),
330330
globals);
331331
}
@@ -342,7 +342,7 @@ _PyFunction_FastCallDict(PyObject *func, PyObject *const *args, Py_ssize_t nargs
342342
return NULL;
343343
}
344344

345-
k = &PyTuple_GET_ITEM(kwtuple, 0);
345+
k = _PyTuple_ITEMS(kwtuple);
346346
pos = i = 0;
347347
while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
348348
/* We must hold strong references because keyword arguments can be
@@ -365,7 +365,7 @@ _PyFunction_FastCallDict(PyObject *func, PyObject *const *args, Py_ssize_t nargs
365365
qualname = ((PyFunctionObject *)func) -> func_qualname;
366366

367367
if (argdefs != NULL) {
368-
d = &PyTuple_GET_ITEM(argdefs, 0);
368+
d = _PyTuple_ITEMS(argdefs);
369369
nd = PyTuple_GET_SIZE(argdefs);
370370
}
371371
else {
@@ -411,7 +411,7 @@ _PyFunction_FastCallKeywords(PyObject *func, PyObject *const *stack,
411411
&& co->co_argcount == PyTuple_GET_SIZE(argdefs)) {
412412
/* function called with no arguments, but all parameters have
413413
a default value: use default values as arguments .*/
414-
stack = &PyTuple_GET_ITEM(argdefs, 0);
414+
stack = _PyTuple_ITEMS(argdefs);
415415
return function_code_fastcall(co, stack, PyTuple_GET_SIZE(argdefs),
416416
globals);
417417
}
@@ -423,7 +423,7 @@ _PyFunction_FastCallKeywords(PyObject *func, PyObject *const *stack,
423423
qualname = ((PyFunctionObject *)func) -> func_qualname;
424424

425425
if (argdefs != NULL) {
426-
d = &PyTuple_GET_ITEM(argdefs, 0);
426+
d = _PyTuple_ITEMS(argdefs);
427427
nd = PyTuple_GET_SIZE(argdefs);
428428
}
429429
else {
@@ -432,7 +432,7 @@ _PyFunction_FastCallKeywords(PyObject *func, PyObject *const *stack,
432432
}
433433
return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
434434
stack, nargs,
435-
nkwargs ? &PyTuple_GET_ITEM(kwnames, 0) : NULL,
435+
nkwargs ? _PyTuple_ITEMS(kwnames) : NULL,
436436
stack + nargs,
437437
nkwargs, 1,
438438
d, (int)nd, kwdefs,
@@ -785,7 +785,7 @@ PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwargs)
785785
}
786786
else {
787787
return _PyCFunction_FastCallDict(func,
788-
&PyTuple_GET_ITEM(args, 0),
788+
_PyTuple_ITEMS(args),
789789
PyTuple_GET_SIZE(args),
790790
kwargs);
791791
}
@@ -898,8 +898,8 @@ _PyObject_Call_Prepend(PyObject *callable,
898898
/* use borrowed references */
899899
stack[0] = obj;
900900
memcpy(&stack[1],
901-
&PyTuple_GET_ITEM(args, 0),
902-
argcount * sizeof(PyObject *));
901+
_PyTuple_ITEMS(args),
902+
argcount * sizeof(PyObject *));
903903

904904
result = _PyObject_FastCallDict(callable,
905905
stack, argcount + 1,
@@ -950,7 +950,7 @@ _PyObject_CallFunctionVa(PyObject *callable, const char *format,
950950
func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */
951951
PyObject *args = stack[0];
952952
result = _PyObject_FastCall(callable,
953-
&PyTuple_GET_ITEM(args, 0),
953+
_PyTuple_ITEMS(args),
954954
PyTuple_GET_SIZE(args));
955955
}
956956
else {

Objects/codeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ intern_strings(PyObject *tuple)
3939
if (v == NULL || !PyUnicode_CheckExact(v)) {
4040
Py_FatalError("non-string found in code slot");
4141
}
42-
PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
42+
PyUnicode_InternInPlace(&_PyTuple_ITEMS(tuple)[i]);
4343
}
4444
}
4545

Objects/descrobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwargs)
247247
}
248248

249249
result = _PyMethodDef_RawFastCallDict(descr->d_method, self,
250-
&PyTuple_GET_ITEM(args, 1), nargs - 1,
250+
&_PyTuple_ITEMS(args)[1], nargs - 1,
251251
kwargs);
252252
result = _Py_CheckFunctionResult((PyObject *)descr, result, NULL);
253253
return result;
@@ -331,7 +331,7 @@ classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
331331
}
332332

333333
result = _PyMethodDef_RawFastCallDict(descr->d_method, self,
334-
&PyTuple_GET_ITEM(args, 1), argc - 1,
334+
&_PyTuple_ITEMS(args)[1], argc - 1,
335335
kwds);
336336
result = _Py_CheckFunctionResult((PyObject *)descr, result, NULL);
337337
return result;

Objects/funcobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ function_call(PyObject *func, PyObject *args, PyObject *kwargs)
581581
PyObject **stack;
582582
Py_ssize_t nargs;
583583

584-
stack = &PyTuple_GET_ITEM(args, 0);
584+
stack = _PyTuple_ITEMS(args);
585585
nargs = PyTuple_GET_SIZE(args);
586586
return _PyFunction_FastCallDict(func, stack, nargs, kwargs);
587587
}

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4662,7 +4662,7 @@ do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
46624662
}
46634663

46644664
C_TRACE(result, _PyCFunction_FastCallDict(func,
4665-
&PyTuple_GET_ITEM(callargs, 1),
4665+
&_PyTuple_ITEMS(callargs)[1],
46664666
nargs - 1,
46674667
kwdict));
46684668
Py_DECREF(func);

Python/getargs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
422422
return 0;
423423
}
424424

425-
stack = &PyTuple_GET_ITEM(args, 0);
425+
stack = _PyTuple_ITEMS(args);
426426
nargs = PyTuple_GET_SIZE(args);
427427
}
428428
else {
@@ -2254,7 +2254,7 @@ vgetargskeywordsfast(PyObject *args, PyObject *keywords,
22542254
return 0;
22552255
}
22562256

2257-
stack = &PyTuple_GET_ITEM(args, 0);
2257+
stack = _PyTuple_ITEMS(args);
22582258
nargs = PyTuple_GET_SIZE(args);
22592259
return vgetargskeywordsfast_impl(stack, nargs, keywords, NULL,
22602260
parser, p_va, flags);
@@ -2461,7 +2461,7 @@ PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t m
24612461
"PyArg_UnpackTuple() argument list is not a tuple");
24622462
return 0;
24632463
}
2464-
stack = &PyTuple_GET_ITEM(args, 0);
2464+
stack = _PyTuple_ITEMS(args);
24652465
nargs = PyTuple_GET_SIZE(args);
24662466

24672467
#ifdef HAVE_STDARG_PROTOTYPES

0 commit comments

Comments
 (0)