how can I keep a PyAny pointer in rust pyclass #2931
Answered
by
birkenfeld
shenjackyuanjie
asked this question in
Questions
-
pub mod camera {
use pyo3::prelude::*;
#[pyclass(name = "Camera_rs")]
pub struct CameraRs{
pub dx: f64,
pub dy: f64,
#[pyo3(get, set)]
pub zoom: f64,
#[pyo3(get, set)]
pub max_zoom: f64,
#[pyo3(get, set)]
pub min_zoom: f64,
pub window: &PyAny,
}
#[pymethods]
impl CameraRs {
#[new]
fn py_new(dx: f64, dy: f64, window: &PyAny) -> PyResult<Self> {
return Ok(CameraRs {dx,
dy,
zoom: 1 as f64,
min_zoom: 1 as f64,
max_zoom: 1 as f64,
window})
}
fn __enter__(&self) -> PyResult<()> {
println!("I enter!");
return Ok(())
}
#[allow(unused_variables)]
fn __exit__(&self, exception_type: &PyAny, exception_value: &PyAny, traceback: &PyAny) -> PyResult<()> {
println!("I exit!");
return Ok(())
}
}
} I want to keep a window pointer in my pyclass, but rustc tells me I can't |
Beta Was this translation helpful? Give feedback.
Answered by
birkenfeld
Jan 30, 2023
Replies: 1 comment 10 replies
-
It needs to be In short: |
Beta Was this translation helpful? Give feedback.
10 replies
Answer selected by
shenjackyuanjie
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It needs to be
Py<PyAny>
. This section of the guide may be helpful to understand the difference.In short:
&'py PyAny
is only valid to use as long as you hold the Python Interpreter Lock (GIL). Therefore it is not safe to put into apyclass
, which can live indefinitely as a Python object.Py<PyAny>
requires aPython
GIL token to access and is therefore safe to share around.