How do we gracefully handle an error during Rust->Python type conversion? #3641
-
I'm having an issue getting a NaiveDate/NaiveDateTime converted to a Python type. I'm porting an internal library for type conversion and schema analysis of text files from pure Python to Rust + pyo3, and the performance improvement is between 15-20X in most cases. However, when dealing with dates, I'm running into an issue where some values are out of range. I guess the conversion is attempted regardless, as the code panics and we get the dreaded PanicException bubbling up in Python:
Does anyone know how I can handle this on the Rust side? I'm new to pyo3 and don't see that methods in ToPyObject or IntoPy return anything other than a PyObject. Not result types are used. edit: I should add, we have considered validating the ranges before attempting a conversion, but not sure we could handle all of the edge cases properly. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, sorry you've hit a painful edge case here. We have a longstanding issue in #1813 but we need to refactor our traits to make sense of this because having What you can do is use the python datetime constructors to create the python objects in your own code and handle errors there. |
Beta Was this translation helpful? Give feedback.
Hi, sorry you've hit a painful edge case here. We have a longstanding issue in #1813 but we need to refactor our traits to make sense of this because having
ToPyObject
andIntoPy
is already confusing, having fallible versions of both for a total of 4 traits seems like too much. Unfortunately it is outside of my capacity to propose a solution right now, but this definitely needs fixing in the mid to long term. Help is also welcome.What you can do is use the python datetime constructors to create the python objects in your own code and handle errors there.