[C++-sig] An issue with wrapping class trees (BPL V2)

Pearu Peterson pearu at cens.ioc.ee
Tue May 28 21:24:56 CEST 2002


Hi David,

On Tue, 28 May 2002, David Abrahams wrote:

> ----- Original Message -----
> From: "Pearu Peterson" <pearu at cens.ioc.ee>
> 
> > Yes, I see it. However, currently my show stopper is how to insert
> > wrapped classes into a class inheritance tree in a 'natural' way :-(.
> > See the end of this message:
> >
> > http://mail.python.org/pipermail/c++-sig/2002-May/001058.html
> 
> Can you send me a minimal, complete example which demonstrates the problem
> you're having?

Sure, please find attached three files:

  cltree.cpp, test_cltree.py, setup_cltree.py

When I run

  python setup_cltree.py build --build-platlib=.
  # I am not using jam as you may notice ;-)
  python test_cltree.py

I get

b= cltree.basic()
s= <__main__.symbol object at 0x80fcd64>
c= cltree.constant()
v= 
Traceback (most recent call last):
  File "test_cltree.py", line 36, in ?
    print 'v=',v
TypeError: bad argument type for built-in operation


Expected behaviour would be the following output

b= cltree.basic()
s= <__main__.symbol object at 0x80fcd64>
c= cltree.constant()
v= cltree.wrapped_variable()
ok


If you look cltree.cpp then I am basically interested in exposing classes 
`basic' and `variable' to Python under the following conditions:

1) class `variable' is a subclass of `basic' also in Python
2) class `variable' is exposed to Python using `variable_wrapper' (so that
   I can re-define constructors)

Classes `symbol' and `constant' are there only for testing, you can
ignore them. In some sense, exposing `variable' is a combination of
exposing `symbol' and `constant' so that different features
(conditions (1) and (2), respectively) of the latter ones are combined in
`variable'. 

There is another issue with the usage of boost::noncopyable (see
comments in cltree.cpp) but that is not important to me at this moment.

Pearu
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cltree.cpp
Type: text/x-c++src
Size: 1930 bytes
Desc: 
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20020528/d3d9e4c9/attachment.cpp>
-------------- next part --------------
#!/usr/bin/env python

from cltree import basic,symbol,constant,variable

b = basic()
c = constant()
s = symbol()
v = variable()

assert isinstance(b,basic)
assert not isinstance(b,symbol)
assert not isinstance(b,constant)
assert not isinstance(b,variable)

assert isinstance(c,basic)
assert isinstance(c,constant)
assert not isinstance(c,symbol)
assert not isinstance(c,variable)

assert not isinstance(s,basic)
assert isinstance(s,symbol)
assert not isinstance(s,constant)
assert not isinstance(s,variable)

assert isinstance(v,basic)
assert not isinstance(v,symbol)
assert not isinstance(v,constant)
assert isinstance(v,variable)

print 'b=',b
assert repr(b)=='cltree.basic()'
print 's=',s
assert repr(s)!='cltree.wrapped_symbol()' # because not isinstance(s,basic)
print 'c=',c
assert repr(c)=='cltree.constant()'
print 'v=',v
assert repr(v)=='cltree.wrapped_variable()'


print 'ok'
-------------- next part --------------
#!/usr/bin/env python2.2

boost_dir = 'boost' # /path/to/boost/dir
gcc_exe = 'gcc'
gpp_exe = 'g++'

import sys,os

if sys.version < '2.2':
    print 'Python >=2.2 is required but got '+sys.version[:6]
    sys.exit()

if not os.path.isdir(boost_dir):
    print 'No Boost directory',boost_dir
    sys.exit()

bpl_dir = os.path.join(boost_dir,'libs/python/src')
bpl_src = ['converter/from_python.cpp',
           'converter/registry.cpp',
           'converter/type_id.cpp',
           'object/class.cpp',
           'object/function.cpp',
           'object/inheritance.cpp',
           'object/life_support.cpp',
           'errors.cpp',
           'module.cpp',
           'objects.cpp',
           'converter/builtin_converters.cpp',
           'converter/callback.cpp',
           ]
bpl_src = [os.path.join(bpl_dir,s) for s in bpl_src]

from distutils.core import setup, Extension

ext = Extension('cltree',
                sources=bpl_src + ['cltree.cpp'],
                include_dirs=[boost_dir],
                define_macros = [('BOOST_PYTHON_DYNAMIC_LIB',None),
                                 ('BOOST_PYTHON_V2',None)],
                )

#+++HACK: replace linker gcc with g++ +++++++++++

from distutils import sysconfig
save_init_posix = sysconfig._init_posix
def my_init_posix():
    save_init_posix()
    g = sysconfig._config_vars
    for n,r in [('LDSHARED',gpp_exe),('CC',gcc_exe)]:
        if g[n][:3]=='gcc':
            print 'my_init_posix: changing %s = %r'%(n,g[n]),
            g[n] = r+g[n][3:]
            print 'to',`g[n]`
sysconfig._init_posix = my_init_posix

if __name__ == "__main__":
    setup (ext_modules = [ext])



More information about the Cplusplus-sig mailing list