Skip to content

Commit

Permalink
Remove support for Python <3.8 from C code
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
rtobar committed Dec 29, 2024
1 parent 88ef0b5 commit db6983c
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 127 deletions.
68 changes: 5 additions & 63 deletions src/c/_cffi_backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,10 @@
#include "malloc_closure.h"


#if PY_VERSION_HEX >= 0x03030000
# define PyText_GetSize PyUnicode_GetLength
#else
# define PyText_GetSize PyUnicode_GetSize
#endif

#if PY_VERSION_HEX < 0x030900a4
# define Py_SET_REFCNT(obj, val) (Py_REFCNT(obj) = (val))
#endif

#if PY_VERSION_HEX >= 0x03080000
# define USE_WRITEUNRAISABLEMSG
#endif

/************************************************************/

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

sz1 = PyText_GetSize(filename_unicode) + 1;
sz1 = PyUnicode_GetLength(filename_unicode) + 1;
sz1 *= 2; /* should not be needed, but you never know */
w1 = alloca(sizeof(wchar_t) * sz1);
sz1 = PyUnicode_AsWideChar(filename_unicode,
Expand Down Expand Up @@ -5141,7 +5131,7 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
if (!(sflags & SF_GCC_ARM_BITFIELDS) && fbitsize >= 0) {
if (!(sflags & SF_MSVC_BITFIELDS)) {
/* GCC: anonymous bitfields (of any size) don't cause alignment */
do_align = PyText_GetSize(fname) > 0;
do_align = PyUnicode_GetLength(fname) > 0;
}
else {
/* MSVC: zero-sized bitfields don't cause alignment */
Expand Down Expand Up @@ -5185,7 +5175,7 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
byteoffset = foffset;
}

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

if (fbitsize == 0) {
if (PyText_GetSize(fname) > 0) {
if (PyUnicode_GetLength(fname) > 0) {
PyErr_Format(PyExc_TypeError,
"field '%s.%s' is declared with :0",
ct->ct_name, PyUnicode_AsUTF8(fname));
Expand Down Expand Up @@ -5345,7 +5335,7 @@ static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
if (sflags & SF_GCC_BIG_ENDIAN)
bitshift = 8 * ftype->ct_size - fbitsize - bitshift;

if (PyText_GetSize(fname) > 0) {
if (PyUnicode_GetLength(fname) > 0) {

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

/* PyErr_WriteUnraisable actually writes the full traceback anyway
from Python 3.4, but we can't really get the formatting of the
Expand Down Expand Up @@ -6037,34 +6026,6 @@ static void _my_PyErr_WriteUnraisable(PyObject *t, PyObject *v, PyObject *tb,
else
PyErr_WriteUnraisable(obj); /* best effort */
PyErr_Clear();

#else

/* version for Python 2.7 and < 3.8 */
PyObject *f;
/* jump through hoops to ensure the tb is attached to v, on Python 3 */
PyErr_NormalizeException(&t, &v, &tb);
if (tb == NULL) {
tb = Py_None;
Py_INCREF(tb);
}
PyException_SetTraceback(v, tb);
f = PySys_GetObject("stderr");
if (f != NULL) {
if (obj != NULL) {
PyFile_WriteString(objdescr, f);
PyFile_WriteObject(obj, f, 0);
PyFile_WriteString(":\n", f);
}
if (extra_error_line != NULL)
PyFile_WriteString(extra_error_line, f);
PyErr_Display(t, v, tb);
}
Py_XDECREF(t);
Py_XDECREF(v);
Py_XDECREF(tb);

#endif
}

static void general_invoke_callback(int decode_args_from_libffi,
Expand Down Expand Up @@ -6114,11 +6075,7 @@ static void general_invoke_callback(int decode_args_from_libffi,
goto error;
if (convert_from_object_fficallback(result, SIGNATURE(1), py_res,
decode_args_from_libffi) < 0) {
#ifdef USE_WRITEUNRAISABLEMSG
extra_error_line = ", trying to convert the result back to C";
#else
extra_error_line = "Trying to convert the result back to C:\n";
#endif
goto error;
}
done:
Expand Down Expand Up @@ -6170,16 +6127,9 @@ static void general_invoke_callback(int decode_args_from_libffi,
_my_PyErr_WriteUnraisable(exc1, val1, tb1,
"From cffi callback ", py_ob,
extra_error_line);
#ifdef USE_WRITEUNRAISABLEMSG
_my_PyErr_WriteUnraisable(exc2, val2, tb2,
"during handling of the above exception by 'onerror'",
NULL, NULL);
#else
extra_error_line = ("\nDuring the call to 'onerror', "
"another exception occurred:\n\n");
_my_PyErr_WriteUnraisable(exc2, val2, tb2,
NULL, NULL, extra_error_line);
#endif
_cffi_stop_error_capture(ecap);
}
}
Expand Down Expand Up @@ -6247,14 +6197,6 @@ static PyObject *prepare_callback_info_tuple(CTypeDescrObject *ct,
infotuple = Py_BuildValue("OOOO", ct, ob, py_rawerr, onerror_ob);
Py_DECREF(py_rawerr);

#if defined(WITH_THREAD) && PY_VERSION_HEX < 0x03070000
/* We must setup the GIL here, in case the callback is invoked in
some other non-Pythonic thread. This is the same as ctypes.
But PyEval_InitThreads() is always a no-op from CPython 3.7
(the call from ctypes was removed some time later I think). */
PyEval_InitThreads();
#endif

return infotuple;
}

Expand Down
13 changes: 0 additions & 13 deletions src/c/call_python.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
#if PY_VERSION_HEX >= 0x03080000
# define HAVE_PYINTERPSTATE_GETDICT
#endif


static PyObject *_current_interp_key(void)
{
PyInterpreterState *interp = PyThreadState_GET()->interp;
#ifdef HAVE_PYINTERPSTATE_GETDICT
return PyInterpreterState_GetDict(interp); /* shared reference */
#else
return interp->modules;
#endif
}

static PyObject *_get_interpstate_dict(void)
Expand All @@ -33,11 +24,7 @@ static PyObject *_get_interpstate_dict(void)
}

interp = tstate->interp;
#ifdef HAVE_PYINTERPSTATE_GETDICT
interpdict = PyInterpreterState_GetDict(interp); /* shared reference */
#else
interpdict = interp->builtins;
#endif
if (interpdict == NULL) {
/* subinterpreter was cleared already, or is being cleared right now,
to a point that is too much for us to continue */
Expand Down
7 changes: 0 additions & 7 deletions src/c/minibuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,6 @@ mb_richcompare(PyObject *self, PyObject *other, int op)

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

/* pfffffffffffff#2: the PySlice_GetIndicesEx() *macro* should not
be called, because C extension modules compiled with it differ
on ABI between 3.6.0, 3.6.1 and 3.6.2. */
#if PY_VERSION_HEX < 0x03070000 && defined(PySlice_GetIndicesEx) && !defined(PYPY_VERSION)
#undef PySlice_GetIndicesEx
#endif

static PyObject *mb_subscript(MiniBufferObj *self, PyObject *item)
{
if (PyIndex_Check(item)) {
Expand Down
10 changes: 1 addition & 9 deletions src/c/misc_thread_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,20 +325,12 @@ static void restore_errno_only(void)
It was added in 3.5.2 but should never be used in 3.5.x
because it is not available in 3.5.0 or 3.5.1.
*/
#if PY_VERSION_HEX >= 0x03050100 && PY_VERSION_HEX < 0x03060000
PyAPI_DATA(void *volatile) _PyThreadState_Current;
#endif

static PyThreadState *get_current_ts(void)
{
#if PY_VERSION_HEX >= 0x030D0000
return PyThreadState_GetUnchecked();
#elif PY_VERSION_HEX >= 0x03060000
return _PyThreadState_UncheckedGet();
#elif defined(_Py_atomic_load_relaxed)
return (PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current);
#else
return (PyThreadState*)_PyThreadState_Current; /* assume atomic read */
return _PyThreadState_UncheckedGet();
#endif
}

Expand Down
35 changes: 0 additions & 35 deletions src/cffi/_embedding.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,6 @@ static int _cffi_initialize_python(void)
goto done;
}

#if PY_VERSION_HEX < 0x03080000
PyAPI_DATA(char *) _PyParser_TokenNames[]; /* from CPython */
#endif

static int _cffi_carefully_make_gil(void)
{
/* This does the basic initialization of Python. It can be called
Expand Down Expand Up @@ -292,27 +288,6 @@ static int _cffi_carefully_make_gil(void)
*/

#ifdef WITH_THREAD
# if PY_VERSION_HEX < 0x03080000
char *volatile *lock = (char *volatile *)_PyParser_TokenNames;
char *old_value, *locked_value;

while (1) { /* spin loop */
old_value = *lock;
locked_value = old_value + 1;
if (old_value[0] == 'E') {
assert(old_value[1] == 'N');
if (cffi_compare_and_swap(lock, old_value, locked_value))
break;
}
else {
assert(old_value[0] == 'N');
/* should ideally do a spin loop instruction here, but
hard to do it portably and doesn't really matter I
think: PyEval_InitThreads() should be very fast, and
this is only run at start-up anyway. */
}
}
# else
# if PY_VERSION_HEX < 0x030C0000
int volatile *lock = (int volatile *)&PyCapsule_Type.tp_version_tag;
int old_value, locked_value = -42;
Expand Down Expand Up @@ -345,26 +320,16 @@ static int _cffi_carefully_make_gil(void)
this is only run at start-up anyway. */
}
}
# endif
#endif

/* call Py_InitializeEx() */
if (!Py_IsInitialized()) {
_cffi_py_initialize();
#if PY_VERSION_HEX < 0x03070000
PyEval_InitThreads();
#endif
PyEval_SaveThread(); /* release the GIL */
/* the returned tstate must be the one that has been stored into the
autoTLSkey by _PyGILState_Init() called from Py_Initialize(). */
}
else {
#if PY_VERSION_HEX < 0x03070000
/* PyEval_InitThreads() is always a no-op from CPython 3.7 */
PyGILState_STATE state = PyGILState_Ensure();
PyEval_InitThreads();
PyGILState_Release(state);
#endif
}

#ifdef WITH_THREAD
Expand Down

0 comments on commit db6983c

Please sign in to comment.