Skip to content

Commit db6983c

Browse files
committed
Remove support for Python <3.8 from C code
Similarly to the previous commit, the bulk of the work is removing code for PY_VERSION_HEX < 0x03080000, removing #if guards around PY_VERSION_HEX >= 0x03080000, and removing unnecessary macro definitions. Signed-off-by: Rodrigo Tobar <[email protected]>
1 parent 88ef0b5 commit db6983c

File tree

5 files changed

+6
-127
lines changed

5 files changed

+6
-127
lines changed

src/c/_cffi_backend.c

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,10 @@
141141
#include "malloc_closure.h"
142142

143143

144-
#if PY_VERSION_HEX >= 0x03030000
145-
# define PyText_GetSize PyUnicode_GetLength
146-
#else
147-
# define PyText_GetSize PyUnicode_GetSize
148-
#endif
149-
150144
#if PY_VERSION_HEX < 0x030900a4
151145
# define Py_SET_REFCNT(obj, val) (Py_REFCNT(obj) = (val))
152146
#endif
153147

154-
#if PY_VERSION_HEX >= 0x03080000
155-
# define USE_WRITEUNRAISABLEMSG
156-
#endif
157-
158148
/************************************************************/
159149

160150
/* base type flag: exactly one of the following: */
@@ -4431,7 +4421,7 @@ static void *b_do_dlopen(PyObject *args, const char **p_printable_filename,
44314421
if (*p_printable_filename == NULL)
44324422
return NULL;
44334423

4434-
sz1 = PyText_GetSize(filename_unicode) + 1;
4424+
sz1 = PyUnicode_GetLength(filename_unicode) + 1;
44354425
sz1 *= 2; /* should not be needed, but you never know */
44364426
w1 = alloca(sizeof(wchar_t) * sz1);
44374427
sz1 = PyUnicode_AsWideChar(filename_unicode,
@@ -5141,7 +5131,7 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
51415131
if (!(sflags & SF_GCC_ARM_BITFIELDS) && fbitsize >= 0) {
51425132
if (!(sflags & SF_MSVC_BITFIELDS)) {
51435133
/* GCC: anonymous bitfields (of any size) don't cause alignment */
5144-
do_align = PyText_GetSize(fname) > 0;
5134+
do_align = PyUnicode_GetLength(fname) > 0;
51455135
}
51465136
else {
51475137
/* MSVC: zero-sized bitfields don't cause alignment */
@@ -5185,7 +5175,7 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
51855175
byteoffset = foffset;
51865176
}
51875177

5188-
if (PyText_GetSize(fname) == 0 &&
5178+
if (PyUnicode_GetLength(fname) == 0 &&
51895179
ftype->ct_flags & (CT_STRUCT|CT_UNION)) {
51905180
/* a nested anonymous struct or union */
51915181
CFieldObject *cfsrc = (CFieldObject *)ftype->ct_extra;
@@ -5256,7 +5246,7 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
52565246
field_offset_bytes &= ~(falign - 1);
52575247

52585248
if (fbitsize == 0) {
5259-
if (PyText_GetSize(fname) > 0) {
5249+
if (PyUnicode_GetLength(fname) > 0) {
52605250
PyErr_Format(PyExc_TypeError,
52615251
"field '%s.%s' is declared with :0",
52625252
ct->ct_name, PyUnicode_AsUTF8(fname));
@@ -5345,7 +5335,7 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
53455335
if (sflags & SF_GCC_BIG_ENDIAN)
53465336
bitshift = 8 * ftype->ct_size - fbitsize - bitshift;
53475337

5348-
if (PyText_GetSize(fname) > 0) {
5338+
if (PyUnicode_GetLength(fname) > 0) {
53495339

53505340
*previous = _add_field(interned_fields, fname, ftype,
53515341
field_offset_bytes, bitshift, fbitsize,
@@ -6000,7 +5990,6 @@ static void _my_PyErr_WriteUnraisable(PyObject *t, PyObject *v, PyObject *tb,
60005990
char *extra_error_line)
60015991
{
60025992
/* like PyErr_WriteUnraisable(), but write a full traceback */
6003-
#ifdef USE_WRITEUNRAISABLEMSG
60045993

60055994
/* PyErr_WriteUnraisable actually writes the full traceback anyway
60065995
from Python 3.4, but we can't really get the formatting of the
@@ -6037,34 +6026,6 @@ static void _my_PyErr_WriteUnraisable(PyObject *t, PyObject *v, PyObject *tb,
60376026
else
60386027
PyErr_WriteUnraisable(obj); /* best effort */
60396028
PyErr_Clear();
6040-
6041-
#else
6042-
6043-
/* version for Python 2.7 and < 3.8 */
6044-
PyObject *f;
6045-
/* jump through hoops to ensure the tb is attached to v, on Python 3 */
6046-
PyErr_NormalizeException(&t, &v, &tb);
6047-
if (tb == NULL) {
6048-
tb = Py_None;
6049-
Py_INCREF(tb);
6050-
}
6051-
PyException_SetTraceback(v, tb);
6052-
f = PySys_GetObject("stderr");
6053-
if (f != NULL) {
6054-
if (obj != NULL) {
6055-
PyFile_WriteString(objdescr, f);
6056-
PyFile_WriteObject(obj, f, 0);
6057-
PyFile_WriteString(":\n", f);
6058-
}
6059-
if (extra_error_line != NULL)
6060-
PyFile_WriteString(extra_error_line, f);
6061-
PyErr_Display(t, v, tb);
6062-
}
6063-
Py_XDECREF(t);
6064-
Py_XDECREF(v);
6065-
Py_XDECREF(tb);
6066-
6067-
#endif
60686029
}
60696030

60706031
static void general_invoke_callback(int decode_args_from_libffi,
@@ -6114,11 +6075,7 @@ static void general_invoke_callback(int decode_args_from_libffi,
61146075
goto error;
61156076
if (convert_from_object_fficallback(result, SIGNATURE(1), py_res,
61166077
decode_args_from_libffi) < 0) {
6117-
#ifdef USE_WRITEUNRAISABLEMSG
61186078
extra_error_line = ", trying to convert the result back to C";
6119-
#else
6120-
extra_error_line = "Trying to convert the result back to C:\n";
6121-
#endif
61226079
goto error;
61236080
}
61246081
done:
@@ -6170,16 +6127,9 @@ static void general_invoke_callback(int decode_args_from_libffi,
61706127
_my_PyErr_WriteUnraisable(exc1, val1, tb1,
61716128
"From cffi callback ", py_ob,
61726129
extra_error_line);
6173-
#ifdef USE_WRITEUNRAISABLEMSG
61746130
_my_PyErr_WriteUnraisable(exc2, val2, tb2,
61756131
"during handling of the above exception by 'onerror'",
61766132
NULL, NULL);
6177-
#else
6178-
extra_error_line = ("\nDuring the call to 'onerror', "
6179-
"another exception occurred:\n\n");
6180-
_my_PyErr_WriteUnraisable(exc2, val2, tb2,
6181-
NULL, NULL, extra_error_line);
6182-
#endif
61836133
_cffi_stop_error_capture(ecap);
61846134
}
61856135
}
@@ -6247,14 +6197,6 @@ static PyObject *prepare_callback_info_tuple(CTypeDescrObject *ct,
62476197
infotuple = Py_BuildValue("OOOO", ct, ob, py_rawerr, onerror_ob);
62486198
Py_DECREF(py_rawerr);
62496199

6250-
#if defined(WITH_THREAD) && PY_VERSION_HEX < 0x03070000
6251-
/* We must setup the GIL here, in case the callback is invoked in
6252-
some other non-Pythonic thread. This is the same as ctypes.
6253-
But PyEval_InitThreads() is always a no-op from CPython 3.7
6254-
(the call from ctypes was removed some time later I think). */
6255-
PyEval_InitThreads();
6256-
#endif
6257-
62586200
return infotuple;
62596201
}
62606202

src/c/call_python.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
1-
#if PY_VERSION_HEX >= 0x03080000
2-
# define HAVE_PYINTERPSTATE_GETDICT
3-
#endif
4-
5-
61
static PyObject *_current_interp_key(void)
72
{
83
PyInterpreterState *interp = PyThreadState_GET()->interp;
9-
#ifdef HAVE_PYINTERPSTATE_GETDICT
104
return PyInterpreterState_GetDict(interp); /* shared reference */
11-
#else
12-
return interp->modules;
13-
#endif
145
}
156

167
static PyObject *_get_interpstate_dict(void)
@@ -33,11 +24,7 @@ static PyObject *_get_interpstate_dict(void)
3324
}
3425

3526
interp = tstate->interp;
36-
#ifdef HAVE_PYINTERPSTATE_GETDICT
3727
interpdict = PyInterpreterState_GetDict(interp); /* shared reference */
38-
#else
39-
interpdict = interp->builtins;
40-
#endif
4128
if (interpdict == NULL) {
4229
/* subinterpreter was cleared already, or is being cleared right now,
4330
to a point that is too much for us to continue */

src/c/minibuffer.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,6 @@ mb_richcompare(PyObject *self, PyObject *other, int op)
208208

209209
/* pfffffffffffff pages of copy-paste from listobject.c */
210210

211-
/* pfffffffffffff#2: the PySlice_GetIndicesEx() *macro* should not
212-
be called, because C extension modules compiled with it differ
213-
on ABI between 3.6.0, 3.6.1 and 3.6.2. */
214-
#if PY_VERSION_HEX < 0x03070000 && defined(PySlice_GetIndicesEx) && !defined(PYPY_VERSION)
215-
#undef PySlice_GetIndicesEx
216-
#endif
217-
218211
static PyObject *mb_subscript(MiniBufferObj *self, PyObject *item)
219212
{
220213
if (PyIndex_Check(item)) {

src/c/misc_thread_common.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -325,20 +325,12 @@ static void restore_errno_only(void)
325325
It was added in 3.5.2 but should never be used in 3.5.x
326326
because it is not available in 3.5.0 or 3.5.1.
327327
*/
328-
#if PY_VERSION_HEX >= 0x03050100 && PY_VERSION_HEX < 0x03060000
329-
PyAPI_DATA(void *volatile) _PyThreadState_Current;
330-
#endif
331-
332328
static PyThreadState *get_current_ts(void)
333329
{
334330
#if PY_VERSION_HEX >= 0x030D0000
335331
return PyThreadState_GetUnchecked();
336-
#elif PY_VERSION_HEX >= 0x03060000
337-
return _PyThreadState_UncheckedGet();
338-
#elif defined(_Py_atomic_load_relaxed)
339-
return (PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current);
340332
#else
341-
return (PyThreadState*)_PyThreadState_Current; /* assume atomic read */
333+
return _PyThreadState_UncheckedGet();
342334
#endif
343335
}
344336

src/cffi/_embedding.h

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,6 @@ static int _cffi_initialize_python(void)
244244
goto done;
245245
}
246246

247-
#if PY_VERSION_HEX < 0x03080000
248-
PyAPI_DATA(char *) _PyParser_TokenNames[]; /* from CPython */
249-
#endif
250-
251247
static int _cffi_carefully_make_gil(void)
252248
{
253249
/* This does the basic initialization of Python. It can be called
@@ -292,27 +288,6 @@ static int _cffi_carefully_make_gil(void)
292288
*/
293289

294290
#ifdef WITH_THREAD
295-
# if PY_VERSION_HEX < 0x03080000
296-
char *volatile *lock = (char *volatile *)_PyParser_TokenNames;
297-
char *old_value, *locked_value;
298-
299-
while (1) { /* spin loop */
300-
old_value = *lock;
301-
locked_value = old_value + 1;
302-
if (old_value[0] == 'E') {
303-
assert(old_value[1] == 'N');
304-
if (cffi_compare_and_swap(lock, old_value, locked_value))
305-
break;
306-
}
307-
else {
308-
assert(old_value[0] == 'N');
309-
/* should ideally do a spin loop instruction here, but
310-
hard to do it portably and doesn't really matter I
311-
think: PyEval_InitThreads() should be very fast, and
312-
this is only run at start-up anyway. */
313-
}
314-
}
315-
# else
316291
# if PY_VERSION_HEX < 0x030C0000
317292
int volatile *lock = (int volatile *)&PyCapsule_Type.tp_version_tag;
318293
int old_value, locked_value = -42;
@@ -345,26 +320,16 @@ static int _cffi_carefully_make_gil(void)
345320
this is only run at start-up anyway. */
346321
}
347322
}
348-
# endif
349323
#endif
350324

351325
/* call Py_InitializeEx() */
352326
if (!Py_IsInitialized()) {
353327
_cffi_py_initialize();
354-
#if PY_VERSION_HEX < 0x03070000
355-
PyEval_InitThreads();
356-
#endif
357328
PyEval_SaveThread(); /* release the GIL */
358329
/* the returned tstate must be the one that has been stored into the
359330
autoTLSkey by _PyGILState_Init() called from Py_Initialize(). */
360331
}
361332
else {
362-
#if PY_VERSION_HEX < 0x03070000
363-
/* PyEval_InitThreads() is always a no-op from CPython 3.7 */
364-
PyGILState_STATE state = PyGILState_Ensure();
365-
PyEval_InitThreads();
366-
PyGILState_Release(state);
367-
#endif
368333
}
369334

370335
#ifdef WITH_THREAD

0 commit comments

Comments
 (0)