adds custom_name to class attributes and uses ordered dicts to store attributes/methods

This commit is contained in:
Dan Eicher committed 2019-04-09 17:47:05 -07:00
1 parent 823d8b2b7d
commit 5768a792c8
3 files changed
+17 -16

No files matched your search

+8 -7
View File
@@ -3,6 +3,7 @@ Wraps C++ class instance/static attributes.
"""
import sys
import collections
PY3 = (sys.version_info[0] >= 3)
if PY3:
@@ -354,7 +355,7 @@ class PyGetSetDef(object):
def empty(self):
return len(self.attributes) == 0
def add_attribute(self, name, getter, setter):
def add_attribute(self, name, getter, setter, custom_name=None):
"""
Add a new attribute
:param name: attribute name
@@ -363,7 +364,7 @@ class PyGetSetDef(object):
"""
assert getter is None or isinstance(getter, PyGetter)
assert setter is None or isinstance(setter, PySetter)
self.attributes.append((name, getter, setter))
self.attributes.append((name, getter, setter, custom_name))
def generate(self, code_sink):
"""
@@ -373,8 +374,8 @@ class PyGetSetDef(object):
if not self.attributes:
return '0'
getsets = {} # attrname -> (getter, setter)
for name, getter, setter in self.attributes:
getsets = collections.OrderedDict() # attrname -> (getter, setter)
for name, getter, setter, custom_name in self.attributes:
getter_name = 'NULL'
if getter is not None:
@@ -396,14 +397,14 @@ class PyGetSetDef(object):
else:
setter_name = setter.c_function_name
assert name not in getsets
getsets[name] = (getter_name, setter_name)
getsets[name] = (getter_name, setter_name, custom_name)
code_sink.writeln("static PyGetSetDef %s[] = {" % self.cname)
code_sink.indent()
for name, (getter_c_name, setter_c_name) in getsets.items():
for name, (getter_c_name, setter_c_name, custom_name) in getsets.items():
code_sink.writeln('{')
code_sink.indent()
code_sink.writeln('(char*) "%s", /* attribute name */' % name)
code_sink.writeln('(char*) "%s", /* attribute name */' % (custom_name or name))
## getter
code_sink.writeln(
+7 -7
View File
@@ -622,7 +622,7 @@ class CppClass(object):
self.is_singleton = is_singleton
self.foreign_cpp_namespace = foreign_cpp_namespace
self.full_name = None # full name with C++ namespaces attached and template parameters
self.methods = {} # name => OverloadedMethod
self.methods = collections.OrderedDict() # name => OverloadedMethod
self._dummy_methods = [] # methods that have parameter/retval binding problems
self.nonpublic_methods = []
self.constructors = [] # (name, wrapper) pairs
@@ -1707,7 +1707,7 @@ public:
setter.stack_where_defined = traceback.extract_stack()
self.static_attributes.add_attribute(name, getter, setter)
def add_custom_instance_attribute(self, name, value_type, getter, is_const=False, setter=None,
def add_custom_instance_attribute(self, name, value_type, getter, is_const=False, setter=None, custom_name=None,
getter_template_parameters=[],
setter_template_parameters=[]):
"""
@@ -1742,10 +1742,10 @@ public:
setter_wrapper = CppCustomInstanceAttributeSetter(value_type, self, name, setter=setter,
template_parameters = setter_template_parameters)
setter_wrapper.stack_where_defined = traceback.extract_stack()
self.instance_attributes.add_attribute(name, getter_wrapper, setter_wrapper)
self.instance_attributes.add_attribute(name, getter_wrapper, setter_wrapper, custom_name)
def add_instance_attribute(self, name, value_type, is_const=False,
getter=None, setter=None):
getter=None, setter=None, custom_name=None):
"""
:param value_type: a ReturnValue object
:param name: attribute name (i.e. the name of the class member variable)
@@ -1774,7 +1774,7 @@ public:
else:
setter_wrapper = CppInstanceAttributeSetter(value_type, self, name, setter=setter)
setter_wrapper.stack_where_defined = traceback.extract_stack()
self.instance_attributes.add_attribute(name, getter_wrapper, setter_wrapper)
self.instance_attributes.add_attribute(name, getter_wrapper, setter_wrapper, custom_name)
def _inherit_helper_class_parent_virtuals(self):
@@ -2016,7 +2016,7 @@ typedef struct {
#if self.slots.get("tp_hash", "NULL") == "NULL":
# self.slots["tp_hash"] = self._generate_tp_hash(code_sink)
if self.slots.get("tp_richcompare", "NULL") == "NULL":
if self.slots.get("tp_richcompare", "NULL") == "NULL" and self.binary_comparison_operators:
self.slots["tp_richcompare"] = self._generate_tp_richcompare(code_sink)
if self.binary_numeric_operators or self.inplace_numeric_operators:
@@ -2580,7 +2580,7 @@ static void
def _generate_tp_richcompare(self, code_sink):
tp_richcompare_function_name = "_wrap_%s__tp_richcompare" % (self.pystruct,)
code_sink.writeln("static PyObject*\n%s (%s *PYBINDGEN_UNUSED(self), %s *other, int opid)"
code_sink.writeln("static PyObject*\n%s (%s *self, %s *other, int opid)"
% (tp_richcompare_function_name, self.pystruct, self.pystruct))
code_sink.writeln("{")
code_sink.indent()
+2 -2
View File
@@ -56,7 +56,7 @@ from pybindgen.converter_functions import PythonToCConverter, CToPythonConverter
from pybindgen import utils
import warnings
import traceback
import collections
class MultiSectionFactory(object):
"""
@@ -248,7 +248,7 @@ class ModuleBase(dict):
self.cpp_namespace_prefix = '::'.join(path)
self.declarations = DeclarationsScope()
self.functions = {} # name => OverloadedFunction
self.functions = collections.OrderedDict() # name => OverloadedFunction
self.classes = []
self.containers = []
self.exceptions = []