Skip to content

Commit b37672d

Browse files
authored
bpo-35059: Cleanup usage of Python macros (GH-10648)
Don't pass complex expressions but regular variables to Python macros. * _datetimemodule.c: split single large "if" into two "if" in date_new(), time_new() and datetime_new(). * _pickle.c, load_extension(): flatten complex "if" expression into more regular C code. * _ssl.c: addbool() now uses a temporary bool_obj to only evaluate the value once. * weakrefobject.c: replace "Py_INCREF(result = proxy);" with "result = proxy; Py_INCREF(result);"
1 parent 2ff8fb7 commit b37672d

File tree

4 files changed

+114
-90
lines changed

4 files changed

+114
-90
lines changed

‎Modules/_datetimemodule.c‎

Lines changed: 87 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2798,20 +2798,22 @@ date_new(PyTypeObject *type, PyObject *args, PyObject *kw)
27982798
int day;
27992799

28002800
/* Check for invocation from pickle with __getstate__ state */
2801-
if (PyTuple_GET_SIZE(args) == 1 &&
2802-
PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) &&
2803-
PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE &&
2804-
MONTH_IS_SANE(PyBytes_AS_STRING(state)[2]))
2805-
{
2806-
PyDateTime_Date *me;
2807-
2808-
me = (PyDateTime_Date *) (type->tp_alloc(type, 0));
2809-
if (me != NULL) {
2810-
char *pdata = PyBytes_AS_STRING(state);
2811-
memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE);
2812-
me->hashcode = -1;
2801+
if (PyTuple_GET_SIZE(args) == 1) {
2802+
state = PyTuple_GET_ITEM(args, 0);
2803+
if (PyBytes_Check(state) &&
2804+
PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE &&
2805+
MONTH_IS_SANE(PyBytes_AS_STRING(state)[2]))
2806+
{
2807+
PyDateTime_Date *me;
2808+
2809+
me = (PyDateTime_Date *) (type->tp_alloc(type, 0));
2810+
if (me != NULL) {
2811+
char *pdata = PyBytes_AS_STRING(state);
2812+
memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE);
2813+
me->hashcode = -1;
2814+
}
2815+
return (PyObject *)me;
28132816
}
2814-
return (PyObject *)me;
28152817
}
28162818

28172819
if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws,
@@ -3913,43 +3915,46 @@ time_new(PyTypeObject *type, PyObject *args, PyObject *kw)
39133915

39143916
/* Check for invocation from pickle with __getstate__ state */
39153917
if (PyTuple_GET_SIZE(args) >= 1 &&
3916-
PyTuple_GET_SIZE(args) <= 2 &&
3917-
PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) &&
3918-
PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE &&
3919-
(0x7F & ((unsigned char) (PyBytes_AS_STRING(state)[0]))) < 24)
3918+
PyTuple_GET_SIZE(args) <= 2)
39203919
{
3921-
PyDateTime_Time *me;
3922-
char aware;
3923-
3924-
if (PyTuple_GET_SIZE(args) == 2) {
3925-
tzinfo = PyTuple_GET_ITEM(args, 1);
3926-
if (check_tzinfo_subclass(tzinfo) < 0) {
3927-
PyErr_SetString(PyExc_TypeError, "bad "
3928-
"tzinfo state arg");
3929-
return NULL;
3930-
}
3931-
}
3932-
aware = (char)(tzinfo != Py_None);
3933-
me = (PyDateTime_Time *) (type->tp_alloc(type, aware));
3934-
if (me != NULL) {
3935-
char *pdata = PyBytes_AS_STRING(state);
3936-
3937-
memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE);
3938-
me->hashcode = -1;
3939-
me->hastzinfo = aware;
3940-
if (aware) {
3941-
Py_INCREF(tzinfo);
3942-
me->tzinfo = tzinfo;
3943-
}
3944-
if (pdata[0] & (1 << 7)) {
3945-
me->data[0] -= 128;
3946-
me->fold = 1;
3920+
state = PyTuple_GET_ITEM(args, 0);
3921+
if (PyBytes_Check(state) &&
3922+
PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE &&
3923+
(0x7F & ((unsigned char) (PyBytes_AS_STRING(state)[0]))) < 24)
3924+
{
3925+
PyDateTime_Time *me;
3926+
char aware;
3927+
3928+
if (PyTuple_GET_SIZE(args) == 2) {
3929+
tzinfo = PyTuple_GET_ITEM(args, 1);
3930+
if (check_tzinfo_subclass(tzinfo) < 0) {
3931+
PyErr_SetString(PyExc_TypeError, "bad "
3932+
"tzinfo state arg");
3933+
return NULL;
3934+
}
39473935
}
3948-
else {
3949-
me->fold = 0;
3936+
aware = (char)(tzinfo != Py_None);
3937+
me = (PyDateTime_Time *) (type->tp_alloc(type, aware));
3938+
if (me != NULL) {
3939+
char *pdata = PyBytes_AS_STRING(state);
3940+
3941+
memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE);
3942+
me->hashcode = -1;
3943+
me->hastzinfo = aware;
3944+
if (aware) {
3945+
Py_INCREF(tzinfo);
3946+
me->tzinfo = tzinfo;
3947+
}
3948+
if (pdata[0] & (1 << 7)) {
3949+
me->data[0] -= 128;
3950+
me->fold = 1;
3951+
}
3952+
else {
3953+
me->fold = 0;
3954+
}
39503955
}
3956+
return (PyObject *)me;
39513957
}
3952-
return (PyObject *)me;
39533958
}
39543959

39553960
if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i", time_kws,
@@ -4552,43 +4557,46 @@ datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw)
45524557

