From de318741ccc8c798e496beccb47fdf7db9bc97a7 Mon Sep 17 00:00:00 2001 From: Roland Kaminski Date: Wed, 11 Dec 2024 15:23:29 +0100 Subject: [PATCH] update cffi + fix emplace calls --- app/pyclingo/_clingo.c | 4 ++++ libgringo/src/input/nongroundparser.cc | 4 ++-- libpyclingo/_clingo.c | 32 +++++++++++++++++++++++--- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/app/pyclingo/_clingo.c b/app/pyclingo/_clingo.c index 91007e19c..3cfdabf06 100644 --- a/app/pyclingo/_clingo.c +++ b/app/pyclingo/_clingo.c @@ -281,11 +281,15 @@ static int search_in_struct_unions(const struct _cffi_type_context_s *ctx, typedef unsigned char _Bool; # endif # endif +# define _cffi_float_complex_t _Fcomplex /* include for it */ +# define _cffi_double_complex_t _Dcomplex /* include for it */ #else # include # if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux) # include # endif +# define _cffi_float_complex_t float _Complex +# define _cffi_double_complex_t double _Complex #endif #ifdef __GNUC__ diff --git a/libgringo/src/input/nongroundparser.cc b/libgringo/src/input/nongroundparser.cc index 6e13e096f..a6d513879 100644 --- a/libgringo/src/input/nongroundparser.cc +++ b/libgringo/src/input/nongroundparser.cc @@ -275,7 +275,7 @@ void NonGroundParser::pushFile(std::string &&file, Logger &log) { } void NonGroundParser::pushStream(std::string &&file, std::unique_ptr in, Logger &log) { - auto res = filenames_.emplace(std::move(file), false); + auto res = filenames_.emplace(std::move(file)); if (!res.second) { report_included("", res.first->c_str(), log); } @@ -344,7 +344,7 @@ void NonGroundParser::include(String file, Location const &loc, bool inbuilt, Lo } else { auto paths = check_file(file.c_str(), loc.beginFilename.c_str()); - if (!paths.first.empty() && !filenames_.emplace(paths.first, false).second) { + if (!paths.first.empty() && !filenames_.emplace(paths.first).second) { report_included(loc, file.c_str(), log); } else if (paths.first.empty() || !push(paths.second, true)) { diff --git a/libpyclingo/_clingo.c b/libpyclingo/_clingo.c index ce29a3d8f..c1037147f 100644 --- a/libpyclingo/_clingo.c +++ b/libpyclingo/_clingo.c @@ -282,11 +282,15 @@ static int search_in_struct_unions(const struct _cffi_type_context_s *ctx, typedef unsigned char _Bool; # endif # endif +# define _cffi_float_complex_t _Fcomplex /* include for it */ +# define _cffi_double_complex_t _Dcomplex /* include for it */ #else # include # if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux) # include # endif +# define _cffi_float_complex_t float _Complex +# define _cffi_double_complex_t double _Complex #endif #ifdef __GNUC__ @@ -963,7 +967,7 @@ static int _cffi_initialize_python(void) if (f != NULL && f != Py_None) { PyFile_WriteString("\nFrom: " _CFFI_MODULE_NAME - "\ncompiled with cffi version: 1.15.1" + "\ncompiled with cffi version: 1.17.1" "\n_cffi_backend module: ", f); modules = PyImport_GetModuleDict(); mod = PyDict_GetItemString(modules, "_cffi_backend"); @@ -1021,6 +1025,15 @@ static int _cffi_carefully_make_gil(void) Python < 3.8 because someone might use a mixture of cffi embedded modules, some of which were compiled before this file changed. + + In Python >= 3.12, this stopped working because that particular + tp_version_tag gets modified during interpreter startup. It's + arguably a bad idea before 3.12 too, but again we can't change + that because someone might use a mixture of cffi embedded + modules, and no-one reported a bug so far. In Python >= 3.12 + we go instead for PyCapsuleType.tp_as_buffer, which is supposed + to always be NULL. We write to it temporarily a pointer to + a struct full of NULLs, which is semantically the same. */ #ifdef WITH_THREAD @@ -1045,19 +1058,32 @@ static int _cffi_carefully_make_gil(void) } } # else +# if PY_VERSION_HEX < 0x030C0000 int volatile *lock = (int volatile *)&PyCapsule_Type.tp_version_tag; - int old_value, locked_value; + int old_value, locked_value = -42; assert(!(PyCapsule_Type.tp_flags & Py_TPFLAGS_HAVE_VERSION_TAG)); +# else + static struct ebp_s { PyBufferProcs buf; int mark; } empty_buffer_procs; + empty_buffer_procs.mark = -42; + PyBufferProcs *volatile *lock = (PyBufferProcs *volatile *) + &PyCapsule_Type.tp_as_buffer; + PyBufferProcs *old_value, *locked_value = &empty_buffer_procs.buf; +# endif while (1) { /* spin loop */ old_value = *lock; - locked_value = -42; if (old_value == 0) { if (cffi_compare_and_swap(lock, old_value, locked_value)) break; } else { +# if PY_VERSION_HEX < 0x030C0000 assert(old_value == locked_value); +# else + /* The pointer should point to a possibly different + empty_buffer_procs from another C extension module */ + assert(((struct ebp_s *)old_value)->mark == -42); +# endif /* 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