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

Use inspect.getsourcelines to get code #1234

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 3 additions & 23 deletions joblib/func_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
def get_func_code(func):
""" Attempts to retrieve a reliable function code hash.

The reason we don't use inspect.getsource is that it caches the
source, whereas we want this to be modified on the fly when the
function is modified.

Returns
-------
func_code: string
Expand All @@ -45,25 +41,9 @@ def get_func_code(func):
"""
source_file = None
try:
code = func.__code__
source_file = code.co_filename
if not os.path.exists(source_file):
# Use inspect for lambda functions and functions defined in an
# interactive shell, or in doctests
source_code = ''.join(inspect.getsourcelines(func)[0])
line_no = 1
if source_file.startswith('<doctest '):
source_file, line_no = re.match(
r'\<doctest (.*\.rst)\[(.*)\]\>', source_file).groups()
line_no = int(line_no)
source_file = '<doctest %s>' % source_file
return source_code, source_file, line_no
# Try to retrieve the source code.
with open_py_source(source_file) as source_file_obj:
first_line = code.co_firstlineno
# All the lines after the function definition:
source_lines = list(islice(source_file_obj, first_line - 1, None))
return ''.join(inspect.getblock(source_lines)), source_file, first_line
source_file = inspect.getsourcefile(func)
code, first_line = inspect.getsourcefile(func)
return ''.join(code), source_file, first_line
except:
# If the source code fails, we use the hash. This is fragile and
# might change from one session to another.
Expand Down
2 changes: 1 addition & 1 deletion joblib/test/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def test_memory_warning_lambda_collisions(tmpdir):

# In recent Python versions, we can retrieve the code of lambdas,
# thus nothing is raised
assert len(warninfo) == 4
assert len(warninfo) == 2


def test_memory_warning_collision_detection(tmpdir):
Expand Down