Skip to content

Commit cf2c307

Browse files
committed
Merge branch 'master' into sh_merge_master
2 parents 485dace + a05bc3d commit cf2c307

File tree

6 files changed

+272
-120
lines changed

6 files changed

+272
-120
lines changed

include/pybind11/detail/class.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,8 @@ extern "C" inline void pybind11_object_dealloc(PyObject *self) {
457457
#endif
458458
}
459459

460+
std::string error_string();
461+
460462
/** Create the type which can be used as a common base for all classes. This is
461463
needed in order to satisfy Python's requirements for multiple inheritance.
462464
Return value: New reference. */
@@ -492,7 +494,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
492494
type->tp_weaklistoffset = offsetof(instance, weakrefs);
493495

494496
if (PyType_Ready(type) < 0) {
495-
pybind11_fail("PyType_Ready failed in make_object_base_type():" + error_string());
497+
pybind11_fail("PyType_Ready failed in make_object_base_type(): " + error_string());
496498
}
497499

498500
setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
@@ -709,7 +711,7 @@ inline PyObject *make_new_python_type(const type_record &rec) {
709711
}
710712

711713
if (PyType_Ready(type) < 0) {
712-
pybind11_fail(std::string(rec.name) + ": PyType_Ready failed (" + error_string() + ")!");
714+
pybind11_fail(std::string(rec.name) + ": PyType_Ready failed: " + error_string());
713715
}
714716

715717
assert(!rec.dynamic_attr || PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));

include/pybind11/detail/type_caster_base.h

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -470,73 +470,6 @@ PYBIND11_NOINLINE bool isinstance_generic(handle obj, const std::type_info &tp)
470470
return isinstance(obj, type);
471471
}
472472

473-
PYBIND11_NOINLINE std::string error_string(const char *called) {
474-
error_scope scope; // Fetch error state (will be restored when this function returns).
475-
if (scope.type == nullptr) {
476-
if (called == nullptr) {
477-
called = "pybind11::detail::error_string()";
478-
}
479-
pybind11_fail("Internal error: " + std::string(called)
480-
+ " called while Python error indicator not set.");
481-
}
482-
483-
PyErr_NormalizeException(&scope.type, &scope.value, &scope.trace);
484-
if (scope.trace != nullptr) {
485-
PyException_SetTraceback(scope.value, scope.trace);
486-
}
487-
488-
std::string errorString;
489-
if (scope.type) {
490-
errorString += handle(scope.type).attr("__name__").cast<std::string>();
491-
errorString += ": ";
492-
}
493-
if (scope.value) {
494-
errorString += (std::string) str(scope.value);
495-
}
496-
497-
#if !defined(PYPY_VERSION)
498-
if (scope.trace) {
499-
auto *trace = (PyTracebackObject *) scope.trace;
500-
501-
/* Get the deepest trace possible */
502-
while (trace->tb_next) {
503-
trace = trace->tb_next;
504-
}
505-
506-
PyFrameObject *frame = trace->tb_frame;
507-
Py_XINCREF(frame);
508-
errorString += "\n\nAt:\n";
509-
while (frame) {
510-
# if PY_VERSION_HEX >= 0x030900B1
511-
PyCodeObject *f_code = PyFrame_GetCode(frame);
512-
# else
513-
PyCodeObject *f_code = frame->f_code;
514-
Py_INCREF(f_code);
515-
# endif
516-
int lineno = PyFrame_GetLineNumber(frame);
517-
errorString += " ";
518-
errorString += handle(f_code->co_filename).cast<std::string>();
519-
errorString += '(';
520-
errorString += std::to_string(lineno);
521-
errorString += "): ";
522-
errorString += handle(f_code->co_name).cast<std::string>();
523-
errorString += '\n';
524-
Py_DECREF(f_code);
525-
# if PY_VERSION_HEX >= 0x030900B1
526-
auto *b_frame = PyFrame_GetBack(frame);
527-
# else
528-
auto *b_frame = frame->f_back;
529-
Py_XINCREF(b_frame);
530-
# endif
531-
Py_DECREF(frame);
532-
frame = b_frame;
533-
}
534-
}
535-
#endif
536-
537-
return errorString;
538-
}
539-
540473
PYBIND11_NOINLINE handle get_object_handle(const void *ptr, const detail::type_info *type) {
541474
auto &instances = get_internals().registered_instances;
542475
auto range = instances.equal_range(ptr);

include/pybind11/pybind11.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,17 +2880,21 @@ void print(Args &&...args) {
28802880
detail::print(c.args(), c.kwargs());
28812881
}
28822882

2883-
error_already_set::~error_already_set() {
2884-
if (m_type) {
2885-
gil_scoped_acquire gil;
2886-
error_scope scope;
2887-
m_type.release().dec_ref();
2888-
m_value.release().dec_ref();
2889-
m_trace.release().dec_ref();
2890-
}
2883+
inline void
2884+
error_already_set::m_fetched_error_deleter(detail::error_fetch_and_normalize *raw_ptr) {
2885+
gil_scoped_acquire gil;
2886+
error_scope scope;
2887+
delete raw_ptr;
2888+
}
2889+
2890+
inline const char *error_already_set::what() const noexcept {
2891+
gil_scoped_acquire gil;
2892+
error_scope scope;
2893+
return m_fetched_error->error_string().c_str();
28912894
}
28922895

28932896
PYBIND11_NAMESPACE_BEGIN(detail)
2897+
28942898
inline function
28952899
get_type_override(const void *this_ptr, const type_info *this_type, const char *name) {
28962900
handle self = get_object_handle(this_ptr, this_type);

0 commit comments

Comments
 (0)