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

utils: get_caller_name: handle cases when source is unavailable #2665

Closed
wants to merge 1 commit into from

Conversation

rokm
Copy link

@rokm rokm commented Oct 17, 2023

Fix get_caller_name to gracefully handle cases when a module's source code is unavailable. In such cases, FrameInfo.code_context is None an cannot be iterated over:

Traceback (most recent call last):
  File "program.py", line 6, in <module>
    app = dash.Dash(__name__)
  File "dash/dash.py", line 399, in __init__
  File "dash/_utils.py", line 290, in get_caller_name
TypeError: 'NoneType' object is not iterable

This happens with PyInstaller, where both dash modules and the program's entry-point script are collected as byte-compiled .pyc modules, without corresponding source .py files.

But it can be also triggered by simply byte-compiling the python script into .pyc, removing the source .py, and running the .pyc.

Fix `get_caller_name` to gracefully handle cases when a module's
source code is unavailable. In such cases, `FrameInfo.code_context`
is `None` an cannot be iterated over.
@alexcjohnson
Copy link
Collaborator

Thanks @rokm - is there anything else we can do in these cases or is the information about caller name irretrievably lost? We use this to infer the app module name so that users can omit __name__ from app = Dash(__name__) - but if we need to make exceptions "in cases X, Y, Z you still need to provide __name__" this gets much more confusing for users.

cc @T4rk1n

@T4rk1n
Copy link
Contributor

T4rk1n commented Oct 18, 2023

It's not possible for inspect to get the frame source code if inspect can't access the source file which seems to be the case when:

  • Running interactive prompt
  • Running compiled bytecode like .pyc and pyinstaller.

For the interactive prompt, the name is always __main__ and the default will works. There might be a way to do without the name check and just use the frame index as it should always be the frame above Dash.__init__

@alexcjohnson
Copy link
Collaborator

Superseded by #2672 - thanks for your help @rokm!

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.

3 participants