[pypy-svn] r28251 - in pypy/dist/pypy: rpython rpython/lltypesystem/module rpython/module rpython/module/test rpython/ootypesystem/module rpython/test translator/c
antocuni at codespeak.net
antocuni at codespeak.net
Sun Jun 4 14:49:07 CEST 2006
Author: antocuni
Date: Sun Jun 4 14:49:05 2006
New Revision: 28251
Added:
pypy/dist/pypy/rpython/lltypesystem/module/ll_os_path.py (contents, props changed)
pypy/dist/pypy/rpython/ootypesystem/module/ll_os_path.py (contents, props changed)
Removed:
pypy/dist/pypy/rpython/ootypesystem/module/support.py
Modified:
pypy/dist/pypy/rpython/extfunctable.py
pypy/dist/pypy/rpython/lltypesystem/module/ll_os.py
pypy/dist/pypy/rpython/module/ll_os.py
pypy/dist/pypy/rpython/module/ll_os_path.py
pypy/dist/pypy/rpython/module/ll_strtod.py
pypy/dist/pypy/rpython/module/support.py
pypy/dist/pypy/rpython/module/test/test_ll_os.py
pypy/dist/pypy/rpython/module/test/test_ll_os_path.py
pypy/dist/pypy/rpython/module/test/test_ll_strtod.py
pypy/dist/pypy/rpython/ootypesystem/module/ll_os.py
pypy/dist/pypy/rpython/rgenop.py
pypy/dist/pypy/rpython/test/test_rbuiltin.py
pypy/dist/pypy/rpython/test/tool.py
pypy/dist/pypy/translator/c/extfunc.py
Log:
(antocuni, nik)
Make many external functions work in ootypesystem. Most functions
(e.g., the ll_os ones) are now classmethods that make use of
typesystem-specific helpers defined in rpython/module/support.py or in
rpython/{ll,oo}typestem/module/ll_*.py.
Then we fixed some places that referenced things that we moved and
brought some tests to ootypesystem too.
Due to this change genc had to be fixed, too.
Modified: pypy/dist/pypy/rpython/extfunctable.py
==============================================================================
--- pypy/dist/pypy/rpython/extfunctable.py (original)
+++ pypy/dist/pypy/rpython/extfunctable.py Sun Jun 4 14:49:05 2006
@@ -28,7 +28,7 @@
except AttributeError:
mod = self.import_module("pypy.rpython.%s.module.%s"
% (type_system.name, lastmodulename))
- ll_function = getattr(mod, ll_function_name)
+ ll_function = getattr(mod.Implementation, ll_function_name)
return ll_function
def import_module(self, module_name):
@@ -155,9 +155,9 @@
return SomeInteger(nonneg=True)
def statannotation(*args):
+ from pypy.rpython.lltypesystem.module.ll_os import Implementation
from pypy.annotation.model import SomeInteger, SomeTuple
- from pypy.rpython.module.ll_os import ll_stat_result
- record_call(ll_stat_result, [SomeInteger()]*10, 'OS_STAT')
+ record_call(Implementation.ll_stat_result, [SomeInteger()]*10, 'OS_STAT')
return SomeTuple((SomeInteger(),)*10)
def frexpannotation(*args):
Modified: pypy/dist/pypy/rpython/lltypesystem/module/ll_os.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/module/ll_os.py (original)
+++ pypy/dist/pypy/rpython/lltypesystem/module/ll_os.py Sun Jun 4 14:49:05 2006
@@ -1,16 +1,29 @@
-import os
-from pypy.rpython.module.support import from_rstr, to_rstr
-from pypy.rpython.module.ll_os import *
+from pypy.rpython.module.support import LLSupport
+from pypy.rpython.module.ll_os import BaseOS
+from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.rarithmetic import intmask
-def ll_os_open(fname, flag, mode):
- return os.open(from_rstr(fname), flag, mode)
-ll_os_open.suggested_primitive = True
+n = 10
+fieldnames = ['item%d' % i for i in range(n)]
+lltypes = [lltype.Signed]*n
+fields = tuple(zip(fieldnames, lltypes))
+STAT_RESULT = lltype.GcStruct('tuple%d' % n, *fields)
-def ll_os_write(fd, astring):
- return os.write(fd, from_rstr(astring))
-ll_os_write.suggested_primitive = True
-
-def ll_os_getcwd():
- return to_rstr(os.getcwd())
-ll_os_getcwd.suggested_primitive = True
+class Implementation(BaseOS, LLSupport):
+
+ def ll_stat_result(stat0, stat1, stat2, stat3, stat4,
+ stat5, stat6, stat7, stat8, stat9):
+ tup = lltype.malloc(STAT_RESULT)
+ tup.item0 = intmask(stat0)
+ tup.item1 = intmask(stat1)
+ tup.item2 = intmask(stat2)
+ tup.item3 = intmask(stat3)
+ tup.item4 = intmask(stat4)
+ tup.item5 = intmask(stat5)
+ tup.item6 = intmask(stat6)
+ tup.item7 = intmask(stat7)
+ tup.item8 = intmask(stat8)
+ tup.item9 = intmask(stat9)
+ return tup
+ ll_stat_result = staticmethod(ll_stat_result)
Added: pypy/dist/pypy/rpython/lltypesystem/module/ll_os_path.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/lltypesystem/module/ll_os_path.py Sun Jun 4 14:49:05 2006
@@ -0,0 +1,7 @@
+from pypy.rpython.module.support import LLSupport
+from pypy.rpython.module.ll_os_path import BaseOsPath
+
+class Implementation(BaseOsPath, LLSupport):
+ pass
+
+
Modified: pypy/dist/pypy/rpython/module/ll_os.py
==============================================================================
--- pypy/dist/pypy/rpython/module/ll_os.py (original)
+++ pypy/dist/pypy/rpython/module/ll_os.py Sun Jun 4 14:49:05 2006
@@ -16,143 +16,141 @@
import os, errno
from pypy.rpython.lltypesystem.lltype import \
GcStruct, Signed, Array, Char, Ptr, malloc
-from pypy.rpython.module.support import to_rstr, from_rstr, ll_strcpy, _ll_strfill
+from pypy.rpython.module.support import ll_strcpy, _ll_strfill
from pypy.rpython.module.support import to_opaque_object, from_opaque_object
from pypy.rpython import ros
from pypy.rpython.rarithmetic import r_longlong
+from pypy.tool.staticmethods import ClassMethods
+import stat
-def ll_read_into(fd, buffer):
- data = os.read(fd, len(buffer.chars))
- _ll_strfill(buffer, data, len(data))
- return len(data)
-ll_read_into.suggested_primitive = True
-
-def ll_os_read(fd, count):
- from pypy.rpython.lltypesystem.rstr import STR
- if count < 0:
- raise OSError(errno.EINVAL, None)
- buffer = malloc(STR, count)
- n = ll_read_into(fd, buffer)
- if n != count:
- s = malloc(STR, n)
- ll_strcpy(s, buffer, n)
- buffer = s
- return buffer
-
-
-
-def ll_os_close(fd):
- os.close(fd)
-ll_os_close.suggested_primitive = True
-
-
-
-def ll_os_dup(fd):
- return os.dup(fd)
-ll_os_dup.suggested_primitive = True
-
-def ll_os_lseek(fd,pos,how):
- return r_longlong(os.lseek(fd,pos,how))
-ll_os_lseek.suggested_primitive = True
-
-def ll_os_isatty(fd):
- return os.isatty(fd)
-ll_os_isatty.suggested_primitive = True
-
-def ll_os_ftruncate(fd,len):
- return os.ftruncate(fd,len)
-ll_os_ftruncate.suggested_primitive = True
-
-n = 10
-fieldnames = ['item%d' % i for i in range(n)]
-lltypes = [Signed]*n
-fields = tuple(zip(fieldnames, lltypes))
-STAT_RESULT = GcStruct('tuple%d' % n, *fields)
-
-from pypy.rpython.rarithmetic import intmask
-
-def ll_stat_result(stat0, stat1, stat2, stat3, stat4,
- stat5, stat6, stat7, stat8, stat9):
- tup = malloc(STAT_RESULT)
- tup.item0 = intmask(stat0)
- tup.item1 = intmask(stat1)
- tup.item2 = intmask(stat2)
- tup.item3 = intmask(stat3)
- tup.item4 = intmask(stat4)
- tup.item5 = intmask(stat5)
- tup.item6 = intmask(stat6)
- tup.item7 = intmask(stat7)
- tup.item8 = intmask(stat8)
- tup.item9 = intmask(stat9)
- return tup
-
-def ll_os_fstat(fd):
- (stat0, stat1, stat2, stat3, stat4,
- stat5, stat6, stat7, stat8, stat9) = os.fstat(fd)
- return ll_stat_result(stat0, stat1, stat2, stat3, stat4,
- stat5, stat6, stat7, stat8, stat9)
-ll_os_fstat.suggested_primitive = True
-
-def ll_os_stat(path):
- (stat0, stat1, stat2, stat3, stat4,
- stat5, stat6, stat7, stat8, stat9) = os.stat(from_rstr(path))
- return ll_stat_result(stat0, stat1, stat2, stat3, stat4,
- stat5, stat6, stat7, stat8, stat9)
-ll_os_stat.suggested_primitive = True
-
-def ll_os_strerror(errnum):
- return to_rstr(os.strerror(errnum))
-ll_os_strerror.suggested_primitive = True
-
-def ll_os_system(cmd):
- return os.system(from_rstr(cmd))
-ll_os_system.suggested_primitive = True
-
-def ll_os_unlink(path):
- os.unlink(from_rstr(path))
-ll_os_unlink.suggested_primitive = True
-
-def ll_os_chdir(path):
- os.chdir(from_rstr(path))
-ll_os_chdir.suggested_primitive = True
-
-def ll_os_mkdir(path, mode):
- os.mkdir(from_rstr(path), mode)
-ll_os_mkdir.suggested_primitive = True
-
-def ll_os_rmdir(path):
- os.rmdir(from_rstr(path))
-ll_os_rmdir.suggested_primitive = True
-
-# this function is not really the os thing, but the internal one.
-def ll_os_putenv(name_eq_value):
- ros.putenv(from_rstr(name_eq_value))
-ll_os_putenv.suggested_primitive = True
-
-def ll_os_unsetenv(name):
- os.unsetenv(from_rstr(name))
-ll_os_unsetenv.suggested_primitive = True
-
-# get the initial environment by indexing
-def ll_os_environ(idx):
- return ros.environ(idx)
-ll_os_environ.suggested_primitive = True
-
-# ____________________________________________________________
-# opendir/readdir
-
-def ll_os_opendir(dirname):
- dir = ros.opendir(from_rstr(dirname))
- return to_opaque_object(dir)
-ll_os_opendir.suggested_primitive = True
-
-def ll_os_readdir(opaquedir):
- dir = from_opaque_object(opaquedir)
- nextentry = dir.readdir()
- return to_rstr(nextentry)
-ll_os_readdir.suggested_primitive = True
-
-def ll_os_closedir(opaquedir):
- dir = from_opaque_object(opaquedir)
- dir.closedir()
-ll_os_closedir.suggested_primitive = True
+
+
+
+class BaseOS:
+ __metaclass__ = ClassMethods
+
+ def ll_os_open(cls, fname, flag, mode):
+ return os.open(cls.from_rstr(fname), flag, mode)
+ ll_os_open.suggested_primitive = True
+
+ def ll_os_write(cls, fd, astring):
+ return os.write(fd, cls.from_rstr(astring))
+ ll_os_write.suggested_primitive = True
+
+ def ll_os_getcwd(cls):
+ return cls.to_rstr(os.getcwd())
+ ll_os_getcwd.suggested_primitive = True
+
+ def ll_read_into(fd, buffer):
+ data = os.read(fd, len(buffer.chars))
+ _ll_strfill(buffer, data, len(data))
+ return len(data)
+ ll_read_into.suggested_primitive = True
+ ll_read_into = staticmethod(ll_read_into)
+
+ def ll_os_read(cls, fd, count):
+ from pypy.rpython.lltypesystem.rstr import STR
+ if count < 0:
+ raise OSError(errno.EINVAL, None)
+ buffer = malloc(STR, count)
+ n = cls.ll_read_into(fd, buffer)
+ if n != count:
+ s = malloc(STR, n)
+ ll_strcpy(s, buffer, n)
+ buffer = s
+ return buffer
+
+
+
+ def ll_os_close(cls, fd):
+ os.close(fd)
+ ll_os_close.suggested_primitive = True
+
+
+
+ def ll_os_dup(cls, fd):
+ return os.dup(fd)
+ ll_os_dup.suggested_primitive = True
+
+ def ll_os_lseek(cls, fd,pos,how):
+ return r_longlong(os.lseek(fd,pos,how))
+ ll_os_lseek.suggested_primitive = True
+
+ def ll_os_isatty(cls, fd):
+ return os.isatty(fd)
+ ll_os_isatty.suggested_primitive = True
+
+ def ll_os_ftruncate(cls, fd,len):
+ return os.ftruncate(fd,len)
+ ll_os_ftruncate.suggested_primitive = True
+
+ def ll_os_fstat(cls, fd):
+ (stat0, stat1, stat2, stat3, stat4,
+ stat5, stat6, stat7, stat8, stat9) = os.fstat(fd)
+ return cls.ll_stat_result(stat0, stat1, stat2, stat3, stat4,
+ stat5, stat6, stat7, stat8, stat9)
+ ll_os_fstat.suggested_primitive = True
+
+ def ll_os_stat(cls, path):
+ (stat0, stat1, stat2, stat3, stat4,
+ stat5, stat6, stat7, stat8, stat9) = os.stat(cls.from_rstr(path))
+ return cls.ll_stat_result(stat0, stat1, stat2, stat3, stat4,
+ stat5, stat6, stat7, stat8, stat9)
+ ll_os_stat.suggested_primitive = True
+
+ def ll_os_strerror(cls, errnum):
+ return cls.to_rstr(os.strerror(errnum))
+ ll_os_strerror.suggested_primitive = True
+
+ def ll_os_system(cls, cmd):
+ return os.system(cls.from_rstr(cmd))
+ ll_os_system.suggested_primitive = True
+
+ def ll_os_unlink(cls, path):
+ os.unlink(cls.from_rstr(path))
+ ll_os_unlink.suggested_primitive = True
+
+ def ll_os_chdir(cls, path):
+ os.chdir(cls.from_rstr(path))
+ ll_os_chdir.suggested_primitive = True
+
+ def ll_os_mkdir(cls, path, mode):
+ os.mkdir(cls.from_rstr(path), mode)
+ ll_os_mkdir.suggested_primitive = True
+
+ def ll_os_rmdir(cls, path):
+ os.rmdir(cls.from_rstr(path))
+ ll_os_rmdir.suggested_primitive = True
+
+ # this function is not really the os thing, but the internal one.
+ def ll_os_putenv(cls, name_eq_value):
+ ros.putenv(cls.from_rstr(name_eq_value))
+ ll_os_putenv.suggested_primitive = True
+
+ def ll_os_unsetenv(cls, name):
+ os.unsetenv(cls.from_rstr(name))
+ ll_os_unsetenv.suggested_primitive = True
+
+ # get the initial environment by indexing
+ def ll_os_environ(cls, idx):
+ return ros.environ(idx)
+ ll_os_environ.suggested_primitive = True
+
+ # ____________________________________________________________
+ # opendir/readdir
+
+ def ll_os_opendir(cls, dirname):
+ dir = ros.opendir(cls.from_rstr(dirname))
+ return to_opaque_object(dir)
+ ll_os_opendir.suggested_primitive = True
+
+ def ll_os_readdir(cls, opaquedir):
+ dir = from_opaque_object(opaquedir)
+ nextentry = dir.readdir()
+ return cls.to_rstr(nextentry)
+ ll_os_readdir.suggested_primitive = True
+
+ def ll_os_closedir(cls, opaquedir):
+ dir = from_opaque_object(opaquedir)
+ dir.closedir()
+ ll_os_closedir.suggested_primitive = True
Modified: pypy/dist/pypy/rpython/module/ll_os_path.py
==============================================================================
--- pypy/dist/pypy/rpython/module/ll_os_path.py (original)
+++ pypy/dist/pypy/rpython/module/ll_os_path.py Sun Jun 4 14:49:05 2006
@@ -4,26 +4,29 @@
# see ll_os.py for comments
-import os
import stat
-from pypy.rpython.module.support import to_rstr, from_rstr, ll_strcpy
-from pypy.rpython.module.ll_os import ll_os_stat
+import os
+from pypy.tool.staticmethods import ClassMethods
# Does a path exist?
# This is false for dangling symbolic links.
-def ll_os_path_exists(path):
- """Test whether a path exists"""
- try:
- st = ll_os_stat(path)
- except OSError:
- return False
- return True
-
-def ll_os_path_isdir(path):
- try:
- st = ll_os_stat(path)
- except OSError:
- return False
- return stat.S_ISDIR(st.item0)
+class BaseOsPath:
+ __metaclass__ = ClassMethods
+
+ def ll_os_path_exists(cls, path):
+ """Test whether a path exists"""
+ try:
+ st = os.stat(cls.from_rstr(path))
+ except OSError:
+ return False
+ return True
+
+ def ll_os_path_isdir(cls, path):
+ try:
+ (stat0, stat1, stat2, stat3, stat4,
+ stat5, stat6, stat7, stat8, stat9) = os.stat(cls.from_rstr(path))
+ except OSError:
+ return False
+ return stat.S_ISDIR(stat0)
Modified: pypy/dist/pypy/rpython/module/ll_strtod.py
==============================================================================
--- pypy/dist/pypy/rpython/module/ll_strtod.py (original)
+++ pypy/dist/pypy/rpython/module/ll_strtod.py Sun Jun 4 14:49:05 2006
@@ -1,15 +1,15 @@
# string -> float helper
from pypy.rpython import rarithmetic
-from pypy.rpython.module.support import to_rstr, from_rstr, ll_strcpy
+from pypy.rpython.module.support import LLSupport, ll_strcpy
def ll_strtod_parts_to_float(sign, beforept, afterpt, exponent):
- return rarithmetic.parts_to_float(from_rstr(sign),
- from_rstr(beforept),
- from_rstr(afterpt),
- from_rstr(exponent))
+ return rarithmetic.parts_to_float(LLSupport.from_rstr(sign),
+ LLSupport.from_rstr(beforept),
+ LLSupport.from_rstr(afterpt),
+ LLSupport.from_rstr(exponent))
ll_strtod_parts_to_float.suggested_primitive = True
def ll_strtod_formatd(fmt, x):
- return to_rstr(rarithmetic.formatd(from_rstr(fmt), x))
+ return LLSupport.to_rstr(rarithmetic.formatd(LLSupport.from_rstr(fmt), x))
ll_strtod_formatd.suggested_primitive = True
Modified: pypy/dist/pypy/rpython/module/support.py
==============================================================================
--- pypy/dist/pypy/rpython/module/support.py (original)
+++ pypy/dist/pypy/rpython/module/support.py Sun Jun 4 14:49:05 2006
@@ -1,24 +1,46 @@
from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.ootypesystem import ootype
from pypy.rpython import extfunctable
from pypy.rpython.lltypesystem.lltype import \
GcStruct, Signed, Array, Char, Ptr, malloc
-
# utility conversion functions
-def to_rstr(s):
- from pypy.rpython.lltypesystem.rstr import STR
- if s is None:
- return lltype.nullptr(STR)
- p = malloc(STR, len(s))
- for i in range(len(s)):
- p.chars[i] = s[i]
- return p
-
-def from_rstr(rs):
- if not rs: # null pointer
- return None
- else:
- return ''.join([rs.chars[i] for i in range(len(rs.chars))])
+
+class LLSupport:
+ _mixin_ = True
+
+ def to_rstr(s):
+ from pypy.rpython.lltypesystem.rstr import STR
+ if s is None:
+ return lltype.nullptr(STR)
+ p = malloc(STR, len(s))
+ for i in range(len(s)):
+ p.chars[i] = s[i]
+ return p
+ to_rstr = staticmethod(to_rstr)
+
+ def from_rstr(rs):
+ if not rs: # null pointer
+ return None
+ else:
+ return ''.join([rs.chars[i] for i in range(len(rs.chars))])
+ from_rstr = staticmethod(from_rstr)
+
+
+class OOSupport:
+ _mixin_ = True
+
+ def to_rstr(s):
+ return ootype.oostring(s, -1)
+ to_rstr = staticmethod(to_rstr)
+
+ def from_rstr(rs):
+ if not rs: # null pointer
+ return None
+ else:
+ return "".join([rs.ll_stritem_nonneg(i) for i in range(rs.ll_strlen())])
+ from_rstr = staticmethod(from_rstr)
+
def ll_strcpy(dst_s, src_s, n):
dstchars = dst_s.chars
Modified: pypy/dist/pypy/rpython/module/test/test_ll_os.py
==============================================================================
--- pypy/dist/pypy/rpython/module/test/test_ll_os.py (original)
+++ pypy/dist/pypy/rpython/module/test/test_ll_os.py Sun Jun 4 14:49:05 2006
@@ -1,44 +1,44 @@
import os
from pypy.tool.udir import udir
-from pypy.rpython.lltypesystem.module.ll_os import *
+from pypy.rpython.lltypesystem.module.ll_os import Implementation as impl
def test_open_read_write_close():
filename = str(udir.join('test_open_read_write_close.txt'))
- rsfilename = to_rstr(filename)
+ rsfilename = impl.to_rstr(filename)
- fd = ll_os_open(rsfilename, os.O_WRONLY | os.O_CREAT, 0777)
- count = ll_os_write(fd, to_rstr("hello world\n"))
+ fd = impl.ll_os_open(rsfilename, os.O_WRONLY | os.O_CREAT, 0777)
+ count = impl.ll_os_write(fd, impl.to_rstr("hello world\n"))
assert count == len("hello world\n")
- ll_os_close(fd)
+ impl.ll_os_close(fd)
- fd = ll_os_open(rsfilename, os.O_RDONLY, 0777)
- data = ll_os_read(fd, 500)
- assert from_rstr(data) == "hello world\n"
- ll_os_close(fd)
+ fd = impl.ll_os_open(rsfilename, os.O_RDONLY, 0777)
+ data = impl.ll_os_read(fd, 500)
+ assert impl.from_rstr(data) == "hello world\n"
+ impl.ll_os_close(fd)
os.unlink(filename)
def test_getcwd():
- data = ll_os_getcwd()
- assert from_rstr(data) == os.getcwd()
+ data = impl.ll_os_getcwd()
+ assert impl.from_rstr(data) == os.getcwd()
def test_strerror():
- data = ll_os_strerror(2)
- assert from_rstr(data) == os.strerror(2)
+ data = impl.ll_os_strerror(2)
+ assert impl.from_rstr(data) == os.strerror(2)
def test_system():
filename = str(udir.join('test_system.txt'))
- arg = to_rstr('python -c "print 1+1" > %s' % filename)
- data = ll_os_system(arg)
+ arg = impl.to_rstr('python -c "print 1+1" > %s' % filename)
+ data = impl.ll_os_system(arg)
assert data == 0
assert file(filename).read().strip() == '2'
os.unlink(filename)
def test_putenv_unsetenv():
filename = str(udir.join('test_putenv.txt'))
- arg = to_rstr('abcdefgh=12345678')
- ll_os_putenv(arg)
+ arg = impl.to_rstr('abcdefgh=12345678')
+ impl.ll_os_putenv(arg)
cmd = '''python -c "import os; print os.environ['abcdefgh']" > %s''' % filename
os.system(cmd)
f = file(filename)
@@ -48,7 +48,7 @@
os.unlink(filename)
posix = __import__(os.name)
if hasattr(posix, "unsetenv"):
- ll_os_unsetenv(to_rstr("abcdefgh"))
+ impl.ll_os_unsetenv(impl.to_rstr("abcdefgh"))
cmd = '''python -c "import os; print repr(os.getenv('abcdefgh'))" > %s''' % filename
os.system(cmd)
f = file(filename)
@@ -60,12 +60,12 @@
test_src = """
import os
from pypy.tool.udir import udir
-from pypy.rpython.module.ll_os import *
+from pypy.rpython.lltypesystem.module.ll_os import Implementation as impl
def test_environ():
count = 0
while 1:
- if not ll_os_environ(count):
+ if not impl.ll_os_environ(count):
break
count += 1
channel.send(count == len(os.environ.keys()))
@@ -82,17 +82,17 @@
def test_opendir_readdir():
dirname = str(udir)
- rsdirname = to_rstr(dirname)
+ rsdirname = impl.to_rstr(dirname)
result = []
- DIR = ll_os_opendir(rsdirname)
+ DIR = impl.ll_os_opendir(rsdirname)
try:
while True:
- nextentry = ll_os_readdir(DIR)
+ nextentry = impl.ll_os_readdir(DIR)
if not nextentry: # null pointer check
break
- result.append(from_rstr(nextentry))
+ result.append(impl.from_rstr(nextentry))
finally:
- ll_os_closedir(DIR)
+ impl.ll_os_closedir(DIR)
assert '.' in result
assert '..' in result
result.remove('.')
Modified: pypy/dist/pypy/rpython/module/test/test_ll_os_path.py
==============================================================================
--- pypy/dist/pypy/rpython/module/test/test_ll_os_path.py (original)
+++ pypy/dist/pypy/rpython/module/test/test_ll_os_path.py Sun Jun 4 14:49:05 2006
@@ -2,15 +2,15 @@
import os
-from pypy.rpython.module.ll_os_path import *
-from pypy.rpython.module.support import to_rstr, from_rstr, ll_strcpy
+from pypy.rpython.lltypesystem.module.ll_os_path import Implementation as impl
+from pypy.rpython.module.support import ll_strcpy
from pypy.rpython.test.test_llinterp import interpret
from pypy.tool.udir import udir
def test_exists():
- filename = to_rstr(str(py.magic.autopath()))
- assert ll_os_path_exists(filename) == True
- assert not ll_os_path_exists(to_rstr(
+ filename = impl.to_rstr(str(py.magic.autopath()))
+ assert impl.ll_os_path_exists(filename) == True
+ assert not impl.ll_os_path_exists(impl.to_rstr(
"strange_filename_that_looks_improbable.sde"))
def test_posixpath():
Modified: pypy/dist/pypy/rpython/module/test/test_ll_strtod.py
==============================================================================
--- pypy/dist/pypy/rpython/module/test/test_ll_strtod.py (original)
+++ pypy/dist/pypy/rpython/module/test/test_ll_strtod.py Sun Jun 4 14:49:05 2006
@@ -1,6 +1,6 @@
from pypy.rpython.module.ll_strtod import ll_strtod_parts_to_float, ll_strtod_formatd
-from pypy.rpython.module.support import to_rstr, from_rstr
+from pypy.rpython.module.support import LLSupport
def test_parts_to_float():
@@ -14,9 +14,9 @@
]
for parts, val in data:
- assert ll_strtod_parts_to_float(*map(to_rstr, parts)) == val
+ assert ll_strtod_parts_to_float(*map(LLSupport.to_rstr, parts)) == val
def test_formatd():
- res = ll_strtod_formatd(to_rstr("%.2f"), 1.5)
- assert from_rstr(res) == "1.50"
+ res = ll_strtod_formatd(LLSupport.to_rstr("%.2f"), 1.5)
+ assert LLSupport.from_rstr(res) == "1.50"
Modified: pypy/dist/pypy/rpython/ootypesystem/module/ll_os.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/module/ll_os.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/module/ll_os.py Sun Jun 4 14:49:05 2006
@@ -1,15 +1,30 @@
-import os
-from pypy.rpython.ootypesystem.module.support import from_rstr, to_rstr
+from pypy.rpython.module.support import OOSupport
+from pypy.rpython.module.ll_os import BaseOS
+from pypy.rpython.ootypesystem import ootype
+from pypy.rpython.rarithmetic import intmask
-def ll_os_open(fname, flag, mode):
- return os.open(from_rstr(fname), flag, mode)
-ll_os_open.suggested_primitive = True
+n = 10
+fieldnames = ['item%d' % i for i in range(n)]
+lltypes = [ootype.Signed]*n
+fields = dict(zip(fieldnames, lltypes))
+STAT_RESULT = ootype.Record(fields)
-def ll_os_write(fd, astring):
- return os.write(fd, from_rstr(astring))
-ll_os_write.suggested_primitive = True
+class Implementation(BaseOS, OOSupport):
+
+ def ll_stat_result(stat0, stat1, stat2, stat3, stat4,
+ stat5, stat6, stat7, stat8, stat9):
+ tup = ootype.new(STAT_RESULT)
+ tup.item0 = intmask(stat0)
+ tup.item1 = intmask(stat1)
+ tup.item2 = intmask(stat2)
+ tup.item3 = intmask(stat3)
+ tup.item4 = intmask(stat4)
+ tup.item5 = intmask(stat5)
+ tup.item6 = intmask(stat6)
+ tup.item7 = intmask(stat7)
+ tup.item8 = intmask(stat8)
+ tup.item9 = intmask(stat9)
+ return tup
+ ll_stat_result = staticmethod(ll_stat_result)
-def ll_os_getcwd():
- return to_rstr(os.getcwd())
-ll_os_getcwd.suggested_primitive = True
Added: pypy/dist/pypy/rpython/ootypesystem/module/ll_os_path.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/ootypesystem/module/ll_os_path.py Sun Jun 4 14:49:05 2006
@@ -0,0 +1,5 @@
+from pypy.rpython.module.support import OOSupport
+from pypy.rpython.module.ll_os_path import BaseOsPath
+
+class Implementation(BaseOsPath, OOSupport):
+ pass
Modified: pypy/dist/pypy/rpython/rgenop.py
==============================================================================
--- pypy/dist/pypy/rpython/rgenop.py (original)
+++ pypy/dist/pypy/rpython/rgenop.py Sun Jun 4 14:49:05 2006
@@ -9,7 +9,7 @@
from pypy.translator.simplify import eliminate_empty_blocks, join_blocks
from pypy.rpython.module.support import init_opaque_object
from pypy.rpython.module.support import to_opaque_object, from_opaque_object
-from pypy.rpython.module.support import from_rstr
+from pypy.rpython.module.support import LLSupport
from pypy.rpython.extregistry import ExtRegistryEntry
@@ -54,7 +54,7 @@
# is opname a runtime value?
def genop(blockcontainer, opname, vars, gv_RESULT_TYPE):
if not isinstance(opname, str):
- opname = from_rstr(opname)
+ opname = LLSupport.from_rstr(opname)
block = from_opaque_object(blockcontainer.obj)
assert block.exits == [], "block already closed"
RESULT_TYPE = from_opaque_object(gv_RESULT_TYPE).value
Modified: pypy/dist/pypy/rpython/test/test_rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rbuiltin.py (original)
+++ pypy/dist/pypy/rpython/test/test_rbuiltin.py Sun Jun 4 14:49:05 2006
@@ -6,67 +6,9 @@
from pypy.tool import udir
from pypy.rpython.rarithmetic import r_uint, intmask
from pypy.annotation.builtin import *
-from pypy.rpython.module.support import to_rstr
from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
import py
-def test_rbuiltin_list():
- def f():
- l=list((1,2,3))
- return l == [1,2,3]
- def g():
- l=list(('he','llo'))
- return l == ['he','llo']
- def r():
- l = ['he','llo']
- l1=list(l)
- return l == l1 and l is not l1
- result = interpret(f,[])
- assert result
-
- result = interpret(g,[])
- assert result
-
- result = interpret(r,[])
- assert result
-
-def test_int_min():
- def fn(i, j):
- return min(i,j)
- ev_fun = interpret(fn, [0, 0])
- assert interpret(fn, (1, 2)) == 1
- assert interpret(fn, (1, -1)) == -1
- assert interpret(fn, (2, 2)) == 2
- assert interpret(fn, (-1, -12)) == -12
-
-def test_int_max():
- def fn(i, j):
- return max(i,j)
- assert interpret(fn, (1, 2)) == 2
- assert interpret(fn, (1, -1)) == 1
- assert interpret(fn, (2, 2)) == 2
- assert interpret(fn, (-1, -12)) == -1
-
-def test_builtin_math_floor():
- import math
- def fn(f):
- return math.floor(f)
- import random
- for i in range(5):
- rv = 1000 * float(i-10) #random.random()
- res = interpret(fn, [rv])
- assert fn(rv) == res
-
-def test_builtin_math_fmod():
- import math
- def fn(f,y):
- return math.fmod(f,y)
-
- for i in range(10):
- for j in range(10):
- rv = 1000 * float(i-10)
- ry = 100 * float(i-10) +0.1
- assert fn(rv,ry) == interpret(fn, (rv, ry))
def enum_direct_calls(translator, func):
blocks = []
@@ -76,173 +18,8 @@
if op.opname == 'direct_call':
yield op
-def test_os_dup():
- import os
- def fn(fd):
- return os.dup(fd)
- res = interpret(fn, [0])
- try:
- os.close(res)
- except OSError:
- pass
- count = 0
- from pypy.rpython.module import ll_os
- for dir_call in enum_direct_calls(test_llinterp.typer.annotator.translator, fn):
- cfptr = dir_call.args[0]
- assert cfptr.value._obj._callable == ll_os.ll_os_dup
- count += 1
- assert count == 1
-
-def test_os_open():
- tmpdir = str(udir.udir.join("os_open_test"))
- import os
- def wr_open(fname):
- return os.open(fname, os.O_WRONLY|os.O_CREAT, 0777)
- def f():
- return wr_open(tmpdir)
- res = interpret(f, [])
- os.close(res)
- count = 0
- from pypy.rpython.lltypesystem.module import ll_os
- for dir_call in enum_direct_calls(test_llinterp.typer.annotator.translator, wr_open):
- cfptr = dir_call.args[0]
- assert cfptr.value._obj._callable == ll_os.ll_os_open
- count += 1
- assert count == 1
-
-def test_os_path_exists():
- import os
- def f(fn):
- return os.path.exists(fn)
- filename = to_rstr(str(py.magic.autopath()))
- assert interpret(f, [filename]) == True
- assert interpret(f, [
- to_rstr("strange_filename_that_looks_improbable.sde")]) == False
-
-def test_os_isdir():
- import os
- def f(fn):
- return os.path.isdir(fn)
- assert interpret(f, [to_rstr("/")]) == True
- assert interpret(f, [to_rstr(str(py.magic.autopath()))]) == False
- assert interpret(f, [to_rstr("another/unlikely/directory/name")]) == False
-def test_pbc_isTrue():
- class C:
- def f(self):
- pass
-
- def g(obj):
- return bool(obj)
- def fn(neg):
- c = C.f
- return g(c)
- assert interpret(fn, [True])
- def fn(neg):
- c = None
- return g(c)
- assert not interpret(fn, [True])
-
-def test_instantiate():
- class A:
- pass
- def f():
- return instantiate(A)
- res = interpret(f, [])
- assert res.super.typeptr.name[0] == 'A'
-
-def test_instantiate_multiple():
- class A:
- pass
- class B(A):
- pass
- def f(i):
- if i == 1:
- cls = A
- else:
- cls = B
- return instantiate(cls)
- res = interpret(f, [1])
- assert res.super.typeptr.name[0] == 'A'
- res = interpret(f, [2])
- assert res.super.typeptr.name[0] == 'B'
-
-
-def test_isinstance_obj():
- _1 = lltype.pyobjectptr(1)
- def f(x):
- return isinstance(x, int)
- res = interpret(f, [_1], someobjects=True)
- assert res is True
- _1_0 = lltype.pyobjectptr(1.0)
- res = interpret(f, [_1_0], someobjects=True)
- assert res is False
-
-
-def test_const_isinstance():
- class B(object):
- pass
- def f():
- b = B()
- return isinstance(b, B)
- res = interpret(f, [])
- assert res is True
-
-def test_isinstance():
- class A(object):
- pass
- class B(A):
- pass
- class C(A):
- pass
- def f(x, y):
- if x == 1:
- a = A()
- elif x == 2:
- a = B()
- else:
- a = C()
- if y == 1:
- res = isinstance(a, A)
- cls = A
- elif y == 2:
- res = isinstance(a, B)
- cls = B
- else:
- res = isinstance(a, C)
- cls = C
- return int(res) + 2 * isinstance(a, cls)
- for x in [1, 2, 3]:
- for y in [1, 2, 3]:
- res = interpret(f, [x, y])
- assert res == isinstance([A(), B(), C()][x-1], [A, B, C][y-1]) * 3
-
-def test_isinstance_list():
- def f(i):
- if i == 0:
- l = []
- else:
- l = None
- return isinstance(l, list)
- res = interpret(f, [0])
- assert res is True
- res = interpret(f, [1])
- assert res is False
-
-def test_hasattr():
- class A(object):
- def __init__(self):
- self.x = 42
- def f(i):
- a = A()
- if i==0: return int(hasattr(A, '__init__'))
- if i==1: return int(hasattr(A, 'y'))
- if i==2: return int(hasattr(42, 'x'))
- for x, y in zip(range(3), (1, 0, 0)):
- res = interpret(f, [x])
- assert res._obj.value == y
- # hmm, would like to test against PyObj, is this the wrong place/way?
def test_we_are_translated():
def f():
@@ -333,6 +110,65 @@
class BaseTestExtfunc(BaseRtypingTest):
+ def test_rbuiltin_list(self):
+ def f():
+ l=list((1,2,3))
+ return l == [1,2,3]
+ def g():
+ l=list(('he','llo'))
+ return l == ['he','llo']
+ def r():
+ l = ['he','llo']
+ l1=list(l)
+ return l == l1 and l is not l1
+ result = self.interpret(f,[])
+ assert result
+
+ result = self.interpret(g,[])
+ assert result
+
+ result = self.interpret(r,[])
+ assert result
+
+ def test_int_min(self):
+ def fn(i, j):
+ return min(i,j)
+ ev_fun = self.interpret(fn, [0, 0])
+ assert self.interpret(fn, (1, 2)) == 1
+ assert self.interpret(fn, (1, -1)) == -1
+ assert self.interpret(fn, (2, 2)) == 2
+ assert self.interpret(fn, (-1, -12)) == -12
+
+ def test_int_max(self):
+ def fn(i, j):
+ return max(i,j)
+ assert self.interpret(fn, (1, 2)) == 2
+ assert self.interpret(fn, (1, -1)) == 1
+ assert self.interpret(fn, (2, 2)) == 2
+ assert self.interpret(fn, (-1, -12)) == -1
+
+ def test_builtin_math_floor(self):
+ import math
+ def fn(f):
+ return math.floor(f)
+ import random
+ for i in range(5):
+ rv = 1000 * float(i-10) #random.random()
+ res = self.interpret(fn, [rv])
+ assert fn(rv) == res
+
+ def test_builtin_math_fmod(self):
+ import math
+ def fn(f,y):
+ return math.fmod(f,y)
+
+ for i in range(10):
+ for j in range(10):
+ rv = 1000 * float(i-10)
+ ry = 100 * float(i-10) +0.1
+ assert fn(rv,ry) == self.interpret(fn, (rv, ry))
+
+
def test_os_getcwd(self):
import os
def fn():
@@ -354,9 +190,58 @@
hello = open(tmpdir).read()
assert hello == "hello world"
-class TestOOtype(BaseTestExtfunc, OORtypeMixin):
- pass
+ def test_os_dup(self):
+ import os
+ def fn(fd):
+ return os.dup(fd)
+ res = self.interpret(fn, [0])
+ try:
+ os.close(res)
+ except OSError:
+ pass
+ count = 0
+ for dir_call in enum_direct_calls(test_llinterp.typer.annotator.translator, fn):
+ cfptr = dir_call.args[0]
+ assert self.get_callable(cfptr.value) == self.ll_os.Implementation.ll_os_dup.im_func
+ count += 1
+ assert count == 1
-class TestLLtype(BaseTestExtfunc, LLRtypeMixin):
- pass
+ def test_os_open(self):
+ tmpdir = str(udir.udir.join("os_open_test"))
+ import os
+ def wr_open(fname):
+ return os.open(fname, os.O_WRONLY|os.O_CREAT, 0777)
+ def f():
+ return wr_open(tmpdir)
+ res = self.interpret(f, [])
+ os.close(res)
+ count = 0
+ for dir_call in enum_direct_calls(test_llinterp.typer.annotator.translator, wr_open):
+ cfptr = dir_call.args[0]
+ assert self.get_callable(cfptr.value) == self.ll_os.Implementation.ll_os_open.im_func
+ count += 1
+ assert count == 1
+
+ def test_os_path_exists(self):
+ import os
+ def f(fn):
+ return os.path.exists(fn)
+ filename = self.string_to_ll(str(py.magic.autopath()))
+ assert self.interpret(f, [filename]) == True
+ assert self.interpret(f, [
+ self.string_to_ll("strange_filename_that_looks_improbable.sde")]) == False
+
+ def test_os_isdir(self):
+ import os
+ def f(fn):
+ return os.path.isdir(fn)
+ assert self.interpret(f, [self.string_to_ll("/")]) == True
+ assert self.interpret(f, [self.string_to_ll(str(py.magic.autopath()))]) == False
+ assert self.interpret(f, [self.string_to_ll("another/unlikely/directory/name")]) == False
+
+class TestLLtype(BaseTestExtfunc, LLRtypeMixin):
+ from pypy.rpython.lltypesystem.module import ll_os
+
+class TestOOtype(BaseTestExtfunc, OORtypeMixin):
+ from pypy.rpython.ootypesystem.module import ll_os
Modified: pypy/dist/pypy/rpython/test/tool.py
==============================================================================
--- pypy/dist/pypy/rpython/test/tool.py (original)
+++ pypy/dist/pypy/rpython/test/tool.py Sun Jun 4 14:49:05 2006
@@ -20,9 +20,16 @@
def ll_to_string(self, s):
return ''.join(s.chars)
+ def string_to_ll(self, s):
+ from pypy.rpython.module.support import LLSupport
+ return LLSupport.to_rstr(s)
+
def ll_to_list(self, l):
return map(None, l.ll_items())[:l.ll_length()]
+ def get_callable(self, fnptr):
+ return fnptr._obj._callable
+
def class_name(self, value):
return "".join(value.super.typeptr.name)[:-1]
@@ -43,9 +50,16 @@
def ll_to_string(self, s):
return s._str
+ def string_to_ll(self, s):
+ from pypy.rpython.module.support import OOSupport
+ return OOSupport.to_rstr(s)
+
def ll_to_list(self, l):
return l._list[:]
+ def get_callable(self, sm):
+ return sm._callable
+
def class_name(self, value):
return ootype.dynamicType(value)._name.split(".")[-1]
Modified: pypy/dist/pypy/translator/c/extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/extfunc.py (original)
+++ pypy/dist/pypy/translator/c/extfunc.py Sun Jun 4 14:49:05 2006
@@ -7,34 +7,37 @@
from pypy.rpython.lltypesystem import rlist
from pypy.rpython.module import ll_time, ll_math, ll_strtod
from pypy.rpython.module import ll_stackless, ll_stack
-from pypy.rpython.lltypesystem.module import ll_os
+from pypy.rpython.lltypesystem.module.ll_os import STAT_RESULT, Implementation as impl
from pypy.module.thread.rpython import ll_thread
# table of functions hand-written in src/ll_*.h
+# Note about *.im_func: The annotator and the rtyper expect direct
+# references to functions, so we cannot insert classmethods here.
+
EXTERNALS = {
- ll_os .ll_os_open: 'LL_os_open',
- ll_os .ll_read_into: 'LL_read_into',
- ll_os .ll_os_write: 'LL_os_write',
- ll_os .ll_os_close: 'LL_os_close',
- ll_os .ll_os_dup: 'LL_os_dup',
- ll_os .ll_os_stat: 'LL_os_stat',
- ll_os .ll_os_fstat: 'LL_os_fstat',
- ll_os .ll_os_lseek: 'LL_os_lseek',
- ll_os .ll_os_isatty: 'LL_os_isatty',
- ll_os .ll_os_ftruncate:'LL_os_ftruncate',
- ll_os .ll_os_strerror: 'LL_os_strerror',
- ll_os .ll_os_system: 'LL_os_system',
- ll_os .ll_os_unlink: 'LL_os_unlink',
- ll_os .ll_os_getcwd: 'LL_os_getcwd',
- ll_os .ll_os_chdir: 'LL_os_chdir',
- ll_os .ll_os_mkdir: 'LL_os_mkdir',
- ll_os .ll_os_rmdir: 'LL_os_rmdir',
- ll_os .ll_os_putenv: 'LL_os_putenv',
- ll_os .ll_os_unsetenv:'LL_os_unsetenv',
- ll_os .ll_os_environ: 'LL_os_environ',
- ll_os .ll_os_opendir: 'LL_os_opendir',
- ll_os .ll_os_readdir: 'LL_os_readdir',
- ll_os .ll_os_closedir:'LL_os_closedir',
+ impl.ll_os_open.im_func: 'LL_os_open',
+ impl.ll_read_into: 'LL_read_into', # it's a staticmethod
+ impl.ll_os_write.im_func: 'LL_os_write',
+ impl.ll_os_close.im_func: 'LL_os_close',
+ impl.ll_os_dup.im_func: 'LL_os_dup',
+ impl.ll_os_stat.im_func: 'LL_os_stat',
+ impl.ll_os_fstat.im_func: 'LL_os_fstat',
+ impl.ll_os_lseek.im_func: 'LL_os_lseek',
+ impl.ll_os_isatty.im_func: 'LL_os_isatty',
+ impl.ll_os_ftruncate.im_func:'LL_os_ftruncate',
+ impl.ll_os_strerror.im_func: 'LL_os_strerror',
+ impl.ll_os_system.im_func: 'LL_os_system',
+ impl.ll_os_unlink.im_func: 'LL_os_unlink',
+ impl.ll_os_getcwd.im_func: 'LL_os_getcwd',
+ impl.ll_os_chdir.im_func: 'LL_os_chdir',
+ impl.ll_os_mkdir.im_func: 'LL_os_mkdir',
+ impl.ll_os_rmdir.im_func: 'LL_os_rmdir',
+ impl.ll_os_putenv.im_func: 'LL_os_putenv',
+ impl.ll_os_unsetenv.im_func:'LL_os_unsetenv',
+ impl.ll_os_environ.im_func: 'LL_os_environ',
+ impl.ll_os_opendir.im_func: 'LL_os_opendir',
+ impl.ll_os_readdir.im_func: 'LL_os_readdir',
+ impl.ll_os_closedir.im_func:'LL_os_closedir',
ll_time.ll_time_clock: 'LL_time_clock',
ll_time.ll_time_sleep: 'LL_time_sleep',
ll_time.ll_time_time: 'LL_time_time',
@@ -88,7 +91,7 @@
yield ('RPyListOfString', LIST_OF_STR)
yield ('RPyFREXP_RESULT', ll_math.FREXP_RESULT)
yield ('RPyMODF_RESULT', ll_math.MODF_RESULT)
- yield ('RPySTAT_RESULT', ll_os.STAT_RESULT)
+ yield ('RPySTAT_RESULT', STAT_RESULT)
def predeclare_utility_functions(db, rtyper, optimize=True):
# Common utility functions
More information about the Pypy-commit
mailing list