<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>PyPy (Posts by Wim Lavrijsen)</title><link>https://www.pypy.org/</link><description></description><atom:link href="https://www.pypy.org/authors/wim-lavrijsen.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2026 &lt;a href="mailto:pypy-dev@pypy.org"&gt;The PyPy Team&lt;/a&gt; </copyright><lastBuildDate>Wed, 30 Sep 2026 20:34:16 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>cppyy status update</title><link>https://www.pypy.org/posts/2013/02/cppyy-status-update-808802896237239604.html</link><dc:creator>Wim Lavrijsen</dc:creator><description>&lt;p&gt;The &lt;a href="https://doc.pypy.org/cppyy.html"&gt;cppyy module&lt;/a&gt;
provides C++ bindings for PyPy by using the reflection information extracted
from C++ header files by means of the
&lt;a href="https://root.cern.ch/drupal/content/reflex"&gt;Reflex package&lt;/a&gt;.
In order to support C++11, the goal is to move away from Reflex and instead use
&lt;a href="https://root.cern.ch/drupal/content/cling"&gt;cling&lt;/a&gt;, an interactive
C++ interpreter, as the backend.
Cling is based on &lt;a href="https://llvm.org/"&gt;llvm&lt;/a&gt;'s
&lt;a href="https://clang.llvm.org/"&gt;clang&lt;/a&gt;.

The use of a real compiler under the hood has the advantage that it is now
possible to cover every conceivable corner case.
The disadvantage, however, is that every corner case actually has to be
covered.
Life is somewhat easier when calls come in from the python interpreter, as
those calls have already been vetted for syntax errors and all lookups are
well scoped.
Furthermore, the real hard work of getting sane responses from and for C++
in an interactive environment is done in cling, not in the bindings.
Nevertheless, it is proving a long road (but for that matter clang does not
support all of C++11 yet), so here's a quick status update showing that good 
progress is being made.

&lt;/p&gt;&lt;p&gt;The following example is on CPython, not PyPy, but moving a third
(after Reflex and
&lt;a href="https://root.cern.ch/root/Cint.html"&gt;CINT&lt;/a&gt;) backend into place
underneath cppyy is straightforward compared to developing the backend
in the first place.

Take this snippet of C++11 code
(&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cpp11.C&lt;/span&gt;&lt;/tt&gt;):

&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;pre&gt;    constexpr int data_size() { return 5; }

    auto N = data_size();

    template&amp;lt;class L, class R&amp;gt;
    struct MyMath {
       static auto add(L l, R r) -&amp;gt; decltype(l+r) { return l + r; }
    };

    template class MyMath&amp;lt;int, int&amp;gt;;&lt;/pre&gt;

&lt;p&gt;As a practical matter, most usage of new C++11 features will live in
implementations, not in declarations, and are thus never seen by the bindings.
The above example is therefore somewhat contrived, but it will serve to show
that these new declarations actually work.
The new features used here are
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;constexpr&lt;/span&gt;&lt;/tt&gt;,
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;auto&lt;/span&gt;&lt;/tt&gt;, and
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;decltype&lt;/span&gt;&lt;/tt&gt;.
Here is how you could use these from CPython, using the
&lt;a href="https://root.cern.ch/viewvc/trunk/bindings/pyroot/"&gt;PyROOT&lt;/a&gt;
package, which has more than a passing resemblance to cppyy, as one is based
on the other:

&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;pre&gt;    import ROOT as gbl
    gbl.gROOT.LoadMacro('cpp11.C')

    print 'N =', gbl.N
    print '1+1 =', gbl.MyMath(int, int).add(1,1)&lt;/pre&gt;

which, when entered into a file
(&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cpp11.py&lt;/span&gt;&lt;/tt&gt;) and executed,
prints the expected results:

&lt;p&gt;&lt;/p&gt;&lt;pre&gt;    $ python cpp11.py
    N = 5
    1+1 = 2&lt;/pre&gt;

