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
use pyo3::prelude::*;
use pyo3::types::PyDict;
use pyo3::wrap_pymodule;

pub mod awaitable;
pub mod buf_and_str;
pub mod comparisons;
pub mod datetime;
pub mod dict_iter;
pub mod enums;
pub mod misc;
pub mod objstore;
pub mod othermod;
pub mod path;
pub mod pyclasses;
pub mod pyfunctions;
pub mod sequence;
pub mod subclassing;

#[pymodule]
fn pyo3_pytests(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_wrapped(wrap_pymodule!(awaitable::awaitable))?;
    #[cfg(not(Py_LIMITED_API))]
    m.add_wrapped(wrap_pymodule!(buf_and_str::buf_and_str))?;
    m.add_wrapped(wrap_pymodule!(comparisons::comparisons))?;
    #[cfg(not(Py_LIMITED_API))]
    m.add_wrapped(wrap_pymodule!(datetime::datetime))?;
    m.add_wrapped(wrap_pymodule!(dict_iter::dict_iter))?;
    m.add_wrapped(wrap_pymodule!(enums::enums))?;
    m.add_wrapped(wrap_pymodule!(misc::misc))?;
    m.add_wrapped(wrap_pymodule!(objstore::objstore))?;
    m.add_wrapped(wrap_pymodule!(othermod::othermod))?;
    m.add_wrapped(wrap_pymodule!(path::path))?;
    m.add_wrapped(wrap_pymodule!(pyclasses::pyclasses))?;
    m.add_wrapped(wrap_pymodule!(pyfunctions::pyfunctions))?;
    m.add_wrapped(wrap_pymodule!(sequence::sequence))?;
    m.add_wrapped(wrap_pymodule!(subclassing::subclassing))?;

    // Inserting to sys.modules allows importing submodules nicely from Python
    // e.g. import pyo3_pytests.buf_and_str as bas

    let sys = PyModule::import_bound(py, "sys")?;
    let sys_modules = sys.getattr("modules")?.downcast_into::<PyDict>()?;
    sys_modules.set_item("pyo3_pytests.awaitable", m.getattr("awaitable")?)?;
    sys_modules.set_item("pyo3_pytests.buf_and_str", m.getattr("buf_and_str")?)?;
    sys_modules.set_item("pyo3_pytests.comparisons", m.getattr("comparisons")?)?;
    sys_modules.set_item("pyo3_pytests.datetime", m.getattr("datetime")?)?;
    sys_modules.set_item("pyo3_pytests.dict_iter", m.getattr("dict_iter")?)?;
    sys_modules.set_item("pyo3_pytests.enums", m.getattr("enums")?)?;
    sys_modules.set_item("pyo3_pytests.misc", m.getattr("misc")?)?;
    sys_modules.set_item("pyo3_pytests.objstore", m.getattr("objstore")?)?;
    sys_modules.set_item("pyo3_pytests.othermod", m.getattr("othermod")?)?;
    sys_modules.set_item("pyo3_pytests.path", m.getattr("path")?)?;
    sys_modules.set_item("pyo3_pytests.pyclasses", m.getattr("pyclasses")?)?;
    sys_modules.set_item("pyo3_pytests.pyfunctions", m.getattr("pyfunctions")?)?;
    sys_modules.set_item("pyo3_pytests.sequence", m.getattr("sequence")?)?;
    sys_modules.set_item("pyo3_pytests.subclassing", m.getattr("subclassing")?)?;

    Ok(())
}