Skip to content

Commit 451de18

Browse files
committed
Merge branch 'master' into pyclass-new-layout
2 parents ab0a731 + eca3bdc commit 451de18

File tree

10 files changed

+89
-19
lines changed

10 files changed

+89
-19
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## Unreleased
99

10+
### Changed
11+
12+
* The blanket implementations for `FromPyObject` for `&T` and `&mut T` are no longer specializable. Implement `PyTryFrom` for your type to control the behavior of `FromPyObject::extract()` for your types.
13+
* The implementation for `IntoPy<U> for T` where `U: FromPy<T>` is no longer specializable. Control the behavior of this via the implementation of `FromPy`.
14+
15+
## [0.8.5]
16+
1017
* Support for `#[name = "foo"]` attribute for `#[pyfunction]` and in `#[pymethods]`. [#692](https://github.com/PyO3/pyo3/pull/692)
1118
* Implemented `FromPyObject` for `HashMap` and `BTreeMap`
1219

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyo3"
3-
version = "0.8.4"
3+
version = "0.8.5"
44
description = "Bindings to Python interpreter"
55
authors = ["PyO3 Project and Contributors <https://github.com/PyO3>"]
66
readme = "README.md"
@@ -22,7 +22,7 @@ appveyor = { repository = "fafhrd91/pyo3" }
2222
libc = "0.2.62"
2323
spin = "0.5.1"
2424
num-traits = "0.2.8"
25-
pyo3cls = { path = "pyo3cls", version = "=0.8.4" }
25+
pyo3cls = { path = "pyo3cls", version = "=0.8.5" }
2626
num-complex = { version = ">= 0.2", optional = true }
2727
num-bigint = { version = ">= 0.2", optional = true }
2828
inventory = "0.1.4"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ name = "string_sum"
5050
crate-type = ["cdylib"]
5151

5252
[dependencies.pyo3]
53-
version = "0.8.4"
53+
version = "0.8.5"
5454
features = ["extension-module"]
5555
```
5656

@@ -95,7 +95,7 @@ Add `pyo3` to your `Cargo.toml` like this:
9595

9696
```toml
9797
[dependencies]
98-
pyo3 = "0.8.4"
98+
pyo3 = "0.8.5"
9999
```
100100

101101
Example program displaying the value of `sys.version` and the current user name:

guide/src/class.md

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ pyo3::inventory::collect!(MyClassGeneratedPyo3Inventory);
8686
# pyo3::py_run!(py, cls, "assert cls.__name__ == 'MyClass'")
8787
```
8888

89+
## Add class to module
90+
91+
Custom Python classes can then be added to a module using `add_class`.
92+
93+
```rust
94+
# use pyo3::prelude::*;
95+
# #[pyclass]
96+
# struct MyClass {
97+
# num: i32,
98+
# debug: bool,
99+
# }
100+
#[pymodule]
101+
fn mymodule(_py: Python, m: &PyModule) -> PyResult<()> {
102+
m.add_class::<MyClass>()?;
103+
Ok(())
104+
}
105+
```
89106

90107
## Get Python objects from `pyclass`
91108
You sometimes need to convert your `pyclass` into a Python object in Rust code (e.g., for testing it).
@@ -568,13 +585,59 @@ use pyo3::types::{PyDict, PyTuple};
568585
#
569586
#[pymethods]
570587
impl MyClass {
571-
#[args(arg1=true, args="*", arg2=10, args3="\"Hello\"", kwargs="**")]
572-
fn method(&self, arg1: bool, args: &PyTuple, arg2: i32, arg3: &str, kwargs: Option<&PyDict>) -> PyResult<i32> {
573-
Ok(1)
588+
#[new]
589+
#[args(num = "-1", debug = "true")]
590+
fn new(num: i32, debug: bool) -> Self {
591+
MyClass { num, debug }
592+
}
593+
594+
#[args(
595+
num = "10",
596+
debug = "true",
597+
py_args = "*",
598+
name = "\"Hello\"",
599+
py_kwargs = "**"
600+
)]
601+
fn method(
602+
&mut self,
603+
num: i32,
604+
debug: bool,
605+
name: &str,
606+
py_args: &PyTuple,
607+
py_kwargs: Option<&PyDict>,
608+
) -> PyResult<String> {
609+
self.debug = debug;
610+
self.num = num;
611+
Ok(format!(
612+
"py_args={:?}, py_kwargs={:?}, name={}, num={}, debug={}",
613+
py_args, py_kwargs, name, self.num, self.debug
614+
))
615+
}
616+
617+
fn make_change(&mut self, num: i32, debug: bool) -> PyResult<String> {
618+
self.num = num;
619+
self.debug = debug;
620+
Ok(format!("num={}, debug={}", self.num, self.debug))
574621
}
575622
}
576623
```
577-
624+
N.B. the position of the `"*"` argument (if included) controls the system of handling positional and keyword arguments. In Python:
625+
```python
626+
import mymodule
627+
628+
mc = mymodule.MyClass()
629+
print(mc.method(44, False, "World", 666, x=44, y=55))
630+
print(mc.method(num=-1, name="World"))
631+
print(mc.make_change(44, False))
632+
print(mc.make_change(debug=False, num=-1))
633+
```
634+
Produces output:
635+
```text
636+
py_args=('World', 666), py_kwargs=Some({'x': 44, 'y': 55}), name=Hello, num=44, debug=false
637+
py_args=(), py_kwargs=None, name=World, num=-1, debug=true
638+
num=44, debug=false
639+
num=-1, debug=false
640+
```
578641

579642
## Class customizations
580643

guide/src/get_started.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ name = "string_sum"
4444
crate-type = ["cdylib"]
4545

4646
[dependencies.pyo3]
47-
version = "0.8.4"
47+
version = "0.8.5"
4848
features = ["extension-module"]
4949
```
5050

