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
d2bb18b2
Commit
d2bb18b2
authored
Jul 22, 2009
by
Alexandre Vassalotti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #6241: Better type checking for the arguments of io.StringIO.
parent
e671fd20
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
5 deletions
+38
-5
Lib/_pyio.py
Lib/_pyio.py
+3
-1
Lib/test/test_memoryio.py
Lib/test/test_memoryio.py
+8
-0
Modules/_io/stringio.c
Modules/_io/stringio.c
+27
-4
No files found.
Lib/_pyio.py
View file @
d2bb18b2
...
@@ -1924,8 +1924,10 @@ class StringIO(TextIOWrapper):
...
@@ -1924,8 +1924,10 @@ class StringIO(TextIOWrapper):
# C version, even under Windows.
# C version, even under Windows.
if
newline
is
None
:
if
newline
is
None
:
self
.
_writetranslate
=
False
self
.
_writetranslate
=
False
if
initial_value
:
if
initial_value
is
not
None
:
if
not
isinstance
(
initial_value
,
str
):
if
not
isinstance
(
initial_value
,
str
):
raise
TypeError
(
"initial_value must be str or None, not {0}"
.
format
(
type
(
initial_value
).
__name__
))
initial_value
=
str
(
initial_value
)
initial_value
=
str
(
initial_value
)
self
.
write
(
initial_value
)
self
.
write
(
initial_value
)
self
.
seek
(
0
)
self
.
seek
(
0
)
...
...
Lib/test/test_memoryio.py
View file @
d2bb18b2
...
@@ -140,6 +140,7 @@ class MemoryTestMixin:
...
@@ -140,6 +140,7 @@ class MemoryTestMixin:
self
.
assertEqual
(
memio
.
getvalue
(),
buf
*
2
)
self
.
assertEqual
(
memio
.
getvalue
(),
buf
*
2
)
memio
.
__init__
(
buf
)
memio
.
__init__
(
buf
)
self
.
assertEqual
(
memio
.
getvalue
(),
buf
)
self
.
assertEqual
(
memio
.
getvalue
(),
buf
)
self
.
assertRaises
(
TypeError
,
memio
.
__init__
,
[])
def
test_read
(
self
):
def
test_read
(
self
):
buf
=
self
.
buftype
(
"1234567890"
)
buf
=
self
.
buftype
(
"1234567890"
)
...
@@ -530,6 +531,13 @@ class PyStringIOTest(MemoryTestMixin, MemorySeekTestMixin, unittest.TestCase):
...
@@ -530,6 +531,13 @@ class PyStringIOTest(MemoryTestMixin, MemorySeekTestMixin, unittest.TestCase):
memio
=
self
.
ioclass
(
"a
\
r
\
n
b
\
r
\
n
"
,
newline
=
None
)
memio
=
self
.
ioclass
(
"a
\
r
\
n
b
\
r
\
n
"
,
newline
=
None
)
self
.
assertEqual
(
memio
.
read
(
5
),
"a
\
n
b
\
n
"
)
self
.
assertEqual
(
memio
.
read
(
5
),
"a
\
n
b
\
n
"
)
def
test_newline_argument
(
self
):
self
.
assertRaises
(
TypeError
,
self
.
ioclass
,
newline
=
b"
\
n
"
)
self
.
assertRaises
(
ValueError
,
self
.
ioclass
,
newline
=
"error"
)
# These should not raise an error
for
newline
in
(
None
,
""
,
"
\
n
"
,
"
\
r
"
,
"
\
r
\
n
"
):
self
.
ioclass
(
newline
=
newline
)
class
CBytesIOTest
(
PyBytesIOTest
):
class
CBytesIOTest
(
PyBytesIOTest
):
ioclass
=
io
.
BytesIO
ioclass
=
io
.
BytesIO
...
...
Modules/_io/stringio.c
View file @
d2bb18b2
...
@@ -550,22 +550,42 @@ stringio_init(stringio *self, PyObject *args, PyObject *kwds)
...
@@ -550,22 +550,42 @@ stringio_init(stringio *self, PyObject *args, PyObject *kwds)
{
{
char
*
kwlist
[]
=
{
"initial_value"
,
"newline"
,
NULL
};
char
*
kwlist
[]
=
{
"initial_value"
,
"newline"
,
NULL
};
PyObject
*
value
=
NULL
;
PyObject
*
value
=
NULL
;
PyObject
*
newline_obj
=
NULL
;
char
*
newline
=
"
\n
"
;
char
*
newline
=
"
\n
"
;
if
(
!
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
"|O
z
:__init__"
,
kwlist
,
if
(
!
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
"|O
O
:__init__"
,
kwlist
,
&
value
,
&
newline
))
&
value
,
&
newline
_obj
))
return
-
1
;
return
-
1
;
/* Parse the newline argument. This used to be done with the 'z'
specifier, however this allowed any object with the buffer interface to
be converted. Thus we have to parse it manually since we only want to
allow unicode objects or None. */
if
(
newline_obj
==
Py_None
)
{
newline
=
NULL
;
}
else
if
(
newline_obj
)
{
if
(
!
PyUnicode_Check
(
newline_obj
))
{
PyErr_Format
(
PyExc_TypeError
,
"newline must be str or None, not %.200s"
,
Py_TYPE
(
newline_obj
)
->
tp_name
);
return
-
1
;
}
newline
=
_PyUnicode_AsString
(
newline_obj
);
if
(
newline
==
NULL
)
return
-
1
;
}
if
(
newline
&&
newline
[
0
]
!=
'\0'
if
(
newline
&&
newline
[
0
]
!=
'\0'
&&
!
(
newline
[
0
]
==
'\n'
&&
newline
[
1
]
==
'\0'
)
&&
!
(
newline
[
0
]
==
'\n'
&&
newline
[
1
]
==
'\0'
)
&&
!
(
newline
[
0
]
==
'\r'
&&
newline
[
1
]
==
'\0'
)
&&
!
(
newline
[
0
]
==
'\r'
&&
newline
[
1
]
==
'\0'
)
&&
!
(
newline
[
0
]
==
'\r'
&&
newline
[
1
]
==
'\n'
&&
newline
[
2
]
==
'\0'
))
{
&&
!
(
newline
[
0
]
==
'\r'
&&
newline
[
1
]
==
'\n'
&&
newline
[
2
]
==
'\0'
))
{
PyErr_Format
(
PyExc_ValueError
,
PyErr_Format
(
PyExc_ValueError
,
"illegal newline value: %
s"
,
newline
);
"illegal newline value: %
R"
,
newline_obj
);
return
-
1
;
return
-
1
;
}
}
if
(
value
&&
value
!=
Py_None
&&
!
PyUnicode_Check
(
value
))
{
if
(
value
&&
value
!=
Py_None
&&
!
PyUnicode_Check
(
value
))
{
PyErr_Format
(
PyExc_
Valu
eError
,
PyErr_Format
(
PyExc_
Typ
eError
,
"initial_value must be str or None, not %.200s"
,
"initial_value must be str or None, not %.200s"
,
Py_TYPE
(
value
)
->
tp_name
);
Py_TYPE
(
value
)
->
tp_name
);
return
-
1
;
return
-
1
;
...
@@ -577,6 +597,9 @@ stringio_init(stringio *self, PyObject *args, PyObject *kwds)
...
@@ -577,6 +597,9 @@ stringio_init(stringio *self, PyObject *args, PyObject *kwds)
Py_CLEAR
(
self
->
writenl
);
Py_CLEAR
(
self
->
writenl
);
Py_CLEAR
(
self
->
decoder
);
Py_CLEAR
(
self
->
decoder
);
assert
((
newline
!=
NULL
&&
newline_obj
!=
Py_None
)
||
(
newline
==
NULL
&&
newline_obj
==
Py_None
));
if
(
newline
)
{
if
(
newline
)
{
self
->
readnl
=
PyUnicode_FromString
(
newline
);
self
->
readnl
=
PyUnicode_FromString
(
newline
);
if
(
self
->
readnl
==
NULL
)
if
(
self
->
readnl
==
NULL
)
...
...
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