Commit 18a48d66 authored by Chris Toshok's avatar Chris Toshok

call _PyFile_SanitizeMode to strip out U (and validate) the mode before passing it along to fopen.

parent 2748c606
......@@ -826,7 +826,14 @@ Box* fileNew(BoxedClass* cls, Box* s, Box* m) {
auto fn = static_cast<BoxedString*>(s);
auto mode = static_cast<BoxedString*>(m);
FILE* f = fopen(fn->data(), mode->data());
// all characters in python mode specifiers are valid in fopen calls except 'U'. we strip it out
// of the string we pass to fopen, but pass it along to the BoxedFile ctor.
auto file_mode = std::unique_ptr<char[]>(new char[mode->size() + 1]);
memmove(&file_mode[0], mode->data(), mode->size() + 1);
_PyFile_SanitizeMode(&file_mode[0]);
checkAndThrowCAPIException();
FILE* f = fopen(fn->data(), &file_mode[0]);
if (!f) {
PyErr_SetFromErrnoWithFilename(IOError, fn->data());
throwCAPIException();
......
import sys
import tempfile
f = open("/dev/null")
print repr(f.read())
......@@ -58,3 +59,32 @@ print f.write("H")
print f.tell()
print f.flush()
print f.close()
# tests for universal newlines
fd, fn = tempfile.mkstemp()
with open(fn, "wb") as f:
f.write("hello world!\r")
f.write("hello world!\r\n")
f.write("hello world!\n")
f.write("hello world!\r")
with open(fn) as f:
print len(f.readlines())
with open(fn, "rU") as f:
print len(f.readlines())
fd, fn = tempfile.mkstemp()
try:
with open(fn, "wU") as f:
print "succeeded"
except Exception as e:
print e
try:
with open(fn, "aU") as f:
print "succeeded"
except Exception as e:
print e
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment