summaryrefslogtreecommitdiffstats
path: root/Objects/fileobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2001-01-07 20:51:39 (GMT)
committerGuido van Rossum <guido@python.org>2001-01-07 20:51:39 (GMT)
commit4ddf0a01f7becb98b223225edeb48fd69e78934b (patch)
treec7a0552b3a2ec06f28ea14d42c4ec3e06d6fc3ff /Objects/fileobject.c
parente3fb18c1c47bda766c8e234368f4884cec1189ce (diff)
downloadcpython-4ddf0a01f7becb98b223225edeb48fd69e78934b.zip
cpython-4ddf0a01f7becb98b223225edeb48fd69e78934b.tar.gz
cpython-4ddf0a01f7becb98b223225edeb48fd69e78934b.tar.bz2
Tim noticed that I had botched get_line_raw(). Looking again, I
realized that this behavior is already present in PyFile_GetLine(), which is the only place that needs it. A little refactoring of that function made get_line_raw() redundant.
Diffstat (limited to 'Objects/fileobject.c')
-rw-r--r--Objects/fileobject.c77
1 files changed, 30 insertions, 47 deletions
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index 35d1034..aaa6a90 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -639,7 +639,7 @@ file_readinto(PyFileObject *f, PyObject *args)
Size argument interpretation:
> 0: max length;
= 0: read arbitrary line;
- < 0: illegal (use get_line_raw() instead)
+ < 0: invalid
*/
#ifdef HAVE_GETC_UNLOCKED
@@ -709,38 +709,27 @@ get_line(PyFileObject *f, int n)
return v;
}
-/* Internal routine to get a line for raw_input():
- strip trailing '\n', raise EOFError if EOF reached immediately
-*/
-
-static PyObject *
-get_line_raw(PyFileObject *f)
-{
- PyObject *line;
-
- line = get_line(f, 0);
- if (line == NULL || PyString_GET_SIZE(line) > 0)
- return line;
- else {
- Py_DECREF(line);
- PyErr_SetString(PyExc_EOFError, "EOF when reading a line");
- return NULL;
- }
-}
-
/* External C interface */
PyObject *
PyFile_GetLine(PyObject *f, int n)
{
+ PyObject *result;
+
if (f == NULL) {
PyErr_BadInternalCall();
return NULL;
}
- if (!PyFile_Check(f)) {
+
+ if (PyFile_Check(f)) {
+ if (((PyFileObject*)f)->f_fp == NULL)
+ return err_closed();
+ result = get_line((PyFileObject *)f, n);
+ }
+ else {
PyObject *reader;
PyObject *args;
- PyObject *result;
+
reader = PyObject_GetAttrString(f, "readline");
if (reader == NULL)
return NULL;
@@ -761,35 +750,29 @@ PyFile_GetLine(PyObject *f, int n)
PyErr_SetString(PyExc_TypeError,
"object.readline() returned non-string");
}
- if (n < 0 && result != NULL) {
- char *s = PyString_AsString(result);
- int len = PyString_Size(result);
- if (len == 0) {
+ }
+
+ if (n < 0 && result != NULL && PyString_Check(result)) {
+ char *s = PyString_AS_STRING(result);
+ int len = PyString_GET_SIZE(result);
+ if (len == 0) {
+ Py_DECREF(result);
+ result = NULL;
+ PyErr_SetString(PyExc_EOFError,
+ "EOF when reading a line");
+ }
+ else if (s[len-1] == '\n') {
+ if (result->ob_refcnt == 1)
+ _PyString_Resize(&result, len-1);
+ else {
+ PyObject *v;
+ v = PyString_FromStringAndSize(s, len-1);
Py_DECREF(result);
- result = NULL;
- PyErr_SetString(PyExc_EOFError,
- "EOF when reading a line");
- }
- else if (s[len-1] == '\n') {
- if (result->ob_refcnt == 1)
- _PyString_Resize(&result, len-1);
- else {
- PyObject *v;
- v = PyString_FromStringAndSize(s,
- len-1);
- Py_DECREF(result);
- result = v;
- }
+ result = v;
}
}
- return result;
}
- if (((PyFileObject*)f)->f_fp == NULL)
- return err_closed();
- if (n < 0)
- return get_line_raw((PyFileObject *)f);
- else
- return get_line((PyFileObject *)f, n);
+ return result;
}
/* Python method */