Skip to content

Commit

Permalink
drop "from cppyy.interactive *" feature from 3.11 onward
Browse files Browse the repository at this point in the history
  • Loading branch information
wlav committed Jun 25, 2022
1 parent dd183a0 commit 64fd890
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/API.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ static bool Initialize()
#if PY_VERSION_HEX < 0x03020000
PyEval_InitThreads();
#endif
#if PY_VERSION_HEX < 0x03080000
Py_Initialize();
#else
PyConfig config;
PyConfig_InitPythonConfig(&config);
PyConfig_SetString(&config, &config.program_name, L"cppyy");
Py_InitializeFromConfig(&config);
#endif
#if PY_VERSION_HEX >= 0x03020000
#if PY_VERSION_HEX < 0x03090000
PyEval_InitThreads();
Expand All @@ -66,10 +73,12 @@ static bool Initialize()
// set the command line arguments on python's sys.argv
#if PY_VERSION_HEX < 0x03000000
char* argv[] = {const_cast<char*>("cppyy")};
#else
#elif PY_VERSION_HEX < 0x03080000
wchar_t* argv[] = {const_cast<wchar_t*>(L"cppyy")};
#endif
#if PY_VERSION_HEX < 0x03080000
PySys_SetArgv(sizeof(argv)/sizeof(argv[0]), argv);
#endif

// force loading of the cppyy module
PyRun_SimpleString(const_cast<char*>("import cppyy"));
Expand Down
1 change: 0 additions & 1 deletion src/CPPInstance.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,6 @@ PyTypeObject CPPInstance_Type = {
0, // tp_as_buffer
Py_TPFLAGS_DEFAULT |
Py_TPFLAGS_BASETYPE |
Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_CHECKTYPES, // tp_flags
(char*)"cppyy object proxy (internal)", // tp_doc
0, // tp_traverse
Expand Down
2 changes: 2 additions & 0 deletions src/CPPOverload.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include "CPyCppyy/Reflex.h"
#include "structmember.h" // from Python
#if PY_VERSION_HEX >= 0x02050000
#if PY_VERSION_HEX < 0x030b0000
#include "code.h" // from Python
#endif
#else
#include "compile.h" // from Python
#endif
Expand Down
5 changes: 4 additions & 1 deletion src/CPyCppyy.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ namespace CPyCppyy {
#if PY_VERSION_HEX < 0x03030000
typedef PyDictEntry* (*dict_lookup_func)(PyDictObject*, PyObject*, long);
#else
#if PY_VERSION_HEX >= 0x03060000
#if PY_VERSION_HEX >= 0x030b0000
typedef Py_ssize_t (*dict_lookup_func)(
PyDictObject*, PyObject*, Py_hash_t, PyObject**);
#elif PY_VERSION_HEX >= 0x03060000
typedef Py_ssize_t (*dict_lookup_func)(
PyDictObject*, PyObject*, Py_hash_t, PyObject***, Py_ssize_t*);
#else
Expand Down
19 changes: 19 additions & 0 deletions src/CPyCppyyModule.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
#include <vector>


// Note: as of py3.11, dictionary objects no longer carry a function pointer for
// the lookup, so it can no longer be shimmed and "from cppyy.interactive import *"
// thus no longer works.
#if PY_VERSION_HEX < 0x030b0000

//- from Python's dictobject.c -------------------------------------------------
#if PY_VERSION_HEX >= 0x03030000
typedef struct PyDictKeyEntry {
Expand Down Expand Up @@ -70,6 +75,8 @@

#endif

#endif // PY_VERSION_HEX < 0x030b0000

//- data -----------------------------------------------------------------------
static PyObject* nullptr_repr(PyObject*)
{
Expand Down Expand Up @@ -184,7 +191,9 @@ namespace {

using namespace CPyCppyy;


//----------------------------------------------------------------------------
#if PY_VERSION_HEX < 0x030b0000
namespace {

class GblGetter {
Expand Down Expand Up @@ -337,9 +346,12 @@ PyDictEntry* CPyCppyyLookDictString(PyDictObject* mp, PyObject* key, long hash)
return ep;
}

#endif // PY_VERSION_HEX < 0x030b0000

//----------------------------------------------------------------------------
static PyObject* SetCppLazyLookup(PyObject*, PyObject* args)
{
#if PY_VERSION_HEX < 0x030b0000
// Modify the given dictionary to install the lookup function that also
// tries the global C++ namespace before failing. Called on a module's dictionary,
// this allows for lazy lookups. This works fine for p3.2 and earlier, but should
Expand All @@ -350,6 +362,11 @@ static PyObject* SetCppLazyLookup(PyObject*, PyObject* args)
return nullptr;

CPYCPPYY_GET_DICT_LOOKUP(dict) = CPyCppyyLookDictString;
#else
// As of py3.11, there is no longer a lookup function pointer in the dict object
// to replace. Since this feature is not widely advertised, it's simply droped
PyErr_Warn(PyExc_RuntimeWarning, (char*)"lazy lookup is no longer supported");
#endif

Py_RETURN_NONE;
}
Expand Down Expand Up @@ -941,6 +958,7 @@ extern "C" void initlibcppyy()
PyEval_InitThreads();
#endif

#if PY_VERSION_HEX < 0x030b0000
// prepare for lazyness (the insert is needed to capture the most generic lookup
// function, just in case ...)
PyObject* dict = PyDict_New();
Expand All @@ -953,6 +971,7 @@ extern "C" void initlibcppyy()
gDictLookupOrg = (dict_lookup_func)((PyDictObject*)dict)->ma_lookup;
#endif
Py_DECREF(dict);
#endif // PY_VERSION_HEX < 0x030b0000

// setup this module
#if PY_VERSION_HEX >= 0x03000000
Expand Down
2 changes: 1 addition & 1 deletion src/MemoryRegulator.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct InitCPyCppyy_NoneType_t {
((PyVarObject&)CPyCppyy_NoneType).ob_size = 0;

CPyCppyy_NoneType.tp_name = const_cast<char*>("CPyCppyy_NoneType");
CPyCppyy_NoneType.tp_flags = Py_TPFLAGS_HAVE_RICHCOMPARE | Py_TPFLAGS_HAVE_GC;
CPyCppyy_NoneType.tp_flags = Py_TPFLAGS_HAVE_RICHCOMPARE;

CPyCppyy_NoneType.tp_traverse = (traverseproc)0;
CPyCppyy_NoneType.tp_clear = (inquiry)0;
Expand Down
2 changes: 2 additions & 0 deletions src/Utility.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@


//- data _____________________________________________________________________
#if PY_VERSION_HEX < 0x030b0000
dict_lookup_func CPyCppyy::gDictLookupOrg = 0;
bool CPyCppyy::gDictLookupActive = false;
#endif

typedef std::map<std::string, std::string> TC2POperatorMapping_t;
static TC2POperatorMapping_t gC2POperatorMapping;
Expand Down
2 changes: 2 additions & 0 deletions src/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ namespace CPyCppyy {

class PyCallable;

#if PY_VERSION_HEX < 0x030b0000
extern dict_lookup_func gDictLookupOrg;
extern bool gDictLookupActive;
#endif

// additional converter functions
unsigned long PyLongOrInt_AsULong(PyObject* pyobject);
Expand Down

0 comments on commit 64fd890

Please sign in to comment.