Skip to content

Commit

Permalink
Intern the __all__ and __name__ identifiers used by PyModule.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreichold committed Apr 4, 2022
1 parent 821b28d commit 5434bbc
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/types/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use crate::exceptions;
use crate::ffi;
use crate::pyclass::PyClass;
use crate::type_object::PyTypeObject;
use crate::types::PyCFunction;
use crate::types::{PyAny, PyDict, PyList};
use crate::types::{PyAny, PyCFunction, PyDict, PyList, PyString};
use crate::{AsPyPointer, IntoPy, PyObject, Python};
use std::ffi::{CStr, CString};
use std::str;
Expand Down Expand Up @@ -168,12 +167,13 @@ impl PyModule {
///
/// `__all__` declares the items that will be imported with `from my_module import *`.
pub fn index(&self) -> PyResult<&PyList> {
match self.getattr("__all__") {
let __all__ = __all__(self.py());
match self.getattr(__all__) {
Ok(idx) => idx.downcast().map_err(PyErr::from),
Err(err) => {
if err.is_instance_of::<exceptions::PyAttributeError>(self.py()) {
let l = PyList::empty(self.py());
self.setattr("__all__", l).map_err(PyErr::from)?;
self.setattr(__all__, l).map_err(PyErr::from)?;
Ok(l)
} else {
Err(err)
Expand Down Expand Up @@ -202,7 +202,6 @@ impl PyModule {
/// May fail if the module does not have a `__file__` attribute.
#[cfg(not(all(windows, PyPy)))]
pub fn filename(&self) -> PyResult<&str> {
use crate::types::PyString;
unsafe {
self.py()
.from_owned_ptr_or_err::<PyString>(ffi::PyModule_GetFilenameObject(self.as_ptr()))?
Expand Down Expand Up @@ -304,7 +303,7 @@ impl PyModule {
{
let py = self.py();
let function = wrapper(py).convert(py)?;
let name = function.getattr(py, "__name__")?;
let name = function.getattr(py, __name__(py))?;
let name = name.extract(py)?;
self.add(name, function)
}
Expand Down Expand Up @@ -389,11 +388,19 @@ impl PyModule {
/// [1]: crate::prelude::pyfunction
/// [2]: crate::wrap_pyfunction
pub fn add_function<'a>(&'a self, fun: &'a PyCFunction) -> PyResult<()> {
let name = fun.getattr("__name__")?.extract()?;
let name = fun.getattr(__name__(self.py()))?.extract()?;
self.add(name, fun)
}
}

fn __all__(py: Python<'_>) -> &PyString {
intern!(py, "__all__")
}

fn __name__(py: Python<'_>) -> &PyString {
intern!(py, "__name__")
}

#[cfg(test)]
mod tests {
use crate::{types::PyModule, Python};
Expand Down

0 comments on commit 5434bbc

Please sign in to comment.