Skip to contentSkip to conversation

bpf: Add Python 3 support to selftests scripts for bpf

PATCH In review Jeremy Cline 3 participants 5 messages Jul 18, 2018 – Jul 23, 2018

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Add Python 3 support to selftests scripts for bpf

Adjust tcp_client.py and tcp_server.py to work with Python 3 by using the print function, marking string literals as bytes, and using the newer exception syntax. This should be functionally equivalent and support Python 2.6 through Python 3.7.

Signed-off-by Jeremy Cline <jcline at redhat.com>
---
 tools/testing/selftests/bpf/tcp_client.py | 12 ++++++------
 tools/testing/selftests/bpf/tcp_server.py | 17 +++++++++--------
 2 files changed, 15 insertions(+), 14 deletions(-)
tools/testing/selftests/bpf/tcp_client.py +6 -6
index 481dccdf140c..9fe5f1b5c020 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 #
 # SPDX-License-Identifier: GPL-2.0
 #
@@ -9,11 +9,11 @@ import subprocess
 import select
 
 def read(sock, n):
-    buf = ''
+    buf = b''
     while len(buf) < n:
         rem = n - len(buf)
         try: s = sock.recv(rem)
-        except (socket.error), e: return ''
+        except (socket.error) as e: return b''
         buf += s
     return buf
 
@@ -22,7 +22,7 @@ def send(sock, s):
     count = 0
     while count < total:
         try: n = sock.send(s)
-        except (socket.error), e: n = 0
+        except (socket.error) as e: n = 0
         if n == 0:
             return count;
         count += n
@@ -39,10 +39,10 @@ try:
 except socket.error as e:
     sys.exit(1)
 
-buf = ''
+buf = b''
 n = 0
 while n < 1000:
-    buf += '+'
+    buf += b'+'
     n += 1
 
 sock.settimeout(1);
tools/testing/selftests/bpf/tcp_server.py +9 -8
index bc454d7d0be2..1d4a40a6584b 100755
@@ -1,7 +1,8 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 #
 # SPDX-License-Identifier: GPL-2.0
 #
+from __future__ import print_function
 
 import sys, os, os.path, getopt
 import socket, time
@@ -9,11 +10,11 @@ import subprocess
 import select
 
 def read(sock, n):
-    buf = ''
+    buf = b''
     while len(buf) < n:
         rem = n - len(buf)
         try: s = sock.recv(rem)
-        except (socket.error), e: return ''
+        except (socket.error) as e: return b''
         buf += s
     return buf
 
@@ -22,7 +23,7 @@ def send(sock, s):
     count = 0
     while count < total:
         try: n = sock.send(s)
-        except (socket.error), e: n = 0
+        except (socket.error) as e: n = 0
         if n == 0:
             return count;
         count += n
@@ -43,7 +44,7 @@ host = socket.gethostname()
 
 try: serverSocket.bind((host, 0))
 except socket.error as msg:
-    print 'bind fails: ', msg
+    print('bind fails: ' + str(msg))
 
 sn = serverSocket.getsockname()
 serverPort = sn[1]
@@ -51,10 +52,10 @@ serverPort = sn[1]
 cmdStr = ("./tcp_client.py %d &") % (serverPort)
 os.system(cmdStr)
 
-buf = ''
+buf = b''
 n = 0
 while n < 500:
-    buf += '.'
+    buf += b'.'
     n += 1
 
 serverSocket.listen(MAX_PORTS)
@@ -79,5 +80,5 @@ while True:
                 serverSocket.close()
                 sys.exit(0)
     else:
-        print 'Select timeout!'
+        print('Select timeout!')
         sys.exit(1)
Signature · 6 lines
2.17.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
maintainer
#1 lore raw
Quoted 6 lines · Jeremy Cline

Adjust tcp_client.py and tcp_server.py to work with Python 3 by using the print function, marking string literals as bytes, and using the newer exception syntax. This should be functionally equivalent and support Python 2.6 through Python 3.7.

Signed-off-by Jeremy Cline <jcline at redhat.com>

Thanks for the patch, Jeremy! Given we also have test_offload.py in BPF kselftests and it is written for python 3 only, it would probably make sense to adapt the tcp_{client,server}.py towards python 3 as well, so we wouldn't need to keep extra compat for 2 and have a consistent version dependency. Lawrence / Jeremy, any objections?

Quoted 111 lines
 tools/testing/selftests/bpf/tcp_client.py | 12 ++++++------
 tools/testing/selftests/bpf/tcp_server.py | 17 +++++++++--------
 2 files changed, 15 insertions(+), 14 deletions(-)
