Debian Codegraph @

pypy/module/cpyext/pyfile.py

raw symbols SCIP
1 from rpython.rtyper.lltypesystem import rffi, lltype
2 from rpython.rlib.rarithmetic import widen
3 from pypy.module.cpyext.api import (
4 cpython_api, CONST_STRING, FILEP)
5 from pypy.module.cpyext.pyobject import PyObject
6 from pypy.module.cpyext.object import Py_PRINT_RAW
7 from pypy.module._io import interp_io
8 from pypy.interpreter.error import OperationError, oefmt
9
10 @cpython_api([PyObject, rffi.INT_real], PyObject)
11 def PyFile_GetLine(space, w_obj, n):
12 """
13 Equivalent to p.readline([n]), this function reads one line from the
14 object p. p may be a file object or any object with a readline()
15 method. If n is 0, exactly one line is read, regardless of the length of
16 the line. If n is greater than 0, no more than n bytes will be read
17 from the file; a partial line can be returned. In both cases, an empty string
18 is returned if the end of the file is reached immediately. If n is less than
19 0, however, one line is read regardless of length, but EOFError is
20 raised if the end of the file is reached immediately."""
21 try:
22 w_readline = space.getattr(w_obj, space.newtext('readline'))
23 except OperationError:
24 raise oefmt(space.w_TypeError,
25 "argument must be a file, or have a readline() method.")
26
27 n = rffi.cast(lltype.Signed, n)
28 if space.is_true(space.gt(space.newint(n), space.newint(0))):
29 return space.call_function(w_readline, space.newint(n))
30 elif space.is_true(space.lt(space.newint(n), space.newint(0))):
31 return space.call_function(w_readline)
32 else:
33 # XXX Raise EOFError as specified
34 return space.call_function(w_readline)
35
36 @cpython_api([CONST_STRING, CONST_STRING], PyObject)
37 def PyFile_FromString(space, filename, mode):
38 """
39 On success, return a new file object that is opened on the file given by
40 filename, with a file mode given by mode, where mode has the same
41 semantics as the standard C routine fopen(). On failure, return NULL."""
42 w_filename = space.newbytes(rffi.charp2str(filename))
43 w_mode = space.newtext(rffi.charp2str(mode))
44 return space.call_method(space.builtin, 'open', w_filename, w_mode)
45
46 @cpython_api([rffi.INT_real, CONST_STRING, CONST_STRING, rffi.INT_real, CONST_STRING, CONST_STRING, CONST_STRING, rffi.INT_real], PyObject)
47 def PyFile_FromFd(space, fd, name, mode, buffering, encoding, errors, newline, closefd):
48 """Create a Python file object from the file descriptor of an already
49 opened file fd. The arguments name, encoding, errors and newline
50 can be NULL to use the defaults; buffering can be -1 to use the
51 default. name is ignored and kept for backward compatibility. Return
52 NULL on failure. For a more comprehensive description of the arguments,
53 please refer to the io.open() function documentation.
54
55 Since Python streams have their own buffering layer, mixing them with
56 OS-level file descriptors can produce various issues (such as unexpected
57 ordering of data).
58
59 Ignore name attribute."""
60
61 if not mode:
62 raise oefmt(space.w_ValueError, "mode is required")
63 mode = rffi.charp2str(mode)
64 if encoding:
65 encoding_ = rffi.charp2str(encoding)
66 else:
67 encoding_ = None
68 if errors:
69 errors_ = rffi.charp2str(errors)
70 else:
71 errors_ = None
72 if newline:
73 newline_ = rffi.charp2str(newline)
74 else:
75 newline_ = None
76 w_ret = interp_io.open(space, space.newint(fd), mode, widen(buffering),
77 encoding_, errors_, newline_, widen(closefd))
78 return w_ret
79
80 @cpython_api([CONST_STRING, PyObject], rffi.INT_real, error=-1)
81 def PyFile_WriteString(space, s, w_p):
82 """Write string s to file object p. Return 0 on success or -1 on
83 failure; the appropriate exception will be set."""
84 w_str = space.newtext(rffi.charp2str(s))
85 space.call_method(w_p, "write", w_str)
86 return 0
87
88 @cpython_api([PyObject, PyObject, rffi.INT_real], rffi.INT_real, error=-1)
89 def PyFile_WriteObject(space, w_obj, w_p, flags):
90 """
91 Write object obj to file object p. The only supported flag for flags is
92 Py_PRINT_RAW; if given, the str() of the object is written
93 instead of the repr(). Return 0 on success or -1 on failure; the
94 appropriate exception will be set."""
95 if rffi.cast(lltype.Signed, flags) & Py_PRINT_RAW:
96 w_str = space.str(w_obj)
97 else:
98 w_str = space.repr(w_obj)
99 space.call_method(w_p, "write", w_str)
100 return 0
101
102 @cpython_api([PyObject], PyObject)
103 def PyOS_FSPath(space, w_path):
104 """
105 Return the file system representation for path. If the object is a str or
106 bytes object, then its reference count is incremented. If the object
107 implements the os.PathLike interface, then __fspath__() is returned as long
108 as it is a str or bytes object. Otherwise TypeError is raised and NULL is
109 returned.
110 """
111 return pyos_fspath(space, w_path)
112
113 def pyos_fspath(space, w_path):
114 if (space.isinstance_w(w_path, space.w_unicode) or
115 space.isinstance_w(w_path, space.w_bytes)):
116 return w_path
117 if not space.lookup(w_path, '__fspath__'):
118 raise oefmt(space.w_TypeError,
119 "expected str, bytes or os.PathLike object, not %T", w_path)
120 w_ret = space.call_method(w_path, '__fspath__')
121 if (space.isinstance_w(w_ret, space.w_unicode) or
122 space.isinstance_w(w_ret, space.w_bytes)):
123 return w_ret
124 raise oefmt(space.w_TypeError,
125 "expected %T.__fspath__() to return str or bytes, not %T", w_path, w_ret)
126