Maciej Fijalkowski avatar Maciej Fijalkowski committed 404ed50

seek support

Comments (0)

Files changed (3)

rpython/rlib/rfile.py

 """ 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):
     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__,
 
 
     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)

rpython/rlib/test/test_rfile.py

             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, [])

rpython/rtyper/lltypesystem/rfile.py

 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)
         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:
             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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.