[pypy-svn] r49399 - pypy/branch/pypy-interp-file/module/_file

arigo at codespeak.net arigo at codespeak.net
Wed Dec 5 19:30:18 CET 2007


Author: arigo
Date: Wed Dec  5 19:30:18 2007
New Revision: 49399

Modified:
   pypy/branch/pypy-interp-file/module/_file/interp_file.py
Log:
file.newlines property.


Modified: pypy/branch/pypy-interp-file/module/_file/interp_file.py
==============================================================================
--- pypy/branch/pypy-interp-file/module/_file/interp_file.py	(original)
+++ pypy/branch/pypy-interp-file/module/_file/interp_file.py	Wed Dec  5 19:30:18 2007
@@ -4,7 +4,7 @@
 from pypy.module._file.interp_stream import StreamErrors, wrap_streamerror
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.gateway import ObjSpace, W_Root, Arguments
-from pypy.interpreter.typedef import TypeDef
+from pypy.interpreter.typedef import TypeDef, GetSetProperty
 from pypy.interpreter.gateway import interp2app
 
 
@@ -281,6 +281,25 @@
     return space.wrap(file)
 descr_file_fdopen.unwrap_spec = [ObjSpace, W_Root, int, str, int]
 
+def descr_file_newlines(space, file):
+    newlines = file.getstream().getnewlines()
+    if newlines == 0:
+        return space.w_None
+    elif newlines == 1:
+        return space.wrap("\r")
+    elif newlines == 2:
+        return space.wrap("\n")
+    elif newlines == 4:
+        return space.wrap("\r\n")
+    result = []
+    if newlines & 1:
+        result.append(space.wrap('\r'))
+    if newlines & 2:
+        result.append(space.wrap('\n'))
+    if newlines & 4:
+        result.append(space.wrap('\r\n'))
+    return space.newtuple(result)
+
 
 W_File.typedef = TypeDef(
     "file",
@@ -303,6 +322,8 @@
 """,
     __new__  = interp2app(descr_file__new__),
     fdopen   = interp2app(descr_file_fdopen, as_classmethod=True),
+    newlines = GetSetProperty(descr_file_newlines, cls=W_File,
+                              doc="end-of-line convention used in this file"),
     **dict([(name, interp2app(getattr(W_File, 'file_' + name)))
                 for name in W_File._exposed_method_names])
     )



More information about the Pypy-commit mailing list