pyo3/types/module.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
use crate::callback::IntoPyCallbackOutput;
use crate::err::{PyErr, PyResult};
use crate::ffi_ptr_ext::FfiPtrExt;
use crate::py_result_ext::PyResultExt;
use crate::pyclass::PyClass;
use crate::types::{
any::PyAnyMethods, list::PyListMethods, PyAny, PyCFunction, PyDict, PyList, PyString,
};
use crate::{exceptions, ffi, Bound, IntoPy, Py, PyObject, Python};
use std::ffi::CString;
use std::str;
#[cfg(feature = "gil-refs")]
use {super::PyStringMethods, crate::PyNativeType};
/// Represents a Python [`module`][1] object.
///
/// Values of this type are accessed via PyO3's smart pointers, e.g. as
/// [`Py<PyModule>`][crate::Py] or [`Bound<'py, PyModule>`][Bound].
///
/// For APIs available on `module` objects, see the [`PyModuleMethods`] trait which is implemented for
/// [`Bound<'py, PyModule>`][Bound].
///
/// As with all other Python objects, modules are first class citizens.
/// This means they can be passed to or returned from functions,
/// created dynamically, assigned to variables and so forth.
///
/// [1]: https://docs.python.org/3/tutorial/modules.html
#[repr(transparent)]
pub struct PyModule(PyAny);
pyobject_native_type_core!(PyModule, pyobject_native_static_type_object!(ffi::PyModule_Type), #checkfunction=ffi::PyModule_Check);
impl PyModule {
/// Creates a new module object with the `__name__` attribute set to `name`.
///
/// # Examples
///
/// ``` rust
/// use pyo3::prelude::*;
///
/// # fn main() -> PyResult<()> {
/// Python::with_gil(|py| -> PyResult<()> {
/// let module = PyModule::new_bound(py, "my_module")?;
///
/// assert_eq!(module.name()?, "my_module");
/// Ok(())
/// })?;
/// # Ok(())}
/// ```
pub fn new_bound<'py>(py: Python<'py>, name: &str) -> PyResult<Bound<'py, PyModule>> {
// Could use PyModule_NewObject, but it doesn't exist on PyPy.
let name = CString::new(name)?;
unsafe {
ffi::PyModule_New(name.as_ptr())
.assume_owned_or_err(py)
.downcast_into_unchecked()
}
}
/// Imports the Python module with the specified name.
///
/// # Examples
///
/// ```no_run
/// # fn main() {
/// use pyo3::prelude::*;
///
/// Python::with_gil(|py| {
/// let module = PyModule::import_bound(py, "antigravity").expect("No flying for you.");
/// });
/// # }
/// ```
///
/// This is equivalent to the following Python expression:
/// ```python
/// import antigravity
/// ```
pub fn import_bound<N>(py: Python<'_>, name: N) -> PyResult<Bound<'_, PyModule>>
where
N: IntoPy<Py<PyString>>,
{
let name: Py<PyString> = name.into_py(py);
unsafe {
ffi::PyImport_Import(name.as_ptr())
.assume_owned_or_err(py)
.downcast_into_unchecked()
}
}
/// Creates and loads a module named `module_name`,
/// containing the Python code passed to `code`
/// and pretending to live at `file_name`.
///
/// <div class="information">
/// <div class="tooltip compile_fail" style="">⚠ ️</div>
/// </div><div class="example-wrap" style="display:inline-block"><pre class="compile_fail" style="white-space:normal;font:inherit;">
//
/// <strong>Warning</strong>: This will compile and execute code. <strong>Never</strong> pass untrusted code to this function!
///
/// </pre></div>
///
/// # Errors
///
/// Returns `PyErr` if:
/// - `code` is not syntactically correct Python.
/// - Any Python exceptions are raised while initializing the module.
/// - Any of the arguments cannot be converted to [`CString`]s.
///
/// # Example: bundle in a file at compile time with [`include_str!`][std::include_str]:
///
/// ```rust
/// use pyo3::prelude::*;
///
/// # fn main() -> PyResult<()> {
/// // This path is resolved relative to this file.
/// let code = include_str!("../../assets/script.py");
///
/// Python::with_gil(|py| -> PyResult<()> {
/// PyModule::from_code_bound(py, code, "example.py", "example")?;
/// Ok(())
/// })?;
/// # Ok(())
/// # }
/// ```
///
/// # Example: Load a file at runtime with [`std::fs::read_to_string`].
///
/// ```rust
/// use pyo3::prelude::*;
///
/// # fn main() -> PyResult<()> {
/// // This path is resolved by however the platform resolves paths,
/// // which also makes this less portable. Consider using `include_str`
/// // if you just want to bundle a script with your module.
/// let code = std::fs::read_to_string("assets/script.py")?;
///
/// Python::with_gil(|py| -> PyResult<()> {
/// PyModule::from_code_bound(py, &code, "example.py", "example")?;
/// Ok(())
/// })?;
/// Ok(())
/// # }
/// ```
pub fn from_code_bound<'py>(
py: Python<'py>,
code: &str,
file_name: &str,
module_name: &str,
) -> PyResult<Bound<'py, PyModule>> {
let data = CString::new(code)?;
let filename = CString::new(file_name)?;
let module = CString::new(module_name)?;
unsafe {
let code = ffi::Py_CompileString(data.as_ptr(), filename.as_ptr(), ffi::Py_file_input)
.assume_owned_or_err(py)?;
ffi::PyImport_ExecCodeModuleEx(module.as_ptr(), code.as_ptr(), filename.as_ptr())
.assume_owned_or_err(py)
.downcast_into()
}
}
}
#[cfg(feature = "gil-refs")]
impl PyModule {
/// Deprecated form of [`PyModule::new_bound`].
#[inline]
#[deprecated(
since = "0.21.0",
note = "`PyModule::new` will be replaced by `PyModule::new_bound` in a future PyO3 version"
)]
pub fn new<'py>(py: Python<'py>, name: &str) -> PyResult<&'py PyModule> {
Self::new_bound(py, name).map(Bound::into_gil_ref)
}
/// Deprecated form of [`PyModule::import_bound`].
#[inline]
#[deprecated(
since = "0.21.0",
note = "`PyModule::import` will be replaced by `PyModule::import_bound` in a future PyO3 version"
)]
pub fn import<N>(py: Python<'_>, name: N) -> PyResult<&PyModule>
where
N: IntoPy<Py<PyString>>,
{
Self::import_bound(py, name).map(Bound::into_gil_ref)
}
/// Deprecated form of [`PyModule::from_code_bound`].
#[inline]
#[deprecated(
since = "0.21.0",
note = "`PyModule::from_code` will be replaced by `PyModule::from_code_bound` in a future PyO3 version"
)]
pub fn from_code<'py>(
py: Python<'py>,
code: &str,
file_name: &str,
module_name: &str,
) -> PyResult<&'py PyModule> {
Self::from_code_bound(py, code, file_name, module_name).map(Bound::into_gil_ref)
}
/// Returns the module's `__dict__` attribute, which contains the module's symbol table.
pub fn dict(&self) -> &PyDict {
self.as_borrowed().dict().into_gil_ref()
}
/// Returns the index (the `__all__` attribute) of the module,
/// creating one if needed.
///
/// `__all__` declares the items that will be imported with `from my_module import *`.
pub fn index(&self) -> PyResult<&PyList> {
self.as_borrowed().index().map(Bound::into_gil_ref)
}
/// Returns the name (the `__name__` attribute) of the module.
///
/// May fail if the module does not have a `__name__` attribute.
pub fn name(&self) -> PyResult<&str> {
self.as_borrowed().name()?.into_gil_ref().to_str()
}
/// Returns the filename (the `__file__` attribute) of the module.
///
/// May fail if the module does not have a `__file__` attribute.
pub fn filename(&self) -> PyResult<&str> {
self.as_borrowed().filename()?.into_gil_ref().to_str()
}
/// Adds an attribute to the module.
///
/// For adding classes, functions or modules, prefer to use [`PyModule::add_class`],
/// [`PyModule::add_function`] or [`PyModule::add_submodule`] instead, respectively.
///
/// # Examples
///
/// ```rust
/// use pyo3::prelude::*;
///
/// #[pymodule]
/// fn my_module(module: &Bound<'_, PyModule>) -> PyResult<()> {
/// module.add("c", 299_792_458)?;
/// Ok(())
/// }
/// ```
///
/// Python code can then do the following:
///
/// ```python
/// from my_module import c
///
/// print("c is", c)
/// ```
///
/// This will result in the following output:
///
/// ```text
/// c is 299792458
/// ```
pub fn add<V>(&self, name: &str, value: V) -> PyResult<()>
where
V: IntoPy<PyObject>,
{
self.as_borrowed().add(name, value)
}
/// Adds a new class to the module.
///
/// Notice that this method does not take an argument.
/// Instead, this method is *generic*, and requires us to use the
/// "turbofish" syntax to specify the class we want to add.
///
/// # Examples
///
/// ```rust
/// use pyo3::prelude::*;
///
/// #[pyclass]
/// struct Foo { /* fields omitted */ }
///
/// #[pymodule]
/// fn my_module(module: &Bound<'_, PyModule>) -> PyResult<()> {
/// module.add_class::<Foo>()?;
/// Ok(())
/// }
/// ```
///
/// Python code can see this class as such:
/// ```python
/// from my_module import Foo
///
/// print("Foo is", Foo)
/// ```
///
/// This will result in the following output:
/// ```text
/// Foo is <class 'builtins.Foo'>
/// ```
///
/// Note that as we haven't defined a [constructor][1], Python code can't actually
/// make an *instance* of `Foo` (or *get* one for that matter, as we haven't exported
/// anything that can return instances of `Foo`).
///
#[doc = concat!("[1]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#constructor")]
pub fn add_class<T>(&self) -> PyResult<()>
where
T: PyClass,
{
self.as_borrowed().add_class::<T>()
}
/// Adds a function or a (sub)module to a module, using the functions name as name.
///
/// Prefer to use [`PyModule::add_function`] and/or [`PyModule::add_submodule`] instead.
pub fn add_wrapped<'a, T>(&'a self, wrapper: &impl Fn(Python<'a>) -> T) -> PyResult<()>
where
T: IntoPyCallbackOutput<PyObject>,
{
self.as_borrowed().add_wrapped(wrapper)
}
/// Adds a submodule to a module.
///
/// This is especially useful for creating module hierarchies.
///
/// Note that this doesn't define a *package*, so this won't allow Python code
/// to directly import submodules by using
/// <span style="white-space: pre">`from my_module import submodule`</span>.
/// For more information, see [#759][1] and [#1517][2].
///
/// # Examples
///
/// ```rust
/// use pyo3::prelude::*;
///
/// #[pymodule]
/// fn my_module(py: Python<'_>, module: &Bound<'_, PyModule>) -> PyResult<()> {
/// let submodule = PyModule::new_bound(py, "submodule")?;
/// submodule.add("super_useful_constant", "important")?;
///
/// module.add_submodule(&submodule)?;
/// Ok(())
/// }
/// ```
///
/// Python code can then do the following:
///
/// ```python
/// import my_module
///
/// print("super_useful_constant is", my_module.submodule.super_useful_constant)
/// ```
///
/// This will result in the following output:
///
/// ```text
/// super_useful_constant is important
/// ```
///
/// [1]: https://github.com/PyO3/pyo3/issues/759
/// [2]: https://github.com/PyO3/pyo3/issues/1517#issuecomment-808664021
pub fn add_submodule(&self, module: &PyModule) -> PyResult<()> {
self.as_borrowed().add_submodule(&module.as_borrowed())
}
/// Add a function to a module.
///
/// Note that this also requires the [`wrap_pyfunction!`][2] macro
/// to wrap a function annotated with [`#[pyfunction]`][1].
///
/// ```rust
/// use pyo3::prelude::*;
///
/// #[pyfunction]
/// fn say_hello() {
/// println!("Hello world!")
/// }
/// #[pymodule]
/// fn my_module(module: &Bound<'_, PyModule>) -> PyResult<()> {
/// module.add_function(wrap_pyfunction!(say_hello, module)?)
/// }
/// ```
///
/// Python code can then do the following:
///
/// ```python
/// from my_module import say_hello
///
/// say_hello()
/// ```
///
/// This will result in the following output:
///
/// ```text
/// Hello world!
/// ```
///
/// [1]: crate::prelude::pyfunction
/// [2]: crate::wrap_pyfunction
pub fn add_function<'a>(&'a self, fun: &'a PyCFunction) -> PyResult<()> {
let name = fun
.as_borrowed()
.getattr(__name__(self.py()))?
.downcast_into::<PyString>()?;
let name = name.to_cow()?;
self.add(&name, fun)
}
}
/// Implementation of functionality for [`PyModule`].
///
/// These methods are defined for the `Bound<'py, PyModule>` smart pointer, so to use method call
/// syntax these methods are separated into a trait, because stable Rust does not yet support
/// `arbitrary_self_types`.
#[doc(alias = "PyModule")]
pub trait PyModuleMethods<'py>: crate::sealed::Sealed {
/// Returns the module's `__dict__` attribute, which contains the module's symbol table.
fn dict(&self) -> Bound<'py, PyDict>;
/// Returns the index (the `__all__` attribute) of the module,
/// creating one if needed.
///
/// `__all__` declares the items that will be imported with `from my_module import *`.
fn index(&self) -> PyResult<Bound<'py, PyList>>;
/// Returns the name (the `__name__` attribute) of the module.
///
/// May fail if the module does not have a `__name__` attribute.
fn name(&self) -> PyResult<Bound<'py, PyString>>;
/// Returns the filename (the `__file__` attribute) of the module.
///
/// May fail if the module does not have a `__file__` attribute.
fn filename(&self) -> PyResult<Bound<'py, PyString>>;
/// Adds an attribute to the module.
///
/// For adding classes, functions or modules, prefer to use [`PyModuleMethods::add_class`],
/// [`PyModuleMethods::add_function`] or [`PyModuleMethods::add_submodule`] instead,
/// respectively.
///
/// # Examples
///
/// ```rust
/// use pyo3::prelude::*;
///
/// #[pymodule]
/// fn my_module(module: &Bound<'_, PyModule>) -> PyResult<()> {
/// module.add("c", 299_792_458)?;
/// Ok(())
/// }
/// ```
///
/// Python code can then do the following:
///
/// ```python
/// from my_module import c
///
/// print("c is", c)
/// ```
///
/// This will result in the following output:
///
/// ```text
/// c is 299792458
/// ```
fn add<N, V>(&self, name: N, value: V) -> PyResult<()>
where
N: IntoPy<Py<PyString>>,
V: IntoPy<PyObject>;
/// Adds a new class to the module.
///
/// Notice that this method does not take an argument.
/// Instead, this method is *generic*, and requires us to use the
/// "turbofish" syntax to specify the class we want to add.
///
/// # Examples
///
/// ```rust
/// use pyo3::prelude::*;
///
/// #[pyclass]
/// struct Foo { /* fields omitted */ }
///
/// #[pymodule]
/// fn my_module(module: &Bound<'_, PyModule>) -> PyResult<()> {
/// module.add_class::<Foo>()?;
/// Ok(())
/// }
/// ```
///
/// Python code can see this class as such:
/// ```python
/// from my_module import Foo
///
/// print("Foo is", Foo)
/// ```
///
/// This will result in the following output:
/// ```text
/// Foo is <class 'builtins.Foo'>
/// ```
///
/// Note that as we haven't defined a [constructor][1], Python code can't actually
/// make an *instance* of `Foo` (or *get* one for that matter, as we haven't exported
/// anything that can return instances of `Foo`).
///
#[doc = concat!("[1]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#constructor")]
fn add_class<T>(&self) -> PyResult<()>
where
T: PyClass;
/// Adds a function or a (sub)module to a module, using the functions name as name.
///
/// Prefer to use [`PyModuleMethods::add_function`] and/or [`PyModuleMethods::add_submodule`]
/// instead.
fn add_wrapped<T>(&self, wrapper: &impl Fn(Python<'py>) -> T) -> PyResult<()>
where
T: IntoPyCallbackOutput<PyObject>;
/// Adds a submodule to a module.
///
/// This is especially useful for creating module hierarchies.
///
/// Note that this doesn't define a *package*, so this won't allow Python code
/// to directly import submodules by using
/// <span style="white-space: pre">`from my_module import submodule`</span>.
/// For more information, see [#759][1] and [#1517][2].
///
/// # Examples
///
/// ```rust
/// use pyo3::prelude::*;
///
/// #[pymodule]
/// fn my_module(py: Python<'_>, module: &Bound<'_, PyModule>) -> PyResult<()> {
/// let submodule = PyModule::new_bound(py, "submodule")?;
/// submodule.add("super_useful_constant", "important")?;
///
/// module.add_submodule(&submodule)?;
/// Ok(())
/// }
/// ```
///
/// Python code can then do the following:
///
/// ```python
/// import my_module
///
/// print("super_useful_constant is", my_module.submodule.super_useful_constant)
/// ```
///
/// This will result in the following output:
///
/// ```text
/// super_useful_constant is important
/// ```
///
/// [1]: https://github.com/PyO3/pyo3/issues/759
/// [2]: https://github.com/PyO3/pyo3/issues/1517#issuecomment-808664021
fn add_submodule(&self, module: &Bound<'_, PyModule>) -> PyResult<()>;
/// Add a function to a module.
///
/// Note that this also requires the [`wrap_pyfunction!`][2] macro
/// to wrap a function annotated with [`#[pyfunction]`][1].
///
/// ```rust
/// use pyo3::prelude::*;
///
/// #[pyfunction]
/// fn say_hello() {
/// println!("Hello world!")
/// }
/// #[pymodule]
/// fn my_module(module: &Bound<'_, PyModule>) -> PyResult<()> {
/// module.add_function(wrap_pyfunction!(say_hello, module)?)
/// }
/// ```
///
/// Python code can then do the following:
///
/// ```python
/// from my_module import say_hello
///
/// say_hello()
/// ```
///
/// This will result in the following output:
///
/// ```text
/// Hello world!
/// ```
///
/// [1]: crate::prelude::pyfunction
/// [2]: crate::wrap_pyfunction
fn add_function(&self, fun: Bound<'_, PyCFunction>) -> PyResult<()>;
}
impl<'py> PyModuleMethods<'py> for Bound<'py, PyModule> {
fn dict(&self) -> Bound<'py, PyDict> {
unsafe {
// PyModule_GetDict returns borrowed ptr; must make owned for safety (see #890).
ffi::PyModule_GetDict(self.as_ptr())
.assume_borrowed(self.py())
.to_owned()
.downcast_into_unchecked()
}
}
fn index(&self) -> PyResult<Bound<'py, PyList>> {
let __all__ = __all__(self.py());
match self.getattr(__all__) {
Ok(idx) => idx.downcast_into().map_err(PyErr::from),
Err(err) => {
if err.is_instance_of::<exceptions::PyAttributeError>(self.py()) {
let l = PyList::empty_bound(self.py());
self.setattr(__all__, &l).map_err(PyErr::from)?;
Ok(l)
} else {
Err(err)
}
}
}
}
fn name(&self) -> PyResult<Bound<'py, PyString>> {
#[cfg(not(PyPy))]
{
unsafe {
ffi::PyModule_GetNameObject(self.as_ptr())
.assume_owned_or_err(self.py())
.downcast_into_unchecked()
}
}
#[cfg(PyPy)]
{
self.dict()
.get_item("__name__")
.map_err(|_| exceptions::PyAttributeError::new_err("__name__"))?
.downcast_into()
.map_err(PyErr::from)
}
}
fn filename(&self) -> PyResult<Bound<'py, PyString>> {
#[cfg(not(PyPy))]
unsafe {
ffi::PyModule_GetFilenameObject(self.as_ptr())
.assume_owned_or_err(self.py())
.downcast_into_unchecked()
}
#[cfg(PyPy)]
{
self.dict()
.get_item("__file__")
.map_err(|_| exceptions::PyAttributeError::new_err("__file__"))?
.downcast_into()
.map_err(PyErr::from)
}
}
fn add<N, V>(&self, name: N, value: V) -> PyResult<()>
where
N: IntoPy<Py<PyString>>,
V: IntoPy<PyObject>,
{
fn inner(
module: &Bound<'_, PyModule>,
name: Bound<'_, PyString>,
value: Bound<'_, PyAny>,
) -> PyResult<()> {
module
.index()?
.append(&name)
.expect("could not append __name__ to __all__");
module.setattr(name, value.into_py(module.py()))
}
let py = self.py();
inner(
self,
name.into_py(py).into_bound(py),
value.into_py(py).into_bound(py),
)
}
fn add_class<T>(&self) -> PyResult<()>
where
T: PyClass,
{
let py = self.py();
self.add(T::NAME, T::lazy_type_object().get_or_try_init(py)?)
}
fn add_wrapped<T>(&self, wrapper: &impl Fn(Python<'py>) -> T) -> PyResult<()>
where
T: IntoPyCallbackOutput<PyObject>,
{
fn inner(module: &Bound<'_, PyModule>, object: Bound<'_, PyAny>) -> PyResult<()> {
let name = object.getattr(__name__(module.py()))?;
module.add(name.downcast_into::<PyString>()?, object)
}
let py = self.py();
inner(self, wrapper(py).convert(py)?.into_bound(py))
}
fn add_submodule(&self, module: &Bound<'_, PyModule>) -> PyResult<()> {
let name = module.name()?;
self.add(name, module)
}
fn add_function(&self, fun: Bound<'_, PyCFunction>) -> PyResult<()> {
let name = fun.getattr(__name__(self.py()))?;
self.add(name.downcast_into::<PyString>()?, fun)
}
}
fn __all__(py: Python<'_>) -> &Bound<'_, PyString> {
intern!(py, "__all__")
}
fn __name__(py: Python<'_>) -> &Bound<'_, PyString> {
intern!(py, "__name__")
}
#[cfg(test)]
mod tests {
use crate::{
types::{module::PyModuleMethods, PyModule},
Python,
};
#[test]
fn module_import_and_name() {
Python::with_gil(|py| {
let builtins = PyModule::import_bound(py, "builtins").unwrap();
assert_eq!(builtins.name().unwrap(), "builtins");
})
}
#[test]
fn module_filename() {
use crate::types::string::PyStringMethods;
Python::with_gil(|py| {
let site = PyModule::import_bound(py, "site").unwrap();
assert!(site
.filename()
.unwrap()
.to_cow()
.unwrap()
.ends_with("site.py"));
})
}
}