uqfoundation/dill

dill.source.findsource IndexError

Opened this issue · 3 comments

When trying to get sources of a decorated function, dill looks up the wrong line number.
This happens, because lines variable has lines of the file where function was defined, but __code__ object is actually a wrapped object that has another source file and co_firstlineno correspons to that, not to original function.
If the wrapped file is big enough, you'll get IndexError on line line = lines[lnum] in findsource function (not that it would work if the file is small of course).

Example:
You'll need two files
dill_index_error.py

import dill

from dill_indexerror_w import w


class SubtractTransform:
    @w
    def transform(self):
        pass


def main():
    obj = SubtractTransform()
    print(dill.source.findsource(obj.transform))


if __name__ == '__main__':
    main()

dill_indexerror_w.py

from functools import wraps

"""
some lines to get index error





































"""

def w(f):
    @wraps(f)
    def inner(*args, **kwargs):
        return f(*args, **kwargs)
    return inner

I think this is fixed by #651

Can't test it rn unfortunately, can you? There is an example in this PR

I cursorily tested the above example after the PR, and didn't see a behavior change. Need to come back to this to understand exactly what the issue is.