Merge branch 'pybind:master' into master

This commit is contained in:
Steve R. Sun 2022-03-03 07:41:19 +08:00 committed by GitHub
commit 842801f7cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 11 deletions

View File

@ -502,9 +502,10 @@ PYBIND11_NOINLINE std::string error_string() {
}
PyFrameObject *frame = trace->tb_frame;
Py_XINCREF(frame);
errorString += "\n\nAt:\n";
while (frame) {
# if PY_VERSION_HEX >= 0x03090000
# if PY_VERSION_HEX >= 0x030900B1
PyCodeObject *f_code = PyFrame_GetCode(frame);
# else
PyCodeObject *f_code = frame->f_code;
@ -514,8 +515,15 @@ PYBIND11_NOINLINE std::string error_string() {
errorString += " " + handle(f_code->co_filename).cast<std::string>() + "("
+ std::to_string(lineno)
+ "): " + handle(f_code->co_name).cast<std::string>() + "\n";
frame = frame->f_back;
Py_DECREF(f_code);
# if PY_VERSION_HEX >= 0x030900B1
auto *b_frame = PyFrame_GetBack(frame);
# else
auto *b_frame = frame->f_back;
Py_XINCREF(b_frame);
# endif
Py_DECREF(frame);
frame = b_frame;
}
}
#endif

View File

@ -82,7 +82,7 @@ template <eval_mode mode = eval_expr, size_t N>
object eval(const char (&s)[N], object global = globals(), 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);
return eval<mode>(expr, std::move(global), std::move(local));
}
inline void exec(const str &expr, object global = globals(), object local = object()) {
@ -91,7 +91,7 @@ inline void exec(const str &expr, object global = globals(), object local = obje
template <size_t N>
void exec(const char (&s)[N], object global = globals(), object local = object()) {
eval<eval_statements>(s, global, local);
eval<eval_statements>(s, std::move(global), std::move(local));
}
#if defined(PYPY_VERSION)

View File

@ -2636,9 +2636,7 @@ get_type_override(const void *this_ptr, const type_info *this_type, const char *
/* Don't call dispatch code if invoked from overridden function.
Unfortunately this doesn't work on PyPy. */
#if !defined(PYPY_VERSION) && PY_VERSION_HEX < 0x030B0000
// TODO: Remove PyPy workaround for Python 3.11.
// Current API fails on 3.11 since co_varnames can be null.
#if !defined(PYPY_VERSION)
# if PY_VERSION_HEX >= 0x03090000
PyFrameObject *frame = PyThreadState_GetFrame(PyThreadState_Get());
if (frame != nullptr) {
@ -2646,9 +2644,11 @@ get_type_override(const void *this_ptr, const type_info *this_type, const char *
// f_code is guaranteed to not be NULL
if ((std::string) str(f_code->co_name) == name && f_code->co_argcount > 0) {
PyObject *locals = PyEval_GetLocals();
if (locals != nullptr && f_code->co_varnames != nullptr) {
PyObject *self_caller
= dict_getitem(locals, PyTuple_GET_ITEM(f_code->co_varnames, 0));
if (locals != nullptr) {
PyObject *co_varnames = PyObject_GetAttrString((PyObject *) f_code, "co_varnames");
PyObject *self_arg = PyTuple_GET_ITEM(co_varnames, 0);
Py_DECREF(co_varnames);
PyObject *self_caller = dict_getitem(locals, self_arg);
if (self_caller == self.ptr()) {
Py_DECREF(f_code);
Py_DECREF(frame);
@ -2694,9 +2694,9 @@ get_type_override(const void *this_ptr, const type_info *this_type, const char *
d.ptr());
if (result == nullptr)
throw error_already_set();
Py_DECREF(result);
if (d["self"].is_none())
return function();
Py_DECREF(result);
#endif
return override;