From patchwork Wed Jul 18 21:36:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeremy Cline X-Patchwork-Id: 10533331 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A42886053F for ; Wed, 18 Jul 2018 21:36:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 910D4297B3 for ; Wed, 18 Jul 2018 21:36:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 82865297CA; Wed, 18 Jul 2018 21:36:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8377C297B3 for ; Wed, 18 Jul 2018 21:36:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730021AbeGRWQ3 (ORCPT ); Wed, 18 Jul 2018 18:16:29 -0400 Received: from mail-qt0-f177.google.com ([209.85.216.177]:33930 "EHLO mail-qt0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729973AbeGRWQ3 (ORCPT ); Wed, 18 Jul 2018 18:16:29 -0400 Received: by mail-qt0-f177.google.com with SMTP id m13-v6so5462512qth.1 for ; Wed, 18 Jul 2018 14:36:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=WnQEd0ZhVezFb2KO/X5flI43ziSJjnemW3TeHBZ8x+Q=; b=JzDFCRVa1LHmRi7jcrlxHo/zUyJ5nsrLuQv1Lkh1SXEgdZtRR1UjDqUWukfdbW3efh 1+RoAybcvnd5ApU4zSjZseGNadMk9NKp9g/+jkOojxBHy2Srd5oXwRszazW9V5wPo5vq cxzYb39abJqAmdFiqGgKaQPv6GQWpBGT+oxddsOLyIwXfiH97mvSZJJAWFwUYQwmxuAn HAf4Mhh84gfOvbgN5O7l+7r4AZF4yKiZMUmFpC16RWZieUv2WxhgPAUpjdHJTStSKEc1 z1yOeW4sJP62vZBQHUPMWCwPX4T+LUmtgsSRPaGtXxY8ivsCBigXZx7G82Hi5qYhtGSC eLGQ== X-Gm-Message-State: AOUpUlHF1fATvV2LFrgXojvNGO7OQXn5GcLoqSETxnvQ3Ty9WTNEISmX ovdMd5jDmpMlFnn5fI+GorgZUw== X-Google-Smtp-Source: AAOMgpeTweAiiC/uRXEa3oV5KC6hRlHPmZxxNL+a0xVr1egEGztMJU+oHF4mQK4zgULG3JOWlM4Uyw== X-Received: by 2002:a0c:c963:: with SMTP id v32-v6mr8227245qvj.215.1531949799993; Wed, 18 Jul 2018 14:36:39 -0700 (PDT) Received: from laptop.jcline.org.com (cpe-2606-A000-1127-D1-82A4-FE7D-5832-CB87.dyn6.twc.com. [2606:a000:1127:d1:82a4:fe7d:5832:cb87]) by smtp.gmail.com with ESMTPSA id f70-v6sm2692540qke.77.2018.07.18.14.36.38 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 18 Jul 2018 14:36:39 -0700 (PDT) From: Jeremy Cline To: Alexei Starovoitov , Daniel Borkmann , Shuah Khan Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Jeremy Cline Subject: [PATCH] bpf: Add Python 3 support to selftests scripts for bpf Date: Wed, 18 Jul 2018 17:36:10 -0400 Message-Id: <20180718213610.19618-1-jcline@redhat.com> X-Mailer: git-send-email 2.17.1 Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- tools/testing/selftests/bpf/tcp_client.py | 12 ++++++------ tools/testing/selftests/bpf/tcp_server.py | 17 +++++++++-------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/tools/testing/selftests/bpf/tcp_client.py b/tools/testing/selftests/bpf/tcp_client.py index 481dccdf140c..9fe5f1b5c020 100755 --- a/tools/testing/selftests/bpf/tcp_client.py +++ b/tools/testing/selftests/bpf/tcp_client.py @@ -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); diff --git a/tools/testing/selftests/bpf/tcp_server.py b/tools/testing/selftests/bpf/tcp_server.py index bc454d7d0be2..1d4a40a6584b 100755 --- a/tools/testing/selftests/bpf/tcp_server.py +++ b/tools/testing/selftests/bpf/tcp_server.py @@ -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)