Skip to content

Commit 38692dd

Browse files
committed
interrogate: Support subclassing C++ objects from Python
This change enables persistent Python wrapper objects for Python subclasses of typed, reference counted C++ objects. That means that these objects will store a reference to `self` on the C++ object, and interrogate will always return that instead of making a new Python wrapper every time it is returned from C++. Practically, this means that you could subclass eg. PandaNode, Event, Fog, what have you, and store these in the scene graph - the Python data in the subclass will be retained and Panda will return your subclass when you ask for the object back rather than creating a new wrapper object without your original data. To do this, Interrogate generates a proxy class inheriting from the C++ type with additional room to store a `self` pointer and a TypeHandle (which is returned by an overridden `get_type()`). This TypeHandle is automatically created by registering the Python subclass with the typing system. The proxy class is only used when the constructor detects that it's constructing for a subtype, so that regular uses of the C++ type are not affected by this mechanism. (The registration with the typing system could use some improvement. There's no regard for namespacing right now, in particular. Furthermore, we could move the registration to an `__init_subclass__()` method, with parameters to specify an existing TypeHandle or to customize the type name.) This creates a reference cycle, which must be cleared somehow. One way this happens is by overriding `unref()` in the proxy, which checks that if the Python and C++ reference counts are both 1, this must be the circular reference, and then it breaks the cycle. Note that this will _only_ work if all other Python references have been cleared before the last C++ reference goes away. For the other case, we need to rely on Python's garbage collector, so these classes also implement tp_traverse and tp_clear. This commit also therefore re-enables Python garbage collector support (ie. defining `__traverse__()`), which was previously disabled due to the problems caused by multiple Python wrappers referring to the same C++ object. To avoid these problems, Panda will only cooperate with Python's GC if the C++ reference count is 1, in which case we can trivially prove that the last reference must be from the Python wrapper. Note that this explains why we need persistent wrappers for traversal to work--if there are multiple Python wrappers pointing to the C++ object, and they are all participating in a reference cycle, the reference count cannot be 1, and cycle detection does not work. By default, the GC is only enabled for Python subclasses, but a class may define `__traverse__()` in order to opt-in, keeping the previous limitation in mind. There is a second mechanism introduced by this commit: classes may define a public `__self__` member of type `PyObject *` if they want to use persistent objects even if they aren't being subclassed. This allows these classes to participate in Python's GC and avoid the situation above. The cycle detection is implemented for PandaNode's Python tags, but this is very incomplete, since the traversal doesn't recurse into children and it won't work if there is more than one wrapper object that is part of a cycle, since PandaNode by default doesn't use persistent wrappers. This problem may need more attention later. Fixes panda3d#1410
1 parent 33cef0a commit 38692dd

File tree

11 files changed

+660
-75
lines changed

11 files changed

+660
-75
lines changed

dtool/src/interrogate/functionRemap.cxx

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,6 @@ get_call_str(const string &container, const vector_string &pexprs) const {
450450
_parameters[pn]._remap->pass_parameter(call, get_parameter_expr(pn, pexprs));
451451

452452
} else {
453-
const char *separator = "";
454-
455453
// If this function is marked as having an extension function, call that
456454
// instead.
457455
if (_extension) {
@@ -488,33 +486,41 @@ get_call_str(const string &container, const vector_string &pexprs) const {
488486
}
489487
}
490488
call << "(";
491-
492-
if (_flags & F_explicit_self) {
493-
// Pass on the PyObject * that we stripped off above.
494-
call << separator << "self";
495-
separator = ", ";
496-
}
497-
if (_flags & F_explicit_cls) {
498-
call << separator << "cls";
499-
separator = ", ";
500-
}
501-
502-
size_t pn;
503-
size_t num_parameters = pexprs.size();
504-
505-
for (pn = _first_true_parameter;
506-
pn < num_parameters; ++pn) {
507-
nassertd(pn < _parameters.size()) break;
508-
call << separator;
509-
_parameters[pn]._remap->pass_parameter(call, get_parameter_expr(pn, pexprs));
510-
separator = ", ";
511-
}
489+
write_call_args(call, pexprs);
512490
call << ")";
513491
}
514492

515493
return call.str();
516494
}
517495

496+
/**
497+
* Writes the arguments to pass to the function.
498+
*/
499+
void FunctionRemap::
500+
write_call_args(std::ostream &call, const vector_string &pexprs) const {
501+
const char *separator = "";
502+
if (_flags & F_explicit_self) {
503+
// Pass on the PyObject * that we stripped off above.
504+
call << separator << "self";
505+
separator = ", ";
506+
}
507+
if (_flags & F_explicit_cls) {
508+
call << separator << "cls";
509+
separator = ", ";
510+
}
511+
512+
size_t pn;
513+
size_t num_parameters = pexprs.size();
514+
515+
for (pn = _first_true_parameter;
516+
pn < num_parameters; ++pn) {
517+
nassertd(pn < _parameters.size()) break;
518+
call << separator;
519+
_parameters[pn]._remap->pass_parameter(call, get_parameter_expr(pn, pexprs));
520+
separator = ", ";
521+
}
522+
}
523+
518524
/**
519525
* Returns the minimum number of arguments that needs to be passed to this
520526
* function.

dtool/src/interrogate/functionRemap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class FunctionRemap {
6161
FunctionWrapperIndex make_wrapper_entry(FunctionIndex function_index);
6262

6363
std::string get_call_str(const std::string &container, const vector_string &pexprs) const;
64+
void write_call_args(std::ostream &out, const vector_string &pexprs) const;
6465

6566
int get_min_num_args() const;
6667
int get_max_num_args() const;

0 commit comments

Comments
 (0)