Skip to content

Commit e2e4733

Browse files
committed
Test examples in user guide with travis
Test could only be activated for Python 3.5 and some tests had to be ignored, see PyO3#381 and PyO3#387.
1 parent d63fac8 commit e2e4733

12 files changed

+59
-11
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ matrix:
1313
env: FEATURES=python2
1414
- name: Python 3.5
1515
python: "3.5"
16-
env: FEATURES=python3
16+
env: FEATURES="python3 test-doc"
1717
- name: Python 3.6
1818
python: "3.6"
1919
env: FEATURES=python3

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ extension-module = []
5555
# are welcome.
5656
# abi3 = []
5757

58+
# Use this feature to test the examples in the user guide
59+
test-doc = []
60+
5861
[workspace]
5962
members = [
6063
"pyo3cls",

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ features = ["extension-module"]
5151
**`src/lib.rs`**
5252

5353
```rust
54+
// Not required when using Rust 2018
55+
extern crate pyo3;
56+
5457
use pyo3::prelude::*;
5558
use pyo3::wrap_pyfunction;
5659

@@ -95,6 +98,9 @@ pyo3 = "0.6.0-alpha.4"
9598
Example program displaying the value of `sys.version`:
9699

97100
```rust
101+
// Not required when using Rust 2018
102+
extern crate pyo3;
103+
98104
use pyo3::prelude::*;
99105
use pyo3::types::PyDict;
100106

ci/travis/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/bash
22
set -ex
33

4+
cargo clean
45
cargo test --features "$FEATURES num-complex"
56
if [ $TRAVIS_JOB_NAME = 'Minimum nightly' ]; then
67
cargo fmt --all -- --check

guide/src/class.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ To define a custom python class, a rust struct needs to be annotated with the
66
`#[pyclass]` attribute.
77

88
```rust
9+
# extern crate pyo3;
910
# use pyo3::prelude::*;
1011

1112
#[pyclass]
@@ -35,6 +36,7 @@ You can get an instance of `PyRef` by `PyRef::new`, which does 3 things:
3536

3637
You can use `PyRef` just like `&T`, because it implements `Deref<Target=T>`.
3738
```rust
39+
# extern crate pyo3;
3840
# use pyo3::prelude::*;
3941
# use pyo3::types::PyDict;
4042
#[pyclass]
@@ -54,6 +56,7 @@ dict.set_item("obj", obj).unwrap();
5456
### `PyRefMut`
5557
`PyRefMut` is a mutable version of `PyRef`.
5658
```rust
59+
# extern crate pyo3;
5760
# use pyo3::prelude::*;
5861
#[pyclass]
5962
struct MyClass {
@@ -71,6 +74,7 @@ obj.num = 5;
7174

7275
You can use it to avoid lifetime problems.
7376
```rust
77+
# extern crate pyo3;
7478
# use pyo3::prelude::*;
7579
#[pyclass]
7680
struct MyClass {
@@ -110,6 +114,7 @@ To declare a constructor, you need to define a class method and annotate it with
110114
attribute. Only the python `__new__` method can be specified, `__init__` is not available.
111115

112116
```rust
117+
# extern crate pyo3;
113118
# use pyo3::prelude::*;
114119
# use pyo3::PyRawObject;
115120
#[pyclass]
@@ -150,7 +155,8 @@ By default `PyObject` is used as default base class. To override default base cl
150155
`new` method accepts `PyRawObject` object. `obj` instance must be initialized
151156
with value of custom class struct. Subclass must call parent's `new` method.
152157

153-
```rust
158+
```rust,ignore
159+
# extern crate pyo3;
154160
# use pyo3::prelude::*;
155161
# use pyo3::PyRawObject;
156162
#[pyclass]
@@ -184,7 +190,7 @@ impl SubClass {
184190
}
185191
186192
fn method2(&self) -> PyResult<()> {
187-
self.get_base().method()
193+
self.get_base().method()
188194
}
189195
}
190196
```
@@ -200,6 +206,7 @@ Descriptor methods can be defined in
200206
attributes. i.e.
201207

202208
```rust
209+
# extern crate pyo3;
203210
# use pyo3::prelude::*;
204211
# #[pyclass]
205212
# struct MyClass {
@@ -223,7 +230,8 @@ If function name starts with `get_` or `set_` for getter or setter respectively.
223230
Descriptor name becomes function name with prefix removed. This is useful in case of
224231
rust's special keywords like `type`.
225232

226-
```rust
233+
```rust,ignore
234+
# extern crate pyo3;
227235
# use pyo3::prelude::*;
228236
# #[pyclass]
229237
# struct MyClass {
@@ -251,7 +259,8 @@ In this case property `num` is defined. And it is available from python code as
251259
Also both `#[getter]` and `#[setter]` attributes accepts one parameter.
252260
If this parameter is specified, it is used as a property name. i.e.
253261

254-
```rust
262+
```rust,ignore
263+
# extern crate pyo3;
255264
# use pyo3::prelude::*;
256265
# #[pyclass]
257266
# struct MyClass {
@@ -278,7 +287,8 @@ In this case the property `number` is defined and is available from python code
278287

279288
For simple cases you can also define getters and setters in your Rust struct field definition, for example:
280289

281-
```rust
290+
```rust,ignore
291+
# extern crate pyo3;
282292
# use pyo3::prelude::*;
283293
#[pyclass]
284294
struct MyClass {
@@ -296,6 +306,7 @@ To define a python compatible method, `impl` block for struct has to be annotate
296306
block with some variations, like descriptors, class method static methods, etc.
297307

298308
```rust
309+
# extern crate pyo3;
299310
# use pyo3::prelude::*;
300311
# #[pyclass]
301312
# struct MyClass {
@@ -323,6 +334,7 @@ The return type must be `PyResult<T>` for some `T` that implements `IntoPyObject
323334
get injected by method wrapper. i.e
324335

325336
```rust
337+
# extern crate pyo3;
326338
# use pyo3::prelude::*;
327339
# #[pyclass]
328340
# struct MyClass {
@@ -346,6 +358,7 @@ To specify a class method for a custom class, the method needs to be annotated
346358
with the `#[classmethod]` attribute.
347359

348360
```rust
361+
# extern crate pyo3;
349362
# use pyo3::prelude::*;
350363
# use pyo3::types::PyType;
351364
# #[pyclass]
@@ -378,6 +391,7 @@ To specify a static method for a custom class, method needs to be annotated with
378391
`IntoPyObject`.
379392

380393
```rust
394+
# extern crate pyo3;
381395
# use pyo3::prelude::*;
382396
# #[pyclass]
383397
# struct MyClass {
@@ -400,6 +414,7 @@ To specify a custom `__call__` method for a custom class, call methods need to b
400414
the `#[call]` attribute. Arguments of the method are specified same as for instance method.
401415

402416
```rust
417+
# extern crate pyo3;
403418
# use pyo3::prelude::*;
404419
use pyo3::types::PyTuple;
405420
# #[pyclass]
@@ -442,6 +457,7 @@ Each parameter could be one of following type:
442457

443458
Example:
444459
```rust
460+
# extern crate pyo3;
445461
# use pyo3::prelude::*;
446462
use pyo3::types::{PyDict, PyTuple};
447463
#
@@ -543,6 +559,8 @@ These correspond to the slots `tp_traverse` and `tp_clear` in the Python C API.
543559
as every cycle must contain at least one mutable reference.
544560
Example:
545561
```rust
562+
extern crate pyo3;
563+
546564
use pyo3::prelude::*;
547565
use pyo3::PyTraverseError;
548566
use pyo3::gc::{PyGCProtocol, PyVisit};
@@ -583,14 +601,16 @@ collector, and it is possible to track them with `gc` module methods.
583601
Iterators can be defined using the
584602
[`PyIterProtocol`](https://docs.rs/pyo3/0.6.0-alpha.4/class/iter/trait.PyIterProtocol.html) trait.
585603
It includes two methods `__iter__` and `__next__`:
586-
* `fn __iter__(&mut self) -> PyResult<impl IntoPyObject>`
587-
* `fn __next__(&mut self) -> PyResult<Option<impl IntoPyObject>>`
604+
* `fn __iter__(slf: PyRefMut<Self>) -> PyResult<impl IntoPyObject>`
605+
* `fn __next__(slf: PyRefMut<Self>) -> PyResult<Option<impl IntoPyObject>>`
588606

589607
Returning `Ok(None)` from `__next__` indicates that that there are no further items.
590608

591609
Example:
592610

593611
```rust
612+
extern crate pyo3;
613+
594614
use pyo3::prelude::*;
595615
use pyo3::PyIterProtocol;
596616

guide/src/conversions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ provides two methods:
2424
Both methods accept `args` and `kwargs` arguments.
2525

2626
```rust
27+
# extern crate pyo3;
2728
use pyo3::prelude::*;
2829
use pyo3::types::{PyDict, PyTuple};
2930

@@ -61,6 +62,7 @@ fn main() {
6162
[`IntoPyDict`][IntoPyDict] trait to convert other dict-like containers, e.g. `HashMap`, `BTreeMap` as well as tuples with up to 10 elements and `Vec`s where each element is a two element tuple.
6263

6364
```rust
65+
# extern crate pyo3;
6466
use pyo3::prelude::*;
6567
use pyo3::types::{IntoPyDict, PyDict};
6668
use std::collections::HashMap;

guide/src/exception.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
You can use the `create_exception!` macro to define a new exception type:
66

77
```rust
8+
# extern crate pyo3;
89
use pyo3::create_exception;
910

1011
create_exception!(module, MyError, pyo3::exceptions::Exception);
@@ -16,6 +17,7 @@ create_exception!(module, MyError, pyo3::exceptions::Exception);
1617
For example:
1718

1819
```rust
20+
# extern crate pyo3;
1921
use pyo3::prelude::*;
2022
use pyo3::create_exception;
2123
use pyo3::types::PyDict;
@@ -40,6 +42,7 @@ fn main() {
4042
To raise an exception, first you need to obtain an exception type and construct a new [`PyErr`](https://docs.rs/pyo3/0.2.7/struct.PyErr.html), then call [`PyErr::restore()`](https://docs.rs/pyo3/0.2.7/struct.PyErr.html#method.restore) method to write the exception back to the Python interpreter's global state.
4143

4244
```rust
45+
# extern crate pyo3;
4346
use pyo3::{Python, PyErr};
4447
use pyo3::exceptions;
4548

@@ -64,6 +67,7 @@ has corresponding rust type, exceptions defined by `create_exception!` and `impo
6467
have rust type as well.
6568

6669
```rust
70+
# extern crate pyo3;
6771
# use pyo3::exceptions;
6872
# use pyo3::prelude::*;
6973
# fn check_for_error() -> bool {false}
@@ -82,6 +86,7 @@ Python has an [`isinstance`](https://docs.python.org/3/library/functions.html#is
8286
in `PyO3` there is a [`Python::is_instance()`](https://docs.rs/pyo3/0.2.7/struct.Python.html#method.is_instance) method which does the same thing.
8387

8488
```rust
89+
# extern crate pyo3;
8590
use pyo3::Python;
8691
use pyo3::types::{PyBool, PyList};
8792

@@ -100,6 +105,7 @@ fn main() {
100105
To check the type of an exception, you can simply do:
101106

102107
```rust
108+
# extern crate pyo3;
103109
# use pyo3::exceptions;
104110
# use pyo3::prelude::*;
105111
# fn main() {
@@ -151,6 +157,7 @@ The code snippet above will raise `OSError` in Python if `TcpListener::bind()` r
151157
types so `try!` macro or `?` operator can be used.
152158

153159
```rust
160+
# extern crate pyo3;
154161
use pyo3::prelude::*;
155162

156163
fn parse_int(s: String) -> PyResult<usize> {
@@ -168,6 +175,7 @@ It is possible to use exception defined in python code as native rust types.
168175
for that exception.
169176

170177
```rust
178+
# extern crate pyo3;
171179
use pyo3::prelude::*;
172180
use pyo3::import_exception;
173181

guide/src/function.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ the function to a [module](./module.md)
66
One way is defining the function in the module definition.
77

88
```rust
9+
# extern crate pyo3;
910
use pyo3::prelude::*;
1011

1112
#[pymodule]
@@ -30,6 +31,7 @@ as first parameter, the function name as second and an instance of `Python`
3031
as third.
3132

3233
```rust
34+
# extern crate pyo3;
3335
use pyo3::prelude::*;
3436
use pyo3::wrap_pyfunction;
3537

@@ -60,6 +62,7 @@ built-ins are new in Python 3 — in Python 2, it is simply considered to be par
6062
of the doc-string.
6163

6264
```rust
65+
# extern crate pyo3;
6366
use pyo3::prelude::*;
6467

6568
/// add(a, b, /)
@@ -73,7 +76,7 @@ fn add(a: u64, b: u64) -> u64 {
7376
```
7477

7578
When annotated like this, signatures are also correctly displayed in IPython.
76-
```
79+
```ignore
7780
>>> pyo3_test.add?
7881
Signature: pyo3_test.add(a, b, /)
7982
Docstring: This function adds two unsigned 64-bit integers.

guide/src/get_started.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ features = ["extension-module"]
4545
**`src/lib.rs`**
4646

4747
```rust
48+
# extern crate pyo3;
4849
use pyo3::prelude::*;
4950
use pyo3::wrap_pyfunction;
5051

@@ -89,6 +90,7 @@ pyo3 = "0.5"
8990
Example program displaying the value of `sys.version`:
9091

9192
```rust
93+
# extern crate pyo3;
9294
use pyo3::prelude::*;
9395
use pyo3::types::PyDict;
9496

guide/src/module.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
As shown in the Getting Started chapter, you can create a module as follows:
44

55
```rust
6+
# extern crate pyo3;
67
use pyo3::prelude::*;
78

89
// add bindings to the generated python module
@@ -52,6 +53,7 @@ Which means that the above Python code will print `This module is implemented in
5253
In python, modules are first class objects. This means can store them as values or add them to dicts or other modules:
5354

5455
```rust
56+
# extern crate pyo3;
5557
use pyo3::prelude::*;
5658
use pyo3::{wrap_pyfunction, wrap_pymodule};
5759
use pyo3::types::PyDict;

guide/src/rust-cpython.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ py_class!(class MyClass |py| {
2525
**pyo3**
2626

2727
```rust
28+
# extern crate pyo3;
2829
use pyo3::prelude::*;
2930
use pyo3::PyRawObject;
3031

tests/test_doc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ fn assert_file<P: AsRef<Path>>(path: P) {
1414
doc.test_file(path.as_ref())
1515
}
1616

17-
#[ignore]
1817
#[test]
18+
#[cfg(feature = "test-doc")]
1919
fn test_guide() {
2020
let guide_path = PathBuf::from("guide").join("src");
2121
for entry in guide_path.read_dir().unwrap() {
2222
assert_file(entry.unwrap().path())
2323
}
2424
}
2525

26-
#[ignore]
2726
#[test]
27+
#[cfg(feature = "test-doc")]
2828
fn test_readme() {
2929
assert_file("README.md")
3030
}

0 commit comments

Comments
 (0)