You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: use rumdl to make more sentences single-line (#5584)
* docs: use `rumdl` to make more sentences single-line
* Update guide/src/class/call.md
Co-authored-by: Lily <code@lilyf.org>
---------
Co-authored-by: Lily <code@lilyf.org>
Copy file name to clipboardExpand all lines: guide/pyclass-parameters.md
+1-2Lines changed: 1 addition & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,8 +28,7 @@
28
28
|`unsendable`| Required if your struct is not [`Send`][params-3]. Rather than using `unsendable`, consider implementing your struct in a thread-safe way by e.g. substituting [`Rc`][params-4] with [`Arc`][params-5]. By using `unsendable`, your class will panic when accessed by another thread. Also note the Python's GC is multi-threaded and while unsendable classes will not be traversed on foreign threads to avoid UB, this can lead to memory leaks. |
29
29
|`weakref`| Allows this class to be [weakly referenceable][params-6]. |
30
30
31
-
All of these parameters can either be passed directly on the `#[pyclass(...)]` annotation, or as one or
32
-
more accompanying `#[pyo3(...)]` annotations, e.g.:
31
+
All of these parameters can either be passed directly on the `#[pyclass(...)]` annotation, or as one or more accompanying `#[pyo3(...)]` annotations, e.g.:
33
32
34
33
```rust,ignore
35
34
// Argument supplied directly to the `#[pyclass]` annotation.
Copy file name to clipboardExpand all lines: guide/src/building-and-distribution.md
+3-7Lines changed: 3 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -255,11 +255,9 @@ As an advanced feature, you can build PyO3 wheel without calling Python interpre
255
255
Also, if the build host Python interpreter is not found or is too old or otherwise unusable, PyO3 will still attempt to compile `abi3` extension modules after displaying a warning message.
256
256
On Unix-like systems this works unconditionally; on Windows you must also set the `RUSTFLAGS` environment variable to contain `-L native=/path/to/python/libs` so that the linker can find `python3.lib`.
257
257
258
-
If the `python3.dll` import library is not available, an experimental `generate-import-lib` crate
259
-
feature may be enabled, and the required library will be created and used by PyO3 automatically.
258
+
If the `python3.dll` import library is not available, an experimental `generate-import-lib` crate feature may be enabled, and the required library will be created and used by PyO3 automatically.
260
259
261
-
*Note*: MSVC targets require LLVM binutils (`llvm-dlltool`) to be available in `PATH` for
262
-
the automatic import library generation feature to work.
260
+
*Note*: MSVC targets require LLVM binutils (`llvm-dlltool`) to be available in `PATH` for the automatic import library generation feature to work.
Any of the `abi3-py3*` features can be enabled instead of setting `PYO3_CROSS_PYTHON_VERSION` in the above examples.
397
395
398
-
`PYO3_CROSS_LIB_DIR` can often be omitted when cross compiling extension modules for Unix and macOS targets,
399
-
or when cross compiling extension modules for Windows and the experimental `generate-import-lib`
400
-
crate feature is enabled.
396
+
`PYO3_CROSS_LIB_DIR` can often be omitted when cross compiling extension modules for Unix and macOS targets, or when cross compiling extension modules for Windows and the experimental `generate-import-lib` crate feature is enabled.
401
397
402
398
The following resources may also be useful for cross-compiling:
Copy file name to clipboardExpand all lines: guide/src/class.md
+10-20Lines changed: 10 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -184,14 +184,11 @@ impl Nonzero {
184
184
}
185
185
```
186
186
187
-
If you want to return an existing object (for example, because your `new`
188
-
method caches the values it returns), `new` can return `pyo3::Py<Self>`.
187
+
If you want to return an existing object (for example, because your `new` method caches the values it returns), `new` can return `pyo3::Py<Self>`.
189
188
190
-
As you can see, the Rust method name is not important here; this way you can
191
-
still, use `new()` for a Rust-level constructor.
189
+
As you can see, the Rust method name is not important here; this way you can still, use `new()` for a Rust-level constructor.
192
190
193
-
If no method marked with `#[new]` is declared, object instances can only be
194
-
created from Rust, but not from Python.
191
+
If no method marked with `#[new]` is declared, object instances can only be created from Rust, but not from Python.
195
192
196
193
For arguments, see the [`Method arguments`](#method-arguments) section below.
197
194
@@ -327,8 +324,7 @@ These parameters are covered in various sections of this guide.
327
324
328
325
### Return type
329
326
330
-
Generally, `#[new]` methods have to return `T: Into<PyClassInitializer<Self>>` or
331
-
`PyResult<T> where T: Into<PyClassInitializer<Self>>`.
327
+
Generally, `#[new]` methods have to return `T: Into<PyClassInitializer<Self>>` or `PyResult<T> where T: Into<PyClassInitializer<Self>>`.
332
328
333
329
For constructors that may fail, you should wrap the return type in a PyResult as well.
334
330
Consult the table below to determine which type your constructor should return:
@@ -547,8 +543,7 @@ impl MyDict {
547
543
# }
548
544
```
549
545
550
-
Here, the `args` and `kwargs` allow creating instances of the subclass passing
551
-
initial items, such as `MyDict(item_sequence)` or `MyDict(a=1, b=2)`.
546
+
Here, the `args` and `kwargs` allow creating instances of the subclass passing initial items, such as `MyDict(item_sequence)` or `MyDict(a=1, b=2)`.
552
547
553
548
## Object properties
554
549
@@ -695,11 +690,9 @@ impl MyClass {
695
690
696
691
Both `&self` and `&mut self` can be used, due to the use of [runtime borrow checking](#bound-and-interior-mutability).
697
692
698
-
The return type must be `PyResult<T>` or `T` for some `T` that implements `IntoPyObject`;
699
-
the latter is allowed if the method cannot raise Python exceptions.
693
+
The return type must be `PyResult<T>` or `T` for some `T` that implements `IntoPyObject`; the latter is allowed if the method cannot raise Python exceptions.
700
694
701
-
A `Python` parameter can be specified as part of method signature, in this case the `py` argument
702
-
gets injected by the method wrapper, e.g.
695
+
A `Python` parameter can be specified as part of method signature, in this case the `py` argument gets injected by the method wrapper, e.g.
703
696
704
697
```rust
705
698
# usepyo3::prelude::*;
@@ -794,8 +787,7 @@ impl MyClass {
794
787
795
788
## Class attributes
796
789
797
-
To create a class attribute (also called [class variable][classattr]), a method without
798
-
any arguments can be annotated with the `#[classattr]` attribute.
790
+
To create a class attribute (also called [class variable][classattr]), a method without any arguments can be annotated with the `#[classattr]` attribute.
799
791
800
792
```rust,no_run
801
793
# use pyo3::prelude::*;
@@ -820,8 +812,7 @@ class creation.
820
812
821
813
> Note: `#[classattr]` does not work with [`#[pyo3(warn(...))]`](./function.md#warn) attribute.
822
814
823
-
If the class attribute is defined with `const` code only, one can also annotate associated
824
-
constants:
815
+
If the class attribute is defined with `const` code only, one can also annotate associated constants:
825
816
826
817
```rust,no_run
827
818
# use pyo3::prelude::*;
@@ -1054,8 +1045,7 @@ impl MyClass {
1054
1045
# }
1055
1046
```
1056
1047
1057
-
Note that `text_signature` on `#[new]` is not compatible with compilation in
1058
-
`abi3` mode until Python 3.10 or greater.
1048
+
Note that `text_signature` on `#[new]` is not compatible with compilation in `abi3` mode until Python 3.10 or greater.
Copy file name to clipboardExpand all lines: guide/src/class/object.md
+4-8Lines changed: 4 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,8 +25,7 @@ mod my_module {
25
25
}
26
26
```
27
27
28
-
At this point Python code can import the module, access the class and create class instances - but
29
-
nothing else.
28
+
At this point Python code can import the module, access the class and create class instances - but nothing else.
30
29
31
30
```python
32
31
from my_module import Number
@@ -99,8 +98,7 @@ It expands and is passed into the `format!` macro in the following ways:
99
98
-`"{0}"` -> `"{}", self.0`
100
99
-`"{x:?}"` -> `"{:?}", self.x`
101
100
102
-
*Note: Depending upon the format string you use, this may require implementation of the `Display` or `Debug` traits for the given Rust types.*
103
-
*Note: the pyclass args `name` and `rename_all` are incompatible with the shorthand format string and will raise a compile time error.*
101
+
*Note: Depending upon the format string you use, this may require implementation of the `Display` or `Debug` traits for the given Rust types.**Note: the pyclass args `name` and `rename_all` are incompatible with the shorthand format string and will raise a compile time error.*
104
102
105
103
```rust,no_run
106
104
# use pyo3::prelude::*;
@@ -239,8 +237,7 @@ impl Number {
239
237
}
240
238
```
241
239
242
-
If you obtain the result by comparing two Rust values, as in this example, you
243
-
can take a shortcut using `CompareOp::matches`:
240
+
If you obtain the result by comparing two Rust values, as in this example, you can take a shortcut using `CompareOp::matches`:
244
241
245
242
```rust,no_run
246
243
use pyo3::class::basic::CompareOp;
@@ -259,8 +256,7 @@ impl Number {
259
256
}
260
257
```
261
258
262
-
It checks that the `std::cmp::Ordering` obtained from Rust's `Ord` matches
263
-
the given `CompareOp`.
259
+
It checks that the `std::cmp::Ordering` obtained from Rust's `Ord` matches the given `CompareOp`.
264
260
265
261
Alternatively, you can implement just equality using `__eq__`:
For more details on Python's iteration protocols, check out [the "Iterator Types" section of the library documentation](https://docs.python.org/library/stdtypes.html#iterator-types).
238
237
239
238
#### Returning a value from iteration
240
239
@@ -363,11 +362,9 @@ This will help libraries such as `numpy` recognise the class as a sequence, howe
Copy file name to clipboardExpand all lines: guide/src/conversions/traits.md
+7-15Lines changed: 7 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,9 +20,7 @@ let v: Vec<i32> = list.extract()?;
20
20
# }
21
21
```
22
22
23
-
This method is available for many Python object types, and can produce a wide
24
-
variety of Rust types, which you can check out in the implementor list of
25
-
[`FromPyObject`].
23
+
This method is available for many Python object types, and can produce a wide variety of Rust types, which you can check out in the implementor list of [`FromPyObject`].
26
24
27
25
[`FromPyObject`] is also implemented for your own Rust types wrapped as Python objects (see [the chapter about classes](../class.md)).
28
26
There, in order to both be able to operate on mutable references *and* satisfy Rust's rules of non-aliasing mutable references, you have to extract the PyO3 reference wrappers [`PyRef`] and [`PyRefMut`].
@@ -36,8 +34,7 @@ Derivation for empty enums, enum variants and structs is not supported.
36
34
37
35
### Deriving [`FromPyObject`] for structs
38
36
39
-
The derivation generates code that will attempt to access the attribute `my_string` on
40
-
the Python object, i.e. `obj.getattr("my_string")`, and call `extract()` on the attribute.
37
+
The derivation generates code that will attempt to access the attribute `my_string` on the Python object, i.e. `obj.getattr("my_string")`, and call `extract()` on the attribute.
41
38
42
39
```rust
43
40
usepyo3::prelude::*;
@@ -447,8 +444,7 @@ enum RustyEnum {
447
444
# }
448
445
```
449
446
450
-
If the input is neither a string nor an integer, the error message will be:
451
-
`"'<INPUT_TYPE>' cannot be cast as 'str | int'"`.
447
+
If the input is neither a string nor an integer, the error message will be: `"'<INPUT_TYPE>' cannot be cast as 'str | int'"`.
@@ -560,16 +556,14 @@ All types in PyO3 implement this trait, as does a `#[pyclass]` which doesn't use
560
556
This trait defines a single method, `into_pyobject()`, which returns a [`Result`] with `Ok` and `Err` types depending on the input value.
561
557
For convenience, there is a companion [`IntoPyObjectExt`] trait which adds methods such as `into_py_any()` which converts the `Ok` and `Err` types to commonly used types (in the case of `into_py_any()`, `Py<PyAny>` and `PyErr` respectively).
562
558
563
-
Occasionally you may choose to implement this for custom types which are mapped to Python types
564
-
*without* having a unique python type.
559
+
Occasionally you may choose to implement this for custom types which are mapped to Python types *without* having a unique python type.
565
560
566
561
### derive macro
567
562
568
563
`IntoPyObject` can be implemented using our derive macro.
569
564
Both `struct`s and `enum`s are supported.
570
565
571
-
`struct`s will turn into a `PyDict` using the field names as keys, tuple `struct`s will turn convert
572
-
into `PyTuple` with the fields in declaration order.
566
+
`struct`s will turn into a `PyDict` using the field names as keys, tuple `struct`s will turn convert into `PyTuple` with the fields in declaration order.
Copy file name to clipboardExpand all lines: guide/src/debugging.md
+3-9Lines changed: 3 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -358,9 +358,7 @@ Automated ways to discover thread safety issues can often be more fruitful than
358
358
[ThreadSanitizer](https://clang.llvm.org/docs/ThreadSanitizer.html) is a thread safety checking runtime that can be used to detect data races triggered by thread safety bugs or incorrect use of thread-unsafe data structures.
359
359
While it can only detect data races triggered by code at runtime, if it does detect something the reports often point to exactly where the problem is happening.
360
360
361
-
To use `ThreadSanitizer` with a library that depends on PyO3, you will need to
362
-
install a nightly Rust toolchain, along with the `rust-src` component, since you
363
-
will need to compile the Rust standard library:
361
+
To use `ThreadSanitizer` with a library that depends on PyO3, you will need to install a nightly Rust toolchain, along with the `rust-src` component, since you will need to compile the Rust standard library:
You will also need a version of CPython compiled using LLVM/Clang with the same major version of LLVM as is currently used to compile nightly Rust.
372
370
As of March 2025, Rust nightly uses LLVM 20.
373
371
374
-
The [cpython_sanity docker images](https://github.com/nascheme/cpython_sanity)
375
-
contain a development environment with a pre-compiled version of CPython 3.13 or
376
-
3.14 as well as optionally NumPy and SciPy, all compiled using LLVM 20 and
377
-
ThreadSanitizer.
372
+
The [cpython_sanity docker images](https://github.com/nascheme/cpython_sanity) contain a development environment with a pre-compiled version of CPython 3.13 or 3.14 as well as optionally NumPy and SciPy, all compiled using LLVM 20 and ThreadSanitizer.
378
373
379
-
After activating a nightly Rust toolchain, you can build your project using
380
-
`ThreadSanitizer` with the following command:
374
+
After activating a nightly Rust toolchain, you can build your project using `ThreadSanitizer` with the following command:
0 commit comments