Author: Amaury Forgeot d'Arc <[email protected]>
Branch:
Changeset: r52485:f6349995a199
Date: 2012-02-14 22:06 +0100
http://bitbucket.org/pypy/pypy/changeset/f6349995a199/
Log: cpyext: Implement PyFile_WriteObject() and PyFile_SoftSpace()
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
@@ -1,7 +1,8 @@
from pypy.rpython.lltypesystem import rffi, lltype
from pypy.module.cpyext.api import (
- cpython_api, CONST_STRING, FILEP, build_type_checkers)
+ cpython_api, CANNOT_FAIL, CONST_STRING, FILEP, build_type_checkers)
from pypy.module.cpyext.pyobject import PyObject, borrow_from
+from pypy.module.cpyext.object import Py_PRINT_RAW
from pypy.interpreter.error import OperationError
from pypy.module._file.interp_file import W_File
@@ -61,11 +62,49 @@
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)
+ w_str = space.wrap(rffi.charp2str(s))
+ space.call_method(w_p, "write", w_str)
+ return 0
+
+@cpython_api([PyObject, PyObject, rffi.INT_real], rffi.INT_real, error=-1)
+def PyFile_WriteObject(space, w_obj, w_p, flags):
+ """
+ Write object obj to file object p. The only supported flag for flags is
+ Py_PRINT_RAW; if given, the str() of the object is written
+ instead of the repr(). Return 0 on success or -1 on failure; the
+ appropriate exception will be set."""
+ if rffi.cast(lltype.Signed, flags) & Py_PRINT_RAW:
+ w_str = space.str(w_obj)
+ else:
+ w_str = space.repr(w_obj)
+ space.call_method(w_p, "write", w_str)
return 0
@cpython_api([PyObject], PyObject)
def PyFile_Name(space, w_p):
"""Return the name of the file specified by p as a string object."""
- return borrow_from(w_p, space.getattr(w_p, space.wrap("name")))
\ No newline at end of file
+ return borrow_from(w_p, space.getattr(w_p, space.wrap("name")))
+
+@cpython_api([PyObject, rffi.INT_real], rffi.INT_real, error=CANNOT_FAIL)
+def PyFile_SoftSpace(space, w_p, newflag):
+ """
+ This function exists for internal use by the interpreter. Set the
+ softspace attribute of p to newflag and return the previous value.
+ p does not have to be a file object for this function to work
+ properly; any object is supported (thought its only interesting if
+ the softspace attribute can be set). This function clears any
+ errors, and will return 0 as the previous value if the attribute
+ either does not exist or if there were errors in retrieving it.
+ There is no way to detect errors from this function, but doing so
+ should not be needed."""
+ try:
+ if newflag:
+ w_newflag = space.w_True
+ else:
+ w_newflag = space.w_False
+ oldflag = space.int_w(space.getattr(w_p, space.wrap("softspace")))
+ space.setattr(w_p, space.wrap("softspace"), w_newflag)
+ return oldflag
+ except OperationError, e:
+ return 0
+
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
@@ -1,5 +1,6 @@
from pypy.module.cpyext.api import fopen, fclose, fwrite
from pypy.module.cpyext.test.test_api import BaseApiTest
+from pypy.module.cpyext.object import Py_PRINT_RAW
from pypy.rpython.lltypesystem import rffi, lltype
from pypy.tool.udir import udir
import pytest
@@ -77,3 +78,28 @@
out = out.replace('\r\n', '\n')
assert out == "test\n"
+ def test_file_writeobject(self, space, api, capfd):
+ w_obj = space.wrap("test\n")
+ w_stdout = space.sys.get("stdout")
+ api.PyFile_WriteObject(w_obj, w_stdout, Py_PRINT_RAW)
+ api.PyFile_WriteObject(w_obj, w_stdout, 0)
+ space.call_method(w_stdout, "flush")
+ out, err = capfd.readouterr()
+ out = out.replace('\r\n', '\n')
+ assert out == "test\n'test\\n'"
+
+ def test_file_softspace(self, space, api, capfd):
+ w_stdout = space.sys.get("stdout")
+ assert api.PyFile_SoftSpace(w_stdout, 1) == 0
+ assert api.PyFile_SoftSpace(w_stdout, 0) == 1
+
+ api.PyFile_SoftSpace(w_stdout, 1)
+ w_ns = space.newdict()
+ space.exec_("print 1,", w_ns, w_ns)
+ space.exec_("print 2,", w_ns, w_ns)
+ api.PyFile_SoftSpace(w_stdout, 0)
+ space.exec_("print 3", w_ns, w_ns)
+ space.call_method(w_stdout, "flush")
+ out, err = capfd.readouterr()
+ out = out.replace('\r\n', '\n')
+ assert out == " 1 23\n"
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit