This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: socket.getsockname() returns string instead of tuple
Type: behavior Stage: resolved
Components: Tests Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Brent Gardner, benjamin.peterson, bennett78, christian.heimes, docs@python
Priority: normal Keywords: patch

Created on 2019-06-25 21:01 by Brent Gardner, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 14392 merged python-dev, 2019-06-26 12:01
PR 16018 merged miss-islington, 2019-09-12 10:03
PR 24991 closed Brent Gardner, 2021-03-23 15:14
Messages (7)
msg346559 - (view) Author: Brent Gardner (Brent Gardner) * Date: 2019-06-25 21:01
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'
msg346579 - (view) Author: Brent Gardner (Brent Gardner) * Date: 2019-06-26 02:51
Changed caused by commit effc12f8e9e20d0951d2ba5883587666bd8218e3 to cpython/Modules/socketmodule.c on Sep 6, 2017.
msg346580 - (view) Author: Brent Gardner (Brent Gardner) * Date: 2019-06-26 02:54
Correction, change caused by a30f6d45ac3e72761b96a8df0527182029eaee24 to cpython/Modules/socketmodule.c on Aug 28, 2017.
msg352086 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2019-09-12 10:02
New changeset 954900a3f98a8c0dea14dd575490237f3f8626b3 by Benjamin Peterson (bggardner) in branch 'master':
closes bpo-37405: Make socket.getsockname() always return a tuple for AF_CAN. (GH-14392)
https://github.com/python/cpython/commit/954900a3f98a8c0dea14dd575490237f3f8626b3
msg352097 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2019-09-12 10:34
New changeset f60fd95dcc189ace8c0a2177a394b9cc20389a1e 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)
https://github.com/python/cpython/commit/f60fd95dcc189ace8c0a2177a394b9cc20389a1e
msg389364 - (view) Author: FRANK BENNETT (bennett78) Date: 2021-03-23 01:58
$ 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+
msg389388 - (view) Author: Brent Gardner (Brent Gardner) * Date: 2021-03-23 14:47
This test was overlooked, and line 2052 should now be removed per the discussion here:

https://github.com/python/cpython/pull/14392#issuecomment-506133908

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.
History
Date User Action Args
2022-04-11 14:59:17adminsetgithub: 81586
2021-03-23 15:14:42Brent Gardnersetpull_requests: + pull_request23749
2021-03-23 14:47:20Brent Gardnersetmessages: + msg389388
2021-03-23 01:58:56bennett78setversions: + Python 3.10, - Python 3.7
nosy: + bennett78

messages: + msg389364

components: + Tests, - Documentation, Extension Modules
2019-09-12 10:34:31benjamin.petersonsetmessages: + msg352097
2019-09-12 10:03:09miss-islingtonsetpull_requests: + pull_request15641
2019-09-12 10:02:52benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg352086

resolution: fixed
stage: patch review -> resolved
2019-06-26 12:01:59python-devsetkeywords: + patch
stage: patch review
pull_requests: + pull_request14206
2019-06-26 05:31:55ned.deilysetnosy: + christian.heimes
2019-06-26 02:54:32Brent Gardnersetmessages: + msg346580
2019-06-26 02:51:26Brent Gardnersetmessages: + msg346579
components: + Extension Modules
2019-06-25 21:01:33Brent Gardnersettype: behavior
2019-06-25 21:01:17Brent Gardnercreate