From bc7f1e3e5901c2dd47e055ad009cce914168a99e Mon Sep 17 00:00:00 2001 From: jessekrubin Date: Tue, 17 Dec 2024 16:10:03 -0800 Subject: [PATCH] py_new but I forgot I used `__new__` some places which I kinda like.... --- crates/ryo3-fnv/src/lib.rs | 4 ++-- crates/ryo3-sqlformat/src/lib.rs | 8 ++++---- crates/ryo3-url/src/lib.rs | 2 +- crates/ryo3-xxhash/src/xxhashers.rs | 12 ++++++------ crates/ryo3/src/fs/fspath.rs | 6 ++++-- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/crates/ryo3-fnv/src/lib.rs b/crates/ryo3-fnv/src/lib.rs index 2e9f607..6ca40c1 100644 --- a/crates/ryo3-fnv/src/lib.rs +++ b/crates/ryo3-fnv/src/lib.rs @@ -14,7 +14,7 @@ pub struct PyFnvHasher { impl PyFnvHasher { #[new] #[pyo3(signature = (s = None))] - fn new(s: Option<&[u8]>) -> Self { + fn py_new(s: Option<&[u8]>) -> Self { match s { Some(s) => { let mut hasher = fnv_rs::FnvHasher::default(); @@ -63,7 +63,7 @@ impl PyFnvHasher { #[pyfunction] pub fn fnv1a(s: &[u8]) -> PyResult { - Ok(PyFnvHasher::new(Some(s))) + Ok(PyFnvHasher::py_new(Some(s))) } pub fn pymod_add(m: &Bound<'_, PyModule>) -> PyResult<()> { diff --git a/crates/ryo3-sqlformat/src/lib.rs b/crates/ryo3-sqlformat/src/lib.rs index a82231c..b3bb438 100644 --- a/crates/ryo3-sqlformat/src/lib.rs +++ b/crates/ryo3-sqlformat/src/lib.rs @@ -29,7 +29,7 @@ pub struct PySqlfmtQueryParams { #[pymethods] impl PySqlfmtQueryParams { #[new] - fn new(params: PyQueryParamsLike) -> PyResult { + fn py_new(params: PyQueryParamsLike) -> PyResult { sqlfmt_params(Some(params)) } @@ -134,11 +134,11 @@ pub fn sqlfmt_params(params: Option) -> PyResult, @@ -164,7 +164,7 @@ pub fn sqlfmt( if let PyQueryParamsLike::PyQueryParams(p) = p { Ok(sqlformat::format(sql, &p.params, &options)) } else { - let py_params = PySqlfmtQueryParams::new(p)?; + let py_params = PySqlfmtQueryParams::py_new(p)?; Ok(sqlformat::format(sql, &py_params.params, &options)) } } else { diff --git a/crates/ryo3-url/src/lib.rs b/crates/ryo3-url/src/lib.rs index 5ae042c..d78e869 100644 --- a/crates/ryo3-url/src/lib.rs +++ b/crates/ryo3-url/src/lib.rs @@ -16,7 +16,7 @@ pub struct PyUrl(pub(crate) url::Url); impl PyUrl { #[new] #[pyo3(signature = (url, *, params = None))] - fn new(url: &str, params: Option<&Bound<'_, PyDict>>) -> PyResult { + fn py_new(url: &str, params: Option<&Bound<'_, PyDict>>) -> PyResult { if let Some(params) = params { let params = params .into_iter() diff --git a/crates/ryo3-xxhash/src/xxhashers.rs b/crates/ryo3-xxhash/src/xxhashers.rs index dd6c88c..3f21d88 100644 --- a/crates/ryo3-xxhash/src/xxhashers.rs +++ b/crates/ryo3-xxhash/src/xxhashers.rs @@ -14,7 +14,7 @@ pub struct PyXxh32 { impl PyXxh32 { #[new] #[pyo3(signature = (b = None, seed = None))] - fn new(b: Option<&[u8]>, seed: Option) -> Self { + fn py_new(b: Option<&[u8]>, seed: Option) -> Self { match b { Some(s) => { let seed = seed.unwrap_or(0); @@ -83,7 +83,7 @@ impl PyXxh32 { #[pyfunction] #[pyo3(signature = (s = None, seed = 0))] pub fn xxh32(s: Option<&[u8]>, seed: Option) -> PyResult { - Ok(PyXxh32::new(s, seed)) + Ok(PyXxh32::py_new(s, seed)) } /// Python-Xxh64 hasher @@ -98,7 +98,7 @@ impl PyXxh64 { /// Create a new Xxh64 hasher #[new] #[pyo3(signature = (b = None, seed = 0))] - fn new(b: Option<&[u8]>, seed: Option) -> Self { + fn py_new(b: Option<&[u8]>, seed: Option) -> Self { match b { Some(s) => { let mut hasher = Xxh64::new(seed.unwrap_or(0)); @@ -168,7 +168,7 @@ impl PyXxh64 { #[pyfunction] #[pyo3(signature = (s = None, seed = 0))] pub fn xxh64(s: Option<&[u8]>, seed: Option) -> PyResult { - Ok(PyXxh64::new(s, seed)) + Ok(PyXxh64::py_new(s, seed)) } #[pyclass(name = "Xxh3", module = "ryo3")] @@ -181,7 +181,7 @@ pub struct PyXxh3 { impl PyXxh3 { #[new] #[pyo3(signature = (b = None, seed = 0, secret = None))] - fn new(b: Option<&[u8]>, seed: Option, secret: Option<[u8; 192]>) -> Self { + fn py_new(b: Option<&[u8]>, seed: Option, secret: Option<[u8; 192]>) -> Self { let seed = seed.unwrap_or(0); let h = match secret { Some(s) => Xxh3Builder::new().with_seed(seed).with_secret(s).build(), @@ -262,7 +262,7 @@ impl PyXxh3 { #[pyfunction] #[pyo3(signature = (s = None, seed = 0, secret = None))] pub fn xxh3(s: Option<&[u8]>, seed: Option, secret: Option<[u8; 192]>) -> PyResult { - Ok(PyXxh3::new(s, seed, secret)) + Ok(PyXxh3::py_new(s, seed, secret)) } pub fn pymod_add(m: &Bound<'_, PyModule>) -> PyResult<()> { diff --git a/crates/ryo3/src/fs/fspath.rs b/crates/ryo3/src/fs/fspath.rs index cfbe010..cd7f45f 100644 --- a/crates/ryo3/src/fs/fspath.rs +++ b/crates/ryo3/src/fs/fspath.rs @@ -502,8 +502,10 @@ impl PyFsPath { self.pth.ends_with(path.as_ref()) } - fn exists(&self) -> bool { - self.pth.exists() + fn exists(&self) -> PyResult { + self.pth + .try_exists() + .map_err(|e| PyFileNotFoundError::new_err(format!("(try_)exists: {e}"))) } fn extension(&self) -> Option {