[pypy-svn] pypy default: cpyext: implement PyFile_WriteString

amauryfa commits-noreply at bitbucket.org
Wed Mar 23 19:06:12 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r42877:00a449667a1c
Date: 2011-03-23 15:09 +0100
http://bitbucket.org/pypy/pypy/changeset/00a449667a1c/

Log:	cpyext: implement PyFile_WriteString

diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -850,13 +850,6 @@
     successful invocation of Py_EnterRecursiveCall()."""
     raise NotImplementedError
 
- at cpython_api([FILE, rffi.CCHARP, rffi.CCHARP, rffi.INT_real], PyObject)
-def PyFile_FromFile(space, fp, name, mode, close):
-    """Create a new PyFileObject from the already-open standard C file
-    pointer, fp.  The function close will be called when the file should be
-    closed.  Return NULL on failure."""
-    raise NotImplementedError
-
 @cpython_api([PyFileObject], lltype.Void)
 def PyFile_IncUseCount(space, p):
     """Increments the PyFileObject's internal use count to indicate
@@ -899,12 +892,6 @@
     borrow_from()
     raise NotImplementedError
 
- at cpython_api([PyFileObject, rffi.INT_real], lltype.Void)
-def PyFile_SetBufSize(space, p, n):
-    """Available on systems with setvbuf() only.  This should only be called
-    immediately after file object creation."""
-    raise NotImplementedError
-
 @cpython_api([PyFileObject, rffi.CCHARP], rffi.INT_real, error=0)
 def PyFile_SetEncoding(space, p, enc):
     """Set the file's encoding for Unicode output to enc. Return 1 on success and 0
@@ -941,12 +928,6 @@
     appropriate exception will be set."""
     raise NotImplementedError
 
- at cpython_api([rffi.CCHARP, PyObject], rffi.INT_real, error=-1)
-def PyFile_WriteString(space, s, p):
-    """Write string s to file object p.  Return 0 on success or -1 on
-    failure; the appropriate exception will be set."""
-    raise NotImplementedError
-
 @cpython_api([], PyObject)
 def PyFloat_GetInfo(space):
     """Return a structseq instance which contains information about the
@@ -2336,28 +2317,6 @@
     (: on Unix, ; on Windows)."""
     raise NotImplementedError
 
- at cpython_api([rffi.CCHARP, ], lltype.Void)
-def PySys_WriteStdout(space, format):
-    """Write the output string described by format to sys.stdout.  No
-    exceptions are raised, even if truncation occurs (see below).
-    
-    format should limit the total size of the formatted output string to
-    1000 bytes or less -- after 1000 bytes, the output string is truncated.
-    In particular, this means that no unrestricted "%s" formats should occur;
-    these should be limited using "%.<N>s" where <N> is a decimal number
-    calculated so that <N> plus the maximum size of other formatted text does not
-    exceed 1000 bytes.  Also watch out for "%f", which can print hundreds of
-    digits for very large numbers.
-    
-    If a problem occurs, or sys.stdout is unset, the formatted message
-    is written to the real (C level) stdout."""
-    raise NotImplementedError
-
- at cpython_api([rffi.CCHARP, ], lltype.Void)
-def PySys_WriteStderr(space, format):
-    """As above, but write to sys.stderr or stderr instead."""
-    raise NotImplementedError
-
 @cpython_api([rffi.INT_real], lltype.Void)
 def Py_Exit(space, status):
     """Exit the current process.  This calls Py_Finalize() and then calls the

diff --git a/pypy/module/cpyext/test/test_pyfile.py b/pypy/module/cpyext/test/test_pyfile.py
--- a/pypy/module/cpyext/test/test_pyfile.py
+++ b/pypy/module/cpyext/test/test_pyfile.py
@@ -60,3 +60,13 @@
     def test_file_setbufsize(self, space, api):
         api.PyFile_SetBufSize()
 
+    def test_file_writestring(self, space, api, capfd):
+        s = rffi.str2charp("test\n")
+        try:
+            api.PyFile_WriteString(s, space.sys.get("stdout"))
+        finally:
+            rffi.free_charp(s)
+        out, err = capfd.readouterr()
+        out = out.replace('\r\n', '\n')
+        assert out == "test\n"
+

diff --git a/pypy/module/cpyext/pyfile.py b/pypy/module/cpyext/pyfile.py
--- a/pypy/module/cpyext/pyfile.py
+++ b/pypy/module/cpyext/pyfile.py
@@ -57,3 +57,12 @@
     """Available on systems with setvbuf() only.  This should only be called
     immediately after file object creation."""
     raise NotImplementedError
+
+ at cpython_api([CONST_STRING, PyObject], rffi.INT_real, error=-1)
+def PyFile_WriteString(space, s, w_p):
+    """Write string s to file object p.  Return 0 on success or -1 on
+    failure; the appropriate exception will be set."""
+    w_s = space.wrap(rffi.charp2str(s))
+    space.call_method(w_p, "write", w_s)
+    return 0
+


More information about the Pypy-commit mailing list