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

DM-32210: Use inspect to determine when we leave the utils module #103

Merged
merged 1 commit into from Oct 16, 2021

Conversation

timj
Copy link
Member

@timj timj commented Oct 12, 2021

Previously using the filename the code could be confused if
there was another lsst/utils directory in the path. Now
using inspect.getmodule the code can no for sure when it
leaves the lsst.utils module.

Checklist

  • ran Jenkins
  • added a release note for user-visible changes to doc/changes

@timj timj requested a review from natelust October 12, 2021 21:38
@codecov
Copy link

codecov bot commented Oct 12, 2021

Codecov Report

Merging #103 (3d2cd24) into master (25fdfb9) will decrease coverage by 0.07%.
The diff coverage is 75.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #103      +/-   ##
==========================================
- Coverage   90.87%   90.80%   -0.08%     
==========================================
  Files          36       36              
  Lines        2083     2087       +4     
==========================================
+ Hits         1893     1895       +2     
- Misses        190      192       +2     
Impacted Files Coverage Δ
python/lsst/utils/timer.py 95.00% <75.00%> (-1.88%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 25fdfb9...3d2cd24. Read the comment docs.

module = inspect.getmodule(s.frame)
if module is None:
continue
if not module.__name__.startswith("lsst.utils"):
Copy link
Contributor

Choose a reason for hiding this comment

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

With inspect and frames you need to be very careful about creating local references to frames, as you can create reference cycles, so the fewer local variables you need the better, and cleaning up variables that contain frames before exiting a function is a good idea. This may be overly defensive for how often this function gets used, but it is good practice, especially if someone uses it as a code reference later on.

Suggested change
if not module.__name__.startswith("lsst.utils"):
for i, s in enumerate(inspect.stack()):
module = inspect.getmodule(s.frame)
if module is None:
continue
if not module.__name__.statswith("lsst.utils"):
stacklevel = i
break
del s
return stacklevel

Copy link
Member Author

Choose a reason for hiding this comment

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

This seems highly unpythonic but I'll make the change.

Previously using the filename the code could be confused if
there was another lsst/utils directory in the path. Now
using inspect.getmodule the code can no for sure when it
leaves the lsst.utils module.
@timj timj merged commit e7cdf9e into master Oct 16, 2021
@timj timj deleted the tickets/DM-32210 branch October 16, 2021 08:14
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.

None yet

2 participants