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

socket.getsockname() returns string instead of tuple #81586

Closed
bggardner mannequin opened this issue Jun 25, 2019 · 7 comments
Closed

socket.getsockname() returns string instead of tuple #81586

bggardner mannequin opened this issue Jun 25, 2019 · 7 comments
Labels
3.10 only security fixes tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error

Comments

@bggardner
Copy link
Mannequin

bggardner mannequin commented Jun 25, 2019

BPO 37405
Nosy @tiran, @benjaminp, @bggardner, @fbennett78
PRs
  • bpo-37405: socket.getsockname() returns string instead of tuple #14392
  • [3.8] closes bpo-37405: Make socket.getsockname() always return a tuple for AF_CAN. (GH-14392) #16018
  • bpo-37405: socket.getsockname() test fix #24991
  • 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 = None
    closed_at = <Date 2019-09-12.10:02:52.337>
    created_at = <Date 2019-06-25.21:01:17.388>
    labels = ['type-bug', 'tests', '3.10']
    title = 'socket.getsockname() returns string instead of tuple'
    updated_at = <Date 2021-03-23.15:14:42.943>
    user = 'https://github.com/bggardner'

    bugs.python.org fields:

    activity = <Date 2021-03-23.15:14:42.943>
    actor = 'Brent Gardner'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2019-09-12.10:02:52.337>
    closer = 'benjamin.peterson'
    components = ['Tests']
    creation = <Date 2019-06-25.21:01:17.388>
    creator = 'Brent Gardner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37405
    keywords = ['patch']
    message_count = 7.0
    messages = ['346559', '346579', '346580', '352086', '352097', '389364', '389388']
    nosy_count = 5.0
    nosy_names = ['christian.heimes', 'benjamin.peterson', 'docs@python', 'Brent Gardner', 'bennett78']
    pr_nums = ['14392', '16018', '24991']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue37405'
    versions = ['Python 3.10']

    @bggardner
    Copy link
    Mannequin Author

    bggardner mannequin commented Jun 25, 2019

    In Python 3.5.3, a socket with type AF_CAN returns a tuple in the form (interface, ) from getsockname(). In Python 3.7.3, getsockname() returns a string (the name of the interface). The documentation states "a tuple is used for the AF_CAN address family". The string will break code that worked in 3.5.3 by raising errors such as "Value Error: too many values to unpack (expected 2)".

    Example:

    #3.5.3
    import socket
    s = socket.socket(socket.AF_CAN, socket.SOCK_RAW, socket.CAN_RAW)
    s.bind(('vcan0',)) # requires tuple
    s.getsockname() # returns tuple: ('vcan0', 29)
    
    #3.7.3
    import socket
    s = socket.socket(socket.AF_CAN, socket.SOCK_RAW, socket.CAN_RAW)
    s.bind(('vcan0',)) # requires tuple
    s.getsockname() # returns string: 'vcan0'

    @bggardner bggardner mannequin added docs Documentation in the Doc dir 3.7 (EOL) end of life labels Jun 25, 2019
    @bggardner bggardner mannequin assigned docspython Jun 25, 2019
    @bggardner bggardner mannequin added the type-bug An unexpected behavior, bug, or error label Jun 25, 2019
    @bggardner
    Copy link
    Mannequin Author

    bggardner mannequin commented Jun 26, 2019

    Changed caused by commit effc12f to cpython/Modules/socketmodule.c on Sep 6, 2017.

    @bggardner bggardner mannequin added the extension-modules C modules in the Modules dir label Jun 26, 2019
    @bggardner
    Copy link
    Mannequin Author

    bggardner mannequin commented Jun 26, 2019

    Correction, change caused by a30f6d4 to cpython/Modules/socketmodule.c on Aug 28, 2017.

    @benjaminp
    Copy link
    Contributor

    New changeset 954900a by Benjamin Peterson (bggardner) in branch 'master':
    closes bpo-37405: Make socket.getsockname() always return a tuple for AF_CAN. (GH-14392)
    954900a

    @benjaminp
    Copy link
    Contributor

    New changeset f60fd95 by Benjamin Peterson (Miss Islington (bot)) in branch '3.8':
    closes bpo-37405: Make socket.getsockname() always return a tuple for AF_CAN. (GH-14392) (GH-16018)
    f60fd95

    @fbennett78
    Copy link
    Mannequin

    fbennett78 mannequin commented Mar 23, 2021

    $ make test TESTOPTS="-v test_socket"

    ERROR: testSendFrame (main.CANTest)
    ----------------------------------------------------------------------

    Traceback (most recent call last):
      File "/s/opt/cpython/debug/test_socket.py", line 2052, in testSendFrame
        self.assertEqual(addr[1], socket.AF_CAN)
    IndexError: tuple index out of range
    $ cat ../.git/config
    https://github.com/bennett78/cpython.git
    
    $ uname -r
    5.4.0-67-genericg$ cat /etc/issue
    Ubuntu 20.04.2 LTS \n \l
    
    $ /s/opt/cpython/debug$ ./python -V
    Python 3.10.0a6+

    @fbennett78 fbennett78 mannequin added tests Tests in the Lib/test dir 3.10 only security fixes and removed docs Documentation in the Doc dir extension-modules C modules in the Modules dir 3.7 (EOL) end of life labels Mar 23, 2021
    @bggardner
    Copy link
    Mannequin Author

    bggardner mannequin commented Mar 23, 2021

    This test was overlooked, and line 2052 should now be removed per the discussion here:

    #14392 (comment)

    In short, getsockname() used to return (interface, socket.AF_CAN), in which the socket.AF_CAN element was superfluous (also undocumented), so it was removed in the fix, and the tuple format was reinstated.

    @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.10 only security fixes tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant