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
5160da1a
Commit
5160da1a
authored
Nov 19, 2013
by
R David Murray
Browse files
Options
Browse Files
Download
Plain Diff
Merge: #19449: Handle non-string keys when generating 'fieldnames' error.
parents
0e60f85c
fb099c9e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
16 additions
and
1 deletion
+16
-1
Lib/csv.py
Lib/csv.py
+1
-1
Lib/test/test_csv.py
Lib/test/test_csv.py
+12
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/csv.py
View file @
5160da1a
...
...
@@ -146,7 +146,7 @@ class DictWriter:
wrong_fields
=
[
k
for
k
in
rowdict
if
k
not
in
self
.
fieldnames
]
if
wrong_fields
:
raise
ValueError
(
"dict contains fields not in fieldnames: "
+
", "
.
join
(
wrong_fields
))
+
", "
.
join
(
[
repr
(
x
)
for
x
in
wrong_fields
]
))
return
[
rowdict
.
get
(
key
,
self
.
restval
)
for
key
in
self
.
fieldnames
]
def
writerow
(
self
,
rowdict
):
...
...
Lib/test/test_csv.py
View file @
5160da1a
...
...
@@ -579,6 +579,18 @@ class TestDictFields(unittest.TestCase):
fileobj
=
StringIO
()
self
.
assertRaises
(
TypeError
,
csv
.
DictWriter
,
fileobj
)
def
test_write_fields_not_in_fieldnames
(
self
):
with
TemporaryFile
(
"w+"
,
newline
=
''
)
as
fileobj
:
writer
=
csv
.
DictWriter
(
fileobj
,
fieldnames
=
[
"f1"
,
"f2"
,
"f3"
])
# Of special note is the non-string key (issue 19449)
with
self
.
assertRaises
(
ValueError
)
as
cx
:
writer
.
writerow
({
"f4"
:
10
,
"f2"
:
"spam"
,
1
:
"abc"
})
exception
=
str
(
cx
.
exception
)
self
.
assertIn
(
"fieldnames"
,
exception
)
self
.
assertIn
(
"'f4'"
,
exception
)
self
.
assertNotIn
(
"'f2'"
,
exception
)
self
.
assertIn
(
"1"
,
exception
)
def
test_read_dict_fields
(
self
):
with
TemporaryFile
(
"w+"
)
as
fileobj
:
fileobj
.
write
(
"1,2,abc
\
r
\
n
"
)
...
...
Misc/NEWS
View file @
5160da1a
...
...
@@ -56,6 +56,9 @@ Core and Builtins
Library
-------
-
Issue
#
19449
:
in
csv
's writerow, handle non-string keys when generating the
error message that certain keys are not in the '
fieldnames
' list.
- Issue #8402: Added the escape() function to the glob module.
- Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.
...
...
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