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
03f17f86
Commit
03f17f86
authored
Apr 10, 2016
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #17339: Improved TypeError message in bytes constructor.
parent
96cdbe7b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
16 deletions
+15
-16
Objects/bytesobject.c
Objects/bytesobject.c
+15
-16
No files found.
Objects/bytesobject.c
View file @
03f17f86
...
...
@@ -3469,31 +3469,24 @@ _PyBytes_FromTuple(PyObject *x)
}
static
PyObject
*
_PyBytes_FromIterator
(
PyObject
*
x
)
_PyBytes_FromIterator
(
PyObject
*
it
,
PyObject
*
x
)
{
char
*
str
;
PyObject
*
it
;
Py_ssize_t
i
,
size
;
_PyBytesWriter
writer
;
_PyBytesWriter_Init
(
&
writer
);
/* For iterator version, create a string object and resize as needed */
size
=
PyObject_LengthHint
(
x
,
64
);
if
(
size
==
-
1
&&
PyErr_Occurred
())
return
NULL
;
_PyBytesWriter_Init
(
&
writer
);
str
=
_PyBytesWriter_Alloc
(
&
writer
,
size
);
if
(
str
==
NULL
)
return
NULL
;
writer
.
overallocate
=
1
;
size
=
writer
.
allocated
;
/* Get the iterator */
it
=
PyObject_GetIter
(
x
);
if
(
it
==
NULL
)
goto
error
;
/* Run the iterator to exhaustion */
for
(
i
=
0
;
;
i
++
)
{
PyObject
*
item
;
...
...
@@ -3529,19 +3522,19 @@ _PyBytes_FromIterator(PyObject *x)
}
*
str
++
=
(
char
)
value
;
}
Py_DECREF
(
it
);
return
_PyBytesWriter_Finish
(
&
writer
,
str
);
error:
_PyBytesWriter_Dealloc
(
&
writer
);
Py_XDECREF
(
it
);
return
NULL
;
}
PyObject
*
PyBytes_FromObject
(
PyObject
*
x
)
{
PyObject
*
it
,
*
result
;
if
(
x
==
NULL
)
{
PyErr_BadInternalCall
();
return
NULL
;
...
...
@@ -3562,13 +3555,19 @@ PyBytes_FromObject(PyObject *x)
if
(
PyTuple_CheckExact
(
x
))
return
_PyBytes_FromTuple
(
x
);
if
(
PyUnicode_Check
(
x
))
{
PyErr_SetString
(
PyExc_TypeError
,
"cannot convert unicode object to bytes"
);
return
NULL
;
if
(
!
PyUnicode_Check
(
x
))
{
it
=
PyObject_GetIter
(
x
);
if
(
it
!=
NULL
)
{
result
=
_PyBytes_FromIterator
(
it
,
x
);
Py_DECREF
(
it
);
return
result
;
}
}
return
_PyBytes_FromIterator
(
x
);
PyErr_Format
(
PyExc_TypeError
,
"cannot convert '%.200s' object to bytes"
,
x
->
ob_type
->
tp_name
);
return
NULL
;
}
static
PyObject
*
...
...
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