Skip to content

Commit

Permalink
added module::import statement
Browse files Browse the repository at this point in the history
  • Loading branch information
Wenzel Jakob committed Oct 13, 2015
1 parent 9329669 commit db028d6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion example/example9.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
example/example9.cpp -- nested modules and internal references
example/example9.cpp -- nested modules, importing modules, and
internal references
Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
Expand Down Expand Up @@ -49,4 +50,6 @@ void init_ex9(py::module &m) {
.def("get_a2", &B::get_a2, "Return the internal A 2", py::return_value_policy::reference_internal)
.def_readwrite("a1", &B::a1) // def_readonly uses an internal reference return policy by default
.def_readwrite("a2", &B::a2);

m.attr("OD") = py::module::import("collections").attr("OrderedDict");
}
2 changes: 2 additions & 0 deletions example/example9.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
print(example.submodule.__name__)

from example.submodule import *
from example import OD

submodule_func()

Expand All @@ -26,3 +27,4 @@
print(b.get_a2())
print(b.a2)

print(OD([(1, 'a'), (2, 'b')]))
1 change: 1 addition & 0 deletions example/example9.ref
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ A[42]
A[42]
A[43]
A[43]
OrderedDict([(1, 'a'), (2, 'b')])
B destructor
A destructor
A destructor
4 changes: 4 additions & 0 deletions include/pybind/pybind.h
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@ class module : public object {
attr(name) = result;
return result;
}

static module import(const char *name) {
return module(PyImport_ImportModule(name), false);
}
};

NAMESPACE_BEGIN(detail)
Expand Down

0 comments on commit db028d6

Please sign in to comment.