Implement a way to reload a cffi based module

Issue #318 wontfix
lf_
created an issue

When I'm developing my cffi based project, I'm frequently adding things to the ffi build and rebuilding. Unfortunately, neither the rebuild nor ipython autoreload actually reloads the library.

I've tried importlib.reload() but it errors out: AttributeError: module '_freeipmi' has no attribute '__path__'

It would be nice if there was a simple way to reload modules.

Comments (4)

  1. Armin Rigo

    Can you give more details? Here is what I tried:

    $ cd demo/   # inside the cffi repository
    $ python3.5 xclient_build.py
    $ python3.5
    >>> import _xclient_cffi
    >>>
    

    In another terminal:

    $ cd demo/
    $ python3.5 xclient_build.py
    

    Back in the first terminal:

    >>> import importlib
    >>> importlib.reload(_xclient_cffi)
    

    but this seems to work.

  2. lf_ reporter
    >>> importlib.reload(boringipmi.lib)
    AttributeError: module '_freeipmi' has no attribute '__path__'
    

    The issue is that I use from _freeipmi import lib, ffi as used in the examples. I've tried importing _freeipmi and reloading it, but unfortunately boringipmi, the python side, still doesn't have the latest version loaded.

  3. Armin Rigo

    boringipmi.lib is not a real Python module object, so e.g. reload() doesn't work on it. The only real module is boringipmi. The issue is the same as if you had a module A that defines, say, a class called Lib. Then there is no automatic way in Python to have both the module A reloaded and the new version of the class Lib used everywhere: if other modules already did from A import Lib, then they already got the old version. If they only did import A and then use A.Lib, then they will see the new version. In general, reload() should not be used except in very well-controlled circumstances.

    There is another issue, which is that reload() never actually reloads a C extension module, in the sense that it will never close and re-open the .so file.

    These two points give the situation so far. Now maybe with a good dose of CPython-version-specific internal hacking, reload(boringipmi.lib) could be made to appear to work anyway... It looks unlikely, though.

  4. Log in to comment