Skip to content

Commit 9c3ac7e

Browse files
authored
Merge pull request #336 from PyO3/rust_2018
Migrate to rust 2018
2 parents 423b5d1 + 0495ee5 commit 9c3ac7e

File tree

120 files changed

+549
-633
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+549
-633
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ dist/
1414
.eggs/
1515
venv*
1616
guide/book/
17-
examples/*/py
17+
examples/*/py*
1818

1919
*.so
2020
*.out

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
88

99
### Added
1010

11-
* Added a `wrap_module!` macro similar to the existing `wrap_function!` macro. Only available on python 3
11+
* Added a `wrap_pymodule!` macro similar to the existing `wrap_pyfunction!` macro. Only available on python 3
12+
* Added support for cross compiling (e.g. to arm v7) by mtp401 in [#327](https://github.com/PyO3/pyo3/pull/327). See the "Cross Compiling" section in the "Building and Distribution" chapter of the guide for more details.
1213

1314
### Changed
1415

1516
* Renamed `add_function` to `add_wrapped` as it now also supports modules.
1617
* Renamed `#[pymodinit]` to `#[pymodule]`.
1718
* Renamed `py_exception` to `create_exception` and refactored the error macros.
19+
* Renamed `wrap_function!` to `wrap_pyfunction!`
20+
* Migrated to the 2018 edition
1821

1922
### Removed
2023

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyo3"
3-
version = "0.6.0-alpha.1"
3+
version = "0.6.0-alpha.2"
44
description = "Bindings to Python interpreter"
55
authors = ["PyO3 Project and Contributors <https://github.com/PyO3>"]
66
readme = "README.md"
@@ -12,6 +12,7 @@ categories = ["api-bindings", "development-tools::ffi"]
1212
license = "Apache-2.0"
1313
exclude = ["/.gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
1414
build = "build.rs"
15+
edition = "2018"
1516

1617
[badges]
1718
travis-ci = { repository = "PyO3/pyo3", branch = "master" }
@@ -22,7 +23,7 @@ codecov = { repository = "PyO3/pyo3", branch = "master", service = "github" }
2223
libc = "0.2.43"
2324
spin = "0.5.0"
2425
num-traits = "0.2.6"
25-
pyo3cls = { path = "pyo3cls", version = "=0.6.0-alpha.1" }
26+
pyo3cls = { path = "pyo3cls", version = "=0.6.0-alpha.2" }
2627
mashup = "0.1.9"
2728
num-complex = { version = "0.2.1", optional = true }
2829

README.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,20 @@ sudo apt install python3-dev python-dev
3131

3232
Pyo3 can be used to generate a native python module.
3333

34-
**`Cargo.toml`:**
34+
**`Cargo.toml`**
3535

3636
```toml
3737
[package]
3838
name = "string-sum"
3939
version = "0.1.0"
40+
edition = "2018"
4041

4142
[lib]
4243
name = "string_sum"
4344
crate-type = ["cdylib"]
4445

4546
[dependencies.pyo3]
46-
version = "0.6.0-alpha.1"
47+
version = "0.6.0-alpha.2"
4748
features = ["extension-module"]
4849
```
4950

@@ -52,10 +53,8 @@ features = ["extension-module"]
5253
```rust
5354
#![feature(specialization)]
5455

55-
#[macro_use]
56-
extern crate pyo3;
57-
5856
use pyo3::prelude::*;
57+
use pyo3::wrap_pyfunction;
5958

6059
#[pyfunction]
6160
/// Formats the sum of two numbers as string
@@ -66,7 +65,7 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
6665
/// This module is a python module implemented in Rust.
6766
#[pymodule]
6867
fn string_sum(py: Python, m: &PyModule) -> PyResult<()> {
69-
m.add_wrapped(wrap_function!(sum_as_string))?;
68+
m.add_wrapped(wrap_pyfunction!(sum_as_string))?;
7069

7170
Ok(())
7271
}
@@ -92,16 +91,14 @@ Add `pyo3` this to your `Cargo.toml`:
9291

9392
```toml
9493
[dependencies]
95-
pyo3 = "0.5"
94+
pyo3 = "0.6.0-alpha.2"
9695
```
9796

9897
Example program displaying the value of `sys.version`:
9998

10099
```rust
101100
#![feature(specialization)]
102101

103-
extern crate pyo3;
104-
105102
use pyo3::prelude::*;
106103
use pyo3::types::PyDict;
107104

@@ -110,11 +107,10 @@ fn main() -> PyResult<()> {
110107
let py = gil.python();
111108
let sys = py.import("sys")?;
112109
let version: String = sys.get("version")?.extract()?;
113-
114110
let locals = PyDict::new(py);
115111
locals.set_item("os", py.import("os")?)?;
116-
let user: String = py.eval("os.getenv('USER') or os.getenv('USERNAME')", None, Some(&locals))?.extract()?;
117-
112+
let code = "os.getenv('USER') or os.getenv('USERNAME') or 'Unknown'";
113+
let user: String = py.eval(code, None, Some(&locals))?.extract()?;
118114
println!("Hello {}, I'm Python {}", user, version);
119115
Ok(())
120116
}

benches/bench_dict.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#![feature(test)]
2-
extern crate pyo3;
2+
33
extern crate test;
4+
use pyo3::prelude::*;
5+
use pyo3::types::IntoPyDict;
46
use test::Bencher;
57

6-
use pyo3::{prelude::*, types::IntoPyDict};
7-
88
#[bench]
99
fn iter_dict(b: &mut Bencher) {
1010
let gil = Python::acquire_gil();

ci/travis/test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
set -ex
33

44
cargo fmt --all -- --check
5-
cargo test --features "$FEATURES"
6-
cargo clippy --features "$FEATURES"
5+
cargo test --features "$FEATURES num-complex"
6+
cargo clippy --features "$FEATURES num-complex"
77

88
for example_dir in examples/*; do
99
tox -c "$example_dir/tox.ini" -e py

examples/rustapi_module/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ authors = ["PyO3 Authors"]
33
name = "rustapi-module"
44
version = "0.1.0"
55
description = "A Python wrapper for the Rust API for purposes of testing"
6+
edition = "2018"
67

78
[dependencies]
89

examples/rustapi_module/src/datetime.rs

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,29 @@ use pyo3::types::{
33
PyDate, PyDateAccess, PyDateTime, PyDelta, PyDeltaAccess, PyTime, PyTimeAccess, PyTuple,
44
PyTzInfo,
55
};
6+
use pyo3::wrap_pyfunction;
67

78
#[pyfunction]
8-
fn make_date(py: Python, year: i32, month: u8, day: u8) -> PyResult<Py<PyDate>> {
9+
fn make_date(py: Python<'_>, year: i32, month: u8, day: u8) -> PyResult<Py<PyDate>> {
910
PyDate::new(py, year, month, day)
1011
}
1112

1213
#[pyfunction]
13-
fn get_date_tuple(py: Python, d: &PyDate) -> Py<PyTuple> {
14+
fn get_date_tuple(py: Python<'_>, d: &PyDate) -> Py<PyTuple> {
1415
PyTuple::new(
1516
py,
1617
&[d.get_year(), d.get_month() as i32, d.get_day() as i32],
1718
)
1819
}
1920

2021
#[pyfunction]
21-
fn date_from_timestamp(py: Python, timestamp: i64) -> PyResult<Py<PyDate>> {
22+
fn date_from_timestamp(py: Python<'_>, timestamp: i64) -> PyResult<Py<PyDate>> {
2223
PyDate::from_timestamp(py, timestamp)
2324
}
2425

2526
#[pyfunction]
2627
fn make_time(
27-
py: Python,
28+
py: Python<'_>,
2829
hour: u8,
2930
minute: u8,
3031
second: u8,
@@ -64,7 +65,7 @@ fn time_with_fold(
6465
}
6566

6667
#[pyfunction]
67-
fn get_time_tuple(py: Python, dt: &PyTime) -> Py<PyTuple> {
68+
fn get_time_tuple(py: Python<'_>, dt: &PyTime) -> Py<PyTuple> {
6869
PyTuple::new(
6970
py,
7071
&[
@@ -92,12 +93,12 @@ fn get_time_tuple_fold(py: Python, dt: &PyTime) -> Py<PyTuple> {
9293
}
9394

9495
#[pyfunction]
95-
fn make_delta(py: Python, days: i32, seconds: i32, microseconds: i32) -> PyResult<Py<PyDelta>> {
96+
fn make_delta(py: Python<'_>, days: i32, seconds: i32, microseconds: i32) -> PyResult<Py<PyDelta>> {
9697
PyDelta::new(py, days, seconds, microseconds, true)
9798
}
9899

99100
#[pyfunction]
100-
fn get_delta_tuple(py: Python, delta: &PyDelta) -> Py<PyTuple> {
101+
fn get_delta_tuple(py: Python<'_>, delta: &PyDelta) -> Py<PyTuple> {
101102
PyTuple::new(
102103
py,
103104
&[
@@ -110,7 +111,7 @@ fn get_delta_tuple(py: Python, delta: &PyDelta) -> Py<PyTuple> {
110111

111112
#[pyfunction]
112113
fn make_datetime(
113-
py: Python,
114+
py: Python<'_>,
114115
year: i32,
115116
month: u8,
116117
day: u8,
@@ -134,7 +135,7 @@ fn make_datetime(
134135
}
135136

136137
#[pyfunction]
137-
fn get_datetime_tuple(py: Python, dt: &PyDateTime) -> Py<PyTuple> {
138+
fn get_datetime_tuple(py: Python<'_>, dt: &PyDateTime) -> Py<PyTuple> {
138139
PyTuple::new(
139140
py,
140141
&[
@@ -168,7 +169,11 @@ fn get_datetime_tuple_fold(py: Python, dt: &PyDateTime) -> Py<PyTuple> {
168169
}
169170

170171
#[pyfunction]
171-
fn datetime_from_timestamp(py: Python, ts: f64, tz: Option<&PyTzInfo>) -> PyResult<Py<PyDateTime>> {
172+
fn datetime_from_timestamp(
173+
py: Python<'_>,
174+
ts: f64,
175+
tz: Option<&PyTzInfo>,
176+
) -> PyResult<Py<PyDateTime>> {
172177
PyDateTime::from_timestamp(py, ts, tz)
173178
}
174179

@@ -189,41 +194,41 @@ impl TzClass {
189194
obj.init(|| TzClass {})
190195
}
191196

192-
fn utcoffset(&self, py: Python, _dt: &PyDateTime) -> PyResult<Py<PyDelta>> {
197+
fn utcoffset(&self, py: Python<'_>, _dt: &PyDateTime) -> PyResult<Py<PyDelta>> {
193198
PyDelta::new(py, 0, 3600, 0, true)
194199
}
195200

196-
fn tzname(&self, _py: Python, _dt: &PyDateTime) -> PyResult<String> {
201+
fn tzname(&self, _py: Python<'_>, _dt: &PyDateTime) -> PyResult<String> {
197202
Ok(String::from("+01:00"))
198203
}
199204

200-
fn dst(&self, _py: Python, _dt: &PyDateTime) -> PyResult<Option<&PyDelta>> {
205+
fn dst(&self, _py: Python<'_>, _dt: &PyDateTime) -> PyResult<Option<&PyDelta>> {
201206
Ok(None)
202207
}
203208
}
204209

205210
#[pymodule]
206-
fn datetime(_py: Python, m: &PyModule) -> PyResult<()> {
207-
m.add_wrapped(wrap_function!(make_date))?;
208-
m.add_wrapped(wrap_function!(get_date_tuple))?;
209-
m.add_wrapped(wrap_function!(date_from_timestamp))?;
210-
m.add_wrapped(wrap_function!(make_time))?;
211-
m.add_wrapped(wrap_function!(get_time_tuple))?;
212-
m.add_wrapped(wrap_function!(make_delta))?;
213-
m.add_wrapped(wrap_function!(get_delta_tuple))?;
214-
m.add_wrapped(wrap_function!(make_datetime))?;
215-
m.add_wrapped(wrap_function!(get_datetime_tuple))?;
216-
m.add_wrapped(wrap_function!(datetime_from_timestamp))?;
211+
fn datetime(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
212+
m.add_wrapped(wrap_pyfunction!(make_date))?;
213+
m.add_wrapped(wrap_pyfunction!(get_date_tuple))?;
214+
m.add_wrapped(wrap_pyfunction!(date_from_timestamp))?;
215+
m.add_wrapped(wrap_pyfunction!(make_time))?;
216+
m.add_wrapped(wrap_pyfunction!(get_time_tuple))?;
217+
m.add_wrapped(wrap_pyfunction!(make_delta))?;
218+
m.add_wrapped(wrap_pyfunction!(get_delta_tuple))?;
219+
m.add_wrapped(wrap_pyfunction!(make_datetime))?;
220+
m.add_wrapped(wrap_pyfunction!(get_datetime_tuple))?;
221+
m.add_wrapped(wrap_pyfunction!(datetime_from_timestamp))?;
217222

218223
// Python 3.6+ functions
219224
#[cfg(Py_3_6)]
220225
{
221-
m.add_wrapped(wrap_function!(time_with_fold))?;
222-
m.add_wrapped(wrap_function!(get_time_tuple_fold))?;
223-
m.add_wrapped(wrap_function!(get_datetime_tuple_fold))?;
226+
m.add_wrapped(wrap_pyfunction!(time_with_fold))?;
227+
m.add_wrapped(wrap_pyfunction!(get_time_tuple_fold))?;
228+
m.add_wrapped(wrap_pyfunction!(get_datetime_tuple_fold))?;
224229
}
225230

226-
m.add_wrapped(wrap_function!(issue_219))?;
231+
m.add_wrapped(wrap_pyfunction!(issue_219))?;
227232

228233
m.add_class::<TzClass>()?;
229234
Ok(())

examples/rustapi_module/src/dict_iter.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use pyo3::prelude::*;
2-
31
use pyo3::exceptions::RuntimeError;
2+
use pyo3::prelude::*;
43
use pyo3::types::PyDict;
54

65
#[pymodule]
7-
fn test_dict(_py: Python, m: &PyModule) -> PyResult<()> {
6+
fn test_dict(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
87
m.add_class::<DictSize>()?;
98
Ok(())
109
}
@@ -21,7 +20,7 @@ impl DictSize {
2120
obj.init(|| DictSize { expected })
2221
}
2322

24-
fn iter_dict(&mut self, _py: Python, dict: &PyDict) -> PyResult<u32> {
23+
fn iter_dict(&mut self, _py: Python<'_>, dict: &PyDict) -> PyResult<u32> {
2524
let mut seen = 0u32;
2625
for (sym, values) in dict.iter() {
2726
seen += 1;

examples/rustapi_module/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#![feature(specialization)]
22

3-
#[macro_use]
4-
extern crate pyo3;
5-
63
pub mod datetime;
74
pub mod dict_iter;
85
pub mod othermod;

examples/rustapi_module/src/othermod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! The code below just tries to use the most important code generation paths
44
55
use pyo3::prelude::*;
6+
use pyo3::wrap_pyfunction;
67

78
#[pyclass]
89
pub struct ModClass {
@@ -29,8 +30,8 @@ fn double(x: i32) -> i32 {
2930
}
3031

3132
#[pymodule]
32-
fn othermod(_py: Python, m: &PyModule) -> PyResult<()> {
33-
m.add_wrapped(wrap_function!(double))?;
33+
fn othermod(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
34+
m.add_wrapped(wrap_pyfunction!(double))?;
3435
m.add_class::<ModClass>()?;
3536

3637
m.add("USIZE_MIN", usize::min_value())?;

examples/rustapi_module/src/subclassing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl Subclassable {
1414
}
1515

1616
#[pymodule]
17-
fn subclassing(_py: Python, m: &PyModule) -> PyResult<()> {
17+
fn subclassing(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
1818
m.add_class::<Subclassable>()?;
1919
Ok(())
2020
}

examples/word-count/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
authors = ["Messense Lv <[email protected]>"]
33
name = "word-count"
44
version = "0.1.0"
5+
edition = "2018"
56

67
[dependencies]
78
rayon = "1.0.2"

0 commit comments

Comments
 (0)