Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: When using Nuitka to package a project, an error occurred when reading the file: "KeyError: 'self'" #12923 #12926

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

CrazySJX
Copy link

Fixes #12923

The reason the current code cannot correctly retrieve the calling parameters is that the inspect module does not store complete frame information for each normal call by default; it only populates the call stack information when an exception occurs. In other words, the frame information that this code relies on may not be complete, which could lead to the inability to retrieve the parameters.

Copy link

welcome bot commented Oct 30, 2024

Hello! 👋 Thanks for opening your first pull request here! ❤️ We will try to get back to you soon. 🚴

@@ -157,6 +157,9 @@ def __init__(
"first_samples": np.array(first_samps),
"last_samples": np.array(last_samps),
}

__import__("inspect").currentframe().f_locals.update(locals())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than adding this to every module before calling super().__init__(...), is there any way this magic could live inside super.__init__(...) itself instead? You mention:

[inspect] only populates the call stack information when an exception occurs

So in theory it should be possible to use inspect.currentframe().f_back to get back to this level and do something there, maybe?

Also not sure why you do __import__("inspect") rather than just import inspect at the top of the file...?

@drammock
Copy link
Member

the inspect module does not store complete frame information for each normal call by default

is there a reference / explanation for this? I saw it mentioned in the nuitka issue thread but it's not immediately obvious to me from reading the docs for the inspect module. I do see this note:

CPython implementation detail: This function relies on Python stack frame support in the interpreter, which isn’t guaranteed to exist in all implementations of Python. If running in an implementation without Python stack frame support this function returns None.

which I think is referring to the problem you encountered, but it would be nice to have that confirmed / have a fuller explanation of what is going wrong.

@larsoner
Copy link
Member

Also a side note -- IIRC the raw._init_kwargs stack frame stuff is a feature used (99% sure!) only by mne-bids, and it's private so we can change it without warning (to anyone other than mne-bids, since I think we have an implicit contract with them to support it). So we could make MNE-Python more robust to incorrect or incomplete information being available based on the Python implementation being used, i.e., make the _get_argvalues() return None when the init kwargs can't be resolved.

In other words, maybe we don't want to try super hard to make this work in all cases? Like if we just set raw._init_kwargs = None when it couldn't be resolved, to hit a problem in practice, a user would have to be using nuitka and MNE-BIDS to write data in a specific way that makes use of _init_kwargs. I think this is a very deep corner case. And to make it work we're jumping through more hoops that we'll need to support. Might not be worth the effort and maintenance burden of whatever solution we come up with...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When using Nuitka to package a project, an error occurred when reading the file: "KeyError: 'self'"
3 participants