diff --git a/pybindgen/cppattribute.py b/pybindgen/cppattribute.py index 83808e3..f2c0006 100644 --- a/pybindgen/cppattribute.py +++ b/pybindgen/cppattribute.py @@ -3,6 +3,7 @@ """ import sys +import collections PY3 = (sys.version_info[0] >= 3) if PY3: @@ -354,7 +355,7 @@ def __init__(self, cname): 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 @@ def add_attribute(self, name, getter, setter): """ 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 @@ def generate(self, code_sink): 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 @@ def generate(self, code_sink): 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( diff --git a/pybindgen/cppclass.py b/pybindgen/cppclass.py index 1971151..c5583ff 100644 --- a/pybindgen/cppclass.py +++ b/pybindgen/cppclass.py @@ -622,7 +622,7 @@ def __init__(self, name, parent=None, incref_method=None, decref_method=None, 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 @@ def add_static_attribute(self, name, value_type, is_const=False): 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 @@ def add_custom_instance_attribute(self, name, value_type, getter, is_const=False 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 @@ def add_instance_attribute(self, name, value_type, is_const=False, 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 @@ def generate(self, code_sink, module): #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 @@ def _generate_destructor(self, code_sink, have_constructor): 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() diff --git a/pybindgen/module.py b/pybindgen/module.py index 08e4e44..42a0e4b 100644 --- a/pybindgen/module.py +++ b/pybindgen/module.py @@ -56,7 +56,7 @@ from pybindgen import utils import warnings import traceback - +import collections class MultiSectionFactory(object): """ @@ -248,7 +248,7 @@ def __init__(self, name, parent=None, docstring=None, cpp_namespace=None): 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 = []