mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
* filemodule.c: added writelines() -- analogous to readlines()
* import.c: fixed core dump when out-of-date .pyc file encountered (again!)
This commit is contained in:
parent
52c1f51554
commit
5a2a683e72
2 changed files with 44 additions and 1 deletions
|
@ -541,6 +541,47 @@ file_write(f, args)
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static object *
|
||||||
|
file_writelines(f, args)
|
||||||
|
fileobject *f;
|
||||||
|
object *args;
|
||||||
|
{
|
||||||
|
int i, n;
|
||||||
|
if (f->f_fp == NULL)
|
||||||
|
return err_closed();
|
||||||
|
if (args == NULL || !is_listobject(args)) {
|
||||||
|
err_setstr(TypeError,
|
||||||
|
"writelines() requires list of strings");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
n = getlistsize(args);
|
||||||
|
f->f_softspace = 0;
|
||||||
|
BGN_SAVE
|
||||||
|
errno = 0;
|
||||||
|
for (i = 0; i < n; i++) {
|
||||||
|
object *line = getlistitem(args, i);
|
||||||
|
int len;
|
||||||
|
int nwritten;
|
||||||
|
if (!is_stringobject(line)) {
|
||||||
|
RET_SAVE
|
||||||
|
err_setstr(TypeError,
|
||||||
|
"writelines() requires list of strings");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
len = getstringsize(line);
|
||||||
|
nwritten = fwrite(getstringvalue(line), 1, len, f->f_fp);
|
||||||
|
if (nwritten != len) {
|
||||||
|
RET_SAVE
|
||||||
|
err_errno(IOError);
|
||||||
|
clearerr(f->f_fp);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
END_SAVE
|
||||||
|
INCREF(None);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
static struct methodlist file_methods[] = {
|
static struct methodlist file_methods[] = {
|
||||||
{"close", file_close},
|
{"close", file_close},
|
||||||
{"flush", file_flush},
|
{"flush", file_flush},
|
||||||
|
@ -552,6 +593,7 @@ static struct methodlist file_methods[] = {
|
||||||
{"seek", file_seek},
|
{"seek", file_seek},
|
||||||
{"tell", file_tell},
|
{"tell", file_tell},
|
||||||
{"write", file_write},
|
{"write", file_write},
|
||||||
|
{"writelines", file_writelines},
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -250,9 +250,10 @@ get_module(m, name, m_ret)
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"# invalid precompiled file \"%s\"\n",
|
"# invalid precompiled file \"%s\"\n",
|
||||||
namebuf);
|
namebuf);
|
||||||
goto read_py;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (co == NULL)
|
||||||
|
goto read_py;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
read_py:
|
read_py:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue