uqfoundation / dill

serialize all of Python

Home Page:http://dill.rtfd.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RecursionError with function that has the same name as its owning module

pauldmccarthy opened this issue · comments

Howdy, I am experiencing a RecursionError with dill 0.3.6 and the master branch (8243111). It seems to be related to the structure of my code, where I have a function which is located within a module of the same name. For example, let's say I have the following code structure:

pkg/
    __init__.py:
        from pkg.somefunc import somefunc
        from pkg.somemod  import modfunc

    somefunc.py:
        def somefunc():
            print('pkg.somefunc.somefunc')

    somemod.py:
        def modfunc():
            print('pkg.somemod.modfunc')

I think that the issue is to do with the dill._dill._import_module (which is used by the dill._dill._locate_function function) - with the above example code structure, these functions produce the following results (I have commented out the __all__ declaration at the top of _dill.py):

import dill._dill as _dill
import pkg

print(_dill._import_module('pkg.somemod'))
print(_dill._locate_function(pkg.modfunc))
print(_dill._import_module('pkg.somefunc'))
print(_dill._locate_function(pkg.somefunc))
<module 'pkg.somemod' from '/.../pkg/somemod.py'>
True
<function pkg.somefunc.somefunc()>
False

The logic within the _import_module function seems to assume that, if given a path to a module pkg.somefunc, then that module must be accessible from within the namespace of the parent package pkg - specifically this line resolves to getattr(__import__('pkg'), 'somefunc'), which in my case will return the pkg.somefunc.somefunc function rather than the pkg.somefunc module.

Triggering the RecursionError is not trivial - the simple example above is not actually enough to trigger the error (there must be some short-circuiting logic somewhere), and I don't know dill (or pickling in general) well enough to come up with a minimal example.

However, I have prepared a small reproducible example based on the codebase I was working in when I came across this issue at https://github.com/pauldmccarthy/dill-issue