Skip to content

Commit 4d54a10

Browse files
authored
use intern! in rust code (#292)
1 parent 40fb291 commit 4d54a10

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

src/lib.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use std::sync::{Arc, Mutex};
88
use std::thread::sleep;
99
use std::time::{Duration, SystemTime};
1010

11-
use pyo3::create_exception;
12-
use pyo3::exceptions::{PyFileNotFoundError, PyOSError, PyPermissionError, PyRuntimeError, PyTypeError};
11+
use pyo3::exceptions::{PyFileNotFoundError, PyOSError, PyPermissionError, PyRuntimeError};
1312
use pyo3::prelude::*;
13+
use pyo3::{create_exception, intern};
1414

1515
use notify::event::{Event, EventKind, ModifyKind, RenameMode};
1616
use notify::{
@@ -243,7 +243,7 @@ impl RustNotify {
243243
})
244244
}
245245

246-
pub fn watch(
246+
fn watch(
247247
slf: &Bound<Self>,
248248
py: Python,
249249
debounce_ms: u64,
@@ -256,14 +256,7 @@ impl RustNotify {
256256
}
257257
let stop_event_is_set: Option<Bound<PyAny>> = match stop_event.is_none(py) {
258258
true => None,
259-
false => {
260-
let func = stop_event.getattr(py, "is_set")?.into_bound(py);
261-
if !func.is_callable() {
262-
return Err(PyTypeError::new_err("'stop_event.is_set' must be callable"));
263-
}
264-
265-
Some(func)
266-
}
259+
false => Some(stop_event.getattr(py, intern!(py, "is_set"))?.into_bound(py)),
267260
};
268261

269262
let mut max_debounce_time: Option<SystemTime> = None;
@@ -279,7 +272,7 @@ impl RustNotify {
279272
Ok(_) => (),
280273
Err(_) => {
281274
slf.borrow().clear();
282-
return Ok("signal".to_object(py));
275+
return Ok(intern!(py, "signal").into_py(py));
283276
}
284277
};
285278

@@ -288,13 +281,13 @@ impl RustNotify {
288281
return wf_error!(error.clone());
289282
}
290283

291-
if let Some(is_set) = stop_event_is_set.as_ref() {
284+
if let Some(is_set) = &stop_event_is_set {
292285
if is_set.call0()?.is_truthy()? {
293286
if slf.borrow().debug {
294287
eprintln!("stop event set, stopping...");
295288
}
296289
slf.borrow().clear();
297-
return Ok("stop".to_object(py));
290+
return Ok(intern!(py, "stop").into_py(py));
298291
}
299292
}
300293

@@ -316,7 +309,7 @@ impl RustNotify {
316309
} else if let Some(max_time) = max_timeout_time {
317310
if SystemTime::now() > max_time {
318311
slf.borrow().clear();
319-
return Ok("timeout".to_object(py));
312+
return Ok(intern!(py, "timeout").into_py(py));
320313
}
321314
}
322315
}
@@ -326,19 +319,19 @@ impl RustNotify {
326319
}
327320

328321
/// https://github.com/PyO3/pyo3/issues/1205#issuecomment-1164096251 for advice on `__enter__`
329-
pub fn __enter__(slf: Py<Self>) -> Py<Self> {
322+
fn __enter__(slf: Py<Self>) -> Py<Self> {
330323
slf
331324
}
332325

333-
pub fn close(&mut self) {
326+
fn close(&mut self) {
334327
self.watcher = WatcherEnum::None;
335328
}
336329

337-
pub fn __exit__(&mut self, _exc_type: PyObject, _exc_value: PyObject, _traceback: PyObject) {
330+
fn __exit__(&mut self, _exc_type: PyObject, _exc_value: PyObject, _traceback: PyObject) {
338331
self.close();
339332
}
340333

341-
pub fn __repr__(&self) -> PyResult<String> {
334+
fn __repr__(&self) -> PyResult<String> {
342335
Ok(format!("RustNotify({:#?})", self.watcher))
343336
}
344337
}

tests/test_rust_notify.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def test_wrong_type_event_is_set(test_dir: Path, time_taken):
212212
watcher = RustNotify([str(test_dir)], False, False, 0, True, False)
213213
event = type('BadEvent', (), {'is_set': 123})()
214214

215-
with pytest.raises(TypeError, match="'stop_event.is_set' must be callable"):
215+
with pytest.raises(TypeError, match="'int' object is not callable"):
216216
watcher.watch(100, 1, 500, event)
217217

218218

0 commit comments

Comments
 (0)