summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1993-10-25 09:59:04 (GMT)
committerGuido van Rossum <guido@python.org>1993-10-25 09:59:04 (GMT)
commit5a2a683e7299a8d8f03c7125c22eb813e3030808 (patch)
treeae452e022de04fa46ce145b7a66aecfbf18dcd0f
parent52c1f515547a96898879fe60e2432b69ff95cca0 (diff)
downloadcpython-5a2a683e7299a8d8f03c7125c22eb813e3030808.zip
cpython-5a2a683e7299a8d8f03c7125c22eb813e3030808.tar.gz
cpython-5a2a683e7299a8d8f03c7125c22eb813e3030808.tar.bz2
* filemodule.c: added writelines() -- analogous to readlines()
* import.c: fixed core dump when out-of-date .pyc file encountered (again!)
-rw-r--r--Objects/fileobject.c42
-rw-r--r--Python/import.c3
2 files changed, 44 insertions, 1 deletions
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index b981bb8..5099f3f 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -541,6 +541,47 @@ file_write(f, args)
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[] = {
{"close", file_close},
{"flush", file_flush},
@@ -552,6 +593,7 @@ static struct methodlist file_methods[] = {
{"seek", file_seek},
{"tell", file_tell},
{"write", file_write},
+ {"writelines", file_writelines},
{NULL, NULL} /* sentinel */
};
diff --git a/Python/import.c b/Python/import.c
index c0f163a..b81a41e 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -250,9 +250,10 @@ get_module(m, name, m_ret)
fprintf(stderr,
"# invalid precompiled file \"%s\"\n",
namebuf);
- goto read_py;
}
}
+ if (co == NULL)
+ goto read_py;
}
else {
read_py: