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

capture_backtrace raises AttributeError on PEP-420 namespace packages #667

Closed
jacksmith15 opened this issue Jul 27, 2021 · 2 comments · Fixed by #668
Closed

capture_backtrace raises AttributeError on PEP-420 namespace packages #667

jacksmith15 opened this issue Jul 27, 2021 · 2 comments · Fixed by #668

Comments

@jacksmith15
Copy link

The new capture_backtrace function in scout_apm.core.backtrace raises an AttributeError when the stack includes a PEP-420 namespace package.

This is caused by the module_filepath function, specifically line 32:

    module_dir = sys.modules[root_module].__file__.rsplit(os.sep, 2)[0]

If sys.modules[root_module] is a PEP-420 namespace package, this will raise

AttributeError: 'NoneType' object has no attribute 'rsplit'

Steps to reproduce

Create a namespace package, with some modules inside, e.g.:

namespace/
    foo/
        __init__.py
    bar/
       __init__.py

Then on an interactive Python shell:

>>> from scout_apm.core.backtrace import module_filepath
>>> from namespace import foo
>>> module_filepath("namespace.foo", "namespace")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jack/venvs/tmp-a17ac7185189989/lib/python3.8/site-packages/scout_apm/core/backtrace.py", line 32, in module_filepath
    module_dir = sys.modules[root_module].__file__.rsplit(os.sep, 2)[0]
AttributeError: 'NoneType' object has no attribute 'rsplit'

Details

  • Tested with version 2.21.0
  • Current workaround is to pin version to 2.20.0
@tim-schilling
Copy link
Collaborator

Hi Jack,

Thank you for the detailed issue. I'm looking into it.

-Tim

tim-schilling added a commit that referenced this issue Jul 27, 2021
A namespace package does not have a file property. Instead utilize
the __path__ attribute and use the first element in the list. While
this may not capture the dynamic cases, it should capture the
necessary data for most cases.

Fixes #667
@tim-schilling
Copy link
Collaborator

@jacksmith15 Thank you for raising this. It will be fixed in v2.21.1 which is being released now. It'll be available within the hour.

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 a pull request may close this issue.

2 participants