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.
This commit is contained in:
Guido van Rossum 2001-01-07 20:51:39 +00:00
parent e3fb18c1c4
commit 4ddf0a01f7

View File

@ -639,7 +639,7 @@ file_readinto(PyFileObject *f, PyObject *args)
Size argument interpretation: Size argument interpretation:
> 0: max length; > 0: max length;
= 0: read arbitrary line; = 0: read arbitrary line;
< 0: illegal (use get_line_raw() instead) < 0: invalid
*/ */
#ifdef HAVE_GETC_UNLOCKED #ifdef HAVE_GETC_UNLOCKED
@ -709,38 +709,27 @@ get_line(PyFileObject *f, int n)
return v; 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 */ /* External C interface */
PyObject * PyObject *
PyFile_GetLine(PyObject *f, int n) PyFile_GetLine(PyObject *f, int n)
{ {
PyObject *result;
if (f == NULL) { if (f == NULL) {
PyErr_BadInternalCall(); PyErr_BadInternalCall();
return NULL; 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 *reader;
PyObject *args; PyObject *args;
PyObject *result;
reader = PyObject_GetAttrString(f, "readline"); reader = PyObject_GetAttrString(f, "readline");
if (reader == NULL) if (reader == NULL)
return NULL; return NULL;
@ -761,35 +750,29 @@ PyFile_GetLine(PyObject *f, int n)
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"object.readline() returned non-string"); "object.readline() returned non-string");
} }
if (n < 0 && result != NULL) { }
char *s = PyString_AsString(result);
int len = PyString_Size(result); if (n < 0 && result != NULL && PyString_Check(result)) {
if (len == 0) { 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); Py_DECREF(result);
result = NULL; result = v;
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;
}
} }
} }
return result;
} }
if (((PyFileObject*)f)->f_fp == NULL) return result;
return err_closed();
if (n < 0)
return get_line_raw((PyFileObject *)f);
else
return get_line((PyFileObject *)f, n);
} }
/* Python method */ /* Python method */