Skip to content

Commit ffb66a3

Browse files
authored
Merge pull request #454 from Alexander-N/doctest
Use doc-comment to test guide and readme
2 parents 7149a1f + faa3fed commit ffb66a3

16 files changed

+25
-89
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ matrix:
1212
include:
1313
- name: Python 3.5
1414
python: "3.5"
15-
env: FEATURES="test-doc"
1615
- name: Python 3.6
1716
python: "3.6"
1817
- name: Python 3.7

Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ pyo3cls = { path = "pyo3cls", version = "=0.7.0-alpha.1" }
2626
mashup = "0.1.9"
2727
num-complex = { version = "0.2.1", optional = true }
2828
inventory = "0.1.3"
29+
doc-comment = "0.3"
2930

3031
[dev-dependencies]
3132
assert_approx_eq = "1.1.0"
32-
docmatic = "0.1.2"
3333
indoc = "0.3.1"
3434

3535
[build-dependencies]
@@ -52,9 +52,6 @@ extension-module = []
5252
# are welcome.
5353
# abi3 = []
5454

55-
# Use this feature to test the examples in the user guide
56-
test-doc = []
57-
5855
[workspace]
5956
members = [
6057
"pyo3cls",

Contributing.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ Here are some things you should check for submitting your pull request:
99
- If applicable, add an entry in the changelog.
1010
- If applicable, add documentation to all new items and extend the guide.
1111
- If applicable, add tests for all new or fixed functions
12-
- Run `cargo test` or if you changed examples in README or the guide,
13-
`cargo test --features test-doc` (you might have to clean out your
14-
`target` directory if it complains about multiple matching libs)
12+
- Run `cargo test` or if you changed examples in README or the guide
1513

1614
You might want to run `tox` (`pip install tox`) locally to check compatibility with all supported python versions. If you're using linux or mac you might find the Makefile helpful for testing.

README.md

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

5555
```rust
56-
// Not required when using Rust 2018
57-
extern crate pyo3;
58-
5956
use pyo3::prelude::*;
6057
use pyo3::wrap_pyfunction;
6158

@@ -100,9 +97,6 @@ pyo3 = "0.7.0-alpha.1"
10097
Example program displaying the value of `sys.version`:
10198

10299
```rust
103-
// Not required when using Rust 2018
104-
extern crate pyo3;
105-
106100
use pyo3::prelude::*;
107101
use pyo3::types::IntoPyDict;
108102

ci/travis/test.sh

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

4-
cargo clean
5-
64
# run `cargo test` only if testing against cpython.
75
if ! [[ $FEATURES == *"pypy"* ]]; then
86
cargo test --features "$FEATURES num-complex"

guide/src/SUMMARY.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
- [Parallelism](parallelism.md)
1010
- [Debugging](debugging.md)
1111
- [Advanced Topics](advanced.md)
12-
- [Building and Distribution](building-and-distribution.md)
12+
- [Building and Distribution](building_and_distribution.md)
1313
- [PyPy support](pypy.md)
14-
- [Appendix: Pyo3 and rust-cpython](rust-cpython.md)
14+
- [Appendix: PyO3 and rust-cpython](rust_cpython.md)

guide/src/class.md

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ 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;
109
# use pyo3::prelude::*;
1110

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

3736
You can use `PyRef` just like `&T`, because it implements `Deref<Target=T>`.
3837
```rust
39-
# extern crate pyo3;
4038
# use pyo3::prelude::*;
4139
# use pyo3::types::PyDict;
4240
#[pyclass]
@@ -56,7 +54,6 @@ dict.set_item("obj", obj).unwrap();
5654
### `PyRefMut`
5755
`PyRefMut` is a mutable version of `PyRef`.
5856
```rust
59-
# extern crate pyo3;
6057
# use pyo3::prelude::*;
6158
#[pyclass]
6259
struct MyClass {
@@ -74,7 +71,6 @@ obj.num = 5;
7471

7572
You can use it to avoid lifetime problems.
7673
```rust
77-
# extern crate pyo3;
7874
# use pyo3::prelude::*;
7975
#[pyclass]
8076
struct MyClass {
@@ -113,7 +109,6 @@ To declare a constructor, you need to define a class method and annotate it with
113109
attribute. Only the python `__new__` method can be specified, `__init__` is not available.
114110

115111
```rust
116-
# extern crate pyo3;
117112
# use pyo3::prelude::*;
118113
# use pyo3::PyRawObject;
119114
#[pyclass]
@@ -155,7 +150,6 @@ By default `PyObject` is used as default base class. To override default base cl
155150
with value of custom class struct. Subclass must call parent's `new` method.
156151

157152
```rust,ignore
158-
# extern crate pyo3;
159153
# use pyo3::prelude::*;
160154
# use pyo3::PyRawObject;
161155
#[pyclass]
@@ -205,7 +199,6 @@ Descriptor methods can be defined in
205199
attributes. i.e.
206200

207201
```rust
208-
# extern crate pyo3;
209202
# use pyo3::prelude::*;
210203
# #[pyclass]
211204
# struct MyClass {
@@ -229,8 +222,7 @@ If function name starts with `get_` or `set_` for getter or setter respectively.
229222
Descriptor name becomes function name with prefix removed. This is useful in case of
230223
rust's special keywords like `type`.
231224

232-
```rust,ignore
233-
# extern crate pyo3;
225+
```rust
234226
# use pyo3::prelude::*;
235227
# #[pyclass]
236228
# struct MyClass {
@@ -258,8 +250,7 @@ In this case property `num` is defined. And it is available from python code as
258250
Also both `#[getter]` and `#[setter]` attributes accepts one parameter.
259251
If this parameter is specified, it is used as a property name. i.e.
260252

261-
```rust,ignore
262-
# extern crate pyo3;
253+
```rust
263254
# use pyo3::prelude::*;
264255
# #[pyclass]
265256
# struct MyClass {
@@ -286,8 +277,7 @@ In this case the property `number` is defined and is available from python code
286277

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

289-
```rust,ignore
290-
# extern crate pyo3;
280+
```rust
291281
# use pyo3::prelude::*;
292282
#[pyclass]
293283
struct MyClass {
@@ -305,7 +295,6 @@ To define a python compatible method, `impl` block for struct has to be annotate
305295
block with some variations, like descriptors, class method static methods, etc.
306296

307297
```rust
308-
# extern crate pyo3;
309298
# use pyo3::prelude::*;
310299
# #[pyclass]
311300
# struct MyClass {
@@ -333,7 +322,6 @@ The return type must be `PyResult<T>` for some `T` that implements `IntoPyObject
333322
get injected by method wrapper. i.e
334323

335324
```rust
336-
# extern crate pyo3;
337325
# use pyo3::prelude::*;
338326
# #[pyclass]
339327
# struct MyClass {
@@ -357,7 +345,6 @@ To specify a class method for a custom class, the method needs to be annotated
357345
with the `#[classmethod]` attribute.
358346

359347
```rust
360-
# extern crate pyo3;
361348
# use pyo3::prelude::*;
362349
# use pyo3::types::PyType;
363350
# #[pyclass]
@@ -390,7 +377,6 @@ To specify a static method for a custom class, method needs to be annotated with
390377
`IntoPyObject`.
391378

392379
```rust
393-
# extern crate pyo3;
394380
# use pyo3::prelude::*;
395381
# #[pyclass]
396382
# struct MyClass {
@@ -413,7 +399,6 @@ To specify a custom `__call__` method for a custom class, call methods need to b
413399
the `#[call]` attribute. Arguments of the method are specified same as for instance method.
414400

415401
```rust
416-
# extern crate pyo3;
417402
# use pyo3::prelude::*;
418403
use pyo3::types::PyTuple;
419404
# #[pyclass]
@@ -456,7 +441,6 @@ Each parameter could be one of following type:
456441

457442
Example:
458443
```rust
459-
# extern crate pyo3;
460444
# use pyo3::prelude::*;
461445
use pyo3::types::{PyDict, PyTuple};
462446
#

guide/src/conversions.md

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

2626
```rust
27-
# extern crate pyo3;
2827
use pyo3::prelude::*;
2928
use pyo3::types::{PyDict, PyTuple};
3029

@@ -62,7 +61,6 @@ fn main() {
6261
[`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.
6362

6463
```rust
65-
# extern crate pyo3;
6664
use pyo3::prelude::*;
6765
use pyo3::types::{IntoPyDict, PyDict};
6866
use std::collections::HashMap;

guide/src/exception.md

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

77
```rust
8-
# extern crate pyo3;
98
use pyo3::create_exception;
109

1110
create_exception!(module, MyError, pyo3::exceptions::Exception);
@@ -17,7 +16,6 @@ create_exception!(module, MyError, pyo3::exceptions::Exception);
1716
For example:
1817

1918
```rust
20-
# extern crate pyo3;
2119
use pyo3::prelude::*;
2220
use pyo3::create_exception;
2321
use pyo3::types::IntoPyDict;
@@ -40,7 +38,6 @@ fn main() {
4038
To raise an exception, first you need to obtain an exception type and construct a new [`PyErr`](https://docs.rs/pyo3/0.7.0-alpha.1/struct.PyErr.html), then call [`PyErr::restore()`](https://docs.rs/pyo3/0.7.0-alpha.1/struct.PyErr.html#method.restore) method to write the exception back to the Python interpreter's global state.
4139

4240
```rust
43-
# extern crate pyo3;
4441
use pyo3::{Python, PyErr};
4542
use pyo3::exceptions;
4643

@@ -65,7 +62,6 @@ has corresponding rust type, exceptions defined by `create_exception!` and `impo
6562
have rust type as well.
6663

6764
```rust
68-
# extern crate pyo3;
6965
# use pyo3::exceptions;
7066
# use pyo3::prelude::*;
7167
# fn check_for_error() -> bool {false}
@@ -84,7 +80,6 @@ Python has an [`isinstance`](https://docs.python.org/3/library/functions.html#is
8480
in `PyO3` there is a [`Python::is_instance()`](https://docs.rs/pyo3/0.7.0-alpha.1/struct.Python.html#method.is_instance) method which does the same thing.
8581

8682
```rust
87-
# extern crate pyo3;
8883
use pyo3::Python;
8984
use pyo3::types::{PyBool, PyList};
9085

@@ -103,7 +98,6 @@ fn main() {
10398
To check the type of an exception, you can simply do:
10499

105100
```rust
106-
# extern crate pyo3;
107101
# use pyo3::exceptions;
108102
# use pyo3::prelude::*;
109103
# fn main() {
@@ -155,7 +149,6 @@ The code snippet above will raise `OSError` in Python if `TcpListener::bind()` r
155149
types so `try!` macro or `?` operator can be used.
156150

157151
```rust
158-
# extern crate pyo3;
159152
use pyo3::prelude::*;
160153

161154
fn parse_int(s: String) -> PyResult<usize> {
@@ -173,7 +166,6 @@ It is possible to use exception defined in python code as native rust types.
173166
for that exception.
174167

175168
```rust
176-
# extern crate pyo3;
177169
use pyo3::prelude::*;
178170
use pyo3::import_exception;
179171

guide/src/function.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ 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;
109
use pyo3::prelude::*;
1110

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

3332
```rust
34-
# extern crate pyo3;
3533
use pyo3::prelude::*;
3634
use pyo3::wrap_pyfunction;
3735

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

6462
```rust
65-
# extern crate pyo3;
6663
use pyo3::prelude::*;
6764

6865
/// add(a, b, /)

guide/src/get_started.md

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

4747
```rust
48-
# extern crate pyo3;
4948
use pyo3::prelude::*;
5049
use pyo3::wrap_pyfunction;
5150

@@ -90,7 +89,6 @@ pyo3 = "0.6"
9089
Example program displaying the value of `sys.version`:
9190

9291
```rust
93-
# extern crate pyo3;
9492
use pyo3::prelude::*;
9593
use pyo3::types::IntoPyDict;
9694

guide/src/module.md

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

55
```rust
6-
# extern crate pyo3;
76
use pyo3::prelude::*;
87

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

5554
```rust
56-
# extern crate pyo3;
5755
use pyo3::prelude::*;
5856
use pyo3::{wrap_pyfunction, wrap_pymodule};
5957
use pyo3::types::IntoPyDict;

guide/src/rust-cpython.md renamed to guide/src/rust_cpython.md

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

2727
```rust
28-
# extern crate pyo3;
2928
use pyo3::prelude::*;
3029
use pyo3::PyRawObject;
3130

src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,24 @@
116116
//! Ok(())
117117
//! }
118118
//! ```
119+
use doc_comment::doctest;
120+
121+
// Test readme and user guide
122+
doctest!("../README.md", readme_md);
123+
doctest!("../guide/src/advanced.md", guide_advanced_md);
124+
doctest!(
125+
"../guide/src/building_and_distribution.md",
126+
guide_building_and_distribution_md
127+
);
128+
doctest!("../guide/src/class.md", guide_class_md);
129+
doctest!("../guide/src/conversions.md", guide_conversions_md);
130+
doctest!("../guide/src/debugging.md", guide_debugging_md);
131+
doctest!("../guide/src/exception.md", guide_exception_md);
132+
doctest!("../guide/src/function.md", guide_function_md);
133+
doctest!("../guide/src/get_started.md", guide_get_started_md);
134+
doctest!("../guide/src/module.md", guide_module_md);
135+
doctest!("../guide/src/parallelism.md", guide_parallelism_md);
136+
doctest!("../guide/src/rust_cpython.md", guide_rust_cpython_md);
119137

120138
pub use crate::class::*;
121139
pub use crate::conversion::{

0 commit comments

Comments
 (0)