[pypy-dev] Annotating space status

Günter Jantzen guenter.jantzen at netcologne.de
Sun Jul 6 23:37:50 CEST 2003


Hello friends,

> > About .append, we should probably complain, just to emphasis the point
that
> > this is not an efficient operation at all (a realloc()). If people
really want
> > the same result in RPython, then "x += [3]" makes the point more
clearly.
>
> BTW, where is the formal definition or RPython?  Or if there isn't
> one, where can I find out more about it?
>

About RPython read please:
http://codespeak.net/pypy/doc/objspace/restrictedpy.html

But as Chris was saying:
"Some hints are here, but this is a moving target..."

I think sooner or later we have to iterate towards a more formal definition
otherwise our Standard-Objectspace will be coded in all different
interpretations
of RPython (like annual rings of a tree)

The problem with the document above is that it describes mainly a
restriction in the use of types, not the restrictions of behaviour.

BTW I remember one of our goals was to target not only C.
I see a danger when RPython mimicries to much C (in its informal
definition - see code examples below).
Maybe I miss *again* the point, but I think at this time we can't know if
"append" will be mapped to "realloc".
For example when we target Java or C++ we will have string classes or
Collections in our target language.
These classes encapsulate memory allocation as an low level detail.

If we want, we can encapsulate this for the target C too,   We can write
C-Libraries with some helper functions
The first C++  Compilers (Glockenspiel) generated intermediate C-Code. Its
possible to write "C with classes".
Not too complicated. I only think about String or lists "classes"
implemented by a collection of functions which takes
as a first argument an object "self". (Strange - this reminds me somehow at
"stringobject.py" or "listobject.py").

Ok, it could be slow. Then we will need different RPythons for different
targets. Not so beauty ...
I think a this time we should be optimistic and use one beauty RPython which
is restricted in its dynamics
but does not care about memory allocation in the target language.

Now the code example: Implementation of __repr__ for Stringobjects :

The comment shows an unrestricted implementation variant.
Then follows the implementation in RPython, which mimics C:

..objspace/std/stringobject.py [line 886 - line 954]
----------------------------------------------------------------------------
-------------------
#for comparison and understandiong of the underlying algorithm the
unrestricted implementation
#def repr__String(space, w_str):
#    u_str = space.unwrap(w_str)
#
#    quote = '\''
#    if '\'' in u_str and not '"' in u_str:
#        quote = '"'
#
#    u_repr = quote
#
#    for i in range(len(u_str)):
#        c = u_str[i]
#        if c == '\\' or c == quote:  u_repr+= '\\'+c
#        elif c == '\t': u_repr+= '\\t'
#        elif c == '\r': u_repr+= '\\r'
#        elif c == '\n': u_repr+= '\\n'
#        elif not _isreadable(c) :
#            u_repr+=  '\\' + hex(ord(c))[-3:]
#        else:
#            u_repr += c
#
#    u_repr += quote
#
#    return space.wrap(u_repr)

def repr__String(space, w_str):
    u_str = space.unwrap(w_str)
    quote = '\''
    if '\'' in u_str and not '"' in u_str:
        quote = '"'

    buflen = 2
    for i in range(len(u_str)):
        c = u_str[i]
        if c in quote+"\\\r\t\n" :
            buflen+= 2
        elif _isreadable(c) :
            buflen+= 1
        else:
            buflen+= 4

    buf = [' ']* buflen

    buf[0] = quote
    j=1
    for i in range(len(u_str)):
        #print buflen-j
        c = u_str[i]
        if c in quote+"\\\r\t\n" :
            buf[j]= '\\'
            j+=1
            if c == quote or c=='\\':  buf[j] = c
            elif c == '\t': buf[j] = 't'
            elif c == '\r': buf[j] = 'r'
            elif c == '\n': buf[j] = 'n'
            j +=1
        elif not _isreadable(c) :
            buf[j]= '\\'
            j+=1
            for x in hex(ord(c))[-3:]:
                buf[j] = x
                j+=1
        else:
            buf[j] = c
            j+=1

    buf[j] = quote

    return space.wrap("".join(buf))

----------------------------------------------------------------------------
-------------------

kind regards
Günter











More information about the Pypy-dev mailing list