Skip to content

Commit 925cce1

Browse files
committed
rename Python::with_gil to Python::attach
1 parent c7ef963 commit 925cce1

File tree

190 files changed

+1526
-1516
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

190 files changed

+1526
-1516
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ abi3-py314 = ["abi3", "pyo3-build-config/abi3-py314", "pyo3-ffi/abi3-py314"]
122122
# Automatically generates `python3.dll` import libraries for Windows targets.
123123
generate-import-lib = ["pyo3-ffi/generate-import-lib"]
124124

125-
# Changes `Python::with_gil` to automatically initialize the Python interpreter if needed.
125+
# Changes `Python::attach` to automatically initialize the Python interpreter if needed.
126126
auto-initialize = []
127127

128128
# Enables `Clone`ing references to Python objects `Py<T>` which panics if the GIL is not held.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ use pyo3::types::IntoPyDict;
152152
use pyo3::ffi::c_str;
153153

154154
fn main() -> PyResult<()> {
155-
Python::with_gil(|py| {
155+
Python::attach(|py| {
156156
let sys = py.import("sys")?;
157157
let version: String = sys.getattr("version")?.extract()?;
158158

examples/plugin/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1111
//import path for python
1212
let path = Path::new("./python_plugin/");
1313
//do useful work
14-
Python::with_gil(|py| {
14+
Python::attach(|py| {
1515
//add the current directory to import path of Python (do not use this in production!)
1616
let syspath: Bound<PyList> = py.import("sys")?.getattr("path")?.extract()?;
1717
syspath.insert(0, &path)?;

guide/src/async-await.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ However, there is an exception for method receivers, so async methods can accept
3939

4040
Even if it is not possible to pass a `py: Python<'py>` parameter to `async fn`, the GIL is still held during the execution of the future – it's also the case for regular `fn` without `Python<'py>`/`Bound<'py, PyAny>` parameter, yet the GIL is held.
4141

42-
It is still possible to get a `Python` marker using [`Python::with_gil`]({{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.with_gil); because `with_gil` is reentrant and optimized, the cost will be negligible.
42+
It is still possible to get a `Python` marker using [`Python::attach`]({{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.attach); because `attach` is reentrant and optimized, the cost will be negligible.
4343

4444
## Release the GIL across `.await`
4545

@@ -66,7 +66,7 @@ where
6666
6767
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
6868
let waker = cx.waker();
69-
Python::with_gil(|py| {
69+
Python::attach(|py| {
7070
py.allow_threads(|| pin!(&mut self.0).poll(&mut Context::from_waker(waker)))
7171
})
7272
}

guide/src/building-and-distribution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ If you encounter these or other complications when linking the interpreter stati
278278
279279
### Import your module when embedding the Python interpreter
280280
281-
When you run your Rust binary with an embedded interpreter, any `#[pymodule]` created modules won't be accessible to import unless added to a table called `PyImport_Inittab` before the embedded interpreter is initialized. This will cause Python statements in your embedded interpreter such as `import your_new_module` to fail. You can call the macro [`append_to_inittab`]({{#PYO3_DOCS_URL}}/pyo3/macro.append_to_inittab.html) with your module before initializing the Python interpreter to add the module function into that table. (The Python interpreter will be initialized by calling `prepare_freethreaded_python`, `with_embedded_python_interpreter`, or `Python::with_gil` with the [`auto-initialize`](features.md#auto-initialize) feature enabled.)
281+
When you run your Rust binary with an embedded interpreter, any `#[pymodule]` created modules won't be accessible to import unless added to a table called `PyImport_Inittab` before the embedded interpreter is initialized. This will cause Python statements in your embedded interpreter such as `import your_new_module` to fail. You can call the macro [`append_to_inittab`]({{#PYO3_DOCS_URL}}/pyo3/macro.append_to_inittab.html) with your module before initializing the Python interpreter to add the module function into that table. (The Python interpreter will be initialized by calling `prepare_freethreaded_python`, `with_embedded_python_interpreter`, or `Python::attach` with the [`auto-initialize`](features.md#auto-initialize) feature enabled.)
282282
283283
## Cross Compiling
284284

guide/src/building-and-distribution/multiple-python-versions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ PyO3 provides the APIs [`Python::version()`] and [`Python::version_info()`] to q
9797
```rust
9898
use pyo3::Python;
9999

100-
Python::with_gil(|py| {
100+
Python::attach(|py| {
101101
// PyO3 supports Python 3.7 and up.
102102
assert!(py.version_info() >= (3, 7));
103103
assert!(py.version_info() >= (3, 7, 0));

guide/src/class.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ struct MyClass {
221221
#[pyo3(get)]
222222
num: i32,
223223
}
224-
Python::with_gil(|py| {
224+
Python::attach(|py| {
225225
let obj = Bound::new(py, MyClass { num: 3 }).unwrap();
226226
{
227227
let obj_ref = obj.borrow(); // Get PyRef
@@ -253,12 +253,12 @@ struct MyClass {
253253
}
254254

255255
fn return_myclass() -> Py<MyClass> {
256-
Python::with_gil(|py| Py::new(py, MyClass { num: 1 }).unwrap())
256+
Python::attach(|py| Py::new(py, MyClass { num: 1 }).unwrap())
257257
}
258258

259259
let obj = return_myclass();
260260

261-
Python::with_gil(move |py| {
261+
Python::attach(move |py| {
262262
let bound = obj.bind(py); // Py<MyClass>::bind returns &Bound<'py, MyClass>
263263
let obj_ref = bound.borrow(); // Get PyRef<T>
264264
assert_eq!(obj_ref.num, 1);
@@ -280,7 +280,7 @@ struct FrozenCounter {
280280
value: AtomicUsize,
281281
}
282282

283-
let py_counter: Py<FrozenCounter> = Python::with_gil(|py| {
283+
let py_counter: Py<FrozenCounter> = Python::attach(|py| {
284284
let counter = FrozenCounter {
285285
value: AtomicUsize::new(0),
286286
};
@@ -290,7 +290,7 @@ let py_counter: Py<FrozenCounter> = Python::with_gil(|py| {
290290

291291
py_counter.get().value.fetch_add(1, Ordering::Relaxed);
292292

293-
Python::with_gil(move |_py| drop(py_counter));
293+
Python::attach(move |_py| drop(py_counter));
294294
```
295295

296296
Frozen classes are likely to become the default thereby guiding the PyO3 ecosystem towards a more deliberate application of interior mutability. Eventually, this should enable further optimizations of PyO3's internals and avoid downstream code paying the cost of interior mutability when it is not actually required.
@@ -423,7 +423,7 @@ impl SubSubClass {
423423
}
424424
}
425425
}
426-
# Python::with_gil(|py| {
426+
# Python::attach(|py| {
427427
# let subsub = pyo3::Py::new(py, SubSubClass::new()).unwrap();
428428
# pyo3::py_run!(py, subsub, "assert subsub.method1() == 10");
429429
# pyo3::py_run!(py, subsub, "assert subsub.method2() == 150");
@@ -473,7 +473,7 @@ impl DictWithCounter {
473473
dict.set_item(key, value)
474474
}
475475
}
476-
# Python::with_gil(|py| {
476+
# Python::attach(|py| {
477477
# let cnt = pyo3::Py::new(py, DictWithCounter::new()).unwrap();
478478
# pyo3::py_run!(py, cnt, "cnt.set('abc', 10); assert cnt['abc'] == 10")
479479
# });
@@ -529,7 +529,7 @@ impl MyDict {
529529

530530
// some custom methods that use `private` here...
531531
}
532-
# Python::with_gil(|py| {
532+
# Python::attach(|py| {
533533
# let cls = py.get_type::<MyDict>();
534534
# pyo3::py_run!(py, cls, "cls(a=1, b=2)")
535535
# });
@@ -800,7 +800,7 @@ impl MyClass {
800800
}
801801
}
802802
803-
Python::with_gil(|py| {
803+
Python::attach(|py| {
804804
let my_class = py.get_type::<MyClass>();
805805
pyo3::py_run!(py, my_class, "assert my_class.my_attribute == 'hello'")
806806
});
@@ -973,7 +973,7 @@ impl MyClass {
973973
}
974974
#
975975
# fn main() -> PyResult<()> {
976-
# Python::with_gil(|py| {
976+
# Python::attach(|py| {
977977
# let inspect = PyModule::import(py, "inspect")?.getattr("signature")?;
978978
# let module = PyModule::new(py, "my_module")?;
979979
# module.add_class::<MyClass>()?;
@@ -1096,7 +1096,7 @@ enum MyEnum {
10961096
OtherVariant,
10971097
}
10981098

1099-
Python::with_gil(|py| {
1099+
Python::attach(|py| {
11001100
let x = Py::new(py, MyEnum::Variant).unwrap();
11011101
let y = Py::new(py, MyEnum::OtherVariant).unwrap();
11021102
let cls = py.get_type::<MyEnum>();
@@ -1119,7 +1119,7 @@ enum MyEnum {
11191119
OtherVariant = 10,
11201120
}
11211121

1122-
Python::with_gil(|py| {
1122+
Python::attach(|py| {
11231123
let cls = py.get_type::<MyEnum>();
11241124
let x = MyEnum::Variant as i32; // The exact value is assigned by the compiler.
11251125
pyo3::py_run!(py, cls x, r#"
@@ -1140,7 +1140,7 @@ enum MyEnum{
11401140
OtherVariant,
11411141
}
11421142

1143-
Python::with_gil(|py| {
1143+
Python::attach(|py| {
11441144
let cls = py.get_type::<MyEnum>();
11451145
let x = Py::new(py, MyEnum::Variant).unwrap();
11461146
pyo3::py_run!(py, cls x, r#"
@@ -1167,7 +1167,7 @@ impl MyEnum {
11671167
}
11681168
}
11691169

1170-
Python::with_gil(|py| {
1170+
Python::attach(|py| {
11711171
let cls = py.get_type::<MyEnum>();
11721172
pyo3::py_run!(py, cls, "assert repr(cls.Answer) == '42'")
11731173
})
@@ -1184,7 +1184,7 @@ enum MyEnum {
11841184
Variant,
11851185
}
11861186

1187-
Python::with_gil(|py| {
1187+
Python::attach(|py| {
11881188
let x = Py::new(py, MyEnum::Variant).unwrap();
11891189
let cls = py.get_type::<MyEnum>();
11901190
pyo3::py_run!(py, x cls, r#"
@@ -1207,7 +1207,7 @@ enum MyEnum{
12071207
C,
12081208
}
12091209

1210-
Python::with_gil(|py| {
1210+
Python::attach(|py| {
12111211
let cls = py.get_type::<MyEnum>();
12121212
let a = Py::new(py, MyEnum::A).unwrap();
12131213
let b = Py::new(py, MyEnum::B).unwrap();
@@ -1263,7 +1263,7 @@ enum Shape {
12631263
}
12641264

12651265
# #[cfg(Py_3_10)]
1266-
Python::with_gil(|py| {
1266+
Python::attach(|py| {
12671267
let circle = Shape::Circle { radius: 10.0 }.into_pyobject(py)?;
12681268
let square = Shape::RegularPolygon(4, 10.0).into_pyobject(py)?;
12691269
let cls = py.get_type::<Shape>();
@@ -1305,7 +1305,7 @@ enum MyEnum {
13051305
Variant { i: i32 },
13061306
}
13071307

1308-
Python::with_gil(|py| {
1308+
Python::attach(|py| {
13091309
let x = Py::new(py, MyEnum::Variant { i: 42 }).unwrap();
13101310
let cls = py.get_type::<MyEnum>();
13111311
pyo3::py_run!(py, x cls, r#"
@@ -1332,7 +1332,7 @@ enum Shape {
13321332
}
13331333

13341334
# #[cfg(Py_3_10)]
1335-
Python::with_gil(|py| {
1335+
Python::attach(|py| {
13361336
let cls = py.get_type::<Shape>();
13371337
pyo3::py_run!(py, cls, r#"
13381338
circle = cls.Circle()
@@ -1452,7 +1452,7 @@ impl pyo3::impl_::pyclass::PyClassImpl for MyClass {
14521452
}
14531453
}
14541454

1455-
# Python::with_gil(|py| {
1455+
# Python::attach(|py| {
14561456
# let cls = py.get_type::<MyClass>();
14571457
# pyo3::py_run!(py, cls, "assert cls.__name__ == 'MyClass'")
14581458
# });

guide/src/class/numeric.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
385385
# use pyo3::PyTypeInfo;
386386
#
387387
# fn main() -> PyResult<()> {
388-
# Python::with_gil(|py| -> PyResult<()> {
388+
# Python::attach(|py| -> PyResult<()> {
389389
# let globals = PyModule::import(py, "__main__")?.dict();
390390
# globals.set_item("Number", Number::type_object(py))?;
391391
#

guide/src/class/object.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ impl Number {
277277
}
278278

279279
# fn main() -> PyResult<()> {
280-
# Python::with_gil(|py| {
280+
# Python::attach(|py| {
281281
# let x = &Bound::new(py, Number(4))?;
282282
# let y = &Bound::new(py, Number(4))?;
283283
# assert!(x.eq(y)?);

guide/src/class/protocols.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ impl Container {
215215
}
216216
}
217217
218-
# Python::with_gil(|py| {
218+
# Python::attach(|py| {
219219
# let container = Container { iter: vec![1, 2, 3, 4] };
220220
# let inst = pyo3::Py::new(py, container).unwrap();
221221
# pyo3::py_run!(py, inst, "assert list(inst) == [1, 2, 3, 4]");
@@ -458,7 +458,7 @@ impl ClassWithGCSupport {
458458

459459
Usually, an implementation of `__traverse__` should do nothing but calls to `visit.call`.
460460
Most importantly, safe access to the GIL is prohibited inside implementations of `__traverse__`,
461-
i.e. `Python::with_gil` will panic.
461+
i.e. `Python::attach` will panic.
462462

463463
> Note: these methods are part of the C API, PyPy does not necessarily honor them. If you are building for PyPy you should measure memory consumption to make sure you do not have runaway memory growth. See [this issue on the PyPy bug tracker](https://github.com/pypy/pypy/issues/3848).
464464

0 commit comments

Comments
 (0)