[C++-sig] Re: Tutorial - Techniques

Nicodemus nicodemus at globalite.com.br
Tue May 20 06:41:26 CEST 2003


David Abrahams wrote:

>Nicodemus <nicodemus at globalite.com.br> writes:
>
>
>>>Did you actually test your MetaInjector thing?  I'm pretty sure you
>>>need to derive the metaclass from the metaclass used for Boost.Python
>>>objects, or you'll get a metaclass conflict.
>>>
>>>
>>Silly me! I did test, but with a pure Python class! 8P
>>
>>Indeed, trying to inject methods in a Boost.Python class gives an
>>error: "TypeError: metatype conflict among bases". So I tried to use
>>your MetaInjector, but it gives the same error. Any idea?
>>
>
>Err, did you take my code literally, and write:
>
>    # The one Boost.Python uses for all wrapped classes.
>    class BoostPythonMetaclass(type): pass
>
>??
>
>That doesn't work, of course.  The "one true metaclass" already exists
>in Boost.Python.  You need to get the metaclass, either by explicitly
>making it available in an extension module, or by getting it from some
>wrapped class:
>
>     BoostPythonMetaclass = some_wrapped_class.__class__
>

Yeah, I tried to use some_wrapped_class.__metaclass__, but the classes 
don't have this attribute. I didn't know you could use the class as a 
metaclass... anyway, the code now works:

    # The one Boost.Python uses for all wrapped classes.
    # You can use here any class exported by Boost instead of "point"
    BoostPythonMetaclass = point.__class__

    class injector(object):
        class __metaclass__(BoostPythonMetaclass):
            def __init__(self, name, bases, dict):
                for b in bases:
                    if type(b) not in (self, type):
                        for k,v in dict.items():
                            setattr(b,k,v)
                return type.__init__(self, name, bases, dict)

    # inject some methods in the point foo
    class more_point(injector, point):
        def __repr__(self):
            return 'Point(x=%s, y=%s)' % (self.x, self.y)
        def foo(self):
            print 'foo!'

>>>About reducing compilation time: this section is very closely related
>>>to http://www.boost.org/libs/python/doc/v2/faq.html#c1204.  Should we
>>>cross-link them somehow?
>>>
>>>
>>I added a blurb to the end of that section pointing to the FAQ.
>>
>
>I think you ought to add some entries to the FAQ which point into the
>techniques paper as well... e.g., "Compilation takes forever! How can
>I make it faster?"
>

Good idea! I added two new entries,  "How do I create sub-packages using 
Boost.Python?" and "Compilation takes too much time and eats too much 
memory! What can I do to make it faster?". Attached is a diff for it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20030519/3124163e/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Techniques.zip
Type: application/zip
Size: 32283 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20030519/3124163e/attachment.zip>


More information about the Cplusplus-sig mailing list