Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
1d3c4bc5
Commit
1d3c4bc5
authored
Jun 06, 2017
by
Dean Moldovan
Browse files
Fix missing default globals in eval/exec when embedding
Fixes #887.
parent
91bbe2f2
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
18 additions
and
17 deletions
+18
-17
include/pybind11/eval.h
include/pybind11/eval.h
+5
-15
include/pybind11/pybind11.h
include/pybind11/pybind11.h
+6
-2
tests/test_embed/test_interpreter.cpp
tests/test_embed/test_interpreter.cpp
+7
-0
No files found.
include/pybind11/eval.h
View file @
1d3c4bc5
...
@@ -27,12 +27,7 @@ enum eval_mode {
...
@@ -27,12 +27,7 @@ enum eval_mode {
};
};
template
<
eval_mode
mode
=
eval_expr
>
template
<
eval_mode
mode
=
eval_expr
>
object
eval
(
str
expr
,
object
global
=
object
(),
object
local
=
object
())
{
object
eval
(
str
expr
,
object
global
=
globals
(),
object
local
=
object
())
{
if
(
!
global
)
{
global
=
reinterpret_borrow
<
object
>
(
PyEval_GetGlobals
());
if
(
!
global
)
global
=
dict
();
}
if
(
!
local
)
if
(
!
local
)
local
=
global
;
local
=
global
;
...
@@ -55,29 +50,24 @@ object eval(str expr, object global = object(), object local = object()) {
...
@@ -55,29 +50,24 @@ object eval(str expr, object global = object(), object local = object()) {
}
}
template
<
eval_mode
mode
=
eval_expr
,
size_t
N
>
template
<
eval_mode
mode
=
eval_expr
,
size_t
N
>
object
eval
(
const
char
(
&
s
)[
N
],
object
global
=
object
(),
object
local
=
object
())
{
object
eval
(
const
char
(
&
s
)[
N
],
object
global
=
globals
(),
object
local
=
object
())
{
/* Support raw string literals by removing common leading whitespace */
/* Support raw string literals by removing common leading whitespace */
auto
expr
=
(
s
[
0
]
==
'\n'
)
?
str
(
module
::
import
(
"textwrap"
).
attr
(
"dedent"
)(
s
))
auto
expr
=
(
s
[
0
]
==
'\n'
)
?
str
(
module
::
import
(
"textwrap"
).
attr
(
"dedent"
)(
s
))
:
str
(
s
);
:
str
(
s
);
return
eval
<
mode
>
(
expr
,
global
,
local
);
return
eval
<
mode
>
(
expr
,
global
,
local
);
}
}
inline
void
exec
(
str
expr
,
object
global
=
object
(),
object
local
=
object
())
{
inline
void
exec
(
str
expr
,
object
global
=
globals
(),
object
local
=
object
())
{
eval
<
eval_statements
>
(
expr
,
global
,
local
);
eval
<
eval_statements
>
(
expr
,
global
,
local
);
}
}
template
<
size_t
N
>
template
<
size_t
N
>
void
exec
(
const
char
(
&
s
)[
N
],
object
global
=
object
(),
object
local
=
object
())
{
void
exec
(
const
char
(
&
s
)[
N
],
object
global
=
globals
(),
object
local
=
object
())
{
eval
<
eval_statements
>
(
s
,
global
,
local
);
eval
<
eval_statements
>
(
s
,
global
,
local
);
}
}
template
<
eval_mode
mode
=
eval_statements
>
template
<
eval_mode
mode
=
eval_statements
>
object
eval_file
(
str
fname
,
object
global
=
object
(),
object
local
=
object
())
{
object
eval_file
(
str
fname
,
object
global
=
globals
(),
object
local
=
object
())
{
if
(
!
global
)
{
global
=
reinterpret_borrow
<
object
>
(
PyEval_GetGlobals
());
if
(
!
global
)
global
=
dict
();
}
if
(
!
local
)
if
(
!
local
)
local
=
global
;
local
=
global
;
...
...
include/pybind11/pybind11.h
View file @
1d3c4bc5
...
@@ -799,8 +799,12 @@ public:
...
@@ -799,8 +799,12 @@ public:
};
};
/// \ingroup python_builtins
/// \ingroup python_builtins
/// Return a dictionary representing the global symbol table, i.e. ``__main__.__dict__``.
/// Return a dictionary representing the global variables in the current execution frame,
inline
dict
globals
()
{
return
module
::
import
(
"__main__"
).
attr
(
"__dict__"
).
cast
<
dict
>
();
}
/// or ``__main__.__dict__`` if there is no frame (usually when the interpreter is embedded).
inline
dict
globals
()
{
PyObject
*
p
=
PyEval_GetGlobals
();
return
reinterpret_borrow
<
dict
>
(
p
?
p
:
module
::
import
(
"__main__"
).
attr
(
"__dict__"
).
ptr
());
}
NAMESPACE_BEGIN
(
detail
)
NAMESPACE_BEGIN
(
detail
)
/// Generic support for creating new Python heap types
/// Generic support for creating new Python heap types
...
...
tests/test_embed/test_interpreter.cpp
View file @
1d3c4bc5
...
@@ -153,3 +153,10 @@ TEST_CASE("Subinterpreter") {
...
@@ -153,3 +153,10 @@ TEST_CASE("Subinterpreter") {
REQUIRE
(
py
::
hasattr
(
py
::
module
::
import
(
"__main__"
),
"main_tag"
));
REQUIRE
(
py
::
hasattr
(
py
::
module
::
import
(
"__main__"
),
"main_tag"
));
REQUIRE
(
py
::
hasattr
(
py
::
module
::
import
(
"widget_module"
),
"extension_module_tag"
));
REQUIRE
(
py
::
hasattr
(
py
::
module
::
import
(
"widget_module"
),
"extension_module_tag"
));
}
}
TEST_CASE
(
"Execution frame"
)
{
// When the interpreter is embedded, there is no execution frame, but `py::exec`
// should still function by using reasonable globals: `__main__.__dict__`.
py
::
exec
(
"var = dict(number=42)"
);
REQUIRE
(
py
::
globals
()[
"var"
][
"number"
].
cast
<
int
>
()
==
42
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment