8000 bpo-35081: Remove Py_BUILD_CORE from datetime.h (GH-10416) · python/cpython@0d12672 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0d12672

Browse files
pgansslevstinner
andcommitted
bpo-35081: Remove Py_BUILD_CORE from datetime.h (GH-10416)
Datetime macros like PyDate_Check() have two implementations, one using the C API capsule and one using direct access to the datetime type symbols defined in _datetimemodule.c. Since the direct access versions of the macros are only used in _datetimemodule.c, they have been moved out of "datetime.h" and into _datetimemodule.c. The _PY_DATETIME_IMPL macro is currently necessary in order to avoid both duplicate definitions of these macros in _datetimemodule.c and unnecessary declarations of C API capsule-related macros and varibles in datetime.h. Co-Authored-By: Victor Stinner <vstinner@redhat.com>
1 parent 3015fb8 commit 0d12672

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

Include/datetime.h

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -180,26 +180,11 @@ typedef struct {
180180
#define PyDateTime_CAPSULE_NAME "datetime.datetime_CAPI"
181181

182182

183-
#ifdef Py_BUILD_CORE
184-
185-
/* Macros for type checking when building the Python core. */
186-
#define PyDate_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateType)
187-
#define PyDate_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateType)
188-
189-
#define PyDateTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateTimeType)
190-
#define PyDateTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateTimeType)
191-
192-
#define PyTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_TimeType)
193-
#define PyTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TimeType)
194-
195-
#define PyDelta_Check(op) PyObject_TypeCheck(op, &PyDateTime_DeltaType)
196-
#define PyDelta_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DeltaType)
197-
198-
#define PyTZInfo_Check(op) PyObject_TypeCheck(op, &PyDateTime_TZInfoType)
199-
#define PyTZInfo_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TZInfoType)
200-
201-
#else
202-
183+
/* This block is only used as part of the public API and should not be
184+
* included in _datetimemodule.c, which does not use the C API capsule.
185+
* See bpo-35081 for more details.
186+
* */
187+
#ifndef _PY_DATETIME_IMPL
203188
/* Define global variable for the C API and a macro for setting it. */
204189
static PyDateTime_CAPI *PyDateTimeAPI = NULL;
205190

@@ -225,6 +210,7 @@ static PyDateTime_CAPI *PyDateTimeAPI = NULL;
225210
#define PyTZInfo_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TZInfoType)
226211
#define PyTZInfo_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->TZInfoType)
227212

213+
228214
/* Macros for accessing constructors in a simplified fashion. */
229215
#define PyDate_FromDate(year, month, day) \
230216
PyDateTimeAPI->Date_FromDate(year, month, day, PyDateTimeAPI->DateType)
@@ -264,7 +250,7 @@ static PyDateTime_CAPI *PyDateTimeAPI = NULL;
264250
PyDateTimeAPI->Date_FromTimestamp( \
265251
(PyObject*) (PyDateTimeAPI->DateType), args)
266252

267-
#endif /* Py_BUILD_CORE */
253+
#endif /* !defined(_PY_DATETIME_IMPL) */
268254

269255
#ifdef __cplusplus
270256
}

Modules/_datetimemodule.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
* http://www.zope.org/Members/fdrake/DateTimeWiki/FrontPage
33
*/
44

5+
/* bpo-35081: Defining this prevents including the C API capsule;
6+
* internal versions of the Py*_Check macros which do not require
7+
* the capsule are defined below */
8+
#define _PY_DATETIME_IMPL
9+
510
#include "Python.h"
11+
#include "datetime.h"
612
#include "structmember.h"
713

814
#include <time.h>
@@ -11,14 +17,21 @@
1117
# include <winsock2.h> /* struct timeval */
1218
#endif
1319

14-
/* Differentiate between building the core module and building extension
15-
* modules.
16-
*/
17-
#ifndef Py_BUILD_CORE
18-
#define Py_BUILD_CORE
19-
#endif
20-
#include "datetime.h"
21-
#undef Py_BUILD_CORE
20+
#define PyDate_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateType)
21+
#define PyDate_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateType)
22+
23+
#define PyDateTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_DateTimeType)
24+
#define PyDateTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DateTimeType)
25+
26+
#define PyTime_Check(op) PyObject_TypeCheck(op, &PyDateTime_TimeType)
27+
#define PyTime_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TimeType)
28+
29+
#define PyDelta_Check(op) PyObject_TypeCheck(op, &PyDateTime_DeltaType)
30+
#define PyDelta_CheckExact(op) (Py_TYPE(op) == &PyDateTime_DeltaType)
31+
32+
#define PyTZInfo_Check(op) PyObject_TypeCheck(op, &PyDateTime_TZInfoType)
33+
#define PyTZInfo_CheckExact(op) (Py_TYPE(op) == &PyDateTime_TZInfoType)
34+
2235

2336
/*[clinic input]
2437
module datetime

0 commit comments

Comments
 (0)
0