Created on 2019-06-25 21:01 by Brent Gardner, last changed 2021-03-23 15:14 by Brent Gardner. This issue is now closed.
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'
Changed caused by commit effc12f8e9e20d0951d2ba5883587666bd8218e3 to cpython/Modules/socketmodule.c on Sep 6, 2017.
Correction, change caused by a30f6d45ac3e72761b96a8df0527182029eaee24 to cpython/Modules/socketmodule.c on Aug 28, 2017.
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
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
$ 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+
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.
messages: + msg389364
components: + Tests, - Documentation, Extension Modules
nosy:
+ benjamin.peterson
messages:
+ msg352086
resolution: fixed
stage: patch review -> resolved