ptf icon indicating copy to clipboard operation
ptf copied to clipboard

"TypeError: ord() expected string of length 1, but int found" thrown in get_mac function in netutils.py

Open ZhaohuiS opened this issue 2 years ago • 0 comments

Install ptf 0.9.3, my python version is 3.7. In my script, just want to get mac address based on device number and port number, such as self.dataplane.get_mac(0, 2). It throws TypeError error. I think it shouldn't use ord(char) in get_mac function for python3. After remove ord, it returns correct mac address. How to resolve this compatible issue for python3? Thanks.

Traceback (most recent call last):
  File "ptftests/dhcp_relay_test.py", line 114, in setUp
    self.server_iface_mac = self.dataplane.get_mac(0, 2)
  File "/env-python3/lib/python3.7/site-packages/ptf/dataplane.py", line 990, in get_mac
    return self.ports[(device_number, port_number)].mac()
  File "/env-python3/lib/python3.7/site-packages/ptf/dataplane.py", line 198, in mac
    return netutils.get_mac(self.interface_name)
  File "/env-python3/lib/python3.7/site-packages/ptf/netutils.py", line 57, in get_mac
    return ":".join(["%02x" % ord(char) for char in get_if(iff, SIOCGIFHWADDR)[18:24]])
  File "/env-python3/lib/python3.7/site-packages/ptf/netutils.py", line 57, in <listcomp>
    return ":".join(["%02x" % ord(char) for char in get_if(iff, SIOCGIFHWADDR)[18:24]])
TypeError: ord() expected string of length 1, but int found
>>> import socket
>>> from fcntl import ioctl
>>> import struct
>>> SIOCGIFHWADDR = 0x8927
>>> s = socket.socket()
>>> ifreq = ioctl(s, SIOCGIFHWADDR, struct.pack("16s16x", 'eth2'.encode("utf-8")))
>>> ifreq
b'eth2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\xfeT\x00\xba~\x02\x00\x00\x00\x00\x00\x00\x00\x00'
>>> ":".join(["%02x" % ord(char) for char in ifreq[18:24]])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
TypeError: ord() expected string of length 1, but int found
>>> ":".join(["%02x" % char for char in ifreq[18:24]])
'fe:54:00:ba:7e:02'
>>> exit()
(env-python3) root@14d1d69ee82f:~# ifconfig eth2
eth2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 9216
        inet6 fe80::fc54:ff:feba:7e02  prefixlen 64  scopeid 0x20<link>
        ether fe:54:00:ba:7e:02  txqueuelen 1000  (Ethernet)
        RX packets 14360  bytes 1683464 (1.6 MiB)
        RX errors 0  dropped 9  overruns 0  frame 0
        TX packets 703  bytes 69618 (67.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ZhaohuiS avatar Apr 06 '22 13:04 ZhaohuiS