[pypy-dev] Built-in modules at app-level
Armin Rigo
arigo at tunes.org
Thu Dec 18 21:34:16 CET 2003
Hello world,
Just a fast note about what we want to do at some point: write all built-in
modules at application-level. Example:
file module/__builtin__.py (or appspace/__builtin__.py):
"""
Built-in functions, exceptions, and other objects.
"""
def sum(sequence, total=0):
for item in sequence:
total = total + item
return total
def _interp_pow(self, w_base, w_exponent, w_modulus=None):
if w_modulus is None:
w_modulus = self.space.w_None
return self.space.pow(w_base, w_exponent, w_modulus)
-+-
So this is essentially the same idea as "def app_xxx" at interpreter-level,
but the other way around. It is just much more clear to write __builtin__ as
above: everything is "normal", works as expected, with the exception of
interpreter-level functions which serve as "hooks".
Turning an "_interp_xxx" function into a built-in function is a hack. The
cleaner and more powerful way to do it is quite funny:
pow = escape("""
def pow(self, w_base, w_exponent, w_modulus=None):
if w_modulus is None:
w_modulus = self.space.w_None
return self.space.pow(w_base, w_exponent, w_modulus)
""")
which is very nice because you can build the string with %s or whatever at
application-level :-) The point is that the string is executed at
interp-level, which means that escape() can only be used during
initialization. After that, the translator will see a normal built-in
function, and translate it into C code.
A bientot,
Armin.
More information about the Pypy-dev
mailing list