Skip to content

Commit

Permalink
update cffi + fix emplace calls
Browse files Browse the repository at this point in the history
  • Loading branch information
rkaminsk committed Dec 11, 2024
1 parent 72c4061 commit de31874
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
4 changes: 4 additions & 0 deletions app/pyclingo/_clingo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <complex.h> for it */
# define _cffi_double_complex_t _Dcomplex /* include <complex.h> for it */
#else
# include <stdint.h>
# if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux)
# include <alloca.h>
# endif
# define _cffi_float_complex_t float _Complex
# define _cffi_double_complex_t double _Complex
#endif

#ifdef __GNUC__
Expand Down
4 changes: 2 additions & 2 deletions libgringo/src/input/nongroundparser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ void NonGroundParser::pushFile(std::string &&file, Logger &log) {
}

void NonGroundParser::pushStream(std::string &&file, std::unique_ptr<std::istream> in, Logger &log) {
auto res = filenames_.emplace(std::move(file), false);
auto res = filenames_.emplace(std::move(file));
if (!res.second) {
report_included("<cmd>", res.first->c_str(), log);
}
Expand Down Expand Up @@ -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)) {
Expand Down
32 changes: 29 additions & 3 deletions libpyclingo/_clingo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <complex.h> for it */
# define _cffi_double_complex_t _Dcomplex /* include <complex.h> for it */
#else
# include <stdint.h>
# if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux)
# include <alloca.h>
# endif
# define _cffi_float_complex_t float _Complex
# define _cffi_double_complex_t double _Complex
#endif

#ifdef __GNUC__
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit de31874

Please sign in to comment.