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
Copy file name to clipboardExpand all lines: CHANGELOG.md
+4Lines changed: 4 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8
8
9
9
## [Unreleased]
10
10
11
+
### Changed
12
+
13
+
- Allow `#[pyo3(crate = "...", text_signature = "...")]` options to be used directly in `#[pyclass(crate = "...", text_signature = "...")]`. [#2234](https://github.com/PyO3/pyo3/pull/2234)
14
+
11
15
### Fixed
12
16
13
17
- Considered `PYTHONFRAMEWORK` when cross compiling in order that on macos cross compiling against a [Framework bundle](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/FrameworkAnatomy.html) is considered shared. [#2233](https://github.com/PyO3/pyo3/pull/2233)
Copy file name to clipboardExpand all lines: guide/src/class.md
+12-19Lines changed: 12 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -195,22 +195,16 @@ Python::with_gil(|py|{
195
195
196
196
## Customizing the class
197
197
198
-
The `#[pyclass]` macro accepts the following parameters:
199
-
200
-
*`name="XXX"` - Set the class name shown in Python code. By default, the struct name is used as the class name.
201
-
*`freelist=XXX` - The `freelist` parameter adds support of free allocation list to custom class.
202
-
The performance improvement applies to types that are often created and deleted in a row,
203
-
so that they can benefit from a freelist. `XXX` is a number of items for the free list.
204
-
*`gc` - Classes with the `gc` parameter participate in Python garbage collection.
205
-
If a custom class contains references to other Python objects that can be collected, the [`PyGCProtocol`]({{#PYO3_DOCS_URL}}/pyo3/class/gc/trait.PyGCProtocol.html) trait has to be implemented.
206
-
*`weakref` - Adds support for Python weak references.
207
-
*`extends=BaseType` - Use a custom base class. The base `BaseType` must implement `PyTypeInfo`. `enum` pyclasses can't use a custom base class.
208
-
*`subclass` - Allows Python classes to inherit from this class. `enum` pyclasses can't be inherited from.
209
-
*`dict` - Adds `__dict__` support, so that the instances of this type have a dictionary containing arbitrary instance variables.
210
-
*`unsendable` - Making it safe to expose `!Send` structs to Python, where all object can be accessed
211
-
by multiple threads. A class marked with `unsendable` panics when accessed by another thread.
212
-
*`module="XXX"` - Set the name of the module the class will be shown as defined in. If not given, the class
213
-
will be a virtual member of the `builtins` module.
`#[pyclass]` can be used with the following parameters:
2
+
3
+
| Parameter | Description |
4
+
| :- | :- |
5
+
| <spanstyle="white-space: pre">`crate = "some::path"`</span> | Path to import the `pyo3` crate, if it's not accessible at `::pyo3`. |
6
+
|`dict`| Gives instances of this class an empty `__dict__` to store custom attributes. |
7
+
| <spanstyle="white-space: pre">`extends = BaseType`</span> | Use a custom baseclass. Defaults to [`PyAny`][params-1]|
8
+
| <spanstyle="white-space: pre">`freelist = N`</span> | Implements a [free list][params-2] of size N. This can improve performance for types that are often created and deleted in quick succession. Profile your code to see whether `freelist` is right for you. |
9
+
| <spanstyle="white-space: pre">`module = "module_name"`</span> | Python code will see the class as being defined in this module. Defaults to `builtins`. |
10
+
| <spanstyle="white-space: pre">`name = "python_name"`</span> | Sets the name that Python sees this class as. Defaults to the name of the Rust struct. |
11
+
| <spanstyle="white-space: pre">`text_signature = "(arg1, arg2, ...)"`</span> | Sets the text signature for the Python class' `__new__` method. |
12
+
|`subclass`| Allows other Python classes and `#[pyclass]` to inherit from this class. Enums cannot be subclassed. |
13
+
|`unsendable`| Required if your struct is not [`Send`][params-3]. Rather than using `unsendable`, consider implementing your struct in a threadsafe way by e.g. substituting [`Rc`][params-4] with [`Arc`][params-5]. By using `unsendable`, your class will panic when accessed by another thread.|
14
+
|`weakref`| Allows this class to be [weakly referenceable][params-6]. |
15
+
16
+
All of these parameters can either be passed directly on the `#[pyclass(...)]` annotation, or as one or
17
+
more accompanying `#[pyo3(...)]` annotations, e.g.:
18
+
19
+
```rust,ignore
20
+
// Argument supplied directly to the `#[pyclass]` annotation.
/// A proc macro used to expose Rust structs and fieldless enums as Python objects.
83
83
///
84
-
/// `#[pyclass]` can be used the following [parameters][2]:
85
-
///
86
-
/// | Parameter | Description |
87
-
/// | :- | :- |
88
-
/// | <span style="white-space: pre">`crate = "some::path"`</span> | Path to import the `pyo3` crate, if it's not accessible at `::pyo3`. |
89
-
/// | `dict` | Gives instances of this class an empty `__dict__` to store custom attributes. |
90
-
/// | <span style="white-space: pre">`extends = BaseType`</span> | Use a custom baseclass. Defaults to [`PyAny`][4] |
91
-
/// | <span style="white-space: pre">`freelist = N`</span> | Implements a [free list][9] of size N. This can improve performance for types that are often created and deleted in quick succession. Profile your code to see whether `freelist` is right for you. |
92
-
/// | <span style="white-space: pre">`module = "module_name"`</span> | Python code will see the class as being defined in this module. Defaults to `builtins`. |
93
-
/// | <span style="white-space: pre">`name = "python_name"`</span> | Sets the name that Python sees this class as. Defaults to the name of the Rust struct. |
94
-
/// | <span style="white-space: pre">`text_signature = "(arg1, arg2, ...)"`</span> | Sets the text signature for the Python class' `__new__` method. |
95
-
/// | `subclass` | Allows other Python classes and `#[pyclass]` to inherit from this class. Enums cannot be subclassed. |
96
-
/// | `unsendable` | Required if your struct is not [`Send`][3]. Rather than using `unsendable`, consider implementing your struct in a threadsafe way by e.g. substituting [`Rc`][7] with [`Arc`][8]. By using `unsendable`, your class will panic when accessed by another thread.|
97
-
/// | `weakref` | Allows this class to be [weakly referenceable][6]. |
98
-
///
99
-
/// All of these parameters can either be passed directly on the `#[pyclass(...)]` annotation, or as one or
100
-
/// more accompanying `#[pyo3(...)]` annotations, e.g.:
101
-
///
102
-
/// ```rust,ignore
103
-
/// // Argument supplied directly to the `#[pyclass]` annotation.
104
-
/// #[pyclass(name = "SomeName", subclass)]
105
-
/// struct MyClass { }
106
-
///
107
-
/// // Argument supplied as a separate annotation.
0 commit comments