[pypy-dev] PyPy newbie question: How to play with types

Armin Rigo arigo at tunes.org
Sun Nov 13 18:12:05 CET 2005

Hi Noam,

On Sun, Nov 13, 2005 at 01:44:32AM +0200, Noam Raphael wrote:
> I want to use PyPy as a way of trying a change I will propose to
> Python.

I suppose that's what we are supposed to make easier :-)

Understanding all the details of PyPy's built-in types is not exactly
straightforward, but I suppose that with good documentation it could be
made accessible.  Let me give you a quick start...

> I have looked into the directories. I have found
> pypy/objspace/dict{object,type}.py. My questions are:
> 1. How do I make new built-in types, play with their inheritence, make
> them uninstantiable? Should I register it somewhere?

Built-in types go in files xxxtype.py.  The StdTypeDef declared in
xxxtype.py declares how the type behaves.  For an example see e.g.
stringtype.py and basestringtype.py.  Inheritance is declared in the
StdTypeDef.  If you want the type name to appear in the built-ins
automatically, register it in model.py.  

Then you can declare one (or zero or several) implementation of a type,
traditionally in xxxobject.py as a W_XxxObject.  It must be registered
by being added to the dictionary 'self.typeorder' in model.py.  Note
that you can also register automatic conversions there, like
str->unicode, but I think it's not necessary in your case.  There is no
inheritance of the W_XxxObject classes -- typically, you don't need a
W_BaseDictObject at all because it's not instantiatable, just like there
is no W_BaseStringObject (and no basestringobject.py).

> 2. Where do I add a new built-in like frozen()? How should I make it
> callable from interpreter-level code?

A new __freeze__ protocol means a new multimethod.  Add it to the
ObjSpace.MethodTable in interpreter/baseobjspace.py.  E.g. follow the
example of 'len' and how various xxxobject.py files define len__Xxx().
(There are name-based hacks going on here.  You don't need to register
'freeze' anywhere else than in ObjSpace.MethodTable, it will pick up the
freeze__Xxx implementations and be available as 'space.freeze()').  Then
follow the example of len() again to make it available as a built-in:
you need to modify module/__builtin__/operation.py and __init__.py
(that's just 4 lines of code to add :-)

A bientot,


More information about the Pypy-dev mailing list