Author terry.reedy
Recipients brett.cannon, mental, terry.reedy
Date 2019-03-15.20:28:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
I finished patching pyclbr to be a complete module class and function browser in summer 2017, for use by IDLE's class browser.

I verified the bug with the import API, with a twist.

>>> import pyclbr
>>> pyclbr.readmodule_ex('<invalid')
AttributeError: 'NoneType' object has no attribute 'submodule_search_locations'
>>> pyclbr.readmodule_ex('<invalid>')

The relevant block in pyclbr is

    spec = importlib.util._find_spec_from_path(fullmodule, search_path)
    _modules[fullmodule] = tree
    # Is module a package?
    if spec.submodule_search_locations is not None:
        tree['__path__'] = spec.submodule_search_locations

Caching (fullmodule, tree) before the spec access is why the repeat call above returns the empty tree.

This block is preceded by a block that explicitly raises 'ImportError', even though no import is attemped.  It is followed by a block that returns the tree in case of error.

        source = spec.loader.get_source(fullmodule)
        if source is None:
            return tree
    except (AttributeError, ImportError):
        # If module is not Python source, we cannot do anything.
        return tree

So either a) the empty tree should be returned when the  NoneType exception is caught, or b) the invalid names should not be cached and the AttributeError turned into an 'ImportError'. 

I believe we should do the latter, as you suggested, and move the caching line down to follow the new try-except.  If you want, submit a PR for the master branch *after* signing the CLA.  If you can, include a new assertRaises test based on the invalid call above.  And check that '<invalid>' is not in pyclbr._modules with assertNotIn('<invalid>', pyclbr._modules).
Date User Action Args
2019-03-15 20:28:38terry.reedysetrecipients: + terry.reedy, brett.cannon, mental
2019-03-15 20:28:38terry.reedysetmessageid: <>
2019-03-15 20:28:38terry.reedylinkissue36298 messages
2019-03-15 20:28:38terry.reedycreate