Skip to content

Commit d3bc337

Browse files
feat : upgraded pyo3 and removed deprecated code incompatible with pyo3 newest version
1 parent 9855f93 commit d3bc337

File tree

6 files changed

+74
-47
lines changed

6 files changed

+74
-47
lines changed

.github/workflows/release-python.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,10 @@ jobs:
164164
twine check artifacts/**/*.tar.gz
165165
twine upload artifacts/**/*.tar.gz
166166
twine check artifacts/**/*.whl
167-
twine upload artifacts/**/*.whl
167+
for file in artifacts/**/*.whl; do
168+
if [[ -f "$file" ]]; then
169+
echo "Uploading $file..."
170+
twine upload "$file" || echo "Failed to upload $file"
171+
fi
172+
done
173+
# twine upload artifacts/**/*.whl

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "polodb_python" # Must match the Python package name
3-
version = "0.1.16"
3+
version = "0.1.17"
44
edition = "2021"
55
description = "Python bindings for PoloDB"
66
license = "Apache License"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "polodb-python"
3-
version = "0.1.16"
3+
version = "0.1.17"
44
description = "Add your description here"
55
readme = "README.md"
66
requires-python = ">=3.9"

src/helper_type_translator.rs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ pub fn convert_py_list_to_vec_document<'a>(py_list_obj: &'a Py<PyAny>) -> Vec<Do
1010
if let Ok(py_list) = py_list_obj.downcast_bound::<PyList>(py) {
1111
// If downcast is successful, return an iterator over the list's items
1212
let iter = py_list.iter().map(|item| {
13-
let py_obj: Py<PyAny> = item.to_object(item.py());
13+
// let py_obj: Py<PyAny> = item.to_object(item.py());
14+
let py_obj2 = item.into_pyobject(py).unwrap();
1415
// Convert each item (expected to be a dictionary) into a BSON document
1516

16-
convert_py_obj_to_document(&py_obj).unwrap()
17+
convert_py_obj_to_document(py_obj2.as_unbound()).unwrap()
1718
});
1819
Vec::from_iter(iter)
1920
} else {
@@ -32,7 +33,8 @@ pub fn convert_py_obj_to_document(py_obj: &Py<PyAny>) -> PyResult<Document> {
3233
for (key, value) in dict.iter() {
3334
// Use `iter()` on the `PyDict`
3435
let key: String = key.extract()?; // Extract the key as a string
35-
let bson_value = convert_py_obj_to_bson(value.to_object(py).as_any())?; // Convert value to BSON
36+
let bson_value =
37+
convert_py_obj_to_bson(value.into_pyobject(py).unwrap().as_unbound())?; // Convert value to BSON
3638
doc.insert(key, bson_value);
3739
}
3840
Ok(doc)
@@ -70,7 +72,8 @@ pub fn convert_py_obj_to_bson(py_obj: &Py<PyAny>) -> PyResult<Bson> {
7072
for (key, value) in dict.iter() {
7173
let key_str: String = key.extract::<String>()?;
7274

73-
let bson_value = convert_py_obj_to_bson(value.to_object(py).as_any())?;
75+
let bson_value =
76+
convert_py_obj_to_bson(value.into_pyobject(py).unwrap().as_unbound())?;
7477
bson_doc.insert(key_str, bson_value);
7578
}
7679
Ok(Bson::Document(bson_doc))
@@ -79,7 +82,8 @@ pub fn convert_py_obj_to_bson(py_obj: &Py<PyAny>) -> PyResult<Bson> {
7982
else if let Ok(list) = py_obj.downcast_bound::<PyList>(py) {
8083
let mut bson_array = Vec::new();
8184
for item in list.iter() {
82-
let bson_item = convert_py_obj_to_bson(item.to_object(py).as_any())?;
85+
let bson_item =
86+
convert_py_obj_to_bson(item.into_pyobject(py).unwrap().as_unbound())?;
8387
bson_array.push(bson_item);
8488
}
8589
Ok(Bson::Array(bson_array))
@@ -133,7 +137,15 @@ pub fn bson_to_py_obj(py: Python, bson: &Bson) -> PyObject {
133137
Bson::Int64(i) => i.into_pyobject(py).unwrap().into(),
134138
Bson::Double(f) => PyFloat::new(py, *f).into_pyobject(py).unwrap().into(),
135139
Bson::String(s) => PyString::new(py, s).into_pyobject(py).unwrap().into(),
136-
Bson::Boolean(b) => PyBool::new(py, *b).into_py(py),
140+
// Bson::Boolean(b) => {
141+
// // PyBool::new(py, *b) -> &PyBool (borrowed), convert it with Py::from
142+
// // Create a &PyBool
143+
// let py_bool_ref = PyBool::new(py, *b).as_unbound().clone_ref(py);
144+
// // Coerce &PyBool to &PyAny by assignment
145+
// let py_any: &PyAny = py_bool_ref ;
146+
// py_bool_ref
147+
// }
148+
Bson::Boolean(b) => PyBool::new(py, *b).as_unbound().clone_ref(py).into_any(),
137149
Bson::Array(arr) => {
138150
// Create an empty PyList without specifying a slice
139151
let py_list = PyList::empty(py); // Use empty method instead of new
@@ -150,25 +162,34 @@ pub fn bson_to_py_obj(py: Python, bson: &Bson) -> PyObject {
150162
py_dict.into_pyobject(py).unwrap().into()
151163
}
152164
Bson::RegularExpression(regex) => {
153-
let re_module = py.import_bound("re").unwrap();
165+
let re_module = py.import("re").unwrap();
154166
re_module
155167
.call_method1("compile", (regex.pattern.as_str(),))
156168
.unwrap()
157-
.to_object(py)
169+
.into_pyobject(py)
170+
.unwrap()
171+
.into()
158172
}
159173
// Handle JavaScript code
160174
Bson::JavaScriptCode(code) => PyString::new(py, code).into_pyobject(py).unwrap().into(),
161175
Bson::Timestamp(ts) => (ts.time, ts.increment).into_pyobject(py).unwrap().into(),
162-
Bson::Binary(bin) => PyBytes::new(py, &bin.bytes).into_pyobject(py).unwrap().into(),
163-
Bson::ObjectId(oid) => PyString::new(py, &oid.to_hex()).into_pyobject(py).unwrap().into(),
176+
Bson::Binary(bin) => PyBytes::new(py, &bin.bytes)
177+
.into_pyobject(py)
178+
.unwrap()
179+
.into(),
180+
Bson::ObjectId(oid) => PyString::new(py, &oid.to_hex())
181+
.into_pyobject(py)
182+
.unwrap()
183+
.into(),
164184
Bson::DateTime(dt) => {
165185
let timestamp = dt.timestamp_millis() / 1000;
166-
let datetime = py
167-
.import_bound("datetime")
186+
let datetime = py.import("datetime").unwrap().getattr("datetime").unwrap();
187+
datetime
188+
.call1((timestamp,))
189+
.unwrap()
190+
.into_pyobject(py)
168191
.unwrap()
169-
.getattr("datetime")
170-
.unwrap();
171-
datetime.call1((timestamp,)).unwrap().to_object(py)
192+
.into()
172193
}
173194
Bson::Symbol(s) => PyString::new(py, s).into_pyobject(py).unwrap().into(),
174195

src/py_database.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use polodb_core::options::UpdateOptions;
77
use polodb_core::{Collection, CollectionT, Database};
88
use pyo3::exceptions::PyOSError;
99
use pyo3::exceptions::PyRuntimeError; // Import PyRuntimeError for error handling
10-
use pyo3::prelude::*;
10+
use pyo3::{prelude::*, IntoPyObjectExt};
1111
use pyo3::types::{PyDict, PyList};
1212
use std::borrow::Borrow;
1313
use std::path::Path;
@@ -31,7 +31,7 @@ impl PyCollection {
3131

3232
// Example: Create a Python object or interact with the Python runtime.
3333
let bson_vec_docs: Vec<Document> =
34-
convert_py_list_to_vec_document(doc.to_object(py).as_any());
34+
convert_py_list_to_vec_document(&doc.into_py_any(py).unwrap());
3535
// let bson_doc = convert_py_to_bson(doc);
3636
match self.inner.insert_many(bson_vec_docs) {
3737
Ok(result) => {
@@ -42,7 +42,7 @@ impl PyCollection {
4242
dict.set_item(key, bson_to_py_obj(py, value)).unwrap();
4343
}
4444

45-
Ok(dict.to_object(py))
45+
Ok(dict.into_py_any(py).unwrap())
4646
}
4747
Err(e) => {
4848
// Raise a Python exception on error
@@ -55,7 +55,7 @@ impl PyCollection {
5555
pub fn insert_one(&self, doc: Py<PyDict>) -> PyResult<PyObject> {
5656
// Acquire the Python GIL (Global Interpreter Lock)
5757
Python::with_gil(|py| {
58-
let bson_doc: Document = match convert_py_obj_to_document(doc.to_object(py).as_any()) {
58+
let bson_doc: Document = match convert_py_obj_to_document(&doc.into_py_any(py).unwrap()) {
5959
Ok(d) => d,
6060
Err(e) => return Err(PyRuntimeError::new_err(format!("Insert many error: {}", e))),
6161
};
@@ -67,7 +67,7 @@ impl PyCollection {
6767
let dict = PyDict::new(py);
6868
let dict_ref = dict.borrow();
6969
dict_ref.set_item("inserted_id", py_inserted_id)?;
70-
Ok(dict.to_object(py))
70+
Ok(dict.into_py_any(py).unwrap())
7171

7272
// Ok(Py::new(py, result)?.to_object(py))
7373
}
@@ -86,15 +86,15 @@ impl PyCollection {
8686
update: Py<PyDict>,
8787
) -> PyResult<Option<PyObject>> {
8888
// Convert PyDict to BSON Document
89-
let filter_doc = convert_py_obj_to_document(filter.to_object(py).as_any())?;
90-
let update_doc = convert_py_obj_to_document(update.to_object(py).as_any())?;
89+
let filter_doc = convert_py_obj_to_document(&filter.into_py_any(py).unwrap())?;
90+
let update_doc = convert_py_obj_to_document(&update.into_py_any(py).unwrap())?;
9191

9292
// Call the Rust method `find_one`
9393
match self.inner.update_one(filter_doc, update_doc) {
9494
Ok(update_result) => {
9595
// Convert BSON Document to Python Dict
9696
let py_result = update_result_to_pydict(py, update_result).unwrap();
97-
Ok(Some(py_result.to_object(py)))
97+
Ok(Some(py_result.into_py_any(py).unwrap()))
9898
}
9999
Err(err) => Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
100100
"Update one error: {}",
@@ -110,15 +110,15 @@ impl PyCollection {
110110
update: Py<PyDict>,
111111
) -> PyResult<Option<PyObject>> {
112112
// Convert PyDict to BSON Document
113-
let filter_doc = convert_py_obj_to_document(filter.to_object(py).as_any())?;
114-
let update_doc = convert_py_obj_to_document(update.to_object(py).as_any())?;
113+
let filter_doc = convert_py_obj_to_document(&filter.into_py_any(py).unwrap())?;
114+
let update_doc = convert_py_obj_to_document(&update.into_py_any(py).unwrap())?;
115115

116116
// Call the Rust method `find_one`
117117
match self.inner.update_many(filter_doc, update_doc) {
118118
Ok(update_result) => {
119119
// Convert BSON Document to Python Dict
120120
let py_result = update_result_to_pydict(py, update_result).unwrap();
121-
Ok(Some(py_result.to_object(py)))
121+
Ok(Some(py_result.into_py_any(py).unwrap()))
122122
}
123123
Err(err) => Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
124124
"Update many error: {}",
@@ -134,8 +134,8 @@ impl PyCollection {
134134
update: Py<PyDict>,
135135
) -> PyResult<Option<PyObject>> {
136136
// Convert PyDict to BSON Document
137-
let filter_doc = convert_py_obj_to_document(filter.to_object(py).as_any())?;
138-
let update_doc = convert_py_obj_to_document(update.to_object(py).as_any())?;
137+
let filter_doc = convert_py_obj_to_document(&filter.into_py_any(py).unwrap())?;
138+
let update_doc = convert_py_obj_to_document(&update.into_py_any(py).unwrap())?;
139139

140140
match self.inner.update_one_with_options(
141141
filter_doc,
@@ -145,7 +145,7 @@ impl PyCollection {
145145
Ok(update_result) => {
146146
// Convert BSON Document to Python Dict
147147
let py_result = update_result_to_pydict(py, update_result).unwrap();
148-
Ok(Some(py_result.to_object(py)))
148+
Ok(Some(py_result.into_py_any(py).unwrap()))
149149
}
150150
Err(err) => Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
151151
"Upsert one error: {}",
@@ -158,7 +158,7 @@ impl PyCollection {
158158
Python::with_gil(|py| {
159159

160160
let bson_vec_pipeline: Vec<Document> =
161-
convert_py_list_to_vec_document(pipeline.to_object(py).as_any());
161+
convert_py_list_to_vec_document(&pipeline.into_py_any(py).unwrap());
162162
match self.inner.aggregate(bson_vec_pipeline).run() {
163163
Ok(result) => {
164164
let vec_result = result.collect::<Result<Vec<Document>, _>>().unwrap();
@@ -167,7 +167,7 @@ impl PyCollection {
167167
.into_iter()
168168
.map(|x| document_to_pydict(py, x).unwrap())
169169
.collect();
170-
Ok(py_result.to_object(py))
170+
Ok(py_result.into_py_any(py).unwrap())
171171
}
172172
Err(e) => {
173173
// Raise a Python exception on error
@@ -184,8 +184,8 @@ impl PyCollection {
184184
update: Py<PyDict>,
185185
) -> PyResult<Option<PyObject>> {
186186
// Convert PyDict to BSON Document
187-
let filter_doc = convert_py_obj_to_document(filter.to_object(py).as_any())?;
188-
let update_doc = convert_py_obj_to_document(update.to_object(py).as_any())?;
187+
let filter_doc = convert_py_obj_to_document(&filter.into_py_any(py).unwrap())?;
188+
let update_doc = convert_py_obj_to_document(&update.into_py_any(py).unwrap())?;
189189

190190
// Call the Rust method `find_one`
191191
match self.inner.update_many_with_options(
@@ -196,7 +196,7 @@ impl PyCollection {
196196
Ok(update_result) => {
197197
// Convert BSON Document to Python Dict
198198
let py_result = update_result_to_pydict(py, update_result).unwrap();
199-
Ok(Some(py_result.to_object(py)))
199+
Ok(Some(py_result.into_py_any(py).unwrap()))
200200
}
201201
Err(err) => Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
202202
"Upsert many error: {}",
@@ -207,9 +207,9 @@ impl PyCollection {
207207

208208
pub fn delete_one(&self, filter: Py<PyDict>) -> PyResult<PyObject> {
209209
// Acquire the Python GIL (Global Interpreter Lock)
210-
// let filter_doc = convert_py_obj_to_document(filter.to_object(py).as_any())?;
210+
// let filter_doc = convert_py_obj_to_document(&filter.into_py_any(py).unwrap())?;
211211
Python::with_gil(|py| {
212-
let bson_doc: Document = match convert_py_obj_to_document(filter.to_object(py).as_any())
212+
let bson_doc: Document = match convert_py_obj_to_document(&filter.into_py_any(py).unwrap())
213213
{
214214
Ok(d) => d,
215215
Err(e) => return Err(PyRuntimeError::new_err(format!("Delete one : {}", e))),
@@ -219,7 +219,7 @@ impl PyCollection {
219219
Ok(delete_result) => {
220220
// Create a Python object from the Rust result and return it
221221
let py_result = delete_result_to_pydict(py, delete_result).unwrap();
222-
Ok(py_result.to_object(py))
222+
Ok(py_result.into_py_any(py).unwrap())
223223

224224
// Ok(Py::new(py, result)?.to_object(py))
225225
}
@@ -234,7 +234,7 @@ impl PyCollection {
234234
pub fn delete_many(&self, filter: Py<PyDict>) -> PyResult<PyObject> {
235235
// Acquire the Python GIL (Global Interpreter Lock)
236236
Python::with_gil(|py| {
237-
let bson_doc: Document = match convert_py_obj_to_document(filter.to_object(py).as_any())
237+
let bson_doc: Document = match convert_py_obj_to_document(&filter.into_py_any(py).unwrap())
238238
{
239239
Ok(d) => d,
240240
Err(e) => return Err(PyRuntimeError::new_err(format!("Delete many : {}", e))),
@@ -244,7 +244,7 @@ impl PyCollection {
244244
Ok(delete_result) => {
245245
// Create a Python object from the Rust result and return it
246246
let py_result = delete_result_to_pydict(py, delete_result).unwrap();
247-
Ok(py_result.to_object(py))
247+
Ok(py_result.into_py_any(py).unwrap())
248248

249249
// Ok(Py::new(py, result)?.to_object(py))
250250
}
@@ -274,14 +274,14 @@ impl PyCollection {
274274

275275
pub fn find_one(&self, py: Python, filter: Py<PyDict>) -> PyResult<Option<PyObject>> {
276276
// Convert PyDict to BSON Document
277-
let filter_doc = convert_py_obj_to_document(filter.to_object(py).as_any())?;
277+
let filter_doc = convert_py_obj_to_document(&filter.into_py_any(py).unwrap())?;
278278

279279
// Call the Rust method `find_one`
280280
match self.inner.find_one(filter_doc) {
281281
Ok(Some(result_doc)) => {
282282
// Convert BSON Document to Python Dict
283283
let py_result = document_to_pydict(py, result_doc).unwrap();
284-
Ok(Some(py_result.to_object(py)))
284+
Ok(Some(py_result.into_py_any(py).unwrap()))
285285
}
286286
Ok(None) => Ok(None), // Return None if no document is found
287287
Err(err) => Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
@@ -292,7 +292,7 @@ impl PyCollection {
292292
}
293293
pub fn find(&self, py: Python, filter: Py<PyDict>) -> PyResult<Option<PyObject>> {
294294
// Convert PyDict to BSON Document
295-
let filter_doc = convert_py_obj_to_document(filter.to_object(py).as_any())?;
295+
let filter_doc = convert_py_obj_to_document(&filter.into_py_any(py).unwrap())?;
296296

297297
// Call the Rust method `find_one`
298298
match self.inner.find(filter_doc).run() {
@@ -302,7 +302,7 @@ impl PyCollection {
302302
.map(|x| document_to_pydict(py, x.unwrap()).unwrap())
303303
.collect();
304304
// let py_result = document_to_pydict(py, result_doc).unwrap();
305-
Ok(Some(py_result.to_object(py)))
305+
Ok(Some(py_result.into_py_any(py).unwrap()))
306306
}
307307
// Ok(None) => Ok(None), // Return None if no document is found
308308
Err(err) => Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(

0 commit comments

Comments
 (0)