Skip to content

Commit 3bd3c79

Browse files
committed
Allow nogil for most types of methods.
This requires a change in conversion providers. +Some other cleanups.
1 parent 570a6f4 commit 3bd3c79

18 files changed

+891
-117
lines changed

CONTRIBUTING.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ This document is displayed because you either opened an issue or you want to pro
44
When interacting with other developers, users or anyone else from our community, please adhere to
55
[the OpenMS CODE OF CONDUCT](https://github.com/OpenMS/OpenMS/blob/develop/CODE_OF_CONDUCT.md)
66

7+
# Setting up dev environment
8+
Install conda/mamba (or use a venv).
9+
Install git.
10+
Install a C++ compiler.
11+
12+
```bash
13+
git clone https://github.com/OpenMS/autowrap
14+
cd autowrap
15+
mamba create -n autowrap -f environment.yml
16+
python -m pytest
17+
```
18+
19+
720
# Reporting an Issue:
821

922
You most likely came here to:
@@ -18,10 +31,10 @@ If you found a bug, e.g. an autowrap tool crashes during code generation, it is
1831
- how you installed autowrap (e.g., from source, pip)
1932
- a description on how to reproduce the bug
2033
- relevant tool output (e.g., error messages)
21-
- data to repoduce the bug (If possible as a GitHub gist. Other platforms like Dropbox, Google Drive links also work. If you can't share the data publicly please indicate this and we will contact you in private.)
34+
- data to reproduce the bug (If possible as a GitHub gist. Other platforms like Dropbox, Google Drive links also work. If you can't share the data publicly please indicate this and we will contact you in private.)
2235

23-
If you are an official OpenMS team meber:
24-
- label your issue using github labels (e.g. as: question, defect) that indicate the type of issue and which components of autowrap (blue labels) are affected. The severity is usually assigned by OpenMS maintainers and used internally to e.g. indicate if a bug is a blocker for a new release.
36+
If you are an official OpenMS team member:
37+
- label your issue using GitHub labels (e.g. as: question, defect) that indicate the type of issue and which components of autowrap (blue labels) are affected. The severity is usually assigned by OpenMS maintainers and used internally to e.g. indicate if a bug is a blocker for a new release.
2538

2639
# Opening a Pull Request
2740

README_DEVELOP

Lines changed: 0 additions & 9 deletions
This file was deleted.

autowrap/CodeGenerator.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,18 +1113,22 @@ def _create_fun_decl_and_input_conversion(self, code, py_name, method, is_free_f
11131113
call_args = []
11141114
checks = []
11151115
in_types = []
1116+
decls = []
1117+
decl_calls = []
11161118
for arg_num, (t, n) in enumerate(args):
11171119
# get new ConversionProvider using the converter registry
11181120
converter = self.cr.get(t)
11191121
converter.cr = self.cr
11201122
py_type = converter.matching_python_type(t)
11211123
py_typing_type = converter.matching_python_type_full(t)
1122-
conv_code, call_as, cleanup = converter.input_conversion(t, n, arg_num)
1124+
conv_code, call_as, cleanup, (decl, decl_call) = converter.input_conversion(t, n, arg_num)
11231125
py_signature_parts.append("%s %s " % (py_type, n))
11241126
py_typing_signature_parts.append("%s: %s " % (n, py_typing_type))
11251127
input_conversion_codes.append(conv_code)
11261128
cleanups.append(cleanup)
11271129
call_args.append(call_as)
1130+
decls.append(decl)
1131+
decl_calls.append(decl_call)
11281132
in_types.append(t)
11291133
checks.append((n, converter.type_check_expression(t, n)))
11301134

@@ -1211,7 +1215,7 @@ def _create_fun_decl_and_input_conversion(self, code, py_name, method, is_free_f
12111215
for conv_code in input_conversion_codes:
12121216
code.add(conv_code)
12131217

1214-
return call_args, cleanups, in_types, stub
1218+
return call_args, cleanups, in_types, stub, decls, decl_calls
12151219

12161220
def _create_wrapper_for_attribute(self, attribute):
12171221
code = Code()
@@ -1228,7 +1232,7 @@ def _create_wrapper_for_attribute(self, attribute):
12281232
converter = self.cr.get(t)
12291233
py_type = converter.matching_python_type(t)
12301234
py_typing_type = converter.matching_python_type_full(t)
1231-
conv_code, call_as, cleanup = converter.input_conversion(t, name, 0)
1235+
conv_code, call_as, cleanup, (decl, decl_call) = converter.input_conversion(t, name, 0)
12321236

12331237
code.add(
12341238
"""
@@ -1323,7 +1327,7 @@ def _create_wrapper_for_attribute(self, attribute):
13231327
code.add(" return py_result")
13241328
return code, stubs
13251329

1326-
def create_wrapper_for_nonoverloaded_method(self, cdcl, py_name, method):
1330+
def create_wrapper_for_nonoverloaded_method(self, cdcl, py_name, method: ResolvedMethod):
13271331
L.info(" create wrapper for %s ('%s')" % (py_name, method))
13281332
meth_code = Code()
13291333

@@ -1332,10 +1336,22 @@ def create_wrapper_for_nonoverloaded_method(self, cdcl, py_name, method):
13321336
cleanups,
13331337
in_types,
13341338
stubs,
1339+
decls,
1340+
decl_calls
13351341
) = self._create_fun_decl_and_input_conversion(meth_code, py_name, method)
13361342

1343+
init = ""
1344+
c_call_args = []
1345+
for decl, decl_call in zip(decls, decl_calls):
1346+
init = init + decl + "\n"
1347+
c_call_args.append(decl_call)
1348+
13371349
# call wrapped method and convert result value back to python
13381350
cpp_name = method.cpp_decl.name
1351+
1352+
if method.with_nogil and init.strip("\n"):
1353+
call_args = c_call_args
1354+
13391355
call_args_str = ", ".join(call_args)
13401356
if method.is_static:
13411357
cy_call_str = "%s.%s(%s)" % (
@@ -1348,13 +1364,15 @@ def create_wrapper_for_nonoverloaded_method(self, cdcl, py_name, method):
13481364

13491365
res_t = method.result_type
13501366
out_converter = self.cr.get(res_t)
1351-
full_call_stmt = out_converter.call_method(res_t, cy_call_str)
1367+
full_call_stmt = out_converter.call_method(res_t, cy_call_str, True, not method.with_nogil)
13521368

13531369
if method.with_nogil:
13541370
meth_code.add(
13551371
"""
1372+
| $init
13561373
| with nogil:
1357-
"""
1374+
|
1375+
""", locals()
13581376
)
13591377
indented = Code()
13601378
else:
@@ -1370,6 +1388,10 @@ def create_wrapper_for_nonoverloaded_method(self, cdcl, py_name, method):
13701388
else:
13711389
indented.add(full_call_stmt)
13721390

1391+
if method.with_nogil:
1392+
meth_code.add(indented)
1393+
indented = meth_code
1394+
13731395
for cleanup in reversed(cleanups):
13741396
if not cleanup:
13751397
continue
@@ -1385,9 +1407,6 @@ def create_wrapper_for_nonoverloaded_method(self, cdcl, py_name, method):
13851407
indented.add(to_py_code)
13861408
indented.add(" return py_result")
13871409

1388-
if method.with_nogil:
1389-
meth_code.add(indented)
1390-
13911410
return meth_code, stubs
13921411

13931412
def create_wrapper_for_free_function(self, decl: ResolvedFunction, out_codes: CodeDict) -> None:
@@ -1445,6 +1464,8 @@ def _create_wrapper_for_free_function(
14451464
cleanups,
14461465
in_types,
14471466
stubs,
1467+
decls,
1468+
decl_calls
14481469
) = self._create_fun_decl_and_input_conversion(fun_code, name, decl, is_free_fun=True)
14491470

14501471
call_args_str = ", ".join(call_args)
@@ -1552,6 +1573,8 @@ def create_wrapper_for_nonoverloaded_constructor(self, class_decl, py_name, cons
15521573
cleanups,
15531574
in_types,
15541575
stubs,
1576+
decls,
1577+
decl_calls
15551578
) = self._create_fun_decl_and_input_conversion(cons_code, py_name, cons_decl)
15561579

15571580
stub_code.extend(stubs)
@@ -1663,6 +1686,8 @@ def create_special_getitem_method(self, mdcl):
16631686
cleanups,
16641687
(in_type,),
16651688
stubs,
1689+
decls,
1690+
decl_calls
16661691
) = self._create_fun_decl_and_input_conversion(meth_code, "__getitem__", mdcl)
16671692

16681693
meth_code.add(
@@ -1790,7 +1815,7 @@ def create_special_setitem_method(self, mdcl):
17901815
# CppObject[ idx ] = value
17911816
#
17921817
cy_call_str = "deref(self.inst.get())[%s]" % call_arg
1793-
code, call_as, cleanup = out_converter.input_conversion(res_t, value_arg, 0)
1818+
code, call_as, cleanup, (decl, decl_call) = out_converter.input_conversion(res_t, value_arg, 0)
17941819
meth_code.add(
17951820
"""
17961821
| $cy_call_str = $call_as

0 commit comments

Comments
 (0)