[pypy-commit] pypy file-support-in-rpython: seek support
fijal
noreply at buildbot.pypy.org
Wed Sep 18 14:25:10 CEST 2013
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: file-support-in-rpython
Changeset: r67001:404ed50b9af2
Date: 2013-09-18 13:46 +0200
http://bitbucket.org/pypy/pypy/changeset/404ed50b9af2/
Log: seek support
diff --git a/rpython/rlib/rfile.py b/rpython/rlib/rfile.py
--- a/rpython/rlib/rfile.py
+++ b/rpython/rlib/rfile.py
@@ -2,8 +2,10 @@
""" This file makes open() and friends RPython
"""
+import os
from rpython.annotator.model import SomeObject, SomeString, SomeInteger
from rpython.rtyper.extregistry import ExtRegistryEntry
+from rpython.rtyper.extfunc import register_external
class SomeFile(SomeObject):
def method_write(self, s_arg):
@@ -17,6 +19,11 @@
def method_close(self):
pass
+ def method_seek(self, s_arg, s_whence=None):
+ assert isinstance(s_arg, SomeInteger)
+ if s_whence is not None:
+ assert isinstance(s_whence, SomeInteger)
+
def rtyper_makekey(self):
return self.__class__,
@@ -36,3 +43,8 @@
def specialize_call(self, hop):
return hop.r_result.rtype_constructor(hop)
+
+ #def ll_os_tmpfile():
+ #pass
+
+ #register_external(os.tmpfile, [], SomeFile(), llimpl=ll_os_tmpfile)
diff --git a/rpython/rlib/test/test_rfile.py b/rpython/rlib/test/test_rfile.py
--- a/rpython/rlib/test/test_rfile.py
+++ b/rpython/rlib/test/test_rfile.py
@@ -53,3 +53,16 @@
assert e == ""
self.interpret(f, [])
+
+ def test_seek(self):
+ fname = str(self.tmpdir.join('file_4'))
+
+ def f():
+ f = open(fname, "w+")
+ f.write("xxx")
+ f.seek(0)
+ assert f.read() == "xxx"
+ f.close()
+
+ f()
+ self.interpret(f, [])
diff --git a/rpython/rtyper/lltypesystem/rfile.py b/rpython/rtyper/lltypesystem/rfile.py
--- a/rpython/rtyper/lltypesystem/rfile.py
+++ b/rpython/rtyper/lltypesystem/rfile.py
@@ -27,6 +27,8 @@
c_feof = rffi.llexternal('feof', [lltype.Ptr(FILE)], rffi.INT)
c_ferror = rffi.llexternal('ferror', [lltype.Ptr(FILE)], rffi.INT)
c_clearerror = rffi.llexternal('clearerr', [lltype.Ptr(FILE)], lltype.Void)
+c_fseek = rffi.llexternal('fseek', [lltype.Ptr(FILE), rffi.LONG, rffi.INT],
+ rffi.INT)
def ll_open(name, mode):
file_wrapper = lltype.malloc(FILE_WRAPPER)
@@ -104,6 +106,14 @@
finally:
rffi.keep_buffer_alive_until_here(raw_buf, gc_buf)
return s
+def ll_seek(file_wrapper, pos, whence):
+ ll_file = file_wrapper.file
+ if not ll_file:
+ raise ValueError("I/O operation on closed file")
+ res = c_fseek(ll_file, pos, whence)
+ if res == -1:
+ errno = rposix.get_errno()
+ raise OSError(errno, os.strerror(errno))
def ll_close(file_wrapper):
if file_wrapper.file:
@@ -153,3 +163,13 @@
arg_1 = hop.inputarg(lltype.Signed, 1)
hop.exception_is_here()
return hop.gendirectcall(ll_read, r_self, arg_1)
+
+ def rtype_method_seek(self, hop):
+ r_self = hop.inputarg(self, 0)
+ arg_1 = hop.inputarg(lltype.Signed, 1)
+ if len(hop.args_v) != 3:
+ arg_2 = hop.inputconst(lltype.Signed, os.SEEK_SET)
+ else:
+ arg_2 = hop.inputarg(lltype.Signed, 2)
+ hop.exception_is_here()
+ return hop.gendirectcall(ll_seek, r_self, arg_1, arg_2)
More information about the pypy-commit
mailing list