Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dune-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Core Modules
dune-common
Commits
a8132037
Commit
a8132037
authored
5 years ago
by
Andreas Dedner
Browse files
Options
Downloads
Patches
Plain Diff
imporve handling of includes in `importclass.py`
parent
aa84555d
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!790
Feature/add python bindings
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
dune/python/test/pythontests.py
+12
-9
12 additions, 9 deletions
dune/python/test/pythontests.py
python/dune/generator/importclass.py
+17
-17
17 additions, 17 deletions
python/dune/generator/importclass.py
with
29 additions
and
26 deletions
dune/python/test/pythontests.py
+
12
−
9
View file @
a8132037
import
io
classExportCode
=
"""
#include <cmath>
from
io
import
StringIO
classACode
=
"""
struct MyClassA {
MyClassA(int a,int b) : a_(a), b_(b) {}
int a_,b_;
};
"""
classBCode
=
"""
#include <cmath>
template <class T> struct MyClassB {
MyClassB(T &t, int p) : a_(std::pow(t.a_,p)), b_(std::pow(t.b_,p)) {}
int a_,b_;
};
"""
runCode
=
"""
template <class T> int run(T &t) {
return t.a_ * t.b_;
}
...
...
@@ -19,16 +23,15 @@ def test_class_export():
from
dune.generator.algorithm
import
run
from
dune.generator
import
path
from
dune.typeregistry
import
generateTypeName
code
=
io
.
StringIO
(
classExportCode
)
cls
=
load
(
"
MyClassA
"
,
code
,
10
,
20
)
print
(
run
(
"
run
"
,
"
myclass.hh
"
,
cls
)
)
cls
=
load
(
"
MyClassA
"
,
StringIO
(
classACode
),
10
,
20
)
assert
run
(
"
run
"
,
StringIO
(
runCode
),
cls
)
==
10
*
20
clsName
,
includes
=
generateTypeName
(
"
MyClassB
"
,
cls
)
cls
=
load
(
clsName
,
[
codes
]
+
inclu
de
s
,
cls
,
2
)
print
(
run
(
"
run
"
,
"
myclass.hh
"
,
cls
)
)
cls
=
load
(
clsName
,
StringIO
(
classBCo
de
)
,
cls
,
2
)
assert
run
(
"
run
"
,
StringIO
(
runCode
),
cls
)
==
10
**
2
*
20
**
2
if
__name__
==
"
__main__
"
:
try
:
from
dune.common.module
import
get_dune_py_dir
_
=
get_dune_py_dir
()
test_class_export
()
except
:
except
ImportError
:
pass
This diff is collapsed.
Click to expand it.
python/dune/generator/importclass.py
+
17
−
17
View file @
a8132037
...
...
@@ -11,27 +11,21 @@ def load(className, includeFiles, *args):
source
=
'
#include <config.h>
\n\n
'
source
+=
'
#define USING_DUNE_PYTHON 1
\n\n
'
includes
=
[]
if
isString
(
includeFiles
):
if
not
os
.
path
.
dirname
(
includeFiles
):
with
open
(
includeFiles
,
"
r
"
)
as
include
:
source
+=
include
.
read
()
source
+=
"
\n
"
else
:
source
+=
"
#include <
"
+
includeFiles
+
"
>
\n
"
includes
+=
[
includeFiles
]
elif
hasattr
(
includeFiles
,
"
readable
"
):
# for IOString
with
includeFiles
as
include
:
source
+=
include
.
read
()
source
+=
"
\n
"
elif
isinstance
(
includeFiles
,
list
):
for
includefile
in
includeFiles
:
if
isString
(
includeFiles
)
or
hasattr
(
includeFiles
,
"
readable
"
):
includeFiles
=
[
includeFiles
]
for
includefile
in
includeFiles
:
if
isString
(
includefile
):
if
not
os
.
path
.
dirname
(
includefile
):
with
open
(
includefile
,
"
r
"
)
as
include
:
source
+=
include
.
read
()
source
+=
"
\n
"
else
:
source
+=
"
#include <
"
+
includefile
+
"
>
\n
"
includes
+=
[
includefile
]
else
:
source
+=
"
#include <
"
+
includefile
+
"
>
\n
"
includes
+=
[
includefile
]
elif
hasattr
(
includefile
,
"
readable
"
):
# for IOString
with
includefile
as
include
:
source
+=
include
.
read
()
source
+=
"
\n
"
argTypes
=
[]
for
arg
in
args
:
...
...
@@ -52,6 +46,7 @@ def load(className, includeFiles, *args):
source
+=
"
PYBIND11_MODULE(
"
+
moduleName
+
"
, module )
\n
"
source
+=
"
{
\n
"
includes
+=
[
"
python/dune/generated/
"
+
moduleName
+
"
.cc
"
]
source
+=
"
auto cls = Dune::Python::insertClass<
"
+
className
+
\
"
>( module,
\"
cls
\"
,
"
+
\
"
Dune::Python::GenerateTypeName(
\"
"
+
className
+
"
\"
),
"
+
\
...
...
@@ -63,4 +58,9 @@ def load(className, includeFiles, *args):
source
+=
"
);
\n
"
source
+=
"
}
"
source
=
"
#ifndef def_
"
+
moduleName
+
\
"
\n
#define def_
"
+
moduleName
+
"
\n
"
+
\
source
+
\
"
\n
#endif
\n
"
return
builder
.
load
(
moduleName
,
source
,
signature
).
cls
(
*
args
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment