[Python-Dev] Parrot -- should life imitate satire?

Simon Cozens simon@netthink.co.uk
Thu, 2 Aug 2001 09:54:09 -0700


On Wed, Aug 01, 2001 at 03:36:07PM +0200, Thomas Wouters wrote:
> A Python object, any Python object, is essentially a struct like this:
> 
> stuct PyExample {
>     int ob_refcnt;                     /* reference count */
>     struct _typeobject *ob_type;       /* type of this object */
> 
>     /* type-specific data goes here */
> 
> }

This looks rather like a Perl SV (scalar value):
struct STRUCT_SV {      /* struct sv { */
    void*   sv_any;     /* pointer to something */
    U32     sv_refcnt;  /* how many references to us */
    U32     sv_flags;   /* what we are */
};

> For instance, the struct to hold a (normal, unbounded) Python integer is
> 
> typedef struct {
>     PyObject_HEAD
>     long ob_ival;
> } PyIntObject;

We use double indirection so that we can switch between types very quickly;
the sv_any pointer points to another structure or an integer/float.

> The struct _typeobject pointer is a pointer to a Type object, which also
> acts as a vtable.

That's sounding more like how we plan to do things for Perl 6.

> >>> def spam(x):
> ...     print "Hello Perl World!"
> >>> dis.dis(spam)
>           0 SET_LINENO               1
[...]

% ./perl -Dts -e 'print "Hello Python people!\n"'
    =>
(-e:1)  null
    =>
(-e:1)  const(PV("Hello Python people!\12"\0))
    =>  PV("Hello Python people!\12"\0)
(-e:1)  stringify

    [ That was constant folding, BTW. - SC]

EXECUTING...

    =>
(-e:0)  enter
    =>
(-e:0)  nextstate
    =>
(-e:1)  pushmark
    =>  *
(-e:1)  const(PV("Hello Python people!\12"\0))
    =>  *  PV("Hello Python people!\12"\0)
(-e:1)  print
Hello Python people!
    =>  SV_YES
(-e:1)  leave

perl -MO=Bytecode,-S -e 'print "Hello Python people!\n"'
will give you a complete disassembly of the bytecode, but
that's the version that can be serialised to disk, so it's
a lot more verbose than it need be - it sets up all the
opcodes, their flags, their sequence numbers, their connections
to other nodes in the opcode tree and so on. 

If you really want to get stuck in, read the internals tutorial
at http://www.netthink.co.uk/downloads/

Simon