Skip to content

Commit b78e80a

Browse files
authored
Update PyO3 to 0.21 (#238)
1 parent 83088e2 commit b78e80a

File tree

34 files changed

+266
-244
lines changed

34 files changed

+266
-244
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88
### Added
9-
- Added SAFT-VR Mie equation of state.[#237](https://github.com/feos-org/feos/pull/237)
9+
- Added SAFT-VR Mie equation of state. [#237](https://github.com/feos-org/feos/pull/237)
1010
- Added ePC-SAFT equation of state. [#229](https://github.com/feos-org/feos/pull/229)
1111

1212
### Changed
@@ -20,6 +20,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
### Removed
2121
- Removed `VirialOrder` from `uvtheory` module. Orders are now variants of the existing `Perturbation` enum. [#226](https://github.com/feos-org/feos/pull/226)
2222

23+
### Packaging
24+
- Updated `quantity` dependency to 0.8. [#238](https://github.com/feos-org/feos/pull/238)
25+
- Updated `num-dual` dependency to 0.9. [#238](https://github.com/feos-org/feos/pull/238)
26+
- Updated `numpy` and `PyO3` dependencies to 0.21. [#238](https://github.com/feos-org/feos/pull/238)
27+
2328
## [0.6.1] - 2024-01-11
2429
- Python only: Release the changes introduced in `feos-core` 0.6.1.
2530

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ members = ["feos-core", "feos-dft", "feos-derive"]
2222
crate-type = ["rlib", "cdylib"]
2323

2424
[dependencies]
25-
quantity = { version = "0.7", optional = true }
26-
num-dual = "0.8"
25+
quantity = { version = "0.8", optional = true }
26+
num-dual = "0.9"
2727
feos-core = { version = "0.6", path = "feos-core" }
2828
feos-dft = { version = "0.6", path = "feos-dft", optional = true }
2929
feos-derive = { version = "0.4", path = "feos-derive" }
30-
numpy = { version = "0.20", optional = true }
30+
numpy = { version = "0.21", optional = true }
3131
ndarray = { version = "0.15", features = ["approx"] }
3232
petgraph = { version = "0.6", optional = true }
3333
thiserror = "1.0"
@@ -42,7 +42,7 @@ itertools = "0.12"
4242
typenum = "1.16"
4343

4444
[dependencies.pyo3]
45-
version = "0.20"
45+
version = "0.21"
4646
features = ["extension-module", "abi3", "abi3-py37"]
4747
optional = true
4848

feos-core/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
### Fixed
2323
- Added comparison for pressures of both phases in `PyPhaseDiagram.to_dict` to make the method work with `spinodal` constructor. [#224](https://github.com/feos-org/feos/pull/224)
2424
- Enforce the total moles when providing `State::new` with molefracs that do not add up to 1. [#227](https://github.com/feos-org/feos/pull/227)
25-
-
25+
26+
### Packaging
27+
- Updated `quantity` dependency to 0.8. [#238](https://github.com/feos-org/feos/pull/238)
28+
- Updated `num-dual` dependency to 0.9. [#238](https://github.com/feos-org/feos/pull/238)
29+
- Updated `numpy` and `PyO3` dependencies to 0.21. [#238](https://github.com/feos-org/feos/pull/238)
2630

2731

2832
## [0.6.1] 2024-01-11

feos-core/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ rustdoc-args = [ "--html-in-header", "./docs-header.html" ]
1818
features = [ "rayon" ]
1919

2020
[dependencies]
21-
quantity = { version = "0.7", optional = true }
22-
num-dual = { version = "0.8", features = ["linalg"] }
21+
quantity = { version = "0.8", optional = true }
22+
num-dual = { version = "0.9", features = ["linalg"] }
2323
ndarray = { version = "0.15", features = ["serde", "approx-0_5"] }
2424
nalgebra = "0.32"
2525
num-traits = "0.2"
@@ -28,8 +28,8 @@ serde = { version = "1.0", features = ["derive"] }
2828
serde_json = "1.0"
2929
indexmap = "2.0"
3030
conv = "0.3"
31-
numpy = { version = "0.20", optional = true }
32-
pyo3 = { version = "0.20", optional = true }
31+
numpy = { version = "0.21", optional = true }
32+
pyo3 = { version = "0.21", optional = true }
3333
rayon = { version = "1.5", optional = true }
3434
typenum = "1.16"
3535
approx = "0.5"

feos-core/src/python/parameter/fragmentation.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ impl_json_handling!(PySmartsRecord);
8585
impl PyChemicalRecord {
8686
#[staticmethod]
8787
pub fn from_smiles(
88-
py: Python<'_>,
89-
identifier: &PyAny,
88+
identifier: &Bound<'_, PyAny>,
9089
smarts: Vec<PySmartsRecord>,
9190
) -> PyResult<Self> {
91+
let py = identifier.py();
9292
let identifier = if let Ok(smiles) = identifier.extract::<String>() {
9393
Identifier::new(None, None, None, Some(&smiles), None, None)
9494
} else if let Ok(identifier) = identifier.extract::<PyIdentifier>() {
@@ -113,7 +113,7 @@ fn fragment_molecule(
113113
smiles: &str,
114114
smarts: Vec<PySmartsRecord>,
115115
) -> PyResult<(Vec<String>, Vec<[usize; 2]>)> {
116-
let chem = py.import("rdkit.Chem")?;
116+
let chem = py.import_bound("rdkit.Chem")?;
117117
let mol = chem.call_method1("MolFromSmiles", (smiles,))?;
118118
let atoms = mol.call_method0("GetNumHeavyAtoms")?.extract::<usize>()?;
119119

@@ -155,7 +155,7 @@ fn fragment_molecule(
155155
.for_each(|(_, m)| m.retain(|m| !(m.len() == 1 && large_segments.contains(&m[0]))));
156156

157157
let bonds = mol.call_method0("GetBonds")?;
158-
let builtins = py.import("builtins")?;
158+
let builtins = py.import_bound("builtins")?;
159159
let bonds = builtins
160160
.call_method1("list", (bonds,))?
161161
.extract::<Vec<&PyAny>>()?;

feos-core/src/python/parameter/mod.rs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,11 @@ macro_rules! impl_binary_record {
212212
#[pymethods]
213213
impl PyBinaryRecord {
214214
#[new]
215-
fn new(id1: PyIdentifier, id2: PyIdentifier, model_record: &PyAny) -> PyResult<Self> {
215+
fn new(
216+
id1: PyIdentifier,
217+
id2: PyIdentifier,
218+
model_record: &Bound<'_, PyAny>,
219+
) -> PyResult<Self> {
216220
if let Ok(mr) = model_record.extract::<f64>() {
217221
Ok(Self(BinaryRecord::new(id1.0, id2.0, mr.try_into()?)))
218222
} else if let Ok(mr) = model_record.extract::<$py_model_record>() {
@@ -264,16 +268,16 @@ macro_rules! impl_binary_record {
264268
}
265269

266270
#[getter]
267-
fn get_model_record(&self, py: Python) -> PyObject {
268-
if let Ok(mr) = f64::try_from(self.0.model_record.clone()) {
269-
mr.to_object(py)
271+
fn get_model_record<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
272+
Ok(if let Ok(mr) = f64::try_from(self.0.model_record.clone()) {
273+
pyo3::types::PyFloat::new_bound(py, mr).into_any()
270274
} else {
271-
$py_model_record(self.0.model_record.clone()).into_py(py)
272-
}
275+
Bound::new(py, $py_model_record(self.0.model_record.clone()))?.into_any()
276+
})
273277
}
274278

275279
#[setter]
276-
fn set_model_record(&mut self, model_record: &PyAny) -> PyResult<()> {
280+
fn set_model_record(&mut self, model_record: &Bound<'_, PyAny>) -> PyResult<()> {
277281
if let Ok(mr) = model_record.extract::<f64>() {
278282
self.0.model_record = mr.try_into()?;
279283
} else if let Ok(mr) = model_record.extract::<$py_model_record>() {
@@ -558,6 +562,8 @@ macro_rules! impl_parameter {
558562
impl_parameter!($parameter, $py_parameter, $py_model_record, PyNoBinaryModelRecord);
559563
};
560564
($parameter:ty, $py_parameter:ty, $py_model_record:ty, $py_binary_model_record:ty) => {
565+
use pyo3::pybacked::*;
566+
561567
#[pymethods]
562568
impl $py_parameter {
563569
/// Creates parameters from records.
@@ -578,14 +584,14 @@ macro_rules! impl_parameter {
578584
)]
579585
fn from_records(
580586
pure_records: Vec<PyPureRecord>,
581-
binary_records: Option<&PyAny>,
587+
binary_records: Option<&Bound<'_, PyAny>>,
582588
identifier_option: IdentifierOption,
583589
) -> PyResult<Self> {
584590
let prs: Vec<_> = pure_records.into_iter().map(|pr| pr.0).collect();
585591
let binary_records = binary_records
586592
.map(|binary_records| {
587593
if let Ok(br) = binary_records.extract::<PyReadonlyArray2<f64>>() {
588-
Ok(Some(br.to_owned_array().mapv(|r| r.try_into().unwrap())))
594+
Ok(Some(br.as_array().mapv(|r| r.try_into().unwrap())))
589595
} else if let Ok(br) = binary_records.extract::<Vec<PyBinaryRecord>>() {
590596
let brs: Vec<_> = br.into_iter().map(|br| br.0).collect();
591597
Ok(<$parameter>::binary_matrix_from_records(
@@ -628,7 +634,7 @@ macro_rules! impl_parameter {
628634
#[pyo3(text_signature = "(pure_records, binary_record=None)")]
629635
fn new_binary(
630636
pure_records: Vec<PyPureRecord>,
631-
binary_record: Option<&PyAny>,
637+
binary_record: Option<&Bound<'_, PyAny>>,
632638
) -> PyResult<Self> {
633639
let prs = pure_records.into_iter().map(|pr| pr.0).collect();
634640
let br = binary_record
@@ -678,11 +684,12 @@ macro_rules! impl_parameter {
678684
text_signature = "(substances, pure_path, binary_path=None, identifier_option)"
679685
)]
680686
fn from_json(
681-
substances: Vec<&str>,
687+
substances: Vec<PyBackedStr>,
682688
pure_path: String,
683689
binary_path: Option<String>,
684690
identifier_option: IdentifierOption,
685691
) -> Result<Self, ParameterError> {
692+
let substances = substances.iter().map(|s| &**s).collect();
686693
Ok(Self(Arc::new(<$parameter>::from_json(
687694
substances,
688695
pure_path,
@@ -708,13 +715,14 @@ macro_rules! impl_parameter {
708715
text_signature = "(input, binary_path=None, identifier_option)"
709716
)]
710717
fn from_multiple_json(
711-
input: Vec<(Vec<&str>, &str)>,
712-
binary_path: Option<&str>,
718+
input: Vec<(Vec<PyBackedStr>, PyBackedStr)>,
719+
binary_path: Option<PyBackedStr>,
713720
identifier_option: Option<IdentifierOption>,
714721
) -> Result<Self, ParameterError> {
722+
let input: Vec<(Vec<&str>, &str)> = input.iter().map(|(c, f)| (c.iter().map(|c| &**c).collect(), &**f)).collect();
715723
Ok(Self(Arc::new(<$parameter>::from_multiple_json(
716724
&input,
717-
binary_path,
725+
binary_path.as_deref(),
718726
identifier_option.unwrap_or(IdentifierOption::Name),
719727
)?)))
720728
}
@@ -730,11 +738,11 @@ macro_rules! impl_parameter {
730738
}
731739

732740
#[getter]
733-
fn get_binary_records<'py>(&self, py: Python<'py>) -> Option<&'py PyArray2<f64>> {
741+
fn get_binary_records<'py>(&self, py: Python<'py>) -> Option<Bound<'py, PyArray2<f64>>> {
734742
self.0
735743
.records()
736744
.1
737-
.map(|r| r.mapv(|r| f64::try_from(r).unwrap()).view().to_pyarray(py))
745+
.map(|r| r.mapv(|r| f64::try_from(r).unwrap()).view().to_pyarray_bound(py))
738746
}
739747
}
740748
};
@@ -743,6 +751,8 @@ macro_rules! impl_parameter {
743751
#[macro_export]
744752
macro_rules! impl_parameter_from_segments {
745753
($parameter:ty, $py_parameter:ty) => {
754+
use pyo3::pybacked::*;
755+
746756
#[pymethods]
747757
impl $py_parameter {
748758
/// Creates parameters from segment records.
@@ -790,12 +800,13 @@ macro_rules! impl_parameter_from_segments {
790800
text_signature = "(substances, pure_path, segments_path, binary_path=None, identifier_option)"
791801
)]
792802
fn from_json_segments(
793-
substances: Vec<&str>,
803+
substances: Vec<PyBackedStr>,
794804
pure_path: String,
795805
segments_path: String,
796806
binary_path: Option<String>,
797807
identifier_option: IdentifierOption,
798808
) -> PyResult<Self> {
809+
let substances: Vec<_> = substances.iter().map(|s| &**s).collect();
799810
Ok(Self(Arc::new(<$parameter>::from_json_segments(
800811
&substances,
801812
pure_path,
@@ -824,15 +835,14 @@ macro_rules! impl_parameter_from_segments {
824835
#[staticmethod]
825836
#[pyo3(text_signature = "(identifier, smarts_records, segment_records, binary_segment_records=None)")]
826837
fn from_smiles(
827-
py: Python<'_>,
828-
identifier: Vec<&PyAny>,
838+
identifier: Vec<Bound<'_,PyAny>>,
829839
smarts_records: Vec<PySmartsRecord>,
830840
segment_records: Vec<PySegmentRecord>,
831841
binary_segment_records: Option<Vec<PyBinarySegmentRecord>>,
832842
) -> PyResult<Self> {
833843
let chemical_records: Vec<_> = identifier
834844
.into_iter()
835-
.map(|i| PyChemicalRecord::from_smiles(py, i, smarts_records.clone()))
845+
.map(|i| PyChemicalRecord::from_smiles(&i, smarts_records.clone()))
836846
.collect::<PyResult<_>>()?;
837847
Self::from_segments(chemical_records, segment_records, binary_segment_records)
838848
}
@@ -857,8 +867,7 @@ macro_rules! impl_parameter_from_segments {
857867
text_signature = "(identifier, smarts_path, segments_path, binary_path=None)"
858868
)]
859869
fn from_json_smiles(
860-
py: Python<'_>,
861-
identifier: Vec<&PyAny>,
870+
identifier: Vec<Bound<'_,PyAny>>,
862871
smarts_path: String,
863872
segments_path: String,
864873
binary_path: Option<String>,
@@ -867,7 +876,6 @@ macro_rules! impl_parameter_from_segments {
867876
let segment_records = PySegmentRecord::from_json(&segments_path)?;
868877
let binary_segment_records = binary_path.map(|p| PyBinarySegmentRecord::from_json(&p)).transpose()?;
869878
Self::from_smiles(
870-
py,
871879
identifier,
872880
smarts_records,
873881
segment_records,

feos-core/src/python/phase_equilibria.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,12 @@ macro_rules! impl_phase_equilibrium {
155155
/// PhaseEquilibrium
156156
#[staticmethod]
157157
#[pyo3(text_signature = "(eos, temperature_or_pressure, liquid_molefracs, tp_init=None, vapor_molefracs=None, max_iter_inner=None, max_iter_outer=None, tol_inner=None, tol_outer=None, verbosity=None)")]
158-
pub fn bubble_point(
158+
pub fn bubble_point<'py>(
159159
eos: $py_eos,
160160
temperature_or_pressure: PySINumber,
161-
liquid_molefracs: &PyArray1<f64>,
161+
liquid_molefracs: &Bound<'py, PyArray1<f64>>,
162162
tp_init: Option<PySINumber>,
163-
vapor_molefracs: Option<&PyArray1<f64>>,
163+
vapor_molefracs: Option<&Bound<'py, PyArray1<f64>>>,
164164
max_iter_inner: Option<usize>,
165165
max_iter_outer: Option<usize>,
166166
tol_inner: Option<f64>,
@@ -232,12 +232,12 @@ macro_rules! impl_phase_equilibrium {
232232
/// PhaseEquilibrium
233233
#[staticmethod]
234234
#[pyo3(text_signature = "(eos, temperature_or_pressure, vapor_molefracs, tp_init=None, liquid_molefracs=None, max_iter_inner=None, max_iter_outer=None, tol_inner=None, tol_outer=None, verbosity=None)")]
235-
pub fn dew_point(
235+
pub fn dew_point<'py>(
236236
eos: $py_eos,
237237
temperature_or_pressure: PySINumber,
238-
vapor_molefracs: &PyArray1<f64>,
238+
vapor_molefracs: &Bound<'py, PyArray1<f64>>,
239239
tp_init: Option<PySINumber>,
240-
liquid_molefracs: Option<&PyArray1<f64>>,
240+
liquid_molefracs: Option<&Bound<'py, PyArray1<f64>>>,
241241
max_iter_inner: Option<usize>,
242242
max_iter_outer: Option<usize>,
243243
tol_inner: Option<f64>,

0 commit comments

Comments
 (0)