From 47557157d84d2821accf81ff6d72041635ccd050 Mon Sep 17 00:00:00 2001 From: Andreas Sandberg Date: Mon, 31 Jul 2017 11:19:41 +0100 Subject: [PATCH] python: Make GlobalExitEvent.getCode() return an int PyBind normally casts integers returned from the C to long in Python. This is normally fine since long in most cases behaves just like an int. However, when passing the return value from getcode() to sys.exit, unexpected behavior ensues. Due to the way the function is defined, any type other than int (with the exception of None) will be treated as an error and be equivalent to sys.exit(1). Since we frequently use the sys.exit(event.getCode()) pattern, we need to ensure that the function returns an integer. This change adds an explicit type conversion to a Python integer in the wrapper code. Change-Id: I73d6b881025064afa2b2e6eb4512fa2a4b0a87da Signed-off-by: Andreas Sandberg Reviewed-by: Jose Marinho Reviewed-by: Curtis Dunham Reviewed-on: https://gem5-review.googlesource.com/4280 Reviewed-by: Jason Lowe-Power Reviewed-by: Joe Gross --- src/python/pybind11/event.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/python/pybind11/event.cc b/src/python/pybind11/event.cc index f9e65685d..88ee6996a 100644 --- a/src/python/pybind11/event.cc +++ b/src/python/pybind11/event.cc @@ -135,7 +135,10 @@ pybind_init_event(py::module &m_native) std::unique_ptr>( m, "GlobalSimLoopExitEvent") .def("getCause", &GlobalSimLoopExitEvent::getCause) - .def("getCode", &GlobalSimLoopExitEvent::getCode) + .def("getCode", [](GlobalSimLoopExitEvent *e) { + return py::reinterpret_steal( + PyInt_FromLong(e->getCode())); + }) ; // Event base class. These should never be returned directly to -- 2.30.2