mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-12 01:10:34 +00:00
Support raw string literals as input for py::eval (#766)
* Support raw string literals as input for py::eval * Dedent only when needed
This commit is contained in:
committed by
Wenzel Jakob
parent
6db60cd945
commit
194d8b99b3
@@ -11,8 +11,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "pybind11.h"
|
||||
|
||||
NAMESPACE_BEGIN(pybind11)
|
||||
@@ -56,6 +54,14 @@ object eval(str expr, object global = object(), object local = object()) {
|
||||
return reinterpret_steal<object>(result);
|
||||
}
|
||||
|
||||
template <eval_mode mode = eval_expr, size_t N>
|
||||
object eval(const char (&s)[N], object global = object(), object local = object()) {
|
||||
/* Support raw string literals by removing common leading whitespace */
|
||||
auto expr = (s[0] == '\n') ? str(module::import("textwrap").attr("dedent")(s))
|
||||
: str(s);
|
||||
return eval<mode>(expr, global, local);
|
||||
}
|
||||
|
||||
template <eval_mode mode = eval_statements>
|
||||
object eval_file(str fname, object global = object(), object local = object()) {
|
||||
if (!global) {
|
||||
|
||||
Reference in New Issue
Block a user