In the example, the C++ code is compiled on-the-fly, rather than first generating
a dictionary as is needed with Reflex.
A deployment model that utilizes stored pre-compiled information is foreseen
to work with larger projects, which may have to pull in headers from many places.

&lt;p&gt;Work is going to continue first on C++03 on cling with CPython (about 85% of
unit tests currently pass), with a bit of work on C++11 support on the side.
Once fully in place, it can be brought into a new backend for cppyy, after 
which the remaining parts of C++11 can be fleshed out for both interpreters.

&lt;/p&gt;&lt;p&gt;Cheers,&lt;br&gt;
Wim Lavrijsen&lt;/p&gt;</description><guid>https://www.pypy.org/posts/2013/02/cppyy-status-update-808802896237239604.html</guid><pubDate>Thu, 28 Feb 2013 00:01:00 GMT</pubDate></item><item><title>C++ objects in cppyy, part 1: Data Members</title><link>https://www.pypy.org/posts/2012/08/c-objects-in-cppyy-part-1-data-members-1105848719513737614.html</link><dc:creator>Wim Lavrijsen</dc:creator><description>&lt;p&gt;The cppyy module makes it possible to call into C++ from PyPy through the
&lt;a href="https://root.cern.ch/drupal/content/reflex"&gt;Reflex package&lt;/a&gt;.
Documentation and setup instructions are
&lt;a href="https://doc.pypy.org/cppyy.html"&gt;available here&lt;/a&gt;.
Recent work has focused on STL, low-level buffers, and code quality, but also
a lot on pythonizations for the
&lt;a href="https://root.cern.ch/drupal/content/cint"&gt;CINT backend&lt;/a&gt;, which is
mostly for High Energy Physics (HEP) use only.
A
&lt;a href="https://www.pypy.org/posts/2012/06/architecture-of-cppyy-9077100041707701102.html"&gt;previous posting&lt;/a&gt; walked
through the high-level structure and organization of the module, where it was
argued why it is necessary to write cppyy in RPython and generate bindings at
run-time for the best performance.
This posting details how access to C++ data structures is provided and is part
of a series of 3 postings on C++ object representation in Python: the second
posting will be about method dispatching, the third will tie up several odds
and ends by showing how the choices presented here and in part 2 work together
to make features such as auto-casting possible.


&lt;/p&gt;&lt;h3&gt;Wrapping Choices&lt;/h3&gt;

&lt;p&gt;Say we have a plain old data type (POD), which is the simplest possible
data structure in C++.
Like for example:

&lt;/p&gt;&lt;pre&gt;    struct A {
        int    m_i;
        double m_d;
    };&lt;/pre&gt;

&lt;p&gt;What should such a POD look like when represented in Python?
Let's start by looking at a Python data structure that is functionally
similar, in that it also carries two public data members of the desired
types.
Something like this:

&lt;/p&gt;&lt;pre&gt;    class A(object):
        def __init__(self):
            self.m_i = 0
            self.m_d = 0.&lt;/pre&gt;

&lt;p&gt;Alright, now how to go about connecting this Python class with the former
C++ POD?
Or rather, how to connect instances of either.
The exact memory layout of a Python
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;A&lt;/span&gt;&lt;/tt&gt;
instance is up to Python, and likewise the layout of a C++
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;A&lt;/span&gt;&lt;/tt&gt; instance is up
to C++.
Both layouts are implementation details of the underlying language, language
implementation, language version, and the platform used.
It should be no surprise then, that for example an
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;int&lt;/span&gt;&lt;/tt&gt; in C++ looks
nothing like a
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;PyIntObject&lt;/span&gt;&lt;/tt&gt;, even
though it is perfectly possible, in both cases, to point out in memory where
the integer value is.
The two representations can thus not make use of the same block of memory
internally.
However, the requirement is that the access to C++ from Python looks and feels
natural in its use, not that the mapping is exact.
Another requirement is that we want access to the actual object from both
Python and C++.
In practice, it is easier to provide natural access to C++ from Python than
the other way around, because the choices of memory layout in C++ are far more
restrictive: the memory layout defines the access, as the actual class
definition is gone at run-time.
The best choice then, is that the Python object will act as a proxy to the C++
object, with the actual data always being in C++.

