Skip to content

Commit c8172e9

Browse files
authored
Merge pull request #1356 from davidhewitt/integration-tests-faq
readme/faq: document `rlib` crate type better
2 parents c021a01 + c1c4a5f commit c8172e9

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ edition = "2018"
4545

4646
[lib]
4747
name = "string_sum"
48+
# "cdylib" is necessary to produce a shared library for Python to import from.
49+
#
50+
# Downstream Rust code (including code in `bin/`, `examples/`, and `tests/`) will not be able
51+
# to `use string_sum;` unless the "rlib" or "lib" crate type is also included, e.g.:
52+
# crate-type = ["cdylib", "rlib"]
4853
crate-type = ["cdylib"]
4954

5055
[dependencies.pyo3]
@@ -91,11 +96,6 @@ rustflags = [
9196

9297
While developing, you can symlink (or copy) and rename the shared library from the target folder: On MacOS, rename `libstring_sum.dylib` to `string_sum.so`, on Windows `libstring_sum.dll` to `string_sum.pyd`, and on Linux `libstring_sum.so` to `string_sum.so`. Then open a Python shell in the same folder and you'll be able to `import string_sum`.
9398

94-
Adding the `cdylib` arguments in the `Cargo.toml` files changes the way your crate is compiled.
95-
Other Rust projects using your crate will have to link against the `.so` or `.pyd` file rather than include your library directly as normal.
96-
In order to make available your crate in the usual way for Rust user, you you might want to consider using both `crate-type = ["cdylib", "rlib"]` so that Rust users can use the `rlib` (the default lib crate type).
97-
Another possibility is to create a new crate to perform the binding.
98-
9999
To build, test and publish your crate as a Python module, you can use [maturin](https://github.com/PyO3/maturin) or [setuptools-rust](https://github.com/PyO3/setuptools-rust). You can find an example for setuptools-rust in [examples/word-count](https://github.com/PyO3/pyo3/tree/master/examples/word-count), while maturin should work on your crate without any configuration.
100100

101101
## Using Python from Rust

guide/src/faq.md

+23
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,29 @@ extension-module = ["pyo3/extension-module"]
2828
default = ["extension-module"]
2929
```
3030

31+
## I can't run `cargo test`: my crate cannot be found for tests in `tests/` directory!
32+
33+
The Rust book suggests to [put integration tests inside a `tests/` directory](https://doc.rust-lang.org/book/ch11-03-test-organization.html#integration-tests).
34+
35+
For a PyO3 `extension-module` project where the `crate-type` is set to `"cdylib"` in your `Cargo.toml`,
36+
the compiler won't be able to find your crate and will display errors such as `E0432` or `E0463`:
37+
38+
```
39+
error[E0432]: unresolved import `my_crate`
40+
--> tests/test_my_crate.rs:1:5
41+
|
42+
1 | use my_crate;
43+
| ^^^^^^^^^^^^ no external crate `my_crate`
44+
```
45+
46+
The best solution is to make your crate types include both `rlib` and `cdylib`:
47+
48+
```
49+
# Cargo.toml
50+
[lib]
51+
crate-type = ["cdylib", "rlib"]
52+
```
53+
3154
## Ctrl-C doesn't do anything while my Rust code is executing!
3255

3356
This is because Ctrl-C raises a SIGINT signal, which is handled by the calling Python process by simply setting a flag to action upon later. This flag isn't checked while Rust code called from Python is executing, only once control returns to the Python interpreter.

src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
//!
5050
//! [lib]
5151
//! name = "string_sum"
52+
//! # "cdylib" is necessary to produce a shared library for Python to import from.
53+
//! #
54+
//! # Downstream Rust code (including code in `bin/`, `examples/`, and `tests/`) will not be able
55+
//! # to `use string_sum;` unless the "rlib" or "lib" crate type is also included, e.g.:
56+
//! # crate-type = ["cdylib", "rlib"]
5257
//! crate-type = ["cdylib"]
5358
//!
5459
//! [dependencies.pyo3]

0 commit comments

Comments
 (0)