@@ -89,7 +89,7 @@ Add `pyo3` to your `Cargo.toml` like this:
8989

9090
```toml
9191
[dependencies]
92-
pyo3 = "0.8.4"
92+
pyo3 = "0.8.5"
9393
```
9494

9595
Example program displaying the value of `sys.version` and the current user name:

pyo3-derive-backend/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyo3-derive-backend"
3-
version = "0.8.4"
3+
version = "0.8.5"
44
description = "Code generation for PyO3 package"
55
authors = ["PyO3 Project and Contributors <https://github.com/PyO3>"]
66
keywords = ["pyo3", "python", "cpython", "ffi"]

pyo3-derive-backend/src/defs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub const OBJECT: Proto = Proto {
3131
MethodProto::Binary {
3232
name: "__delattr__",
3333
arg: "Name",
34-
pyres: true,
34+
pyres: false,
3535
proto: "pyo3::class::basic::PyObjectDelAttrProtocol",
3636
},
3737
MethodProto::Unary {

pyo3cls/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyo3cls"
3-
version = "0.8.4"
3+
version = "0.8.5"
44
description = "Proc macros for PyO3 package"
55
authors = ["PyO3 Project and Contributors <https://github.com/PyO3>"]
66
keywords = ["pyo3", "python", "cpython", "ffi"]
@@ -17,7 +17,7 @@ proc-macro = true
1717
quote = "1"
1818
proc-macro2 = "1"
1919
syn = { version = "1", features = ["full", "extra-traits"] }
20-
pyo3-derive-backend = { path = "../pyo3-derive-backend", version = "=0.8.4" }
20+
pyo3-derive-backend = { path = "../pyo3-derive-backend", version = "=0.8.5" }
2121

2222
[features]
2323
unsound-subclass = ["pyo3-derive-backend/unsound-subclass"]

src/conversion.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl<T, U> IntoPy<U> for T
146146
where
147147
U: FromPy<T>,
148148
{
149-
default fn into_py(self, py: Python) -> U {
149+
fn into_py(self, py: Python) -> U {
150150
U::from_py(self, py)
151151
}
152152
}
@@ -250,7 +250,7 @@ where
250250
T: PyTryFrom<'a>,
251251
{
252252
#[inline]
253-
default fn extract(ob: &'a PyAny) -> PyResult<&'a T> {
253+
fn extract(ob: &'a PyAny) -> PyResult<&'a T> {
254254
Ok(T::try_from(ob)?)
255255
}
256256
}
@@ -261,7 +261,7 @@ where
261261
T: PyTryFrom<'a>,
262262
{
263263
#[inline]
264-
default fn extract(ob: &'a PyAny) -> PyResult<&'a mut T> {
264+
fn extract(ob: &'a PyAny) -> PyResult<&'a mut T> {
265265
Ok(T::try_from_mut(ob)?)
266266
}
267267
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
//! crate-type = ["cdylib"]
4949
//!
5050
//! [dependencies.pyo3]
51-
//! version = "0.8.4"
51+
//! version = "0.8.5"
5252
//! features = ["extension-module"]
5353
//! ```
5454
//!
@@ -93,7 +93,7 @@
9393
//!
9494
//! ```toml
9595
//! [dependencies]
96-
//! pyo3 = "0.8.4"
96+
//! pyo3 = "0.8.5"
9797
//! ```
9898
//!
9999
//! Example program displaying the value of `sys.version`:

0 commit comments

Comments
 (0)