&lt;/p&gt;&lt;p&gt;From here it follows that if the
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;m_i&lt;/span&gt;&lt;/tt&gt; data member
lives in C++, then Python needs some kind of helper to access it.
Conveniently, since version 2.2, Python has a
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;property&lt;/span&gt;&lt;/tt&gt; construct
that can take a getter and setter function that are called when the property
is used in Python code, and present it to the programmer as if it were a data
member.
So we arrive at this (note how the
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;property&lt;/span&gt;&lt;/tt&gt; instance
is a variable at the class level):

&lt;/p&gt;&lt;pre&gt;    class A(object):
        def __init__(self):
            self._cppthis = construct_new_A()
        m_i = property(get_m_i, set_m_i)
        m_d = property(get_m_d, set_m_d)&lt;/pre&gt;

&lt;p&gt;The
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;construct_new_A&lt;/span&gt;&lt;/tt&gt;
helper is not very interesting (the reflection layer can provide for it
directly), and methods are a subject for part 2 of this posting, so focus on
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;get_m_i&lt;/span&gt;&lt;/tt&gt;
and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;set_m_i&lt;/span&gt;&lt;/tt&gt;.
In order for the getter to work, the method needs to have access to the C++
instance for which the Python object is a proxy.
On access, Python will call the getter function with the proxy instance for
which it is called.
The proxy has a
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;_cppthis&lt;/span&gt;&lt;/tt&gt; data
member from which the C++ instance can be accessed (think of it as a pointer)
and all is good, at least for
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;m_i&lt;/span&gt;&lt;/tt&gt;.
The second data member
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;m_d&lt;/span&gt;&lt;/tt&gt;, however,
requires some more work: it is located at some offset into
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;_cppthis&lt;/span&gt;&lt;/tt&gt;.
This offset can be obtained from the reflection information, which lets the
C++ compiler calculate it, so details such as
&lt;a href="https://en.wikipedia.org/wiki/Byte_padding#Data_structure_padding"&gt;byte padding&lt;/a&gt;
are fully accounted for.
Since the setter also needs the offset, and since both share some more details
such as the containing class and type information of the data member, it is
natural to create a custom property class.
The getter and setter methods then become bound methods of an instance of that
custom property,
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;CPPDataMember&lt;/span&gt;&lt;/tt&gt;, and
there is one such instance per data member.
Think of something along these lines:

&lt;/p&gt;&lt;pre&gt;    def make_datamember(cppclass, name):
        cppdm = cppyy.CPPDataMember(cppclass, name)
        return property(cppdm.get, cppdm.set)&lt;/pre&gt;

where the
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;make_datamember&lt;/span&gt;&lt;/tt&gt;
function replaces the call to
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;property&lt;/span&gt;&lt;/tt&gt; in the
class definition above.

&lt;p&gt;Now hold on a minute!
Before it was argued that Python and C++ can not share the same underlying
memory structure, because of choices internal to the language.
But if on the Python side choices are being made by the developer of the
language bindings, that is no longer a limitation.
In other words, why not go through e.g. the Python extension API, and do
this:

&lt;/p&gt;&lt;pre&gt;    struct A_pyproxy {
        PyObject_HEAD
        int    m_i;
        double m_d;
    };&lt;/pre&gt;

