Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
17e43e58
Commit
17e43e58
authored
Feb 27, 2007
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added some comments and docstrings. More is needed.
parent
b3f568f1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
16 deletions
+62
-16
Lib/io.py
Lib/io.py
+62
-16
No files found.
Lib/io.py
View file @
17e43e58
...
...
@@ -3,6 +3,9 @@
"""New I/O library.
This is an early prototype; eventually some of this will be
reimplemented in C and the rest may be turned into a package.
See PEP XXX; for now: http://docs.google.com/Doc?id=dfksfvqd_1cn5g5m
"""
...
...
@@ -13,7 +16,34 @@ __all__ = ["open", "RawIOBase", "FileIO", "SocketIO", "BytesIO"]
import
os
def
open
(
filename
,
mode
=
"r"
,
buffering
=
None
,
*
,
encoding
=
None
):
"""Replacement for the built-in open function, with encoding parameter."""
"""Replacement for the built-in open function.
Args:
filename: string giving the name of the file to be opened
mode: optional mode string; see below
buffering: optional int >= 0 giving the buffer size; values
can be: 0 = unbuffered, 1 = line buffered,
larger = fully buffered
encoding: optional string giving the text encoding (*must* be given
as a keyword argument)
Mode strings characters:
'r': open for reading (default)
'w': open for writing, truncating the file first
'a': open for writing, appending to the end if the file exists
'b': binary mode
't': text mode (default)
'+': open a disk file for updating (implies reading and writing)
Constraints:
- encoding must not be given when a binary mode is given
- buffering must not be zero when a text mode is given
Returns:
Depending on the mode and buffering arguments, either a raw
binary stream, a buffered binary stream, or a buffered text
stream, open for reading and/or writing.
"""
assert
isinstance
(
filename
,
str
)
assert
isinstance
(
mode
,
str
)
assert
buffering
is
None
or
isinstance
(
buffering
,
int
)
...
...
@@ -22,11 +52,11 @@ def open(filename, mode="r", buffering=None, *, encoding=None):
if
modes
-
set
(
"arwb+t"
)
or
len
(
mode
)
>
len
(
modes
):
raise
ValueError
(
"invalid mode: %r"
%
mode
)
reading
=
"r"
in
modes
writing
=
"w"
in
modes
or
"a"
in
modes
binary
=
"b"
in
modes
writing
=
"w"
in
modes
appending
=
"a"
in
modes
updating
=
"+"
in
modes
text
=
"t"
in
modes
or
not
binary
text
=
"t"
in
modes
binary
=
"b"
in
modes
if
text
and
binary
:
raise
ValueError
(
"can't have text and binary mode at once"
)
if
reading
+
writing
+
appending
>
1
:
...
...
@@ -42,6 +72,12 @@ def open(filename, mode="r", buffering=None, *, encoding=None):
(
updating
and
"+"
or
""
))
if
buffering
is
None
:
buffering
=
8
*
1024
# International standard buffer size
# Should default to line buffering if os.isatty(raw.fileno())
try
:
bs
=
os
.
fstat
(
raw
.
fileno
()).
st_blksize
except
(
os
.
error
,
AttributeError
):
if
bs
>
1
:
buffering
=
bs
if
buffering
<
0
:
raise
ValueError
(
"invalid buffering size"
)
if
buffering
==
0
:
...
...
@@ -50,21 +86,33 @@ def open(filename, mode="r", buffering=None, *, encoding=None):
raise
ValueError
(
"can't have unbuffered text I/O"
)
if
updating
:
buffer
=
BufferedRandom
(
raw
,
buffering
)
elif
writing
:
elif
writing
or
appending
:
buffer
=
BufferedWriter
(
raw
,
buffering
)
else
:
assert
reading
buffer
=
BufferedReader
(
raw
,
buffering
)
if
binary
:
return
buffer
assert
text
textio
=
TextIOWrapper
(
buffer
)
# Universal newlines default to on
# XXX What about newline conventions?
textio
=
TextIOWrapper
(
buffer
,
encoding
)
return
textio
class
RawIOBase
:
"""Base class for raw binary I/O."""
"""Base class for raw binary I/O.
This class provides dummy implementations for all methods that
derived classes can override selectively; the default
implementations represent a file that cannot be read, written or
seeked.
The read() method is implemented by calling readinto(); derived
classes that want to support readon only need to implement
readinto() as a primitive operation.
"""
# XXX Add individual method docstrings
def
read
(
self
,
n
):
b
=
bytes
(
n
.
__index__
())
...
...
@@ -112,6 +160,8 @@ class FileIO(RawIOBase):
"""Raw I/O implementation for OS files."""
# XXX More docs
def
__init__
(
self
,
filename
,
mode
):
self
.
_seekable
=
None
self
.
_mode
=
mode
...
...
@@ -166,14 +216,6 @@ class FileIO(RawIOBase):
self
.
_seekable
=
True
return
self
.
_seekable
# XXX(nnorwitz): is there any reason to redefine __enter__ & __exit__?
# Both already have the same impl in the base class.
def
__enter__
(
self
):
return
self
def
__exit__
(
self
,
*
args
):
self
.
close
()
def
fileno
(
self
):
return
self
.
_fd
...
...
@@ -182,6 +224,8 @@ class SocketIO(RawIOBase):
"""Raw I/O implementation for stream sockets."""
# XXX More docs
def
__init__
(
self
,
sock
,
mode
):
assert
mode
in
(
"r"
,
"w"
,
"rw"
)
self
.
_sock
=
sock
...
...
@@ -212,6 +256,8 @@ class BytesIO(RawIOBase):
"""Raw I/O implementation for bytes, like StringIO."""
# XXX More docs
def
__init__
(
self
,
inital_bytes
=
None
):
self
.
_buffer
=
b""
self
.
_pos
=
0
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment