Skip to content

Commit dbc0e5b

Browse files
committed
fix: check def and def_static overload
1 parent fae02e5 commit dbc0e5b

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

pybind11_weaver/entity/klass/method.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from pylibclang import cindex
88

99
from pybind11_weaver.utils import fn, common
10-
from pybind11_weaver.entity import entity_base
1110
from . import klass
1211

1312
_logger = logging.getLogger(__name__)
@@ -20,6 +19,13 @@
2019
_call_bind_method = """AddMethod_{method_identifier}();"""
2120

2221

22+
def get_def_type(cursor):
23+
if cursor.is_static_method():
24+
return "def_static"
25+
else:
26+
return "def"
27+
28+
2329
class Method:
2430

2531
def __init__(self, fn_cursor: cindex.Cursor, inject_docstring: bool, bind_name: str, identifier_name: str,
@@ -41,7 +47,7 @@ def get_def_stmt(self, pybind11_obj_sym: str):
4147
const char * _pb11_weaver_comment_str = {comment};
4248
{disable_bind}
4349
#ifndef {self.disable_mark}
44-
{pybind11_obj_sym}.{self.get_def_type()}(\"{self.bind_name}\",{fn_ptr}{',_pb11_weaver_comment_str' if should_add else ''});
50+
{pybind11_obj_sym}.{get_def_type(self.fn_cursor)}(\"{self.bind_name}\",{fn_ptr}{',_pb11_weaver_comment_str' if should_add else ''});
4551
#endif
4652
return _pb11_weaver_comment_str;
4753
"""
@@ -50,12 +56,6 @@ def get_def_stmt(self, pybind11_obj_sym: str):
5056
def get_call_stmt(self):
5157
return _call_bind_method.format(method_identifier=self.identifier_name)
5258

53-
def get_def_type(self):
54-
if self.fn_cursor.is_static_method():
55-
return "def_static"
56-
else:
57-
return "def"
58-
5959

6060
def is_explicit_instantiation(cursor: cindex.Cursor):
6161
source_file = cursor.location.file
@@ -94,7 +94,7 @@ class GenMethod:
9494

9595
def __init__(self, kls_entity: "klass.KlassEntity"):
9696
self.kls_entity = kls_entity
97-
self.added_method: Dict[str, int] = collections.defaultdict(int)
97+
self.added_method: Dict[str, List[Method]] = collections.defaultdict(list)
9898

9999
@staticmethod
100100
def is_virtual(cursor: cindex.Cursor):
@@ -109,7 +109,7 @@ def run(self, pybind11_obj_sym: str) -> Tuple[List[str], List[str]]:
109109
codes = []
110110
extra_codes: List[str] = []
111111
kls_entity = self.kls_entity
112-
methods: Dict[str, Method] = dict()
112+
methods = []
113113

114114
root_cursor = kls_entity.cursor
115115
if common.is_concreate_template(kls_entity.cursor):
@@ -128,17 +128,25 @@ def run(self, pybind11_obj_sym: str) -> Tuple[List[str], List[str]]:
128128
cursor) and not common.is_operator_overload(cursor):
129129
bind_name = fn.fn_python_name(cursor)
130130
unique_name = bind_name
131-
if self.added_method[bind_name] != 0:
132-
unique_name += str(self.added_method[bind_name])
133-
self.added_method[bind_name] += 1
131+
while len(self.added_method[bind_name]) != 0:
132+
if get_def_type(cursor) != get_def_type(self.added_method[bind_name][0].fn_cursor):
133+
bind_name = bind_name + "_"
134+
unique_name = bind_name
135+
_logger.warning(
136+
f"pybind11 does not support mix def and def_static overloading, bind {cursor.spelling} to {unique_name}")
137+
138+
else:
139+
unique_name = bind_name + str(len(self.added_method[bind_name]))
140+
break
134141
self.is_virtual(cursor) # print warning
135142
disable_mark = f"PB11_WEAVER_DISABLE_{self.kls_entity.get_pb11weaver_struct_name()}_{unique_name}"
136-
methods[unique_name] = Method(cursor, kls_entity.gu.io_config.gen_docstring, bind_name, unique_name,
137-
disable_mark)
143+
methods.append(Method(cursor, kls_entity.gu.io_config.gen_docstring, bind_name, unique_name,
144+
disable_mark))
145+
self.added_method[bind_name].append(methods[-1])
138146

139147
call_method_bind = []
140148
method_bind_body = []
141-
for _, method in methods.items():
149+
for method in methods:
142150
call_method_bind.append(method.get_call_stmt())
143151
method_bind_body.append(method.get_def_stmt(pybind11_obj_sym))
144152

0 commit comments

Comments
 (0)