45534558
/* Check for invocation from pickle with __getstate__ state */
45544559
if (PyTuple_GET_SIZE(args) >= 1 &&
4555-
PyTuple_GET_SIZE(args) <= 2 &&
4556-
PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) &&
4557-
PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE &&
4558-
MONTH_IS_SANE(PyBytes_AS_STRING(state)[2] & 0x7F))
4560+
PyTuple_GET_SIZE(args) <= 2)
45594561
{
4560-
PyDateTime_DateTime *me;
4561-
char aware;
4562-
4563-
if (PyTuple_GET_SIZE(args) == 2) {
4564-
tzinfo = PyTuple_GET_ITEM(args, 1);
4565-
if (check_tzinfo_subclass(tzinfo) < 0) {
4566-
PyErr_SetString(PyExc_TypeError, "bad "
4567-
"tzinfo state arg");
4568-
return NULL;
4569-
}
4570-
}
4571-
aware = (char)(tzinfo != Py_None);
4572-
me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware));
4573-
if (me != NULL) {
4574-
char *pdata = PyBytes_AS_STRING(state);
4575-
4576-
memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE);
4577-
me->hashcode = -1;
4578-
me->hastzinfo = aware;
4579-
if (aware) {
4580-
Py_INCREF(tzinfo);
4581-
me->tzinfo = tzinfo;
4582-
}
4583-
if (pdata[2] & (1 << 7)) {
4584-
me->data[2] -= 128;
4585-
me->fold = 1;
4562+
state = PyTuple_GET_ITEM(args, 0);
4563+
if (PyBytes_Check(state) &&
4564+
PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE &&
4565+
MONTH_IS_SANE(PyBytes_AS_STRING(state)[2] & 0x7F))
4566+
{
4567+
PyDateTime_DateTime *me;
4568+
char aware;
4569+
4570+
if (PyTuple_GET_SIZE(args) == 2) {
4571+
tzinfo = PyTuple_GET_ITEM(args, 1);
4572+
if (check_tzinfo_subclass(tzinfo) < 0) {
4573+
PyErr_SetString(PyExc_TypeError, "bad "
4574+
"tzinfo state arg");
4575+
return NULL;
4576+
}
45864577
}
4587-
else {
4588-
me->fold = 0;
4578+
aware = (char)(tzinfo != Py_None);
4579+
me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware));
4580+
if (me != NULL) {
4581+
char *pdata = PyBytes_AS_STRING(state);
4582+
4583+
memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE);
4584+
me->hashcode = -1;
4585+
me->hastzinfo = aware;
4586+
if (aware) {
4587+
Py_INCREF(tzinfo);
4588+
me->tzinfo = tzinfo;
4589+
}
4590+
if (pdata[2] & (1 << 7)) {
4591+
me->data[2] -= 128;
4592+
me->fold = 1;
4593+
}
4594+
else {
4595+
me->fold = 0;
4596+
}
45894597
}
4598+
return (PyObject *)me;
45904599
}
4591-
return (PyObject *)me;
45924600
}
45934601

45944602
if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO$i", datetime_kws,

‎Modules/_pickle.c‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5858,14 +5858,20 @@ load_extension(UnpicklerObject *self, int nbytes)
58585858
/* Since the extension registry is manipulable via Python code,
58595859
* confirm that pair is really a 2-tuple of strings.
58605860
*/
5861-
if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5862-
!PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5863-
!PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5864-
Py_DECREF(py_code);
5865-
PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5866-
"isn't a 2-tuple of strings", code);
5867-
return -1;
5861+
if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2) {
5862+
goto error;
5863+
}
5864+
5865+
module_name = PyTuple_GET_ITEM(pair, 0);
5866+
if (!PyUnicode_Check(module_name)) {
5867+
goto error;
5868+
}
5869+
5870+
class_name = PyTuple_GET_ITEM(pair, 1);
5871+
if (!PyUnicode_Check(class_name)) {
5872+
goto error;
58685873
}
5874+
58695875
/* Load the object. */
58705876
obj = find_class(self, module_name, class_name);
58715877
if (obj == NULL) {
@@ -5881,6 +5887,12 @@ load_extension(UnpicklerObject *self, int nbytes)
58815887
}
58825888
PDATA_PUSH(self->stack, obj, -1);
58835889
return 0;
5890+
5891+
error:
5892+
Py_DECREF(py_code);
5893+
PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5894+
"isn't a 2-tuple of strings", code);
5895+
return -1;
58845896
}
58855897

58865898
static int

‎Modules/_ssl.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5983,9 +5983,12 @@ PyInit__ssl(void)
59835983
PyModule_AddIntConstant(m, "PROTO_TLSv1_2", PY_PROTO_TLSv1_2);
59845984
PyModule_AddIntConstant(m, "PROTO_TLSv1_3", PY_PROTO_TLSv1_3);
59855985

5986-
#define addbool(m, v, b) \
5987-
Py_INCREF((b) ? Py_True : Py_False); \
5988-
PyModule_AddObject((m), (v), (b) ? Py_True : Py_False);
5986+
#define addbool(m, key, value) \
5987+
do { \
5988+
PyObject *bool_obj = (value) ? Py_True : Py_False; \
5989+
Py_INCREF(bool_obj); \
5990+
PyModule_AddObject((m), (key), bool_obj); \
5991+
} while (0)
59895992

59905993
#if HAVE_SNI
59915994
addbool(m, "HAS_SNI", 1);

‎Objects/weakrefobject.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,8 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
833833
to avoid violating the invariants of the list
834834
of weakrefs for ob. */
835835
Py_DECREF(result);
836-
Py_INCREF(result = proxy);
836+
result = proxy;
837+
Py_INCREF(result);
837838
goto skip_insert;
838839
}
839840
prev = ref;

0 commit comments

Comments
 (0)