Skip to content

Commit e5c6458

Browse files
committed
intern strings
1 parent 2327f36 commit e5c6458

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/random.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use std::ptr::NonNull;
5252

5353
use pyo3::{
5454
exceptions::PyRuntimeError,
55-
ffi,
55+
ffi, intern,
5656
prelude::*,
5757
sync::GILOnceCell,
5858
types::{DerefToPyAny, PyCapsule, PyType},
@@ -100,18 +100,21 @@ pub trait PyBitGeneratorMethods {
100100

101101
impl<'py> PyBitGeneratorMethods for Bound<'py, PyBitGenerator> {
102102
fn lock(&self) -> PyResult<PyBitGeneratorGuard> {
103-
let capsule = self.getattr("capsule")?.downcast_into::<PyCapsule>()?;
104-
let lock = self.getattr("lock")?;
103+
let py = self.py();
104+
let capsule = self
105+
.getattr(intern!(py, "capsule"))?
106+
.downcast_into::<PyCapsule>()?;
107+
let lock = self.getattr(intern!(py, "lock"))?;
105108
// we’re holding the GIL, so there’s no race condition checking the lock and acquiring it later.
106-
if lock.call_method0("locked")?.extract()? {
109+
if lock.call_method0(intern!(py, "locked"))?.extract()? {
107110
return Err(PyRuntimeError::new_err("BitGenerator is already locked"));
108111
}
109-
lock.call_method0("acquire")?;
112+
lock.call_method0(intern!(py, "acquire"))?;
110113

111114
assert_eq!(capsule.name()?, Some(ffi::c_str!("BitGenerator")));
112115
let ptr = capsule.pointer() as *mut bitgen_t;
113116
let Some(non_null) = NonNull::new(ptr) else {
114-
lock.call_method0("release")?;
117+
lock.call_method0(intern!(py, "release"))?;
115118
return Err(PyRuntimeError::new_err("Invalid BitGenerator capsule"));
116119
};
117120
Ok(PyBitGeneratorGuard {
@@ -146,7 +149,7 @@ impl Drop for PyBitGeneratorGuard {
146149
fn drop(&mut self) {
147150
// ignore errors. This includes when `try_release` was called manually.
148151
let _ = Python::with_gil(|py| -> PyResult<_> {
149-
self.lock.bind(py).call_method0("release")?;
152+
self.lock.bind(py).call_method0(intern!(py, "release"))?;
150153
Ok(())
151154
});
152155
}
@@ -157,7 +160,7 @@ impl Drop for PyBitGeneratorGuard {
157160
impl<'py> PyBitGeneratorGuard {
158161
/// Release the lock, allowing for checking for errors.
159162
pub fn release(self, py: Python<'py>) -> PyResult<()> {
160-
self.lock.bind(py).call_method0("release")?;
163+
self.lock.bind(py).call_method0(intern!(py, "release"))?;
161164
Ok(())
162165
}
163166

0 commit comments

Comments
 (0)