&lt;p&gt;Doing so would save on
&lt;a href="https://en.wikipedia.org/wiki/Malloc"&gt;malloc overhead&lt;/a&gt; and remove
a pointer indirection.
There are some technical issues specific to PyPy for such a choice: there is
no such thing as
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;PyPyObject_HEAD&lt;/span&gt;&lt;/tt&gt;
and the layout of objects is not a given as that is decided only at
translation time.
But assume that those issues can be solved, and also accept that there is no
problem in creating structure definitions like this at run-time, since the
reflection layer can provide both the required size and access to the
placement
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;new operator&lt;/span&gt;&lt;/tt&gt;
(compare e.g. CPython's
&lt;a href="https://docs.python.org/library/struct.html"&gt;struct module&lt;/a&gt;).
There is then still a more fundamental problem: it must be possible to take
over ownership in Python from instances created in C++ and vice-versa.
With a proxy scheme, that is trivial: just pass the pointer and do the
necessary bookkeeping.
With an embedded object, however, not every use case can be implemented: e.g.
if an object is created in Python, passed to C++, and deleted in C++, it
must have been allocated independently.
The proxy approach is therefore still the best choice, although embedding
objects may provide for optimizations in some use cases.


&lt;/p&gt;&lt;h3&gt;Inheritance&lt;/h3&gt;

