Skip to content

Dict traversal and CachedProperty updates. #483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 1, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implemented circular references traversal for Boost.Python.instance o…
…bjects.
  • Loading branch information
jordanbriere committed Jun 17, 2023
commit ab33bc872f319140e5e9361fd4d30a74b803405f
37 changes: 37 additions & 0 deletions src/core/sp_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ CPythonManager g_PythonManager;
// Forward declarations.
//---------------------------------------------------------------------------------
void InitConverters();
void EnableDictTraversal();


//---------------------------------------------------------------------------------
Expand Down Expand Up @@ -168,6 +169,9 @@ bool CPythonManager::Initialize( void )
// And of course, the plugins directory for script imports.
AddToSysPath("/plugins");

// Enable circular references traversal
EnableDictTraversal();

// Initialize all converters
InitConverters();

Expand Down Expand Up @@ -262,6 +266,39 @@ bool CPythonManager::Shutdown( void )
}


//---------------------------------------------------------------------------------
// Circular references traversal
//---------------------------------------------------------------------------------
struct dict_traversal
{
static int is_gc(PyObject *self)
{
return !!downcast<objects::instance<> >(self)->dict;
}

static int traverse(PyObject *self, visitproc visit, void *arg)
{
Py_VISIT(downcast<objects::instance<> >(self)->dict);
return 0;
}

static int clear(PyObject *self)
{
Py_CLEAR(downcast<objects::instance<> >(self)->dict);
return 0;
}
};

void EnableDictTraversal()
{
PyTypeObject *type = objects::class_type().get();
type->tp_flags |= Py_TPFLAGS_HAVE_GC;
type->tp_is_gc = dict_traversal::is_gc;
type->tp_traverse = dict_traversal::traverse;
type->tp_clear = dict_traversal::clear;
}


//---------------------------------------------------------------------------------
// Converters
//---------------------------------------------------------------------------------
Expand Down