[Python-Dev] Re: pirate (python+parrot)
A.M. Kuchling
amk at amk.ca
Mon Aug 4 11:22:39 EDT 2003
On Fri, 1 Aug 2003 23:09:06 -0400 (EDT), Michal Wallace <michal at sabren.com>
wrote:
> in helping out. There's a to-do list and help wanted
> section in the README, but one thing I could *really*
> use some help with is figuring out how to get the core C types working as
> PMCs. A PMC is supposedly very similar to a PyObject, so I'm wondering if
> it's
> possible to bridge the two without doing a lot of
> extra work. I just don't know enough C to judge.
Excellent work on pirate; you've already advanced the translation much
further than I did.
I don't believe there's much chance for PMC implementation beyond writing a
new
PythonString/Int/whatever PMC from scratch. Let me explain why I think
that.
PMCs and PyObjects are similar in the general sense. Both are
collections of pointers to functions that do things for the specified
type. PyObjects contain instances information for a type -- e.g. the
contents of a string, the value of an int, the FILE * for a file.
PyTypeObjects are PyObjects that contain a bunch of pointers to C
functions that do things to a type: hash it, try to convert it to a
number, get an attribute from it. For example, when Python encounters
'obj1 % obj2', it gets the type object for obj1 and calls
type_obj->as_number->nb_remainder(obj1, obj2). Poke around
Include/object.h for a full list.
Parrot's PMCs are similar, but the list of operations they support is
different. I can't find a source file or document that lists them
all, so looking at classes/default.pmc may be the best way to get a
list.
For example, I was wondering how to implement Python's slicing in
Parrot. In Python, there's a tp_as_sequence structure that has a
bunch of sequence-related functions, one of which is sq_slice. This
has the signature (PyObject *seq, int index_low, int index_high).
Strings implement this function, as do lists, so:
s = 'abcdef'
print s[0:3]
and
s = [1,2,"string", {}]
print s[0:3]
both work.
Parrot's PMCs have a substring method, but not a generic slice method,
so I'm not sure what you would compile the above Python code to. You
might end up having to compile it to PASM that was basically:
if (s is a string):
call substring(s, 0, 3)
elif (s is an array):
call subarray(s, 0, 3)
You might have a base PythonSequence PMC and have
PythonString/PythonList/PythonArray/etc. all inherit from it, which
would simplify the generated code, but I don't know if I can just
invent a new set of PMC operations -- sq_slice, sq_assign_slice,
sq_length -- or if introducing new ones requires modifying Parrot
itself.
(In fact I can't figure out how you'd get a slice of an array in
CVS Parrot: none of the methods in classes/array.pmc or
classes/perlarray.pmc seems relevant. I may well be missing it,
though.)
Therefore, I don't think there's any simple way of taking the existing
Python
interpreter code in Objects/listobject.c, stringobject.c, and turning
them into PMC code; the types and function signatures are just too
different.
IMHO, simply getting Python's sequence operations (slices, slice
assignment) working will be a significant milestone for Parrot/Python work,
because doing it will require figuring out how to support Python's
different semantics within the currently rather Perl-centric set of PMC
methods. (Plus, sequence operations are needed by most Python programs --
the very first function in
pystone.py needs them, for example, which is why I started looking at
them.)
--amk
After all, we are human beings, and not creatures of infinite
possibilities.
-- Robertson Davies, _Conversations_
More information about the Python-Dev
mailing list