-
Notifications
You must be signed in to change notification settings - Fork 806
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
Add uuid python convertions #4836
Closed
Closed
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
bdfa56a
feat: add uuid support
JeanArhancet da8d3be
derive(FromPyObject): support raw identifiers (#4814)
Tpt a05980a
Fix copy/paste typo in PyListMethods docs (#4818)
LilyFoote eab55e5
fix nightly ci (#4816)
Icxolu 9f8d8fd
switch trait sealing in `impl_` module (#4817)
Icxolu a6ad65c
Raise target-lexicon version and update for API changes (#4822)
alex 2bf84e1
Fix struct layouts on GraalPy (#4802)
msimacek 0b39228
reintroduce `vectorcall` optimization with new `PyCallArgs` trait (#4…
Icxolu 83ad2bc
Fix generating import lib for python3.13t when abi3 feature is enable…
messense 6e2dd9c
fix: cross-compilation compatibility checks for Windows (#4800)
kahojyun ede56ce
fix error with complex enums with many fields (#4832)
Icxolu 428c64d
docs: Expand docs on when and why allow_threads is necessary (#4767)
ngoldbaum d487913
ci: add more tests for cross-compilation (#4773)
davidhewitt 69f9aa6
Add an API to set rpath when using macOS system Python (#4833)
messense 9282c2e
Fix PyDict issues on free-threaded build (#4788)
ngoldbaum da144b8
Allow useless conversion (#4838)
mejrs 1e56dab
Implement locked iteration for PyList (#4789)
ngoldbaum 813e25c
fix: clippy error
JeanArhancet 196967e
fix: apply review feedback and correct lint issues
JeanArhancet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ pub mod rust_decimal; | |
pub mod serde; | ||
pub mod smallvec; | ||
mod std; | ||
pub mod uuid; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,301 @@ | ||
#![cfg(feature = "uuid")] | ||
|
||
//! Conversions to and from [uuid](https://docs.rs/uuid/latest/uuid/)'s [`Uuid`] type. | ||
//! | ||
//! This is useful for converting Python's uuid.UUID into and from a native Rust type. | ||
//! | ||
//! # Setup | ||
//! | ||
//! To use this feature, add to your **`Cargo.toml`**: | ||
//! | ||
//! ```toml | ||
//! [dependencies] | ||
#![doc = concat!("pyo3 = { version = \"", env!("CARGO_PKG_VERSION"), "\", features = [\"uuid\"] }")] | ||
//! uuid = "1.11.0" | ||
//! ``` | ||
//! | ||
//! Note that you must use a compatible version of uuid and PyO3. | ||
//! The required uuid version may vary based on the version of PyO3. | ||
//! | ||
//! # Example | ||
//! | ||
//! Rust code to create a function that parses a UUID string and returns it as a `Uuid`: | ||
//! | ||
//! ```rust | ||
//! use pyo3::prelude::*; | ||
//! use pyo3::exceptions::PyValueError; | ||
//! use uuid::Uuid; | ||
//! | ||
//! #[pyfunction] | ||
//! fn parse_uuid(s: &str) -> PyResult<Uuid> { | ||
//! Uuid::parse_str(s).map_err(|e| PyValueError::new_err(e.to_string())) | ||
//! } | ||
//! | ||
//! #[pymodule] | ||
//! fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> { | ||
//! m.add_function(wrap_pyfunction!(parse_uuid, m)?)?; | ||
//! Ok(()) | ||
//! } | ||
//! ``` | ||
//! | ||
//! Python code that validates the functionality | ||
//! | ||
//! | ||
//! ```python | ||
//! from my_module import parse_uuid | ||
//! import uuid | ||
//! | ||
//! py_uuid = uuid.uuid4() | ||
//! rust_uuid = parse_uuid(str(py_uuid)) | ||
//! | ||
//! assert py_uuid == rust_uuid | ||
//! ``` | ||
use uuid::Uuid; | ||
|
||
use crate::conversion::IntoPyObject; | ||
use crate::exceptions::{PyTypeError, PyValueError}; | ||
use crate::instance::Bound; | ||
use crate::sync::GILOnceCell; | ||
use crate::types::any::PyAnyMethods; | ||
use crate::types::bytearray::PyByteArrayMethods; | ||
use crate::types::{ | ||
IntoPyDict, PyByteArray, PyBytes, PyBytesMethods, PyInt, PyStringMethods, PyType, | ||
}; | ||
use crate::{FromPyObject, Py, PyAny, PyErr, PyObject, PyResult, Python}; | ||
#[allow(deprecated)] | ||
use crate::{IntoPy, ToPyObject}; | ||
|
||
static UUID_CLS: GILOnceCell<Py<PyType>> = GILOnceCell::new(); | ||
|
||
fn get_uuid_cls(py: Python<'_>) -> PyResult<&Bound<'_, PyType>> { | ||
UUID_CLS.import(py, "uuid", "UUID") | ||
} | ||
|
||
impl FromPyObject<'_> for Uuid { | ||
fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<Self> { | ||
let py = obj.py(); | ||
|
||
if let Ok(uuid_cls) = get_uuid_cls(py) { | ||
if obj.is_exact_instance(&uuid_cls) { | ||
let uuid_int: u128 = obj.getattr("int")?.extract()?; | ||
return Ok(Uuid::from_u128(uuid_int.to_le())); | ||
} | ||
} | ||
|
||
if obj.is_instance_of::<PyBytes>() || obj.is_instance_of::<PyByteArray>() { | ||
let bytes = if let Ok(py_bytes) = obj.downcast::<PyBytes>() { | ||
py_bytes.as_bytes() | ||
} else if let Ok(py_bytearray) = obj.downcast::<PyByteArray>() { | ||
&py_bytearray.to_vec() | ||
} else { | ||
return Err(PyTypeError::new_err( | ||
"Expected bytes or bytearray for UUID extraction.", | ||
)); | ||
}; | ||
|
||
return Uuid::from_slice(bytes) | ||
.map_err(|_| PyValueError::new_err("The given bytes value is not a valid UUID.")); | ||
} | ||
|
||
if obj.is_instance_of::<PyInt>() { | ||
let uuid_int: u128 = obj.extract().map_err(|_| { | ||
PyTypeError::new_err( | ||
"Expected integer for UUID extraction but got an incompatible type.", | ||
) | ||
})?; | ||
return Ok(Uuid::from_u128(uuid_int)); | ||
} | ||
|
||
let py_str = &obj.str()?; | ||
let rs_str = &py_str.to_cow()?; | ||
Uuid::parse_str(&rs_str) | ||
.map_err(|e| PyValueError::new_err(format!("Invalid UUID string: {e}"))) | ||
} | ||
} | ||
|
||
#[allow(deprecated)] | ||
impl ToPyObject for Uuid { | ||
#[inline] | ||
fn to_object(&self, py: Python<'_>) -> PyObject { | ||
self.into_pyobject(py).unwrap().into_any().unbind() | ||
} | ||
} | ||
|
||
#[allow(deprecated)] | ||
impl IntoPy<PyObject> for Uuid { | ||
#[inline] | ||
fn into_py(self, py: Python<'_>) -> PyObject { | ||
self.into_pyobject(py).unwrap().into_any().unbind() | ||
} | ||
} | ||
|
||
impl<'py> IntoPyObject<'py> for Uuid { | ||
type Target = PyAny; | ||
type Output = Bound<'py, Self::Target>; | ||
type Error = PyErr; | ||
|
||
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> { | ||
let uuid_cls = get_uuid_cls(py)?; | ||
let kwargs = [("int", self.as_u128())].into_py_dict(py)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are much more efficient calling as positional arguments, it might be worth benchmarking that here. (I guess the tuple would be something like |
||
|
||
Ok(uuid_cls | ||
.call((), Some(&kwargs)) | ||
.expect("failed to call uuid.UUID") | ||
.into_pyobject(py)?) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::types::dict::PyDictMethods; | ||
use crate::types::{PyDict, PyString}; | ||
use std::ffi::CString; | ||
use uuid::Uuid; | ||
|
||
macro_rules! convert_constants { | ||
($name:ident, $rs:expr, $py:literal) => { | ||
#[test] | ||
fn $name() -> PyResult<()> { | ||
Python::with_gil(|py| { | ||
let rs_orig = $rs; | ||
let rs_uuid = rs_orig.into_pyobject(py).unwrap(); | ||
let locals = PyDict::new(py); | ||
locals.set_item("rs_uuid", &rs_uuid).unwrap(); | ||
|
||
py.run( | ||
&CString::new(format!( | ||
"import uuid\npy_uuid = uuid.UUID('{}')\nassert py_uuid == rs_uuid", | ||
$py | ||
)) | ||
.unwrap(), | ||
None, | ||
Some(&locals), | ||
) | ||
.unwrap(); | ||
|
||
let py_uuid = locals.get_item("py_uuid").unwrap().unwrap(); | ||
let py_result: Uuid = py_uuid.extract().unwrap(); | ||
assert_eq!(rs_orig, py_result); | ||
|
||
Ok(()) | ||
}) | ||
} | ||
}; | ||
} | ||
|
||
convert_constants!( | ||
convert_nil, | ||
Uuid::nil(), | ||
"00000000-0000-0000-0000-000000000000" | ||
); | ||
convert_constants!( | ||
convert_max, | ||
Uuid::max(), | ||
"ffffffff-ffff-ffff-ffff-ffffffffffff" | ||
); | ||
|
||
convert_constants!( | ||
convert_uuid_v4, | ||
Uuid::parse_str("a4f6d1b9-1898-418f-b11d-ecc6fe1e1f00").unwrap(), | ||
"a4f6d1b9-1898-418f-b11d-ecc6fe1e1f00" | ||
); | ||
|
||
convert_constants!( | ||
convert_uuid_v3, | ||
Uuid::parse_str("6fa459ea-ee8a-3ca4-894e-db77e160355e").unwrap(), | ||
"6fa459ea-ee8a-3ca4-894e-db77e160355e" | ||
); | ||
|
||
convert_constants!( | ||
convert_uuid_v1, | ||
Uuid::parse_str("a6cc5730-2261-11ee-9c43-2eb5a363657c").unwrap(), | ||
"a6cc5730-2261-11ee-9c43-2eb5a363657c" | ||
); | ||
|
||
#[test] | ||
fn test_uuid_str() { | ||
Python::with_gil(|py| { | ||
let s = PyString::new(py, "a6cc5730-2261-11ee-9c43-2eb5a363657c"); | ||
let uuid: Uuid = s.extract().unwrap(); | ||
assert_eq!( | ||
uuid, | ||
Uuid::parse_str("a6cc5730-2261-11ee-9c43-2eb5a363657c").unwrap() | ||
); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_uuid_bytes() { | ||
Python::with_gil(|py| { | ||
let s = PyBytes::new( | ||
py, | ||
&[ | ||
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, | ||
0xd6, 0xd7, 0xd8, | ||
], | ||
); | ||
let uuid: Uuid = s.extract().unwrap(); | ||
assert_eq!( | ||
uuid, | ||
Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8").unwrap() | ||
); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_invalid_uuid_bytes() { | ||
Python::with_gil(|py| { | ||
let s = PyBytes::new( | ||
py, | ||
&[ | ||
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, | ||
0xd6, 0xd7, | ||
], | ||
); | ||
let uuid: Result<Uuid, PyErr> = s.extract(); | ||
assert!(uuid.is_err()) | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_uuid_int() { | ||
Python::with_gil(|py| { | ||
let v = 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8u128; | ||
let obj: Bound<'_, PyInt> = v.into_pyobject(py).unwrap(); | ||
let uuid: Uuid = obj.extract().unwrap(); | ||
assert_eq!( | ||
uuid, | ||
Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8").unwrap() | ||
); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_invalid_uuid_int() { | ||
Python::with_gil(|py| { | ||
let v = -42; | ||
let obj: Bound<'_, PyInt> = v.into_pyobject(py).unwrap(); | ||
let uuid: Result<Uuid, PyErr> = obj.extract(); | ||
assert!(uuid.is_err()) | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_uuid_incorrect_length() { | ||
Python::with_gil(|py| { | ||
let s = PyString::new(py, "123e4567-e89b-12d3-a456-42661417400"); | ||
let uuid: Result<Uuid, PyErr> = s.extract(); | ||
assert!(uuid.is_err()) | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_invalid_uuid_string() { | ||
Python::with_gil(|py| { | ||
let s = PyString::new(py, "invalid-uuid-str"); | ||
let uuid: Result<Uuid, PyErr> = s.extract(); | ||
assert!(uuid.is_err()) | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest we might want to stay away from
Bytearray::to_vec
(see #4736). Maybe for the moment just avoid byte arrays? Or maybe we want to go through the buffer protocol? That's not really much better 🤔