New cppclass.MemoryPolicy and memory_policy CppClass parameter. Closes Bug #247536 (CppClass: improve consistency of memory management parameters).
This commit is contained in:
1 parent
b44e4da782
commit
08e6d918d4
3 files changed
+139
-100
No files matched your search
+105
-75
@@ -19,6 +19,82 @@ import settings
|
||||
import utils
|
||||
|
||||
|
||||
class MemoryPolicy(object):
|
||||
"""memory management policy for a C++ class or C/C++ struct"""
|
||||
def __init__(self):
|
||||
if type(self) is MemoryPolicy:
|
||||
raise NotImplementedError("class is abstract")
|
||||
|
||||
def get_free_code(self, object_expression):
|
||||
"""
|
||||
Return a code statement to free an underlying C/C++ object.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class ReferenceCountingPolicy(MemoryPolicy):
|
||||
def write_incref(self, code_block, obj_expr):
|
||||
"""
|
||||
Write code to increase the reference code of an object of this
|
||||
class (the real C++ class, not the wrapper). Should only be
|
||||
called if the class supports reference counting, as reported
|
||||
by the attribute `has_reference_counting'.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def write_decref(self, code_block, obj_expr):
|
||||
"""
|
||||
Write code to decrease the reference code of an object of this
|
||||
class (the real C++ class, not the wrapper). Should only be
|
||||
called if the class supports reference counting, as reported
|
||||
by the attribute `has_reference_counting'.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class ReferenceCountingMethodsPolicy(ReferenceCountingPolicy):
|
||||
def __init__(self, incref_method, decref_method, peekref_method=None):
|
||||
super(ReferenceCountingMethodsPolicy, self).__init__()
|
||||
self.incref_method = incref_method
|
||||
self.decref_method = decref_method
|
||||
self.peekref_method = peekref_method
|
||||
|
||||
def write_incref(self, code_block, obj_expr):
|
||||
code_block.write_code('%s->%s();' % (obj_expr, self.incref_method))
|
||||
|
||||
def write_decref(self, code_block, obj_expr):
|
||||
code_block.write_code('%s->%s();' % (obj_expr, self.decref_method))
|
||||
|
||||
def get_free_code(self, obj_expr):
|
||||
return ('%s->%s();' % (obj_expr, self.decref_method))
|
||||
|
||||
|
||||
class ReferenceCountingFunctionsPolicy(ReferenceCountingPolicy):
|
||||
def __init__(self, incref_function, decref_function, peekref_function=None):
|
||||
super(ReferenceCountingFunctionsPolicy, self).__init__()
|
||||
self.incref_function = incref_function
|
||||
self.decref_function = decref_function
|
||||
self.peekref_function = peekref_function
|
||||
|
||||
def write_incref(self, code_block, obj_expr):
|
||||
code_block.write_code('%s(%s);' % (self.incref_function, obj_expr))
|
||||
|
||||
def write_decref(self, code_block, obj_expr):
|
||||
code_block.write_code('%s(%s);' % (self.decref_function, obj_expr))
|
||||
|
||||
def get_free_code(self, obj_expr):
|
||||
return ('%s(%s);' % (obj_expr, self.decref_function))
|
||||
|
||||
|
||||
class FreeFunctionPolicy(MemoryPolicy):
|
||||
def __init__(self, free_function):
|
||||
super(FreeFunctionPolicy, self).__init__()
|
||||
self.free_function = free_function
|
||||
|
||||
def get_free_code(self, obj_expr):
|
||||
return ('%s(%s);' % (self.free_function, obj_expr))
|
||||
|
||||
|
||||
def default_instance_creation_function(cpp_class, code_block, lvalue,
|
||||
parameters, construct_type_name):
|
||||
"""
|
||||
@@ -347,7 +423,7 @@ class CppClass(object):
|
||||
template_parameters=(), custom_template_class_name=None,
|
||||
incomplete_type=False, free_function=None,
|
||||
incref_function=None, decref_function=None,
|
||||
python_name=None):
|
||||
python_name=None, memory_policy=None):
|
||||
"""
|
||||
@param name: class name
|
||||
@param parent: optional parent class wrapper
|
||||
@@ -375,6 +451,11 @@ class CppClass(object):
|
||||
@param incref_function: same as incref_method, but as a function instead of method
|
||||
@param decref_function: same as decref_method, but as a function instead of method
|
||||
@param python_name: name of the class as it will appear from Python side
|
||||
|
||||
@param memory_policy: memory management policy; if None, it
|
||||
inherits from the parent class. Only root classes can have a
|
||||
memory policy defined.
|
||||
@type memory_policy: L{MemoryPolicy}
|
||||
"""
|
||||
assert outer_class is None or isinstance(outer_class, CppClass)
|
||||
self.incomplete_type = incomplete_type
|
||||
@@ -421,31 +502,26 @@ class CppClass(object):
|
||||
self.parent = parent
|
||||
assert parent is None or isinstance(parent, CppClass)
|
||||
|
||||
assert (incref_method is None and decref_method is None) \
|
||||
or (incref_method is not None and decref_method is not None)
|
||||
if incref_method is None and parent is not None:
|
||||
self.incref_method = parent.incref_method
|
||||
self.decref_method = parent.decref_method
|
||||
self.peekref_method = parent.peekref_method
|
||||
if parent is None:
|
||||
assert memory_policy is None or isinstance(memory_policy, MemoryPolicy)
|
||||
self.memory_policy = memory_policy
|
||||
if free_function:
|
||||
warnings.warn("Use FreeFunctionPolicy and memory_policy parameter.", DeprecationWarning)
|
||||
assert self.memory_policy is None
|
||||
self.memory_policy = FreeFunctionPolicy(free_function)
|
||||
elif incref_method:
|
||||
warnings.warn("Use ReferenceCountingMethodsPolicy and memory_policy parameter.", DeprecationWarning)
|
||||
assert self.memory_policy is None
|
||||
self.memory_policy = ReferenceCountingMethodsPolicy(incref_method, decref_method, peekref_method)
|
||||
elif incref_function:
|
||||
warnings.warn("Use ReferenceCountingFunctionsPolicy and memory_policy parameter.", DeprecationWarning)
|
||||
assert self.memory_policy is None
|
||||
self.memory_policy = ReferenceCountingFunctionsPolicy(incref_function, decref_function)
|
||||
else:
|
||||
self.incref_method = incref_method
|
||||
self.decref_method = decref_method
|
||||
self.peekref_method = peekref_method
|
||||
assert memory_policy is None, "only root classes may define memory policies"
|
||||
assert not (free_function or incref_method or incref_function), "only root classes may define memory policies"
|
||||
self.memory_policy = None
|
||||
|
||||
self.free_function = free_function
|
||||
|
||||
assert (incref_function is None and decref_function is None) \
|
||||
or (incref_function is not None and decref_function is not None)
|
||||
if incref_function is None and parent is not None:
|
||||
self.incref_function = parent.incref_function
|
||||
self.decref_function = parent.decref_function
|
||||
else:
|
||||
self.incref_function = incref_function
|
||||
self.decref_function = decref_function
|
||||
|
||||
self.has_reference_counting = (self.incref_function is not None
|
||||
or self.incref_method is not None)
|
||||
|
||||
if automatic_type_narrowing is None:
|
||||
if parent is None:
|
||||
self.automatic_type_narrowing = settings.automatic_type_narrowing
|
||||
@@ -601,38 +677,6 @@ class CppClass(object):
|
||||
cls = cls.parent
|
||||
return False
|
||||
|
||||
def write_incref(self, code_block, obj_expr):
|
||||
"""
|
||||
Write code to increase the reference code of an object of this
|
||||
class (the real C++ class, not the wrapper). Should only be
|
||||
called if the class supports reference counting, as reported
|
||||
by the attribute `has_reference_counting'.
|
||||
"""
|
||||
if self.incref_method is not None:
|
||||
assert self.incref_function is None
|
||||
code_block.write_code('%s->%s();' % (obj_expr, self.incref_method))
|
||||
elif self.incref_function is not None:
|
||||
assert self.incref_method is None
|
||||
code_block.write_code('%s(%s);' % (self.incref_function, obj_expr))
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
def write_decref(self, code_block, obj_expr):
|
||||
"""
|
||||
Write code to decrease the reference code of an object of this
|
||||
class (the real C++ class, not the wrapper). Should only be
|
||||
called if the class supports reference counting, as reported
|
||||
by the attribute `has_reference_counting'.
|
||||
"""
|
||||
if self.decref_method is not None:
|
||||
assert self.decref_function is None
|
||||
code_block.write_code('%s->%s();' % (obj_expr, self.decref_method))
|
||||
elif self.decref_function is not None:
|
||||
assert self.decref_method is None
|
||||
code_block.write_code('%s(%s);' % (self.decref_function, obj_expr))
|
||||
else:
|
||||
raise AssertionError
|
||||
|
||||
def add_helper_class_hook(self, hook):
|
||||
"""
|
||||
Add a hook function to be called just prior to a helper class
|
||||
@@ -1482,27 +1526,13 @@ typedef struct {
|
||||
if self.is_singleton:
|
||||
delete_code = ''
|
||||
else:
|
||||
if self.decref_method is not None:
|
||||
if self.memory_policy is not None:
|
||||
delete_code = ("if (self->obj) {\n"
|
||||
" %s *tmp = self->obj;\n"
|
||||
" self->obj = NULL;\n"
|
||||
" tmp->%s();\n"
|
||||
" %s\n"
|
||||
"}"
|
||||
% (self.full_name, self.decref_method,))
|
||||
elif self.decref_function is not None:
|
||||
delete_code = ("if (self->obj) {\n"
|
||||
" %s *tmp = self->obj;\n"
|
||||
" self->obj = NULL;\n"
|
||||
" %s(tmp);\n"
|
||||
"}"
|
||||
% (self.full_name, self.decref_function,))
|
||||
elif self.free_function is not None:
|
||||
delete_code = ("if (self->obj) {\n"
|
||||
" %s *tmp = self->obj;\n"
|
||||
" self->obj = NULL;\n"
|
||||
" %s(tmp);\n"
|
||||
"}"
|
||||
% (self.full_name, self.free_function,))
|
||||
% (self.full_name, self.memory_policy.get_free_code('tmp')))
|
||||
else:
|
||||
if self.incomplete_type:
|
||||
raise CodeGenerationError("Cannot finish generating class %s: "
|
||||
@@ -1537,10 +1567,10 @@ static void
|
||||
if self.helper_class is None:
|
||||
visit_self = ''
|
||||
else:
|
||||
if self.peekref_method is None:
|
||||
if not isinstance(self.memory_policy, ReferenceCountingMethodsPolicy):
|
||||
peekref_code = ''
|
||||
else:
|
||||
peekref_code = " && self->obj->%s() == 1" % self.peekref_method
|
||||
peekref_code = " && self->obj->%s() == 1" % self.memory_policy.peekref_method
|
||||
visit_self = '''
|
||||
if (self->obj %s)
|
||||
Py_VISIT((PyObject *) self);
|
||||
|
||||
@@ -3,7 +3,7 @@ import warnings
|
||||
from typehandlers.base import ForwardWrapperBase, ReverseWrapperBase, \
|
||||
Parameter, ReturnValue, TypeConfigurationError
|
||||
|
||||
from cppclass import CppClass
|
||||
import cppclass
|
||||
|
||||
|
||||
###
|
||||
@@ -12,7 +12,7 @@ from cppclass import CppClass
|
||||
class CppClassParameterBase(Parameter):
|
||||
"Base class for all C++ Class parameter handlers"
|
||||
CTYPES = []
|
||||
cpp_class = CppClass('dummy') # CppClass instance
|
||||
cpp_class = cppclass.CppClass('dummy') # CppClass instance
|
||||
DIRECTIONS = [Parameter.DIRECTION_IN]
|
||||
|
||||
def __init__(self, ctype, name, direction=Parameter.DIRECTION_IN, is_const=False, default_value=None):
|
||||
@@ -37,7 +37,7 @@ class CppClassParameterBase(Parameter):
|
||||
class CppClassReturnValueBase(ReturnValue):
|
||||
"Class return handlers -- base class"
|
||||
CTYPES = []
|
||||
cpp_class = CppClass('dummy') # CppClass instance
|
||||
cpp_class = cppclass.CppClass('dummy') # CppClass instance
|
||||
|
||||
def __init__(self, ctype):
|
||||
super(CppClassReturnValueBase, self).__init__(ctype)
|
||||
@@ -48,13 +48,13 @@ class CppClassReturnValueBase(ReturnValue):
|
||||
class CppClassParameter(CppClassParameterBase):
|
||||
"Class handlers"
|
||||
CTYPES = []
|
||||
cpp_class = CppClass('dummy') # CppClass instance
|
||||
cpp_class = cppclass.CppClass('dummy') # CppClass instance
|
||||
DIRECTIONS = [Parameter.DIRECTION_IN]
|
||||
|
||||
def convert_python_to_c(self, wrapper):
|
||||
"parses python args to get C++ value"
|
||||
assert isinstance(wrapper, ForwardWrapperBase)
|
||||
assert isinstance(self.cpp_class, CppClass)
|
||||
assert isinstance(self.cpp_class, cppclass.CppClass)
|
||||
|
||||
if self.take_value_from_python_self:
|
||||
self.py_name = 'self'
|
||||
@@ -152,12 +152,13 @@ class CppClassParameter(CppClassParameterBase):
|
||||
class CppClassRefParameter(CppClassParameterBase):
|
||||
"Class& handlers"
|
||||
CTYPES = []
|
||||
cpp_class = CppClass('dummy') # CppClass instance
|
||||
cpp_class = cppclass.CppClass('dummy') # CppClass instance
|
||||
DIRECTIONS = [Parameter.DIRECTION_IN,
|
||||
Parameter.DIRECTION_OUT,
|
||||
Parameter.DIRECTION_INOUT]
|
||||
|
||||
def __init__(self, ctype, name, direction=Parameter.DIRECTION_IN, is_const=False, default_value=None, default_value_type=None):
|
||||
def __init__(self, ctype, name, direction=Parameter.DIRECTION_IN, is_const=False,
|
||||
default_value=None, default_value_type=None):
|
||||
"""
|
||||
@param ctype: C type, normally 'MyClass*'
|
||||
@param name: parameter name
|
||||
@@ -171,7 +172,7 @@ class CppClassRefParameter(CppClassParameterBase):
|
||||
def convert_python_to_c(self, wrapper):
|
||||
"parses python args to get C++ value"
|
||||
assert isinstance(wrapper, ForwardWrapperBase)
|
||||
assert isinstance(self.cpp_class, CppClass)
|
||||
assert isinstance(self.cpp_class, cppclass.CppClass)
|
||||
|
||||
if self.direction == Parameter.DIRECTION_IN:
|
||||
if self.take_value_from_python_self:
|
||||
@@ -336,7 +337,7 @@ class CppClassRefParameter(CppClassParameterBase):
|
||||
class CppClassReturnValue(CppClassReturnValueBase):
|
||||
"Class return handlers"
|
||||
CTYPES = []
|
||||
cpp_class = CppClass('dummy') # CppClass instance
|
||||
cpp_class = cppclass.CppClass('dummy') # CppClass instance
|
||||
REQUIRES_ASSIGNMENT_CONSTRUCTOR = True
|
||||
|
||||
def __init__(self, ctype, is_const=False):
|
||||
@@ -388,7 +389,7 @@ class CppClassReturnValue(CppClassReturnValueBase):
|
||||
class CppClassPtrParameter(CppClassParameterBase):
|
||||
"Class* handlers"
|
||||
CTYPES = []
|
||||
cpp_class = CppClass('dummy') # CppClass instance
|
||||
cpp_class = cppclass.CppClass('dummy') # CppClass instance
|
||||
DIRECTIONS = [Parameter.DIRECTION_IN]
|
||||
SUPPORTS_TRANSFORMATIONS = True
|
||||
|
||||
@@ -437,7 +438,7 @@ class CppClassPtrParameter(CppClassParameterBase):
|
||||
def convert_python_to_c(self, wrapper):
|
||||
"parses python args to get C++ value"
|
||||
assert isinstance(wrapper, ForwardWrapperBase)
|
||||
assert isinstance(self.cpp_class, CppClass)
|
||||
assert isinstance(self.cpp_class, cppclass.CppClass)
|
||||
|
||||
if self.take_value_from_python_self:
|
||||
self.py_name = 'self'
|
||||
@@ -452,10 +453,10 @@ class CppClassPtrParameter(CppClassParameterBase):
|
||||
wrapper.call_params.append(value)
|
||||
|
||||
if self.transfer_ownership:
|
||||
if not self.cpp_class.has_reference_counting:
|
||||
if not isinstance(self.cpp_class.memory_policy, cppclass.ReferenceCountingPolicy):
|
||||
wrapper.after_call.write_code('%s->obj = NULL;' % (self.py_name,))
|
||||
else:
|
||||
self.cpp_class.write_incref(wrapper.before_call, "%s->obj" % self.py_name)
|
||||
self.cpp_class.memory_policy.write_incref(wrapper.before_call, "%s->obj" % self.py_name)
|
||||
|
||||
|
||||
def convert_c_to_python(self, wrapper):
|
||||
@@ -477,7 +478,8 @@ class CppClassPtrParameter(CppClassParameterBase):
|
||||
## automatic_type_narrowing is active and we are not forced to
|
||||
## make a copy of the object
|
||||
if (self.cpp_class.automatic_type_narrowing
|
||||
and (self.transfer_ownership or self.cpp_class.has_reference_counting)):
|
||||
and (self.transfer_ownership or isinstance(self.cpp_class.memory_policy,
|
||||
cppclass.ReferenceCountingPolicy))):
|
||||
|
||||
typeid_map_name = self.cpp_class.get_type_narrowing_root().typeid_map_name
|
||||
wrapper_type = wrapper.declarations.declare_variable(
|
||||
@@ -507,7 +509,7 @@ class CppClassPtrParameter(CppClassParameterBase):
|
||||
wrapper.before_call.write_code("%s->obj = %s;" % (py_name, value))
|
||||
wrapper.build_params.add_parameter("N", [py_name])
|
||||
else:
|
||||
if not self.cpp_class.has_reference_counting:
|
||||
if not isinstance(self.cpp_class.memory_policy, cppclass.ReferenceCountingPolicy):
|
||||
## The PyObject gets a temporary pointer to the
|
||||
## original value; the pointer is converted to a
|
||||
## copy in case the callee retains a reference to
|
||||
@@ -550,7 +552,7 @@ class CppClassPtrParameter(CppClassParameterBase):
|
||||
wrapper.after_call.write_code('}')
|
||||
else:
|
||||
## The PyObject gets a new reference to the same obj
|
||||
self.cpp_class.write_incref(wrapper.before_call, value)
|
||||
self.cpp_class.memory_policy.write_incref(wrapper.before_call, value)
|
||||
if self.is_const:
|
||||
wrapper.before_call.write_code("%s->obj = (%s*) (%s);" %
|
||||
(py_name, self.cpp_class.full_name, value))
|
||||
@@ -611,7 +613,7 @@ class CppClassPtrReturnValue(CppClassReturnValueBase):
|
||||
"Class* return handler"
|
||||
CTYPES = []
|
||||
SUPPORTS_TRANSFORMATIONS = True
|
||||
cpp_class = CppClass('dummy') # CppClass instance
|
||||
cpp_class = cppclass.CppClass('dummy') # CppClass instance
|
||||
|
||||
def __init__(self, ctype, caller_owns_return=None, custodian=None, is_const=False):
|
||||
"""
|
||||
@@ -673,7 +675,8 @@ class CppClassPtrReturnValue(CppClassReturnValueBase):
|
||||
## automatic_type_narrowing is active and we are not forced to
|
||||
## make a copy of the object
|
||||
if (self.cpp_class.automatic_type_narrowing
|
||||
and (self.caller_owns_return or self.cpp_class.has_reference_counting)):
|
||||
and (self.caller_owns_return or isinstance(self.cpp_class.memory_policy,
|
||||
cppclass.ReferenceCountingPolicy))):
|
||||
|
||||
typeid_map_name = self.cpp_class.get_type_narrowing_root().typeid_map_name
|
||||
wrapper_type = wrapper.declarations.declare_variable(
|
||||
@@ -704,14 +707,14 @@ class CppClassPtrReturnValue(CppClassReturnValueBase):
|
||||
if self.caller_owns_return:
|
||||
wrapper.after_call.write_code("%s->obj = %s;" % (py_name, value))
|
||||
else:
|
||||
if not self.cpp_class.has_reference_counting:
|
||||
if not isinstance(self.cpp_class.memory_policy, cppclass.ReferenceCountingPolicy):
|
||||
## The PyObject creates its own copy
|
||||
self.cpp_class.write_create_instance(wrapper.after_call,
|
||||
"%s->obj" % py_name,
|
||||
'*'+value)
|
||||
else:
|
||||
## The PyObject gets a new reference to the same obj
|
||||
self.cpp_class.write_incref(wrapper.after_call, value)
|
||||
self.cpp_class.memory_policy.write_incref(wrapper.after_call, value)
|
||||
if self.is_const:
|
||||
wrapper.after_call.write_code("%s->obj = (%s*) (%s);" %
|
||||
(py_name, self.cpp_class.full_name, value))
|
||||
@@ -771,14 +774,14 @@ class CppClassPtrReturnValue(CppClassReturnValueBase):
|
||||
|
||||
## now the hairy part :)
|
||||
if self.caller_owns_return:
|
||||
if not self.cpp_class.has_reference_counting:
|
||||
if not isinstance(self.cpp_class.memory_policy, cppclass.ReferenceCountingPolicy):
|
||||
## the caller receives a copy
|
||||
self.cpp_class.write_create_instance(wrapper.after_call,
|
||||
"%s" % self.value,
|
||||
'*'+value)
|
||||
else:
|
||||
## the caller gets a new reference to the same obj
|
||||
self.cpp_class.write_incref(wrapper.after_call, value)
|
||||
self.cpp_class.memory_policy.write_incref(wrapper.after_call, value)
|
||||
if self.is_const:
|
||||
wrapper.after_call.write_code(
|
||||
"%s = const_cast< %s* >(%s);" %
|
||||
|
||||
@@ -11,7 +11,7 @@ from typehandlers.codesink import FileCodeSink, CodeSink, NullCodeSink
|
||||
import typehandlers.base
|
||||
from typehandlers.base import ReturnValue, Parameter, TypeLookupError, TypeConfigurationError, NotSupportedError
|
||||
from pygccxml.declarations.enumeration import enumeration_t
|
||||
from cppclass import CppClass
|
||||
from cppclass import CppClass, ReferenceCountingMethodsPolicy
|
||||
from pygccxml.declarations import type_traits
|
||||
from pygccxml.declarations import cpptypes
|
||||
from pygccxml.declarations import calldef
|
||||
@@ -1273,9 +1273,15 @@ pybindgen.settings.error_handler = ErrorHandler()
|
||||
if cpp_type is class_wrapper and is_reference:
|
||||
have_copy_constructor = True
|
||||
|
||||
methods_to_ignore = []
|
||||
if isinstance(class_wrapper.memory_policy, ReferenceCountingMethodsPolicy):
|
||||
methods_to_ignore.extend([class_wrapper.memory_policy.incref_method,
|
||||
class_wrapper.memory_policy.decref_method,
|
||||
class_wrapper.memory_policy.peekref_method,
|
||||
])
|
||||
|
||||
for member in cls.get_members():
|
||||
if member.name in [class_wrapper.incref_method, class_wrapper.decref_method,
|
||||
class_wrapper.peekref_method]:
|
||||
if member.name in methods_to_ignore:
|
||||
continue
|
||||
|
||||
global_annotations, parameter_annotations = \
|
||||
|
||||
Reference in new issue
Block a user