File tree 2 files changed +14
-3
lines changed
2 files changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -1342,13 +1342,18 @@ class module_ : public object {
1342
1342
// GraalPy doesn't support PyModule_GetFilenameObject,
1343
1343
// so getting by attribute (see PR #5584)
1344
1344
handle this_module = m_ptr;
1345
- result.attr (" __file__" ) = this_module.attr (" __file__" );
1345
+ if (object this_file = getattr (this_module, " __file__" , none ())) {
1346
+ result.attr (" __file__" ) = this_file;
1347
+ }
1346
1348
#else
1347
1349
handle this_file = PyModule_GetFilenameObject (m_ptr);
1348
- if (!this_file) {
1350
+ if (this_file) {
1351
+ result.attr (" __file__" ) = this_file;
1352
+ } else if (PyErr_ExceptionMatches (PyExc_SystemError) != 0 ) {
1353
+ PyErr_Clear ();
1354
+ } else {
1349
1355
throw error_already_set ();
1350
1356
}
1351
- result.attr (" __file__" ) = this_file;
1352
1357
#endif
1353
1358
attr (name) = result;
1354
1359
return result;
Original file line number Diff line number Diff line change @@ -61,6 +61,9 @@ PYBIND11_EMBEDDED_MODULE(widget_module, m) {
61
61
.def_property_readonly (" the_message" , &Widget::the_message);
62
62
63
63
m.def (" add" , [](int i, int j) { return i + j; });
64
+
65
+ auto sub = m.def_submodule (" sub" );
66
+ sub.def (" add" , [](int i, int j) { return i + j; });
64
67
}
65
68
66
69
PYBIND11_EMBEDDED_MODULE (trampoline_module, m) {
@@ -316,6 +319,9 @@ TEST_CASE("Restart the interpreter") {
316
319
auto cpp_module = py::module_::import (" widget_module" );
317
320
REQUIRE (cpp_module.attr (" add" )(1 , 2 ).cast <int >() == 3 );
318
321
322
+ // Also verify submodules work
323
+ REQUIRE (cpp_module.attr (" sub" ).attr (" add" )(1 , 41 ).cast <int >() == 42 );
324
+
319
325
// C++ type information is reloaded and can be used in python modules.
320
326
auto py_module = py::module_::import (" test_interpreter" );
321
327
auto py_widget = py_module.attr (" DerivedWidget" )(" Hello after restart" );
You can’t perform that action at this time.
0 commit comments