tools/testing/selftests/bpf/tcp_client.py +6 -6
index 481dccdf140c..9fe5f1b5c020 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 #
 # SPDX-License-Identifier: GPL-2.0
 #
@@ -9,11 +9,11 @@ import subprocess
 import select
 
 def read(sock, n):
-    buf = ''
+    buf = b''
     while len(buf) < n:
         rem = n - len(buf)
         try: s = sock.recv(rem)
-        except (socket.error), e: return ''
+        except (socket.error) as e: return b''
         buf += s
     return buf
 
@@ -22,7 +22,7 @@ def send(sock, s):
     count = 0
     while count < total:
         try: n = sock.send(s)
-        except (socket.error), e: n = 0
+        except (socket.error) as e: n = 0
         if n == 0:
             return count;
         count += n
@@ -39,10 +39,10 @@ try:
 except socket.error as e:
     sys.exit(1)
 
-buf = ''
+buf = b''
 n = 0
 while n < 1000:
-    buf += '+'
+    buf += b'+'
     n += 1
 
 sock.settimeout(1);
tools/testing/selftests/bpf/tcp_server.py +8 -7
index bc454d7d0be2..1d4a40a6584b 100755
@@ -1,7 +1,8 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 #
 # SPDX-License-Identifier: GPL-2.0
 #
+from __future__ import print_function
 
 import sys, os, os.path, getopt
 import socket, time
@@ -9,11 +10,11 @@ import subprocess
 import select
 
 def read(sock, n):
-    buf = ''
+    buf = b''
     while len(buf) < n:
         rem = n - len(buf)
         try: s = sock.recv(rem)
-        except (socket.error), e: return ''
+        except (socket.error) as e: return b''
         buf += s
     return buf
 
@@ -22,7 +23,7 @@ def send(sock, s):
     count = 0
     while count < total:
         try: n = sock.send(s)
-        except (socket.error), e: n = 0
+        except (socket.error) as e: n = 0
         if n == 0:
             return count;
         count += n
@@ -43,7 +44,7 @@ host = socket.gethostname()
 
 try: serverSocket.bind((host, 0))
 except socket.error as msg:
-    print 'bind fails: ', msg
+    print('bind fails: ' + str(msg))
 
 sn = serverSocket.getsockname()
 serverPort = sn[1]
@@ -51,10 +52,10 @@ serverPort = sn[1]
 cmdStr = ("./tcp_client.py %d &") % (serverPort)
 os.system(cmdStr)
 
-buf = ''
+buf = b''
 n = 0
 while n < 500:
-    buf += '.'
+    buf += b'.'
     n += 1
 
 serverSocket.listen(MAX_PORTS)

tools/testing/selftests/bpf/tcp_server.py while True: · line 84

Quoted 6 lines
tools/testing/selftests/bpf/tcp_server.py +1 -1
@@ -79,5 +80,5 @@ while True:
                 serverSocket.close()
                 sys.exit(0)
     else:
-        print 'Select timeout!'
+        print('Select timeout!')
         sys.exit(1)

--
To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw

PATCH 1/1 bpf: Add Python 3 support to selftests scripts for bpf

Hi Daniel,

Quoted 6 lines · Daniel Borkmann
Quoted 5 lines · Jeremy Cline

Adjust tcp_client.py and tcp_server.py to work with Python 3 by using the print function, marking string literals as bytes, and using the newer exception syntax. This should be functionally equivalent and support Python 2.6 through Python 3.7.

Quoted 7 lines
Quoted 1 line
Signed-off-by Jeremy Cline <jcline at redhat.com>

Thanks for the patch, Jeremy! Given we also have test_offload.py in BPF kselftests and it is written for python 3 only, it would probably make sense to adapt the tcp_{client,server}.py towards python 3 as well, so we wouldn't need to keep extra compat for 2 and have a consistent version dependency. Lawrence / Jeremy, any objections?

I certainly don't object to Python 3 only and I'm happy to drop the Python 2 compatibility from this patch if that's okay.

Quoted 109 lines
Quoted 109 lines
 tools/testing/selftests/bpf/tcp_client.py | 12 ++++++------
 tools/testing/selftests/bpf/tcp_server.py | 17 +++++++++--------
 2 files changed, 15 insertions(+), 14 deletions(-)
tools/testing/selftests/bpf/tcp_client.py +6 -6
index 481dccdf140c..9fe5f1b5c020 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 #
 # SPDX-License-Identifier: GPL-2.0
 #
@@ -9,11 +9,11 @@ import subprocess
 import select
 
 def read(sock, n):
-    buf = ''
+    buf = b''
     while len(buf) < n:
         rem = n - len(buf)
         try: s = sock.recv(rem)
