[pypy-svn] r79824 - in pypy/branch/cpyext-darwin/pypy/module/cpyext: . test
danchr at codespeak.net
danchr at codespeak.net
Sat Dec 4 20:51:13 CET 2010
Author: danchr
Date: Sat Dec 4 20:51:11 2010
New Revision: 79824
Modified:
pypy/branch/cpyext-darwin/pypy/module/cpyext/object.py
pypy/branch/cpyext-darwin/pypy/module/cpyext/stubs.py
pypy/branch/cpyext-darwin/pypy/module/cpyext/test/test_object.py
Log:
Implement PyFile_GetLine()
Modified: pypy/branch/cpyext-darwin/pypy/module/cpyext/object.py
==============================================================================
--- pypy/branch/cpyext-darwin/pypy/module/cpyext/object.py (original)
+++ pypy/branch/cpyext-darwin/pypy/module/cpyext/object.py Sat Dec 4 20:51:11 2010
@@ -431,6 +431,32 @@
PyFile_Check, PyFile_CheckExact = build_type_checkers("File", W_File)
+ at cpython_api([PyObject, rffi.INT_real], PyObject)
+def PyFile_GetLine(space, w_obj, n):
+ """
+ Equivalent to p.readline([n]), this function reads one line from the
+ object p. p may be a file object or any object with a readline()
+ method. If n is 0, exactly one line is read, regardless of the length of
+ the line. If n is greater than 0, no more than n bytes will be read
+ from the file; a partial line can be returned. In both cases, an empty string
+ is returned if the end of the file is reached immediately. If n is less than
+ 0, however, one line is read regardless of length, but EOFError is
+ raised if the end of the file is reached immediately."""
+ try:
+ w_readline = space.getattr(w_obj, space.wrap('readline'))
+ except OperationError:
+ raise OperationError(
+ space.w_TypeError, space.wrap(
+ "argument must be a file, or have a readline() method."))
+
+ n = rffi.cast(lltype.Signed, n)
+ if space.is_true(space.gt(space.wrap(n), space.wrap(0))):
+ return space.call_function(w_readline, space.wrap(n))
+ elif space.is_true(space.lt(space.wrap(n), space.wrap(0))):
+ return space.call_function(w_readline)
+ else:
+ # XXX Raise EOFError as specified
+ return space.call_function(w_readline)
@cpython_api([CONST_STRING, CONST_STRING], PyObject)
def PyFile_FromString(space, filename, mode):
"""
Modified: pypy/branch/cpyext-darwin/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/branch/cpyext-darwin/pypy/module/cpyext/stubs.py (original)
+++ pypy/branch/cpyext-darwin/pypy/module/cpyext/stubs.py Sat Dec 4 20:51:11 2010
@@ -824,22 +824,6 @@
"""
raise NotImplementedError
- at cpython_api([PyObject, rffi.INT_real], PyObject)
-def PyFile_GetLine(space, p, n):
- """
-
-
-
- Equivalent to p.readline([n]), this function reads one line from the
- object p. p may be a file object or any object with a readline()
- method. If n is 0, exactly one line is read, regardless of the length of
- the line. If n is greater than 0, no more than n bytes will be read
- from the file; a partial line can be returned. In both cases, an empty string
- is returned if the end of the file is reached immediately. If n is less than
- 0, however, one line is read regardless of length, but EOFError is
- raised if the end of the file is reached immediately."""
- raise NotImplementedError
-
@cpython_api([PyObject], PyObject)
def PyFile_Name(space, p):
"""Return the name of the file specified by p as a string object."""
Modified: pypy/branch/cpyext-darwin/pypy/module/cpyext/test/test_object.py
==============================================================================
--- pypy/branch/cpyext-darwin/pypy/module/cpyext/test/test_object.py (original)
+++ pypy/branch/cpyext-darwin/pypy/module/cpyext/test/test_object.py Sat Dec 4 20:51:11 2010
@@ -4,7 +4,7 @@
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
from pypy.rpython.lltypesystem import rffi, lltype
from pypy.module.cpyext.api import Py_LT, Py_LE, Py_NE, Py_EQ,\
- Py_GE, Py_GT
+ Py_GE, Py_GT, fopen, fclose, fwrite
from pypy.tool.udir import udir
class TestObject(BaseApiTest):
@@ -196,6 +196,37 @@
space.call_method(w_file, "close")
assert (udir / "_test_file").read() == "text"
+ def test_file_getline(self, space, api):
+ filename = rffi.str2charp(str(udir / "_test_file"))
+
+ mode = rffi.str2charp("w")
+ w_file = api.PyFile_FromString(filename, mode)
+ space.call_method(w_file, "write",
+ space.wrap("line1\nline2\nline3\nline4"))
+ space.call_method(w_file, "close")
+
+ rffi.free_charp(mode)
+ mode = rffi.str2charp("r")
+ w_file = api.PyFile_FromString(filename, mode)
+ rffi.free_charp(filename)
+ rffi.free_charp(mode)
+
+ w_line = api.PyFile_GetLine(w_file, 0)
+ assert space.str_w(w_line) == "line1\n"
+
+ w_line = api.PyFile_GetLine(w_file, 4)
+ assert space.str_w(w_line) == "line"
+
+ w_line = api.PyFile_GetLine(w_file, 0)
+ assert space.str_w(w_line) == "2\n"
+
+ # XXX We ought to raise an EOFError here, but don't
+ w_line = api.PyFile_GetLine(w_file, -1)
+ # assert api.PyErr_Occurred() is space.w_EOFError
+ assert space.str_w(w_line) == "line3\n"
+
+ space.call_method(w_file, "close")
+
class AppTestObject(AppTestCpythonExtensionBase):
def setup_class(cls):
AppTestCpythonExtensionBase.setup_class.im_func(cls)
More information about the Pypy-commit
mailing list