wlav / cppyy

Other
407 stars 42 forks source link

inspect.getmembers() on cppyy.gbl not detecting enums and functions #45

Open cgdae opened 2 years ago

cgdae commented 2 years ago

inspect.getmembers() does not seem to see C++ functions and enums inside cppyy.gbl. But it does see functions and enums inside a C++ namespace within cppyy.gbl.

The same behaviour occurs with dir().

This is with cppyy.__version__ = 2.3.0

I understand that cppyy is generally lazy about creating things, but given that it seems to be able to enumerate functions and enums in a C++ namespace within cppyy.gbl, i was hoping it would be able to do so for cppyy.gbl itself.

So, is this expected behaviour? Can anything be done to allow detection of enums and functions within cppyy.gbl?

Python code that demonstrates the problem:

import cppyy
import inspect

def show( namespace):
    '''
    Show items in <namespace> whose names do not start with an
    underscore.
    '''
    print( f'{namespace}:')
    for n, v in inspect.getmembers( namespace):
        if not n.startswith( '_'):
            print( f'    {n}={v}')

# Create some C++ functions, enums and a namespace:
cppyy.cppdef('''
        enum { FOO };
        void foo() {}
        namespace N
        {
            enum { BAR };
            void bar() {}
        }
        ''')

show( cppyy.gbl)    # Does not show FOO or foo().
show( cppyy.gbl.N)  # Shows BAR and bar().

# foo() and FOO do exist if we ask for them explicitly:
print( f'cppyy.__version__={cppyy.__version__}')
print( f'cppyy.gbl.foo={cppyy.gbl.FOO}')
print( f'cppyy.gbl.foo={cppyy.gbl.foo}')
print( f'cppyy.gbl.N.bar={cppyy.gbl.N.BAR}')
print( f'cppyy.gbl.N.bar={cppyy.gbl.N.bar}')

show( cppyy.gbl)    # Now shows FOO and foo().

Output:

<namespace cppyy.gbl at 0x176b020>:
    CppyyLegacy=<namespace cppyy.gbl.CppyyLegacy at 0x2466320>
    N=<namespace cppyy.gbl.N at 0x2ba5960>
    std=<namespace cppyy.gbl.std at 0x176c000>
<namespace cppyy.gbl.N at 0x2ba5960>:
    BAR=0
    bar=<C++ overload "bar" at 0x7f19213164c0>
cppyy.__version__=2.3.0
cppyy.gbl.foo=0
cppyy.gbl.foo=<C++ overload "foo" at 0x7f1921316900>
cppyy.gbl.N.bar=0
cppyy.gbl.N.bar=<C++ overload "bar" at 0x7f1921706740>
<namespace cppyy.gbl at 0x176b020>:
    CppyyLegacy=<namespace cppyy.gbl.CppyyLegacy at 0x2466320>
    FOO=0
    N=<namespace cppyy.gbl.N at 0x2ba5960>
    foo=<C++ overload "foo" at 0x7f1921316680>
    std=<namespace cppyy.gbl.std at 0x176c000>

Thanks in advance for any help here.

wlav commented 2 years ago

Looks like upstream isn't providing any enums (still looking into that one).

The function is missing b/.c __dir__ didn't perform updates. Was b/c it used to leak, but that's no longer the case, so I added the forced update/load.

cgdae commented 2 years ago

Thanks, good to know this is understood. Will try to figure out how to build cppyy myself and test with latest version.