Add newline arg to open().

This commit is contained in:
Guido van Rossum 2007-04-11 01:09:33 +00:00
parent 4109356d29
commit 9cbb5f0731
1 changed files with 8 additions and 6 deletions

View File

@ -413,11 +413,13 @@ The ``open()`` Built-in Function
The ``open()`` built-in function is specified by the following
pseudo-code::
def open(filename, mode="r", buffering=None, *, encoding=None):
assert isinstance(filename, str)
def open(filename, mode="r", buffering=None, *,
encoding=None, newline=None):
assert isinstance(filename, (str, int))
assert isinstance(mode, str)
assert buffering is None or isinstance(buffering, int)
assert encoding is None or isinstance(encoding, str)
assert newline in (None, "\n", "\r\n")
modes = set(mode)
if modes - set("arwb+t") or len(mode) > len(modes):
raise ValueError("invalid mode: %r" % mode)
@ -434,7 +436,9 @@ pseudo-code::
if not (reading or writing or appending):
raise ValueError("must have exactly one of read/write/append mode")
if binary and encoding is not None:
raise ValueError("binary modes doesn't take an encoding")
raise ValueError("binary modes doesn't take an encoding arg")
if binary and newline is not None:
raise ValueError("binary modes doesn't take a newline arg")
# XXX Need to spec the signature for FileIO()
raw = FileIO(filename, mode)
if buffering is None:
@ -456,9 +460,7 @@ pseudo-code::
if binary:
return buffer
assert text
# XXX Need to do something about universal newlines?
textio = TextIOWrapper(buffer)
return textio
return TextIOWrapper(buffer, encoding, newline)
Copyright