&lt;p&gt;The next step, is to take a more complicated C++ class, one with inheritance
(I'm leaving out details such as constructors etc., for brevity):

&lt;/p&gt;&lt;pre&gt;    class A {
    public:
        virtual ~A() {}
        int    m_i;
        double m_d;
    };

    class B : public A {
    public:
        virtual ~B() {}
        int    m_j;
    };&lt;/pre&gt;

&lt;p&gt;From the previous discussion, it should already be clear what this will look
like in Python:

&lt;/p&gt;&lt;pre&gt;    class A(object):
        def __init__(self):
            self._cppthis = construct_new_A()
        m_i = make_datamember('A', 'm_i')
        m_d = make_datamember('A', 'm_d')

    class B(A):
        def __init__(self):
            self._cppthis = construct_new_B()
        m_j = make_datamember('B', 'm_j')&lt;/pre&gt;

&lt;p&gt;There are some minor adjustments needed, however.
For one, the offset of the
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;m_i&lt;/span&gt;&lt;/tt&gt; data member
may be no longer zero: it is possible that a virtual function dispatch table
(&lt;a href="https://en.wikipedia.org/wiki/Virtual_method_table"&gt;vtable&lt;/a&gt;)
pointer is added at the beginning of
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;A&lt;/span&gt;&lt;/tt&gt; (an alternative
is to have the vtable pointer at the end of the object).
But if
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;m_i&lt;/span&gt;&lt;/tt&gt; is handled the
same way as
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;m_d&lt;/span&gt;&lt;/tt&gt;, with the
offset provided by the compiler, then the compiler will add the bits, if any,
for the vtable pointer and all is still fine.
A real problem could come in however, with a call of the
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;m_i&lt;/span&gt;&lt;/tt&gt; property on
an instance of
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;B&lt;/span&gt;&lt;/tt&gt;: in that case,
the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;_cppthis&lt;/span&gt;&lt;/tt&gt;
points to a &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;B&lt;/span&gt;&lt;/tt&gt;
instance, whereas the getter/setter pair expect an
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;A&lt;/span&gt;&lt;/tt&gt; instance.
In practice, this is usually not a problem: compilers will align
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;A&lt;/span&gt;&lt;/tt&gt; and
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;B&lt;/span&gt;&lt;/tt&gt; and calculate
an offset for
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;m_j&lt;/span&gt;&lt;/tt&gt; from the start
of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;A&lt;/span&gt;&lt;/tt&gt;.
Still, that is an implementation detail (even though it is one that can be
determined at run-time and thus taken advantage of by the JIT), so it can not
be relied upon.
The &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;m_i&lt;/span&gt;&lt;/tt&gt; getter
thus needs to take into account that it can be called with a derived type,
and so it needs to add an additional offset.
With that modification, the code looks something like this (as you would have
guessed, this is getting more and more into pseudo-code territory, although it
is conceptually close to the actual implementation in cppyy):

&lt;/p&gt;&lt;pre&gt;    def get_m_i(self):
        return int(self._cppthis + offset(A, m_i) + offset(self.__class__, A))&lt;/pre&gt;

&lt;p&gt;Which is a shame, really, because the offset between
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;B&lt;/span&gt;&lt;/tt&gt; and
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;A&lt;/span&gt;&lt;/tt&gt; is going
to be zero most of the time in practice, and the JIT can not completely
&lt;a href="https://www.pypy.org/posts/2011/03/controlling-tracing-of-interpreter-with_15-3281215865169782921.html"&gt;elide&lt;/a&gt;
the offset calculation (as we will see later; it is easy enough to elide if
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;self.__class__&lt;/span&gt;&lt;/tt&gt; is
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;A&lt;/span&gt;&lt;/tt&gt;, though).
One possible solution is to repeat the properties for each derived class, i.e.
to have a
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;get_B_m_i&lt;/span&gt;&lt;/tt&gt; etc., but
that looks ugly on the Python side and anyway
does not work in all cases: e.g. with multiple inheritance where there are
data members with the same name in both bases, or if
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;B&lt;/span&gt;&lt;/tt&gt; itself has a
public data member called
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;m_i&lt;/span&gt;&lt;/tt&gt; that shadows
the one from &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;A&lt;/span&gt;&lt;/tt&gt;.
The optimization then, is achieved by making
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;B&lt;/span&gt;&lt;/tt&gt; in charge of the
offset calculations, by making
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;offset&lt;/span&gt;&lt;/tt&gt; a method of
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;B&lt;/span&gt;&lt;/tt&gt;, like so:

&lt;/p&gt;&lt;pre&gt;    def get_m_i(self):
        return int(self._cppthis + offset(A, m_i) + self.offset(A))&lt;/pre&gt;

&lt;p&gt;The insight is that by scanning the inheritance hierarchy of a derived
class like &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;B&lt;/span&gt;&lt;/tt&gt;, you
can know statically whether it may sometimes need offsets, or whether the
offsets are always going to be zero.
Hence, if the offsets are always zero, the method
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;offset&lt;/span&gt;&lt;/tt&gt; on
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;B&lt;/span&gt;&lt;/tt&gt; will
simply return the literal
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;0&lt;/span&gt;&lt;/tt&gt; as its
implementation, with the JIT taking care of the rest through inlining and
constant folding.
If the offset could be non-zero, then the method will perform an actual
calculation, and it will let the JIT elide the call only if possible.


&lt;/p&gt;&lt;h3&gt;Multiple Virtual Inheritance&lt;/h3&gt;

&lt;p&gt;Next up would be multiple inheritance, but that is not very interesting: we
already have the offset calculation between the actual and base class, which
is all that is needed to resolve any multiple inheritance hierarchy.
So, skip that and move on to multiple &lt;i&gt;virtual&lt;/i&gt; inheritance.
That that is going to be a tad more complicated will be clear if you show the
following code snippet to any old C++ hand and see how they respond.
Most likely you will be told: "Don't ever do that."
But if code can be written, it will be written, and so for the sake of the
argument, what would this look like in Python:

&lt;/p&gt;&lt;pre&gt;    class A {
    public:
        virtual ~A() {}
        int m_a;
    };

    class B : public virtual A {
    public:
        virtual ~B() {}
        int m_b;
    };

    class C : public virtual A {
    public:
        virtual ~C() {}
        int m_c;
    };

    class D : public virtual B, public virtual C {
    public:
        virtual ~D() {}
        int m_d;
    };&lt;/pre&gt;

&lt;p&gt;Actually, nothing changes from what we have seen so far: the scheme as laid
out above is fully sufficient.
For example, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;D&lt;/span&gt;&lt;/tt&gt;
would simply look like:

&lt;/p&gt;&lt;pre&gt;    class D(B, C):
        def __init__(self):
            self._cppthis = construct_new_D()
        m_d = make_datamember('D', 'm_d')&lt;/pre&gt;

&lt;p&gt;Point being, the only complication added by the multiple virtual
inheritance, is that navigation of the C++ instance happens with pointers
internal to the instance rather than with offsets.
However, it is still a fixed offset from any location to any other location
within the instance as its parts are laid out consecutively in memory (this is
not a requirement, but it is the most efficient, so it is what is used in
practice).
But what you can not do, is determine the offset statically: you need a live
(i.e. constructed) object for any offset calculations.
In Python, everything is always done dynamically, so that is of itself not a
limitation.
Furthermore,
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;self&lt;/span&gt;&lt;/tt&gt; is already
passed to the offset calculation (remember that this was done to put the
calculation in the derived class, to optimize the common case of zero
offset), thus a live C++ instance is there precisely when it is needed.
The call to the offset calculation is hard to elide, since the instance will
be passed to a C++ helper and so the most the JIT can do is guard on the
instance's memory address, which is likely to change between traces.
Instead, explicit caching is needed on the base and derived types, allowing
the JIT to elide the lookup in the explicit cache.


&lt;/p&gt;&lt;h3&gt;Static Data Members and Global Variables&lt;/h3&gt;

&lt;p&gt;That, so far, covers all access to instance data members.
Next up are static data members and global variables.
A complication here is that a Python
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;property&lt;/span&gt;&lt;/tt&gt; needs to
live on the class in order to work its magic.
Otherwise, if you get the property, it will simply return the getter function,
and if you set it, it will dissappear.
The logical conclusion then, is that a
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;property&lt;/span&gt;&lt;/tt&gt;
representing a static or global variable, needs to live on the class of the
class, or the metaclass.
If done directly though, that would mean that every static data member is
available from every class, since all Python classes have the same metaclass,
which is class
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;type&lt;/span&gt;&lt;/tt&gt; (and which is
its own metaclass).
To prevent that from happening and because
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;type&lt;/span&gt;&lt;/tt&gt; is actually
immutable, each proxy class needs to have its own custom metaclass.
Furthermore, since static data can also be accessed on the instance, the
class, too, gets a
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;property&lt;/span&gt;&lt;/tt&gt; object
for each static data member.
Expressed in code, for a basic C++ class, this looks as follows:

&lt;/p&gt;&lt;pre&gt;    class A {
    public:
        static int s_i;
    };&lt;/pre&gt;

&lt;p&gt;Paired with some Python code such as this, needed to expose the static
variable both on the class and the instance level:

&lt;/p&gt;&lt;pre&gt;    meta_A = type(CppClassMeta, 'meta_A', [CPPMetaBase], {})
    meta_A.s_i = make_datamember('A', 's_i')

    class A(object):
        __metaclass__ = meta_A
        s_i = make_datamember('A', 's_i')&lt;/pre&gt;

&lt;p&gt;Inheritance adds no complications for the access of static data per se, but
there is the issue that the metaclasses must follow the same hierarchy as the
proxy classes, for the Python method resolution order (MRO) to work.
In other words, there are two complete, parallel class hierarchies that map
one-to-one: a hierarchy for the proxy classes and one for their metaclasses.

&lt;/p&gt;&lt;p&gt;A parallel class hierarchy is used also in other highly dynamic,
object-oriented environments, such as for example
&lt;a href="https://en.wikipedia.org/wiki/Metaclass#In_Smalltalk-80"&gt;Smalltalk&lt;/a&gt;.
In Smalltalk as well, class-level constructs, such as class methods and data
members, are defined for the class in the metaclass.
A metaclass hierarchy has further uses, such as lazy loading of nested
classes and member templates (this would be coded up in the base class of all
metaclasses:
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;CPPMetaBase&lt;/span&gt;&lt;/tt&gt;), and
makes it possible to distribute these over different reflection libraries.
With this in place, you can write Python codes like so:

&lt;/p&gt;&lt;pre&gt;    &amp;gt;&amp;gt;&amp;gt;&amp;gt; from cppyy.gbl import A
    &amp;gt;&amp;gt;&amp;gt;&amp;gt; a = A()
    &amp;gt;&amp;gt;&amp;gt;&amp;gt; a.s_i = 42
    &amp;gt;&amp;gt;&amp;gt;&amp;gt; print A.s_i == a.s_i
    True
    &amp;gt;&amp;gt;&amp;gt;&amp;gt; # etc.&lt;/pre&gt;

&lt;p&gt;The implementation of the getter for
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;s_i&lt;/span&gt;&lt;/tt&gt; is a lot
easier than for instance data: the static data lives at a fixed, global,
address, so no offset calculations are needed.
The same is done for global data or global data living in namespaces:
namespaces are represented as Python classes, and global data are implemented
as properties on them.
The need for a metaclass is one of the reasons why it is easier for namespaces
to be classes: module objects are too restrictive.
And even though namespaces are not modules, you still can, with
some limitations,
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;import&lt;/span&gt;&lt;/tt&gt; from
them anyway.

&lt;/p&gt;&lt;p&gt;It is common that global objects themselves are pointers, and therefore it
is allowed that the stored
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;_cppthis&lt;/span&gt;&lt;/tt&gt; is not a
pointer to a C++ object, but rather a pointer to a pointer to a C++ object.
A double pointer, as it were.
This way, if the C++ code updates the global pointer, it will automatically
reflect on the Python side in the proxy.
Likewise, if on the Python side the pointer gets set to a different variable,
it is the pointer that gets updated, and this will be visible on the C++ side.
In general, however, the same caveat as for normal Python code applies: in
order to set a global object, it needs to be set within the scope of that
global object.
As an example, consider the following code for a C++ namespace
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;NS&lt;/span&gt;&lt;/tt&gt; with
global variable
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;g_a&lt;/span&gt;&lt;/tt&gt;, which behaves
the same as Python code for what concerns the visibility of changes to the
global variable:

&lt;/p&gt;&lt;pre&gt;    &amp;gt;&amp;gt;&amp;gt;&amp;gt; from cppyy.gbl import NS, A
    &amp;gt;&amp;gt;&amp;gt;&amp;gt; from NS import g_a
    &amp;gt;&amp;gt;&amp;gt;&amp;gt; g_a = A(42)                     # does NOT update C++ side
    &amp;gt;&amp;gt;&amp;gt;&amp;gt; print NS.g_a.m_i
    13                                   # the old value happens to be 13
    &amp;gt;&amp;gt;&amp;gt;&amp;gt; NS.g_a = A(42)                  # does update C++ side
    &amp;gt;&amp;gt;&amp;gt;&amp;gt; print NS.g_a.m_i
    42
    &amp;gt;&amp;gt;&amp;gt;&amp;gt; # etc.&lt;/pre&gt;


&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;That covers all there is to know about data member access of C++ classes in
Python through a reflection layer!
A few final notes: RPython does not support metaclasses, and so the
construction of proxy classes (code like
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;make_datamember&lt;/span&gt;&lt;/tt&gt;
above) happens in Python code instead.
There is an overhead penalty of about 2x over pure RPython code associated
with that, due to extra guards that get inserted by the JIT.
A factor of 2 sounds like a lot, but the overhead is tiny to begin with, and
2x of tiny is still tiny and it's not easy to measure.
The class definition of the custom property,
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;CPPDataMember&lt;/span&gt;&lt;/tt&gt;, is
in RPython code, to be transparent to the JIT.
The actual offset calculations are in the reflection layer.
Having the proxy class creation in Python, with structural code in RPython,
complicates matters if proxy classes need to be constructed on-demand.
For example, if an instance of an as-of-yet unseen type is returned by a
method.
Explaining how that is solved is a topic of part 2, method calls, so stay
tuned.

&lt;/p&gt;&lt;p&gt;This posting laid out the reasoning behind the object representation of C++
objects in Python by cppyy for the purpose of data member access.
It explained how the chosen representation of offsets gives rise to a very
pythonic representation, which allows Python introspection tools to work as
expected.
It also explained some of the optimizations done for the benefit of the JIT.
Next up are method calls, which will be described in part 2.&lt;/p&gt;</description><guid>https://www.pypy.org/posts/2012/08/c-objects-in-cppyy-part-1-data-members-1105848719513737614.html</guid><pubDate>Mon, 13 Aug 2012 09:26:00 GMT</pubDate></item></channel></rss>