Skip to content

Can PyModule::from_code enforce a non-empty filename? #4769

Open
@sxlijin

Description

@sxlijin

We ran into an issue recently where our pyo3-based library was causing user code to hang, and it took me a full day and a half to track it down. Once we were able to identify the problem (it was hidden behind a few layers of abstraction - unhandled error in a background thread and then some, see BoundaryML/baml#1214), we realized that the root cause of the issue was that in our pyo3 code, we had a PyModule::from_code(..., file_name = "", ...) call.

It turns out that when a user calls inspect.stack() in a Python context, where one of the stack frames references a variable created in this way, inspect.stack() will actually throw because empty filenames (understandably) are errors.

I wonder if PyModule::from_code should just always return PyErr in this scenario.

Pros:

  • safer out-of-the-box behavior (user code is more likely to be compatible with inspect)

Cons:

  • would a user ever want to deliberately set the filename to ""?
  • is this too much of a backwards-breaking change? since this would cause runtime errors, not build errors on pyo3 upgrade

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions