{"record":{"id":"e2cb959a322f75e1","repo":"PyO3/pyo3","slug":"the-weakref-weak-reference-instance-should-be-va","errorCode":null,"errorMessage":"The 'weakref' weak reference instance should be valid (non-null and actually a weakref reference)","messagePattern":"The 'weakref' weak reference instance should be valid \\(non-null and actually a weakref reference\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/types/weakref/anyref.rs","lineNumber":332,"sourceCode":"    /// })\n    /// # }\n    /// ```\n    ///\n    /// # Panics\n    /// This function panics is the current object is invalid.\n    /// If used properly this is never the case. (NonNull and actually a weakref type)\n    ///\n    /// [`PyWeakref_GetRef`]: https://docs.python.org/3/c-api/weakref.html#c.PyWeakref_GetRef\n    /// [`weakref.ReferenceType`]: https://docs.python.org/3/library/weakref.html#weakref.ReferenceType\n    /// [`weakref.ref`]: https://docs.python.org/3/library/weakref.html#weakref.ref\n    fn upgrade(&self) -> Option<Bound<'py, PyAny>>;\n}\n\nimpl<'py> PyWeakrefMethods<'py> for Bound<'py, PyWeakref> {\n    fn upgrade(&self) -> Option<Bound<'py, PyAny>> {\n        let mut obj: *mut ffi::PyObject = core::ptr::null_mut();\n        match unsafe { ffi::compat::PyWeakref_GetRef(self.as_ptr(), &mut obj) } {\n            core::ffi::c_int::MIN..=-1 => panic!(\"The 'weakref' weak reference instance should be valid (non-null and actually a weakref reference)\"),\n            0 => None,\n            1..=core::ffi::c_int::MAX => Some(unsafe { obj.assume_owned_unchecked(self.py()) }),\n        }\n    }\n}\n\n#[cfg(test)]\nmod tests {\n    #[allow(unused_imports, reason = \"conditionally used\")]\n    use crate::platform::prelude::*;\n    use crate::types::any::{PyAny, PyAnyMethods};\n    use crate::types::weakref::{PyWeakref, PyWeakrefMethods, PyWeakrefProxy, PyWeakrefReference};\n    use crate::{Bound, PyResult, Python};\n\n    fn new_reference<'py>(object: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyWeakref>> {\n        let reference = PyWeakrefReference::new(object)?;\n        reference.cast_into().map_err(Into::into)\n    }","sourceCodeStart":314,"sourceCodeEnd":350,"githubUrl":"https://github.com/PyO3/pyo3/blob/ac9b6899d348be4d54614d060dea53a645a12e36/src/types/weakref/anyref.rs#L314-L350","documentation":"Guard panic in the PyWeakrefMethods::upgrade implementation for Bound<PyWeakref>. It fires only when PyWeakref_GetRef returns a negative status, i.e. the stored pointer is not actually a weakref.reference instance (or is invalid) — a broken type invariant that PyO3's safe wrappers assume can never happen, since the Bound<'py, PyWeakref> type guarantees a non-null genuine weakref.","triggerScenarios":"Thrown at src/types/weakref/anyref.rs:332 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Verify the value was obtained from PyWeakref::new / weakref.ref and not cast or from_object'd from an arbitrary Python object","If interoperating with C or FFI, check the pointer with PyWeakref_Check before wrapping it in Bound<PyWeakref>","Ensure the weakref object has not been created from a mismatched interpreter or already-invalidated runtime"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"ac9b6899d348be4d54614d060dea53a645a12e36","analyzedAt":"2026-09-05T09:20:35.319Z","contentChangedAt":"2026-09-05T09:20:35.319Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}