From 9dd04bf0364d0f36ba34ba15f4b25ce0f51848a8 Mon Sep 17 00:00:00 2001 From: vvanglro Date: Thu, 29 Jan 2026 16:36:35 +0800 Subject: [PATCH 1/4] chore(deps): bump pyo3 to 0.27 and simplify ffi interfaces Upgrade PyO3 from 0.20.1 to 0.27.2 and infer from 0.15.0 to 0.19.0. Replace legacy reference types with Bound APIs and owned handles. Accept &str directly in from_path to leverage automatic Python-to-Rust string conversion. Switch blocking calls from allow_threads to detach for explicit GIL release during file operations. Add lint configuration for PyPy-specific cfg flags. --- Cargo.lock | 286 ++++++++++++++++++++++++++++------------------------- Cargo.toml | 7 +- src/lib.rs | 19 ++-- 3 files changed, 166 insertions(+), 146 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 10e2984..48121d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,24 +1,34 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "autocfg" -version = "1.1.0" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" [[package]] -name = "bitflags" -version = "1.3.2" +name = "bumpalo" +version = "3.19.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510" [[package]] name = "byteorder" -version = "1.4.3" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "cc" +version = "1.2.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6354c81bbfd62d9cfa9cb3c773c2b7b2a3a482d569de977fd0e961f6e7c00583" +dependencies = [ + "find-msvc-tools", + "shlex", +] [[package]] name = "cfb" @@ -33,9 +43,15 @@ dependencies = [ [[package]] name = "cfg-if" -version = "1.0.0" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "find-msvc-tools" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8591b0bcc8a98a64310a2fae1bb3e9b8564dd10e381e6e28010fde8e8e8568db" [[package]] name = "fnv" @@ -43,105 +59,97 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "indoc" -version = "1.0.9" +version = "2.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa799dd5ed20a7e349f3b4639aa80d74549c81716d9ec4f994c9b5815598306" +checksum = "79cf5c93f93228cf8efb3ba362535fb11199ac548a09ce117c9b1adc3030d706" +dependencies = [ + "rustversion", +] [[package]] name = "infer" -version = "0.15.0" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb33622da908807a06f9513c19b3c1ad50fab3e4137d82a78107d502075aa199" +checksum = "a588916bfdfd92e71cacef98a63d9b1f0d74d6599980d11894290e7ddefffcf7" dependencies = [ "cfb", ] [[package]] -name = "libc" -version = "0.2.147" +name = "js-sys" +version = "0.3.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" +checksum = "8c942ebf8e95485ca0d52d97da7c5a2c387d0e7f0ba4c35e93bfcaee045955b3" +dependencies = [ + "once_cell", + "wasm-bindgen", +] [[package]] -name = "lock_api" -version = "0.4.10" +name = "libc" +version = "0.2.180" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1cc9717a20b1bb222f333e6a92fd32f7d8a18ddc5a3191a11af45dcbf4dcd16" -dependencies = [ - "autocfg", - "scopeguard", -] +checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" [[package]] name = "magic-rs" -version = "0.1.0" +version = "0.2.0" dependencies = [ "infer", "pyo3", - "pyo3-build-config", + "pyo3-build-config 0.20.1", ] [[package]] name = "memoffset" -version = "0.9.0" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" dependencies = [ "autocfg", ] [[package]] name = "once_cell" -version = "1.18.0" +version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] -name = "parking_lot" -version = "0.12.1" +name = "portable-atomic" +version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" -dependencies = [ - "lock_api", - "parking_lot_core", -] - -[[package]] -name = "parking_lot_core" -version = "0.9.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447" -dependencies = [ - "cfg-if", - "libc", - "redox_syscall", - "smallvec", - "windows-targets", -] +checksum = "f89776e4d69bb58bc6993e99ffa1d11f228b839984854c7daeb5d37f87cbe950" [[package]] name = "proc-macro2" -version = "1.0.66" +version = "1.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" dependencies = [ "unicode-ident", ] [[package]] name = "pyo3" -version = "0.19.2" +version = "0.27.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e681a6cfdc4adcc93b4d3cf993749a4552018ee0a9b65fc0ccfad74352c72a38" +checksum = "ab53c047fcd1a1d2a8820fe84f05d6be69e9526be40cb03b73f86b6b03e6d87d" dependencies = [ - "cfg-if", "indoc", "libc", "memoffset", - "parking_lot", - "pyo3-build-config", + "once_cell", + "portable-atomic", + "pyo3-build-config 0.27.2", "pyo3-ffi", "pyo3-macros", "unindent", @@ -149,29 +157,39 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.19.2" +version = "0.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "076c73d0bc438f7a4ef6fdd0c3bb4732149136abd952b110ac93e4edb13a6ba5" +checksum = "5503d0b3aee2c7a8dbb389cd87cd9649f675d4c7f60ca33699a3e3859d81a891" dependencies = [ "once_cell", - "target-lexicon", + "target-lexicon 0.12.16", +] + +[[package]] +name = "pyo3-build-config" +version = "0.27.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b455933107de8642b4487ed26d912c2d899dec6114884214a0b3bb3be9261ea6" +dependencies = [ + "python3-dll-a", + "target-lexicon 0.13.4", ] [[package]] name = "pyo3-ffi" -version = "0.19.2" +version = "0.27.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e53cee42e77ebe256066ba8aa77eff722b3bb91f3419177cf4cd0f304d3284d9" +checksum = "1c85c9cbfaddf651b1221594209aed57e9e5cff63c4d11d1feead529b872a089" dependencies = [ "libc", - "pyo3-build-config", + "pyo3-build-config 0.27.2", ] [[package]] name = "pyo3-macros" -version = "0.19.2" +version = "0.27.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfeb4c99597e136528c6dd7d5e3de5434d1ceaf487436a3f03b2d56b6fc9efd1" +checksum = "0a5b10c9bf9888125d917fb4d2ca2d25c8df94c7ab5a52e13313a07e050a3b02" dependencies = [ "proc-macro2", "pyo3-macros-backend", @@ -181,50 +199,52 @@ dependencies = [ [[package]] name = "pyo3-macros-backend" -version = "0.19.2" +version = "0.27.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "947dc12175c254889edc0c02e399476c2f652b4b9ebd123aa655c224de259536" +checksum = "03b51720d314836e53327f5871d4c0cfb4fb37cc2c4a11cc71907a86342c40f9" dependencies = [ + "heck", "proc-macro2", + "pyo3-build-config 0.27.2", "quote", "syn", ] [[package]] -name = "quote" -version = "1.0.32" +name = "python3-dll-a" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965" +checksum = "d381ef313ae70b4da5f95f8a4de773c6aa5cd28f73adec4b4a31df70b66780d8" dependencies = [ - "proc-macro2", + "cc", ] [[package]] -name = "redox_syscall" -version = "0.3.5" +name = "quote" +version = "1.0.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" +checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4" dependencies = [ - "bitflags", + "proc-macro2", ] [[package]] -name = "scopeguard" -version = "1.2.0" +name = "rustversion" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" [[package]] -name = "smallvec" -version = "1.11.0" +name = "shlex" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" [[package]] name = "syn" -version = "1.0.109" +version = "2.0.114" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a" dependencies = [ "proc-macro2", "quote", @@ -233,81 +253,79 @@ dependencies = [ [[package]] name = "target-lexicon" -version = "0.12.11" +version = "0.12.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d0e916b1148c8e263850e1ebcbd046f333e0683c724876bb0da63ea4373dc8a" +checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" [[package]] -name = "unicode-ident" -version = "1.0.11" +name = "target-lexicon" +version = "0.13.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" +checksum = "b1dd07eb858a2067e2f3c7155d54e929265c264e6f37efe3ee7a8d1b5a1dd0ba" [[package]] -name = "unindent" -version = "0.1.11" +name = "unicode-ident" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1766d682d402817b5ac4490b3c3002d91dfa0d22812f341609f97b08757359c" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" [[package]] -name = "uuid" -version = "1.4.1" +name = "unindent" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79daa5ed5740825c40b389c5e50312b9c86df53fccd33f281df655642b43869d" +checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3" [[package]] -name = "windows-targets" -version = "0.48.1" +name = "uuid" +version = "1.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05d4b17490f70499f20b9e791dcf6a299785ce8af4d709018206dc5b4953e95f" +checksum = "ee48d38b119b0cd71fe4141b30f5ba9c7c5d9f4e7a3a8b4a674e4b6ef789976f" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "js-sys", + "wasm-bindgen", ] [[package]] -name = "windows_aarch64_gnullvm" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" - -[[package]] -name = "windows_i686_gnu" -version = "0.48.0" +name = "wasm-bindgen" +version = "0.2.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" - -[[package]] -name = "windows_i686_msvc" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" +checksum = "64024a30ec1e37399cf85a7ffefebdb72205ca1c972291c51512360d90bd8566" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] [[package]] -name = "windows_x86_64_gnu" -version = "0.48.0" +name = "wasm-bindgen-macro" +version = "0.2.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" +checksum = "008b239d9c740232e71bd39e8ef6429d27097518b6b30bdf9086833bd5b6d608" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] [[package]] -name = "windows_x86_64_gnullvm" -version = "0.48.0" +name = "wasm-bindgen-macro-support" +version = "0.2.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" +checksum = "5256bae2d58f54820e6490f9839c49780dff84c65aeab9e772f15d5f0e913a55" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] [[package]] -name = "windows_x86_64_msvc" -version = "0.48.0" +name = "wasm-bindgen-shared" +version = "0.2.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" +checksum = "1f01b580c9ac74c8d8f0c0e4afb04eeef2acf145458e52c03845ee9cd23e3d12" +dependencies = [ + "unicode-ident", +] diff --git a/Cargo.toml b/Cargo.toml index fcc516a..05579d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,8 +36,8 @@ name = "_magic_rs" crate-type = ["cdylib"] [dependencies] -infer = "0.15.0" -pyo3 = { version = "=0.20.1", features = ["extension-module", "generate-import-lib"] } +infer = "0.19.0" +pyo3 = { version = "=0.27.2", features = ["extension-module", "generate-import-lib"] } [build-dependencies] pyo3-build-config = { version = "=0.20.1", features = ["resolve-config"] } @@ -50,3 +50,6 @@ lto = "fat" opt-level = 3 panic = "abort" strip = true + +[lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(PyPy)'] } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 2d68492..a30fc6b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ use infer::{get, get_from_path, MatcherType, Type}; use pyo3::create_exception; use pyo3::exceptions::PyValueError; use pyo3::prelude::*; -use pyo3::types::{PyBytes, PyString}; +use pyo3::types::PyBytes; use std::path::Path; create_exception!(_magic_rs, CantMatchTypeError, PyValueError, "Cant match type error"); @@ -57,9 +57,9 @@ impl PyMagic { #[pyfunction] #[pyo3(signature = (buf))] -fn from_bytes(_py: Python, buf: &PyBytes) -> PyResult { - let buf = buf.as_bytes(); - match _py.allow_threads(|| get(buf)) { +fn from_bytes(_py: Python, buf: Py) -> PyResult { + let buf = buf.as_bytes(_py); + match _py.detach(|| get(buf)) { None => Err(CantMatchTypeError::new_err("Cant match type error")), Some(resp) => Ok(PyMagic::new(resp)), } @@ -67,10 +67,9 @@ fn from_bytes(_py: Python, buf: &PyBytes) -> PyResult { #[pyfunction] #[pyo3(signature = (path))] -fn from_path(_py: Python, path: &PyString) -> PyResult> { - let path_str = path.to_str().unwrap(); - let path = Path::new(path_str); - match _py.allow_threads(|| get_from_path(path)) { +fn from_path(_py: Python, path: &str) -> PyResult> { + let path = Path::new(path); + match _py.detach(|| get_from_path(path)) { Ok(None) => Err(CantMatchTypeError::new_err("Cant match type or path error")), Ok(Some(resp)) => Ok(Option::from(PyMagic::new(resp))), _ => Err(CantMatchTypeError::new_err("Cant match type or path error")), @@ -78,14 +77,14 @@ fn from_path(_py: Python, path: &PyString) -> PyResult> { } #[pymodule] -fn _magic_rs(_py: Python, module: &PyModule) -> PyResult<()> { +fn _magic_rs(_py: Python, module: &Bound<'_, PyModule>) -> PyResult<()> { module.add_class::()?; module.add_function(wrap_pyfunction!(from_bytes, module)?)?; module.add_function(wrap_pyfunction!(from_path, module)?)?; module.add("CantMatchTypeError", _py.get_type::())?; #[cfg(not(PyPy))] - pyo3::prepare_freethreaded_python(); + pyo3::Python::initialize(); Ok(()) } From adca29f9ba80620dd1ad32f95f4b410ffa5e3fcd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 29 Jan 2026 08:38:51 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 05579d3..3034cd6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,4 +52,4 @@ panic = "abort" strip = true [lints.rust] -unexpected_cfgs = { level = "warn", check-cfg = ['cfg(PyPy)'] } \ No newline at end of file +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(PyPy)'] } From 0954ad8f7ec323f38eabad10116cf99a3903a927 Mon Sep 17 00:00:00 2001 From: vvanglro Date: Thu, 29 Jan 2026 16:53:05 +0800 Subject: [PATCH 3/4] fix: resolve lifetime issues in detached closures Update pyo3-build-config to 0.27.2 and use owned types within detached closures to ensure data persists across thread boundaries. This fixes lifetime errors where references were being passed into closures that might outlive their scope. --- Cargo.lock | 26 +++++--------------------- Cargo.toml | 2 +- src/lib.rs | 14 +++++++------- 3 files changed, 13 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 48121d9..a54eda0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -105,7 +105,7 @@ version = "0.2.0" dependencies = [ "infer", "pyo3", - "pyo3-build-config 0.20.1", + "pyo3-build-config", ] [[package]] @@ -149,22 +149,12 @@ dependencies = [ "memoffset", "once_cell", "portable-atomic", - "pyo3-build-config 0.27.2", + "pyo3-build-config", "pyo3-ffi", "pyo3-macros", "unindent", ] -[[package]] -name = "pyo3-build-config" -version = "0.20.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5503d0b3aee2c7a8dbb389cd87cd9649f675d4c7f60ca33699a3e3859d81a891" -dependencies = [ - "once_cell", - "target-lexicon 0.12.16", -] - [[package]] name = "pyo3-build-config" version = "0.27.2" @@ -172,7 +162,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b455933107de8642b4487ed26d912c2d899dec6114884214a0b3bb3be9261ea6" dependencies = [ "python3-dll-a", - "target-lexicon 0.13.4", + "target-lexicon", ] [[package]] @@ -182,7 +172,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c85c9cbfaddf651b1221594209aed57e9e5cff63c4d11d1feead529b872a089" dependencies = [ "libc", - "pyo3-build-config 0.27.2", + "pyo3-build-config", ] [[package]] @@ -205,7 +195,7 @@ checksum = "03b51720d314836e53327f5871d4c0cfb4fb37cc2c4a11cc71907a86342c40f9" dependencies = [ "heck", "proc-macro2", - "pyo3-build-config 0.27.2", + "pyo3-build-config", "quote", "syn", ] @@ -251,12 +241,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "target-lexicon" -version = "0.12.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" - [[package]] name = "target-lexicon" version = "0.13.4" diff --git a/Cargo.toml b/Cargo.toml index 05579d3..9cb4a43 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,7 @@ infer = "0.19.0" pyo3 = { version = "=0.27.2", features = ["extension-module", "generate-import-lib"] } [build-dependencies] -pyo3-build-config = { version = "=0.20.1", features = ["resolve-config"] } +pyo3-build-config = { version = "=0.27.2", features = ["resolve-config"] } [profile.release] codegen-units = 1 diff --git a/src/lib.rs b/src/lib.rs index a30fc6b..ecb1d92 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ use pyo3::create_exception; use pyo3::exceptions::PyValueError; use pyo3::prelude::*; use pyo3::types::PyBytes; -use std::path::Path; + create_exception!(_magic_rs, CantMatchTypeError, PyValueError, "Cant match type error"); @@ -57,9 +57,9 @@ impl PyMagic { #[pyfunction] #[pyo3(signature = (buf))] -fn from_bytes(_py: Python, buf: Py) -> PyResult { - let buf = buf.as_bytes(_py); - match _py.detach(|| get(buf)) { +fn from_bytes(py: Python, buf: Py) -> PyResult { + let bytes = buf.as_bytes(py).to_vec(); + match py.detach(move || get(&bytes)) { None => Err(CantMatchTypeError::new_err("Cant match type error")), Some(resp) => Ok(PyMagic::new(resp)), } @@ -68,11 +68,11 @@ fn from_bytes(_py: Python, buf: Py) -> PyResult { #[pyfunction] #[pyo3(signature = (path))] fn from_path(_py: Python, path: &str) -> PyResult> { - let path = Path::new(path); - match _py.detach(|| get_from_path(path)) { + let path_buf = std::path::PathBuf::from(path); + match _py.detach(move || get_from_path(path_buf)) { Ok(None) => Err(CantMatchTypeError::new_err("Cant match type or path error")), Ok(Some(resp)) => Ok(Option::from(PyMagic::new(resp))), - _ => Err(CantMatchTypeError::new_err("Cant match type or path error")), + Err(_) => Err(CantMatchTypeError::new_err("Cant match type or path error")), } } From e28c5a55d80a076fb6f4e6c3edf1a0a7d1bad01c Mon Sep 17 00:00:00 2001 From: vvanglro Date: Thu, 29 Jan 2026 16:55:20 +0800 Subject: [PATCH 4/4] style --- src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index ecb1d92..5ab56a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,6 @@ use pyo3::exceptions::PyValueError; use pyo3::prelude::*; use pyo3::types::PyBytes; - create_exception!(_magic_rs, CantMatchTypeError, PyValueError, "Cant match type error"); #[pyclass]