-        except (socket.error), e: return ''
+        except (socket.error) as e: return b''
         buf += s
     return buf
 
@@ -22,7 +22,7 @@ def send(sock, s):
     count = 0
     while count < total:
         try: n = sock.send(s)
-        except (socket.error), e: n = 0
+        except (socket.error) as e: n = 0
         if n == 0:
             return count;
         count += n
@@ -39,10 +39,10 @@ try:
 except socket.error as e:
     sys.exit(1)
 
-buf = ''
+buf = b''
 n = 0
 while n < 1000:
-    buf += '+'
+    buf += b'+'
     n += 1
 
 sock.settimeout(1);
tools/testing/selftests/bpf/tcp_server.py +8 -7
index bc454d7d0be2..1d4a40a6584b 100755
@@ -1,7 +1,8 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 #
 # SPDX-License-Identifier: GPL-2.0
 #
+from __future__ import print_function
 
 import sys, os, os.path, getopt
 import socket, time
@@ -9,11 +10,11 @@ import subprocess
 import select
 
 def read(sock, n):
-    buf = ''
+    buf = b''
     while len(buf) < n:
         rem = n - len(buf)
         try: s = sock.recv(rem)
-        except (socket.error), e: return ''
+        except (socket.error) as e: return b''
         buf += s
     return buf
 
@@ -22,7 +23,7 @@ def send(sock, s):
     count = 0
     while count < total:
         try: n = sock.send(s)
-        except (socket.error), e: n = 0
+        except (socket.error) as e: n = 0
         if n == 0:
             return count;
         count += n
@@ -43,7 +44,7 @@ host = socket.gethostname()
 
 try: serverSocket.bind((host, 0))
 except socket.error as msg:
-    print 'bind fails: ', msg
+    print('bind fails: ' + str(msg))
 
 sn = serverSocket.getsockname()
 serverPort = sn[1]
@@ -51,10 +52,10 @@ serverPort = sn[1]
 cmdStr = ("./tcp_client.py %d &") % (serverPort)
 os.system(cmdStr)
 
-buf = ''
+buf = b''
 n = 0
 while n < 500:
-    buf += '.'
+    buf += b'.'
     n += 1
 
Quoted 10 lines

tools/testing/selftests/bpf/tcp_server.py while True: · line 84

Quoted 9 lines
tools/testing/selftests/bpf/tcp_server.py +1 -1
 serverSocket.listen(MAX_PORTS)
@@ -79,5 +80,5 @@ while True:
                 serverSocket.close()
                 sys.exit(0)
     else:
-        print 'Select timeout!'
+        print('Select timeout!')
         sys.exit(1)

--
To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 11 lines · Jeremy Cline

Hi Daniel,

Quoted 8 lines · Daniel Borkmann
Quoted 6 lines · Jeremy Cline

Adjust tcp_client.py and tcp_server.py to work with Python 3 by using the print function, marking string literals as bytes, and using the newer exception syntax. This should be functionally equivalent and support Python 2.6 through Python 3.7.

Signed-off-by Jeremy Cline <jcline at redhat.com>
Quoted 8 lines
Quoted 5 lines

Thanks for the patch, Jeremy! Given we also have test_offload.py in BPF kselftests and it is written for python 3 only, it would probably make sense to adapt the tcp_{client,server}.py towards python 3 as well, so we wouldn't need to keep extra compat for 2 and have a consistent version dependency. Lawrence / Jeremy, any objections?

I certainly don't object to Python 3 only and I'm happy to drop the Python 2 compatibility from this patch if that's okay.

This (well, along with introducing python in the first place, which took me by surprise), sounds like a policy decision that should be made clear in the kselftest documentation (Documentation/dev-tools/kselftest.rst). Currently, that file does not mention any python requirement.

That said, I agree that python2 support is no longer necessary.

My use-case (which may be unusual?): We try to run all of kselftest against a variety of kernels and architectures for every push to next, mainline, and stable/lts branches. It seems that this is not a common usecase, but shouldn't it be?

Dan

Quoted 122 lines
Quoted 110 lines
Quoted 110 lines

 tools/testing/selftests/bpf/tcp_client.py | 12 ++++++------
 tools/testing/selftests/bpf/tcp_server.py | 17 +++++++++--------
 2 files changed, 15 insertions(+), 14 deletions(-)
tools/testing/selftests/bpf/tcp_client.py +6 -6
index 481dccdf140c..9fe5f1b5c020 100755
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 #
 # SPDX-License-Identifier: GPL-2.0
 #
@@ -9,11 +9,11 @@ import subprocess
 import select
 
 def read(sock, n):
