Author: Lars Wassermann <[email protected]>
Branch: 
Changeset: r410:b654baadddd1
Date: 2013-05-23 11:29 +0200
http://bitbucket.org/pypy/lang-smalltalk/changeset/b654baadddd1/

Log:    FilePlugin modifications:
        - added modification time and creation time to the directory lookup
        primitive
        - added FileOpen/Close primitives
        - added FileAtEnd primitive

diff --git a/spyvm/plugins/fileplugin.py b/spyvm/plugins/fileplugin.py
--- a/spyvm/plugins/fileplugin.py
+++ b/spyvm/plugins/fileplugin.py
@@ -1,10 +1,11 @@
-import os
+import os, stat
 from spyvm import model, error
 from spyvm.plugins.plugin import Plugin
 from spyvm.primitives import PrimitiveFailedError, index1_0
 
 
 FilePlugin = Plugin()
+os.stat_float_times(False)
 
 @FilePlugin.expose_primitive(unwrap_spec=[object])
 def primitiveDirectoryDelimitor(interp, s_frame, w_rcvr):
@@ -22,12 +23,52 @@
     if index >= len(contents):
         return space.w_nil
     py_name = sorted(contents)[index]
-    file_path = os.path.join(full_path, py_name)
-    assert os.path.exists(file_path)
+    try:
+        file_path = os.path.join(full_path, py_name)
+    except OSError:
+        raise PrimitiveFailedError
+    file_info = os.stat(file_path)
     name = space.wrap_string(py_name)
-    creationTime = space.wrap_int(0)
-    modificationTime = space.wrap_int(0)
-    dirFlag = space.w_true if os.path.isdir(file_path) else space.w_false
-    fileSize = space.wrap_int(os.path.getsize(file_path))
+    creationTime = smalltalk_timestamp(space, file_info.st_ctime)
+    modificationTime = smalltalk_timestamp(space, file_info.st_mtime)
+    dirFlag = space.w_true if stat.S_IFDIR & file_info.st_mode else 
space.w_false
+    fileSize = space.wrap_int(file_info.st_size)
     return space.wrap_list([name, creationTime, modificationTime, dirFlag, 
fileSize])
 
[email protected]_primitive(unwrap_spec=[object, str, object])
+def primitiveFileOpen(interp, s_frame, w_rcvr, file_path, w_writeable_flag):
+    space = interp.space
+    if w_writeable_flag is space.w_true:
+        mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC
+    else:
+        mode = os.O_RDONLY
+        if not os.path.exists(file_path):
+            return space.w_nil
+    try:
+        file_descriptor = os.open(file_path, mode, 0666)
+    except OSError:
+        raise PrimitiveFailedError()
+    return space.wrap_int(file_descriptor)
+
[email protected]_primitive(unwrap_spec=[object, int])
+def primitiveFileClose(interp, s_frame, w_rcvr, fd):
+    try:
+        os.close(fd)
+    except OSError:
+        raise PrimitiveFailedError()
+    return w_rcvr
+
[email protected]_primitive(unwrap_spec=[object, int])
+def primitiveFileAtEnd(interp, s_frame, w_rcvr, fd):
+    py_file = os.fdopen(fd)
+    stat = os.fstat(fd)
+    if py_file.tell() < stat.st_size:
+        return interp.space.w_true
+    else:
+        return interp.space.w_false
+
+def smalltalk_timestamp(space, sec_since_epoch):
+    import time
+    from spyvm.primitives import secs_between_1901_and_1970
+    sec_since_1901 = sec_since_epoch + secs_between_1901_and_1970
+    return space.wrap_uint(sec_since_1901)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to