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
5aac3ed7
Commit
5aac3ed7
authored
Dec 20, 2015
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #25766: Special method __bytes__() now works in str subclasses.
parent
5185597a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
6 deletions
+20
-6
Lib/test/test_bytes.py
Lib/test/test_bytes.py
+6
-0
Misc/NEWS
Misc/NEWS
+2
-0
Objects/bytesobject.c
Objects/bytesobject.c
+12
-6
No files found.
Lib/test/test_bytes.py
View file @
5aac3ed7
...
...
@@ -779,6 +779,12 @@ class BytesTest(BaseBytesTest, unittest.TestCase):
def
__index__
(
self
):
return
42
self
.
assertEqual
(
bytes
(
A
()),
b'a'
)
# Issue #25766
class
A
(
str
):
def
__bytes__
(
self
):
return
b'abc'
self
.
assertEqual
(
bytes
(
A
(
'
\
u20ac
'
)),
b'abc'
)
self
.
assertEqual
(
bytes
(
A
(
'
\
u20ac
'
),
'iso8859-15'
),
b'
\
xa4
'
)
# Issue #24731
class
A
:
def
__bytes__
(
self
):
...
...
Misc/NEWS
View file @
5aac3ed7
...
...
@@ -10,6 +10,8 @@ Release date: tba
Core and Builtins
-----------------
- Issue #25766: Special method __bytes__() now works in str subclasses.
- Issue #25421: __sizeof__ methods of builtin types now use dynamic basic size.
This allows sys.getsize() to work correctly with their subclasses with
__slots__ defined.
...
...
Objects/bytesobject.c
View file @
5aac3ed7
...
...
@@ -3175,11 +3175,11 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return
PyBytes_FromStringAndSize
(
NULL
,
0
);
}
if
(
PyUnicode_Check
(
x
)
)
{
if
(
encoding
!=
NULL
)
{
/* Encode via the codec registry */
if
(
encoding
==
NULL
)
{
if
(
!
PyUnicode_Check
(
x
)
)
{
PyErr_SetString
(
PyExc_TypeError
,
"
string argument without an encoding
"
);
"
encoding without a string argument
"
);
return
NULL
;
}
new
=
PyUnicode_AsEncodedString
(
x
,
encoding
,
errors
);
...
...
@@ -3189,10 +3189,11 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return
new
;
}
/* If it's not unicode, there can't be encoding or errors */
if
(
encoding
!=
NULL
||
errors
!=
NULL
)
{
if
(
errors
!=
NULL
)
{
PyErr_SetString
(
PyExc_TypeError
,
"encoding or errors without a string argument"
);
PyUnicode_Check
(
x
)
?
"string argument without an encoding"
:
"errors without a string argument"
);
return
NULL
;
}
...
...
@@ -3217,6 +3218,11 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
else
if
(
PyErr_Occurred
())
return
NULL
;
if
(
PyUnicode_Check
(
x
))
{
PyErr_SetString
(
PyExc_TypeError
,
"string argument without an encoding"
);
return
NULL
;
}
/* Is it an integer? */
size
=
PyNumber_AsSsize_t
(
x
,
PyExc_OverflowError
);
if
(
size
==
-
1
&&
PyErr_Occurred
())
{
...
...
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