Make more constructors return &PyX instead of Py<PyX>#446
Make more constructors return &PyX instead of Py<PyX>#446konstin merged 1 commit intoPyO3:masterfrom
Conversation
|
Missing changelog entry; I'd like to combine this with the one for PySet when both are merged. |
|
CC @pganssle for the datetime constructors |
|
The datetime constructors broke a bunch of tests. I have a half-completed branch somewhere that fixes the tests, but IIRC it was semi-blocked on #392. @birkenfeld If you don't want to be bothered with fixing the |
|
@pganssle I've found them now 😄 Do you know how to change the The pyfunctions seem to be fine with it. Although, trying to import that module makes python3 segfault... |
Changing it to return |
|
Sorry, can you be more specific what you want to separate? Without the datetime changes there isn't much left here... |
|
Also it's unclear to me how to get the Py from the reference 😄 |
Yeah but there's many examples for datetime module and I thought it's tough stuff to fix all of them.
use pyo3::AsPyPointer;
#[pyfunction]
fn make_date(py: Python<'_>, year: i32, month: u8, day: u8) -> PyResult<Py<PyDate>> {
PyDate::new(py, year, month, day).map(|d| unsafe { Py::from_borrowed_ptr(d.as_ptr()) })
}This works but maybe we need an easier way. |
Uh oh. Should unsafe really be required here? |
|
I think we need a generic impl of that snippet: Lines 137 to 141 in 874d8a0 This would also avoid unsafe in downstream code |
|
This seems to work: impl<'a, T: AsPyPointer> FromPy<&'a T> for Py<T> {
fn from_py(tuple: &'a T, _py: Python) -> Py<T> {
unsafe { Py::from_borrowed_ptr(tuple.as_ptr()) }
}
}Might be good idea to investigate this for soundness and make another pull request of that. |
|
@konstin I noticed we can return #[pyfunction]
fn make_date(py: Python, year: i32, month: u8, day: u8) -> PyResult<&PyDate> {
PyDate::new(py, year, month, day)
}But when we have #[pyfunction]
fn datetime_from_timestamp(py: Python, ts: f64, tz: Option<&PyTzInfo>) -> PyResult<&PyDateTime> {
PyDateTime::from_timestamp(py, ts, tz)
}I think this can be resolved by hacking proc-macro codes, but not sure. |
|
@kngwyu Why can't we use: fn datetime_from_timestamp(py: Python<'p>,
ts: f64, tz: Option<&'p PyTzInfo>)
-> PyResult<&'p PyDateTime> {The |
|
@pganssle |
|
@pganssle #[pyfunction]
fn datetime_from_timestamp<'py>(
py: Python<'py>,
ts: f64,
tz: Option<&'py PyTzInfo>,
) -> PyResult<&'py PyDateTime> {
PyDateTime::from_timestamp(py, ts, tz)
}But now methods can't take lifetime specifiers so we can't compile fn utcoffset<'py>(&self, py: Python<'py>, _dt: &'py PyDateTime) -> PyResult<&'py PyDelta> {
PyDelta::new(py, 0, 3600, 0, true)
}Edited |
Codecov Report
@@ Coverage Diff @@
## master #446 +/- ##
==========================================
- Coverage 87.87% 87.76% -0.11%
==========================================
Files 65 65
Lines 3398 3393 -5
==========================================
- Hits 2986 2978 -8
- Misses 412 415 +3
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #446 +/- ##
==========================================
- Coverage 87.87% 87.85% -0.02%
==========================================
Files 65 65
Lines 3398 3393 -5
==========================================
- Hits 2986 2981 -5
Misses 412 412
Continue to review full report at Codecov.
|
|
Whoops, rustfmt fixed. |
|
Thank you! |
fixes #405
Remaining one is PyTuple, where I'm running into a test failure. Will provide as a separate PR.