Skip to content

Fix subpackages for templates, add tests #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions robotpy_build/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,10 @@ def header_hook(self, header, data):
qualname = tmpl_data.qualname
if "::" not in qualname:
qualname = f"::{qualname}"
tmpl_data = tmpl_data.dict()
tmpl_data["x_qualname_"] = qualname.translate(self._qualname_trans)
templates[k] = tmpl_data
tmpl_data_d = tmpl_data.dict()
tmpl_data_d["x_qualname_"] = qualname.translate(self._qualname_trans)
self._add_subpackage(tmpl_data_d, tmpl_data)
templates[k] = tmpl_data_d

data["templates"] = templates

Expand Down Expand Up @@ -605,6 +606,11 @@ def class_hook(self, cls, data):
template_parameter_list = ""

if class_data.template_params:
if class_data.subpackage:
raise ValueError(
f"{cls_name}: classes with subpackages must define subpackage on template instantiation"
)

template_args = []
template_params = []

Expand Down
7 changes: 6 additions & 1 deletion robotpy_build/hooks_datacfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,9 @@ class ClassData(Model):
shared_ptr: bool = True

#: If specified, put the class in a sub.pack.age. Ignored
#: for functions attached to a class.
#: for functions attached to a class. When template parameters
#: are used, must define subpackage on template instances
#: instead
subpackage: Optional[str] = None

#: Extra 'using' directives to insert into the trampoline and the
Expand Down Expand Up @@ -375,6 +377,9 @@ class MyClass {};
#: Template parameters to use
params: List[str]

#: If specified, put the template instantiation in a sub.pack.age
subpackage: Optional[str] = None


class HooksDataYaml(Model):
"""
Expand Down
2 changes: 1 addition & 1 deletion robotpy_build/templates/cls.cpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void init_{{ mod_fn }}(py::module &m) {

{# templates #}
{% for name, tmpl_data in templates.items() %}
rpygen::bind_{{ tmpl_data.x_qualname_ }}<{{ tmpl_data.params | join(', ') }}>(m, "{{ name }}");
rpygen::bind_{{ tmpl_data.x_qualname_ }}<{{ tmpl_data.params | join(', ') }}>({{ tmpl_data.x_module_var }}, "{{ name }}");
{% endfor %}

{# class declarations #}
Expand Down
2 changes: 2 additions & 0 deletions robotpy_build/templates/pybind11.cpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ py::enum_<{{ enum.x_namespace }}{{ enum.name }}>({{ scope }}, "{{ enum.x_name }}

{%- if cls.parent -%}
{{ cls.parent.x_varname }}
{%- elif cls.data.template_params -%}
m
{%- else -%}
{{ cls.x_module_var }}
{%- endif -%}
Expand Down
20 changes: 20 additions & 0 deletions tests/cpp/gen/ft/subpkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
functions:
sp_func:
subpackage: subpkg
classes:
SPClass:
shared_ptr: true
subpackage: subpkg

SPTemplate:
shared_ptr: true
template_params:
- T

templates:
SPTemplate:
subpackage: subpkg
qualname: SPTemplate
params:
- int
1 change: 1 addition & 0 deletions tests/cpp/pyproject.toml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ generate = [
{ ignore = "ignore.h" },
{ overloads = "overloads.h" },
{ rename = "rename.h" },
{ subpkg = "subpkg.h" },
{ static_only = "static_only.h" },
{ type_caster = "type_caster.h" },
{ type_caster_nested = "type_caster_nested.h" },
Expand Down
13 changes: 13 additions & 0 deletions tests/cpp/rpytest/ft/include/subpkg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

#pragma once

struct SPClass
{
};

template <typename T>
struct SPTemplate
{
};

inline int sp_func() { return 3; }
4 changes: 4 additions & 0 deletions tests/cpp/rpytest/ft/subpkg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# autogenerated by 'robotpy-build create-imports rpytest.ft rpytest.ft._rpytest_ft.subpkg'
from ._rpytest_ft.subpkg import SPClass, sp_func

__all__ = ["SPClass", "sp_func"]
4 changes: 4 additions & 0 deletions tests/test_ft_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,7 @@ def __init__(self):
with pytest.raises(TypeError):
PyGoodAbstract()
assert called == [True]


def test_subpkg():
from rpytest.ft.subpkg import SPClass