-    buf = ''
+    buf = b''
     while len(buf) < n:
         rem = n - len(buf)
         try: s = sock.recv(rem)
-        except (socket.error), e: return ''
+        except (socket.error) as e: return b''
         buf += s
     return buf
 
@@ -22,7 +22,7 @@ def send(sock, s):
     count = 0
     while count < total:
         try: n = sock.send(s)
-        except (socket.error), e: n = 0
+        except (socket.error) as e: n = 0
         if n == 0:
             return count;
         count += n
@@ -39,10 +39,10 @@ try:
 except socket.error as e:
     sys.exit(1)
 
-buf = ''
+buf = b''
 n = 0
 while n < 1000:
-    buf += '+'
+    buf += b'+'
     n += 1
 
 sock.settimeout(1);
tools/testing/selftests/bpf/tcp_server.py +8 -7
index bc454d7d0be2..1d4a40a6584b 100755
@@ -1,7 +1,8 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 #
 # SPDX-License-Identifier: GPL-2.0
 #
+from __future__ import print_function
 
 import sys, os, os.path, getopt
 import socket, time
@@ -9,11 +10,11 @@ import subprocess
 import select
 
 def read(sock, n):
-    buf = ''
+    buf = b''
     while len(buf) < n:
         rem = n - len(buf)
         try: s = sock.recv(rem)
-        except (socket.error), e: return ''
+        except (socket.error) as e: return b''
         buf += s
     return buf
 
@@ -22,7 +23,7 @@ def send(sock, s):
     count = 0
     while count < total:
         try: n = sock.send(s)
-        except (socket.error), e: n = 0
+        except (socket.error) as e: n = 0
         if n == 0:
             return count;
         count += n
@@ -43,7 +44,7 @@ host = socket.gethostname()
 
 try: serverSocket.bind((host, 0))
 except socket.error as msg:
-    print 'bind fails: ', msg
+    print('bind fails: ' + str(msg))
 
 sn = serverSocket.getsockname()
 serverPort = sn[1]
@@ -51,10 +52,10 @@ serverPort = sn[1]
 cmdStr = ("./tcp_client.py %d &") % (serverPort)
 os.system(cmdStr)
 
-buf = ''
+buf = b''
 n = 0
 while n < 500:
-    buf += '.'
+    buf += b'.'
     n += 1
 
Quoted 10 lines

tools/testing/selftests/bpf/tcp_server.py while True: · line 84

Quoted 9 lines
tools/testing/selftests/bpf/tcp_server.py +1 -1
 serverSocket.listen(MAX_PORTS)
@@ -79,5 +80,5 @@ while True:
                 serverSocket.close()
                 sys.exit(0)
     else:
-        print 'Select timeout!'
+        print('Select timeout!')
         sys.exit(1)
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#4 lore raw
Quoted 10 lines · Dan Rue
Quoted 9 lines · Jeremy Cline
Quoted 8 lines · Daniel Borkmann
Quoted 6 lines · Jeremy Cline

Adjust tcp_client.py and tcp_server.py to work with Python 3 by using the print function, marking string literals as bytes, and using the newer exception syntax. This should be functionally equivalent and support Python 2.6 through Python 3.7.

Signed-off-by Jeremy Cline <jcline at redhat.com>
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

Thanks for the patch, Jeremy! Given we also have test_offload.py in BPF kselftests and it is written for python 3 only, it would probably make sense to adapt the tcp_{client,server}.py towards python 3 as well, so we wouldn't need to keep extra compat for 2 and have a consistent version dependency. Lawrence / Jeremy, any objections?

I certainly don't object to Python 3 only and I'm happy to drop the Python 2 compatibility from this patch if that's okay.

Sounds good, lets do it, please respin with that.

Quoted 4 lines

This (well, along with introducing python in the first place, which took me by surprise), sounds like a policy decision that should be made clear in the kselftest documentation (Documentation/dev-tools/kselftest.rst). Currently, that file does not mention any python requirement.

Right now each selftest subdir has a config file which lists dependencies, perhaps it makes sense to have another standardized file there (e.g. 'deps') which lists user space dependencies, so it's immediately visible what is needed to run all tests from there. Thoughts?

Quoted 6 lines

That said, I agree that python2 support is no longer necessary.

My use-case (which may be unusual?): We try to run all of kselftest against a variety of kernels and architectures for every push to next, mainline, and stable/lts branches. It seems that this is not a common usecase, but shouldn't it be?

As far as I'm aware the intel lkp-tests bot seems also to regularly run the kselftests for x86, if also done from arm side e.g. on latest mainline, even better.

Thanks,
Daniel
--
To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html