Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collections.UserString encode method returns a string #80763

Closed
treyhunner opened this issue Apr 9, 2019 · 9 comments
Closed

collections.UserString encode method returns a string #80763

treyhunner opened this issue Apr 9, 2019 · 9 comments
Assignees
Labels
3.8 only security fixes 3.9 only security fixes easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@treyhunner
Copy link
Member

BPO 36582
Nosy @rhettinger, @asqui, @Mariatta, @treyhunner, @csabella, @tirkarthi, @mblahay
PRs
  • bpo-36582: Make collections.UserString.encode() return bytes, not str #13138
  • [3.8] bpo-36582: Make collections.UserString.encode() return bytes, not str (GH-13138) #15557
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/rhettinger'
    closed_at = <Date 2019-08-28.05:00:29.638>
    created_at = <Date 2019-04-09.23:38:53.294>
    labels = ['easy', '3.8', 'type-bug', 'library', '3.9']
    title = 'collections.UserString encode method returns a string'
    updated_at = <Date 2019-08-28.05:00:29.637>
    user = 'https://github.com/treyhunner'

    bugs.python.org fields:

    activity = <Date 2019-08-28.05:00:29.637>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2019-08-28.05:00:29.638>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2019-04-09.23:38:53.294>
    creator = 'trey'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 36582
    keywords = ['patch', 'easy']
    message_count = 9.0
    messages = ['339818', '339824', '341550', '341563', '341593', '341594', '341649', '350653', '350654']
    nosy_count = 7.0
    nosy_names = ['rhettinger', 'dfortunov', 'Mariatta', 'trey', 'cheryl.sabella', 'xtreak', 'mblahay']
    pr_nums = ['13138', '15557']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue36582'
    versions = ['Python 3.8', 'Python 3.9']

    @treyhunner
    Copy link
    Member Author

    It looks like the encode method for UserString incorrectly wraps its return value in a str call.

    >>> from collections import UserString
    >>> UserString("hello").encode('utf-8') == b'hello'
    False
    >>> UserString("hello").encode('utf-8')
    "b'hello'"
    >>> type(UserString("hello").encode('utf-8'))
    <class 'collections.UserString'>
    

    @treyhunner treyhunner added 3.7 (EOL) end of life stdlib Python modules in the Lib dir labels Apr 9, 2019
    @rhettinger
    Copy link
    Contributor

    Trey, would you like to submit a PR to fix this? (Be sure to add a test case).

    @rhettinger rhettinger added the 3.8 only security fixes label Apr 10, 2019
    @rhettinger rhettinger self-assigned this Apr 10, 2019
    @rhettinger rhettinger added the type-bug An unexpected behavior, bug, or error label Apr 10, 2019
    @tirkarthi
    Copy link
    Member

    I think this is an easy issue. The relevant code is at

    def encode(self, encoding=None, errors=None): # XXX improve this?
    where the encoded result has to be fixed. Trey, if you haven't started working on it I think it's a good first issue for sprints.

    A simple unittest patch that fails on master. This can have additional tests with both encoding and errors present and both of them absent hitting all three code paths in the function.

    diff --git a/Lib/test/test_userstring.py b/Lib/test/test_userstring.py
    index 71528223d3..81a4908dbd 100644
    --- a/Lib/test/test_userstring.py
    +++ b/Lib/test/test_userstring.py
    @@ -39,6 +39,11 @@ class UserStringTest(
    # we don't fix the arguments, because UserString can't cope with it
    getattr(object, methodname)(*args)

    + def test_encode(self):
    + data = UserString("hello")
    + self.assertEqual(data.encode(encoding='utf-8'), b'hello')

     if __name__ == "__main__":
         unittest.main()

    @asqui
    Copy link
    Mannequin

    asqui mannequin commented May 6, 2019

    I'll pick this up in the PyCon US 2019 sprint this afternoon.

    @mblahay
    Copy link
    Mannequin

    mblahay mannequin commented May 6, 2019

    I will pick this on up

    @mblahay
    Copy link
    Mannequin

    mblahay mannequin commented May 6, 2019

    My mistake, dfortunov is already working on this one.

    @asqui
    Copy link
    Mannequin

    asqui mannequin commented May 6, 2019

    PR submitted here:
    #13138

    Rather than adding three different tests for the different code paths I chose to collapse the three different code paths by surfacing the underlying str.encode() defaults in the method signature of UserString.encode(), taking it down to a one-line implementation.

    @XTreak: Thanks for the super-helpful triage and failing test case!

    @rhettinger
    Copy link
    Contributor

    New changeset 2a16eea by Raymond Hettinger (Daniel Fortunov) in branch 'master':
    bpo-36582: Make collections.UserString.encode() return bytes, not str (GH-13138)
    2a16eea

    @rhettinger rhettinger added 3.9 only security fixes and removed 3.7 (EOL) end of life labels Aug 28, 2019
    @rhettinger
    Copy link
    Contributor

    New changeset 2cb82d2 by Raymond Hettinger (Miss Islington (bot)) in branch '3.8':
    bpo-36582: Make collections.UserString.encode() return bytes, not str (GH-13138) (GH-15557)
    2cb82d2

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes 3.9 only security fixes easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants