Skip to content

Commit

Permalink
Merge pull request #18 from dan-eicher/master
Browse files Browse the repository at this point in the history
Ordered dicts and custom_names
  • Loading branch information
gjcarneiro authored Apr 10, 2019
2 parents 823d8b2 + 5768a79 commit 82c5fd2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
15 changes: 8 additions & 7 deletions pybindgen/cppattribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import sys
import collections

PY3 = (sys.version_info[0] >= 3)
if PY3:
Expand Down Expand Up @@ -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
Expand All @@ -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):
"""
Expand All @@ -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:
Expand All @@ -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(
Expand Down
14 changes: 7 additions & 7 deletions pybindgen/cppclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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=[]):
"""
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions pybindgen/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
from pybindgen import utils
import warnings
import traceback

import collections

class MultiSectionFactory(object):
"""
Expand Down Expand Up @@ -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 = []
Expand Down

0 comments on commit 82c5fd2

Please sign in to comment.