-
Notifications
You must be signed in to change notification settings - Fork 790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
compile_error test for PyClass: Send #948
Conversation
Because |
See the compile_fail test I added. At the moment it's possible to make this
And then yes, But accessing the same |
Not sure what you're suggesting with this point? |
I had a wrong idea, please don't mind that. How about adding bounds only for the code related to PyObject/Py? impl<T: PyClass + Send + Sync> ToPyObject for &PyCell<T> {
fn to_object(&self, py: Python<'_>) -> PyObject {
unsafe { PyObject::from_borrowed_ptr(py, self.as_ptr()) }
}
}
impl<T> Py<T> {
/// Create a new instance `Py<T>`.
///
/// This method is **soft-duplicated** since PyO3 0.9.0.
/// Use [`PyCell::new`](../pycell/struct.PyCell.html#method.new) and
/// `Py::from` instead.
pub fn new(py: Python, value: impl Into<PyClassInitializer<T>>) -> PyResult<Py<T>>
where
T: PyClass + Send + Sync,
T::BaseLayout: PyBorrowFlagLayout<T::BaseType>,
{
let initializer = value.into();
let obj = unsafe { initializer.create_cell(py)? };
let ob = unsafe { Py::from_owned_ptr(obj as _) };
Ok(ob)
}
}
impl<'a, T> std::convert::From<PyRef<'a, T>> for Py<T>
where
T: PyClass + Send + Sync,
{
fn from(pyref: PyRef<'a, T>) -> Self {
unsafe { Py::from_borrowed_ptr(pyref.as_ptr()) }
}
} But if you find this hack not so meaningful, I'm for adding |
Hmm, I'm not sure it's enough. If we give a Like the existence of this function:
Even giving a So this is why I think the bounds have to go on |
Also, I'm not sure why the compile_fail test is giving a slightly different output under Windows... I've asked upstream at dtolnay/trybuild#73 Workaround is to just not have a compile_fail test for this, but I think it's nice to have one... |
👍
Thank you for filing the issue. |
Rethinking the problem with Python's threading module: though it doesn't work concurrently, isn't it still unsafe to access Rust's |
Yes, which is I think why we need |
From what I understand, we only need Evidence: |
@programmerjake In addition, because unsafe impl Sync for PyObject {}
unsafe impl<T> Sync for Py<T> {} ? |
Good catch @programmerjake, I agree you're correct that we need only the
I can't see a way a user could write unsafe concurrent access with |
Agreed. Was writing a much more confusing reply that was basically saying the same thing when you replied. |
We will want to make sure that |
I think we can just clone it and move it to another thread, but I'm not sure how it affects the user code now 🤔 |
As part of the invariant that Note that it is Undefined Behavior to read the reference count using a plain memory read (what |
Changed to just use
Completely agree. I'll open a new issue now to clean up |
6257ad2
to
6e42c2a
Compare
Now that #966 is merged I rebased this to be just the compile_error test which is blocked on trybuild. |
Doesn't look like the trybuild PR is moving any time soon... Also, as we added Closing for now and we can perhaps revisit in the future. |
I realised that we have to require
Send
+Sync
for#[pyclass]
.Otherwise we violate Rust's thread safety by allowing things like
Rc<RefCell<T>>
to cross between threads, which isn't good!