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
2a16eea7
Commit
2a16eea7
authored
Aug 28, 2019
by
Daniel Fortunov
Committed by
Raymond Hettinger
Aug 27, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-36582: Make collections.UserString.encode() return bytes, not str (GH-13138)
parent
98d90f74
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
20 additions
and
6 deletions
+20
-6
Lib/collections/__init__.py
Lib/collections/__init__.py
+4
-6
Lib/test/test_userstring.py
Lib/test/test_userstring.py
+14
-0
Misc/ACKS
Misc/ACKS
+1
-0
Misc/NEWS.d/next/Library/2019-05-07-17-42-36.bpo-36582.L_dxR6.rst
...S.d/next/Library/2019-05-07-17-42-36.bpo-36582.L_dxR6.rst
+1
-0
No files found.
Lib/collections/__init__.py
View file @
2a16eea7
...
...
@@ -1184,12 +1184,10 @@ class UserString(_collections_abc.Sequence):
if
isinstance
(
sub
,
UserString
):
sub
=
sub
.
data
return
self
.
data
.
count
(
sub
,
start
,
end
)
def
encode
(
self
,
encoding
=
None
,
errors
=
None
):
# XXX improve this?
if
encoding
:
if
errors
:
return
self
.
__class__
(
self
.
data
.
encode
(
encoding
,
errors
))
return
self
.
__class__
(
self
.
data
.
encode
(
encoding
))
return
self
.
__class__
(
self
.
data
.
encode
())
def
encode
(
self
,
encoding
=
'utf-8'
,
errors
=
'strict'
):
encoding
=
'utf-8'
if
encoding
is
None
else
encoding
errors
=
'strict'
if
errors
is
None
else
errors
return
self
.
data
.
encode
(
encoding
,
errors
)
def
endswith
(
self
,
suffix
,
start
=
0
,
end
=
_sys
.
maxsize
):
return
self
.
data
.
endswith
(
suffix
,
start
,
end
)
def
expandtabs
(
self
,
tabsize
=
8
):
...
...
Lib/test/test_userstring.py
View file @
2a16eea7
...
...
@@ -51,6 +51,20 @@ class UserStringTest(
str3
=
ustr3
(
'TEST'
)
self
.
assertEqual
(
fmt2
%
str3
,
'value is TEST'
)
def
test_encode_default_args
(
self
):
self
.
checkequal
(
b'hello'
,
'hello'
,
'encode'
)
# Check that encoding defaults to utf-8
self
.
checkequal
(
b'
\
xf0
\
xa3
\
x91
\
x96
'
,
'
\
U00023456
'
,
'encode'
)
# Check that errors defaults to 'strict'
self
.
checkraises
(
UnicodeError
,
'
\
ud800
'
,
'encode'
)
def
test_encode_explicit_none_args
(
self
):
self
.
checkequal
(
b'hello'
,
'hello'
,
'encode'
,
None
,
None
)
# Check that encoding defaults to utf-8
self
.
checkequal
(
b'
\
xf0
\
xa3
\
x91
\
x96
'
,
'
\
U00023456
'
,
'encode'
,
None
,
None
)
# Check that errors defaults to 'strict'
self
.
checkraises
(
UnicodeError
,
'
\
ud800
'
,
'encode'
,
None
,
None
)
if
__name__
==
"__main__"
:
unittest
.
main
()
Misc/ACKS
View file @
2a16eea7
...
...
@@ -512,6 +512,7 @@ Arnaud Fontaine
Michael Foord
Amaury Forgeot d'Arc
Doug Fort
Daniel Fortunov
Evens Fortuné
Chris Foster
John Fouhy
...
...
Misc/NEWS.d/next/Library/2019-05-07-17-42-36.bpo-36582.L_dxR6.rst
0 → 100644
View file @
2a16eea7
Fix ``UserString.encode()`` to correctly return ``bytes`` rather than a ``UserString`` instance.
\ No newline at end of file
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