[Python-Dev] creating __all__ in extension modules

Fred L. Drake, Jr. fdrake@acm.org
Sun, 4 Feb 2001 11:26:51 -0500 (EST)


Skip Montanaro writes:
 > I thought I answered this question already when Fredrik asked it.  In os.py,

  You did, and I'd have responded then had I been able to spare the
time to reply.  (I wasn't ignoring the topic.)

 > to build its __all__ list based upon the myriad different sets of symbols it
 > might have after it's fancy footwork importing from various os-dependent
 > modules, I think it's easiest to rely on those modules telling os what it
 > should export.

  But since C extensions inherantly control their exports very
tightly, perhaps the right approach is to create the __all__ value in
the code that needs it -- it usually won't be needed for C extensions,
and the os module is a fairly special case anyway.
  Perhaps this helper would be a good approach:

def _get_exports_list(module):
    try:
        return list(module.__all__)
    except AttributeError:
        return [n for n in dir(module) if n[0] != '_']

  The os module could then use:

_OS_EXPORTS = ['path', ...]
if 'posix' in _names:
    ...
    __all__ = _get_exports_list(posix)
    del posix
elif ...:
    ...

_OS_EXPORTS = ['linesep', <all locally provded stuff>]
__all__.extend(_OS_EXPORTS)


  -Fred

-- 
Fred L. Drake, Jr.  <fdrake at acm.org>
PythonLabs at Digital Creations