<div class="gmail_quote"><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">---------- Forwarded message ----------<br>From: Jim Bosch &lt;<a href="mailto:talljimbo@gmail.com">talljimbo@gmail.com</a>&gt;<br>
To: Development of Python/C++ integration &lt;<a href="mailto:cplusplus-sig@python.org">cplusplus-sig@python.org</a>&gt;<br>Cc: <br>Date: Tue, 07 Feb 2012 11:14:54 -0500<br>Subject: Re: [C++-sig] How to define a Python metaclass with Boost.Python?<br>
On 02/07/2012 01:35 AM, paulcmnt wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The Python C API has the `PyObject *PyType_Type` object, which is equivalent<br>
to `type` in the interpreter. If I want to define a metaclass in C++, how<br>
can I set `type` as one of its bases in Boost.Python? Also, what other<br>
things should I take into consideration when defining a Python metaclass in<br>
C++? It&#39;d be ideal if there was a Boost.Python solution to this. If not, a<br>
solution that uses the Python C API (or a combination of Boost and the C<br>
API) is good as well. (The version I&#39;m using is Python 3.)<br>
<br>
</blockquote>
<br>
I&#39;m afraid this isn&#39;t really supported.  All Boost.Python classes have to use a specific Boost.Python metaclass that&#39;s defined deep in the bowels of some Boost.Python source file, and there&#39;s really no way to override that at present.<br>

<br>
This might be a nice feature for the future, but I haven&#39;t thought too deeply about how to make it work or why one would want to do it.  If you have a specific use case for a custom metaclass, I&#39;d love to hear what it is.<br>

<br>
Jim<br><br></blockquote><div><br></div><div>In my application I have a base <font face="&#39;courier new&#39;, monospace">Event</font> class and both C++ and Python inherit from this base and define new types of events. Clients can subscribe a certain function to a certain type of event.</div>
<div><br></div><div><font face="&#39;courier new&#39;, monospace">&gt;&gt;&gt; events = EventDispatcher()</font></div><div><font face="&#39;courier new&#39;, monospace">&gt;&gt;&gt; sub = events.subscribe(KeyboardEvent, on_keyboard_event)</font></div>
<div><font face="&#39;courier new&#39;, monospace">&gt;&gt;&gt; sub.cancel()</font></div><div><br></div><div>When an event is posted to the<font face="&#39;courier new&#39;, monospace"> EventDispatcher </font>(from either C++ or Python), all functions that are subscribed to that event, <i>or to a base of that event</i>, are called.</div>
<div><br></div><div>Because of how the system is supposed to work, I need a common way to determine type and parents of an event at runtime, regardless of whether the event class was defined in C++ or Python; I do this with a custom <font face="&#39;courier new&#39;, monospace">EventTypeInfo</font> class.</div>
<div><br></div><div>The metaclass that I would like to define in C++ would add a <font face="&#39;courier new&#39;, monospace">__cpp_event_typeinfo</font> attribute to each event class defined in Python, which I can then grab when I need it. </div>
<div><br></div><div><b>A possible solution.</b> I though a good enough way of defining a metaclass in Boost.Python would be to the equivalent of this code below. Do you think there are any safety problem with doing something like this, some compatibly problem that could arise with the other Boost.Python classes?</div>
<div><br></div><div><font face="&#39;courier new&#39;, monospace">&gt;&gt;&gt; class Meta:</font></div><div><font face="&#39;courier new&#39;, monospace">...    pass</font></div><div><font face="&#39;courier new&#39;, monospace">...</font></div>
<div><font face="&#39;courier new&#39;, monospace">&gt;&gt;&gt; Meta = type(Meta.__name__, (type,), dict(Meta.__dict__))</font></div><div><br></div></div>