Skip to content

Accessing package data #1543

Answered by bicarlsen
bicarlsen asked this question in Q&A
Mar 30, 2023 · 3 comments · 8 replies
Discussion options

You must be logged in to vote

Perhaps I should call __exit__ based on the result of path.extract?

fn resources_path(py: Python<'_>, package: &str) -> PyResult<PathBuf> {
    let resources = py.import("importlib.resources")?;
    let files = resources.call_method1("files", (package,))?;
    let files = resources.call_method1("as_file", (files,))?;
    let path = files.call_method0("__enter__")?; // enter python context manager
    match path.extract() {
        Ok(path) => {
            let none = py.None();
            files.call_method1("__exit__", (&none, &none, &none))?;
            Ok(path)
        }, 
        Err(err) => {
            files.call_method1(
                "__exit__",
                (err.get_type(py),

Replies: 3 comments 8 replies

Comment options

You must be logged in to vote
3 replies
@bicarlsen
Comment options

@messense
Comment options

@messense
Comment options

Comment options

You must be logged in to vote
3 replies
@bicarlsen
Comment options

Answer selected by messense
@messense
Comment options

@bicarlsen
Comment options

Comment options

You must be logged in to vote
2 replies
@messense
Comment options

@bicarlsen
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants