mirror of
https://github.com/pocketpy/pocketpy
synced 2025-11-09 21:20:17 +00:00
Compare commits
No commits in common. "8024f46b544698ef51d1ca0bb50a29cf0d411a29" and "a2c0bddf794c4257ff100e1c91ff045810e6c32e" have entirely different histories.
8024f46b54
...
a2c0bddf79
@ -28,7 +28,7 @@ int main() {
|
|||||||
py::exec(script);
|
py::exec(script);
|
||||||
std::cout << "Numpy script executed successfully." << std::endl;
|
std::cout << "Numpy script executed successfully." << std::endl;
|
||||||
}
|
}
|
||||||
catch (const py::python_error& e) {
|
catch (const py::error_already_set& e) {
|
||||||
// Catch and print Python exceptions
|
// Catch and print Python exceptions
|
||||||
std::cerr << "Python error: " << e.what() << std::endl;
|
std::cerr << "Python error: " << e.what() << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
@ -76,8 +76,6 @@ inline bool isinstance(handle obj, type type) { return py_isinstance(obj.ptr(),
|
|||||||
|
|
||||||
inline bool python_error::match(type type) const { return isinstance(m_exception.ptr(), type); }
|
inline bool python_error::match(type type) const { return isinstance(m_exception.ptr(), type); }
|
||||||
|
|
||||||
inline bool error_already_set::match(type type) const { return py_matchexc(type.index()); }
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
constexpr inline bool is_pyobject_v =
|
constexpr inline bool is_pyobject_v =
|
||||||
std::is_base_of_v<object, std::decay_t<T>> || std::is_same_v<type, T>;
|
std::is_base_of_v<object, std::decay_t<T>> || std::is_same_v<type, T>;
|
||||||
|
|||||||
@ -25,12 +25,7 @@ private:
|
|||||||
object m_exception;
|
object m_exception;
|
||||||
};
|
};
|
||||||
|
|
||||||
class error_already_set : public std::exception {
|
using error_already_set = python_error;
|
||||||
public:
|
|
||||||
bool match(py_Type type) const { return py_matchexc(type); }
|
|
||||||
|
|
||||||
bool match(type type) const;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <auto Fn, typename... Args>
|
template <auto Fn, typename... Args>
|
||||||
inline auto raise_call(Args&&... args) {
|
inline auto raise_call(Args&&... args) {
|
||||||
@ -53,17 +48,7 @@ inline auto raise_call(Args&&... args) {
|
|||||||
throw python_error(what, std::move(e));
|
throw python_error(what, std::move(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
class stop_iteration {
|
class stop_iteration {};
|
||||||
public:
|
|
||||||
stop_iteration() = default;
|
|
||||||
|
|
||||||
stop_iteration(object value) : m_value(std::move(value)) {}
|
|
||||||
|
|
||||||
object value() const { return m_value; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
object m_value;
|
|
||||||
};
|
|
||||||
|
|
||||||
class cast_error : public std::runtime_error {
|
class cast_error : public std::runtime_error {
|
||||||
using std::runtime_error::runtime_error;
|
using std::runtime_error::runtime_error;
|
||||||
|
|||||||
@ -562,21 +562,12 @@ private:
|
|||||||
py_exception(tp_IndexError, e.what());
|
py_exception(tp_IndexError, e.what());
|
||||||
} catch(std::range_error& e) {
|
} catch(std::range_error& e) {
|
||||||
py_exception(tp_ValueError, e.what());
|
py_exception(tp_ValueError, e.what());
|
||||||
} catch(stop_iteration& e) {
|
} catch(stop_iteration&) { StopIteration(); } catch(index_error& e) {
|
||||||
if(auto value_ptr = e.value().ptr()) {
|
|
||||||
bool ok = py_tpcall(tp_StopIteration, 1, value_ptr);
|
|
||||||
if(ok) { py_raise(py_retval()); }
|
|
||||||
} else {
|
|
||||||
StopIteration();
|
|
||||||
}
|
|
||||||
} catch(index_error& e) {
|
|
||||||
py_exception(tp_IndexError, e.what());
|
py_exception(tp_IndexError, e.what());
|
||||||
} catch(key_error& e) { py_exception(tp_KeyError, e.what()); } catch(value_error& e) {
|
} catch(key_error& e) { py_exception(tp_KeyError, e.what()); } catch(value_error& e) {
|
||||||
py_exception(tp_ValueError, e.what());
|
py_exception(tp_ValueError, e.what());
|
||||||
} catch(type_error& e) { py_exception(tp_TypeError, e.what()); } catch(import_error& e) {
|
} catch(type_error& e) { py_exception(tp_TypeError, e.what()); } catch(import_error& e) {
|
||||||
py_exception(tp_ImportError, e.what());
|
py_exception(tp_ImportError, e.what());
|
||||||
} catch(error_already_set&) {
|
|
||||||
// exception already set, do nothing
|
|
||||||
} catch(attribute_error& e) {
|
} catch(attribute_error& e) {
|
||||||
py_exception(tp_AttributeError, e.what());
|
py_exception(tp_AttributeError, e.what());
|
||||||
} catch(std::exception& e) { py_exception(tp_RuntimeError, e.what()); }
|
} catch(std::exception& e) { py_exception(tp_RuntimeError, e.what()); }
|
||||||
|
|||||||
@ -40,37 +40,6 @@ TEST_F(PYBIND11_TEST, exception_cpp_to_python) {
|
|||||||
throw py::stop_iteration();
|
throw py::stop_iteration();
|
||||||
});
|
});
|
||||||
py::exec("try:\n test_stop_iteration()\nexcept StopIteration as e:\n pass");
|
py::exec("try:\n test_stop_iteration()\nexcept StopIteration as e:\n pass");
|
||||||
|
|
||||||
m.def("test_stop_iteration_value", []() {
|
|
||||||
throw py::stop_iteration(py::int_(42));
|
|
||||||
});
|
|
||||||
py::exec(
|
|
||||||
"try:\n test_stop_iteration_value()\nexcept StopIteration as e:\n assert e.value == 42");
|
|
||||||
|
|
||||||
m.def("test_error_already_set", []() {
|
|
||||||
KeyError(none().ptr());
|
|
||||||
throw py::error_already_set();
|
|
||||||
});
|
|
||||||
py::exec("try:\n test_error_already_set()\nexcept KeyError as e:\n pass");
|
|
||||||
|
|
||||||
m.def("test_error_already_set_matches", []() {
|
|
||||||
try {
|
|
||||||
KeyError(none().ptr());
|
|
||||||
throw py::error_already_set();
|
|
||||||
} catch(py::error_already_set& e) {
|
|
||||||
if(e.match(tp_KeyError)) { return; }
|
|
||||||
std::rethrow_exception(std::current_exception());
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
StopIteration();
|
|
||||||
throw py::error_already_set();
|
|
||||||
} catch(py::error_already_set& e) {
|
|
||||||
if(e.match(type(tp_StopIteration))) { return; }
|
|
||||||
std::rethrow_exception(std::current_exception());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
py::exec("test_error_already_set_matches()");
|
|
||||||
TEST_EXCEPTION(index_error, IndexError);
|
TEST_EXCEPTION(index_error, IndexError);
|
||||||
// FIXME: TEST_EXCEPTION(key_error, KeyError);
|
// FIXME: TEST_EXCEPTION(key_error, KeyError);
|
||||||
TEST_EXCEPTION(value_error, ValueError);
|
TEST_EXCEPTION(value_error, ValueError);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user