All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH nft v9]tests: py: fix pyhton3
@ 2019-06-19 17:57 Shekhar Sharma
  2019-06-20 14:37 ` Pablo Neira Ayuso
  2019-06-27 12:37 ` Eric Garver
  0 siblings, 2 replies; 7+ messages in thread
From: Shekhar Sharma @ 2019-06-19 17:57 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Shekhar Sharma

This patch changes the file to run on both python2 and python3.

The tempfile module has been imported and used.
Although the previous replacement of cmp() by eric works, 
I have replaced cmp(a,b) by ((a>b)-(a<b)) which works correctly.

Thanks!


Signed-off-by: Shekhar Sharma <shekhar250198@gmail.com>
---
The version hystory of this patch is:
v1:conversion to py3 by changing the print statements.
v2:add the '__future__' package for compatibility with py2 and py3.
v3:solves the 'version' problem in argparse by adding a new argument.
v4:uses .format() method to make print statements clearer.
v5:updated the shebang and corrected the sequence of import statements.
v6:resent the same with small changes
v7:resent with small changes
v9: replaced os module with tempfile and replaced cmp(a,b)
    with ((a>b)-(a<b)).


 tests/py/nft-test.py | 55 +++++++++++++++++++++++---------------------
 1 file changed, 29 insertions(+), 26 deletions(-)

diff --git a/tests/py/nft-test.py b/tests/py/nft-test.py
index 09d00dba..7ebcc8f1 100755
--- a/tests/py/nft-test.py
+++ b/tests/py/nft-test.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python2
+#!/usr/bin/env python
 #
 # (C) 2014 by Ana Rey Botello <anarey@gmail.com>
 #
@@ -13,12 +13,15 @@
 # Thanks to the Outreach Program for Women (OPW) for sponsoring this test
 # infrastructure.
 
+from __future__ import print_function
 import sys
 import os
 import argparse
 import signal
 import json
 import traceback
+import tempfile
+
 
 TESTS_PATH = os.path.dirname(os.path.abspath(__file__))
 sys.path.insert(0, os.path.join(TESTS_PATH, '../../py/'))
@@ -490,7 +493,7 @@ def set_check_element(rule1, rule2):
     pos1 = rule1.find("{")
     pos2 = rule2.find("{")
 
-    if (cmp(rule1[:pos1], rule2[:pos2]) != 0):
+    if (((rule1[:pos1] > rule2[:pos2]) - (rule1[:pos1] < rule2[:pos2])) != 0):
         return ret;
 
     end1 = rule1.find("}")
@@ -501,13 +504,13 @@ def set_check_element(rule1, rule2):
         list2 = (rule2[pos2 + 1:end2].replace(" ", "")).split(",")
         list1.sort()
         list2.sort()
-        if cmp(list1, list2) == 0:
+        if ((list1 > list2) - (list1 < list2)  == 0):
             ret = 0
 
     if ret != 0:
         return ret
 
-    return cmp(rule1[end1:], rule2[end2:])
+    return ((rule1[end1:] > rule2[end2:]) - (rule1[end1:] < rule2[end2:]))
 
 
 def obj_add(o, test_result, filename, lineno):
@@ -770,7 +773,7 @@ def rule_add(rule, filename, lineno, force_all_family_option, filename_path):
             unit_tests += 1
             table_flush(table, filename, lineno)
 
-            payload_log = os.tmpfile()
+            payload_log = tempfile.TemporaryFile(mode="w+")
 
             # Add rule and check return code
             cmd = "add rule %s %s %s" % (table, chain, rule[0])
@@ -910,7 +913,7 @@ def rule_add(rule, filename, lineno, force_all_family_option, filename_path):
                               gotf.name, 1)
 
             table_flush(table, filename, lineno)
-            payload_log = os.tmpfile()
+            payload_log = tempfile.TemporaryFile(mode="w+")
 
             # Add rule in JSON format
             cmd = json.dumps({ "nftables": [{ "add": { "rule": {
@@ -1016,9 +1019,9 @@ def execute_cmd(cmd, filename, lineno, stdout_log=False, debug=False):
     :param debug: temporarily set these debug flags
     '''
     global log_file
-    print >> log_file, "command: %s" % cmd
+    print("command: {}".format(cmd), file=log_file)
     if debug_option:
-        print cmd
+        print(cmd)
 
     if debug:
         debug_old = nftables.get_debug()
@@ -1212,7 +1215,7 @@ def run_test_file(filename, force_all_family_option, specific_file):
         sys.stdout.flush()
 
         if signal_received == 1:
-            print "\nSignal received. Cleaning up and Exitting..."
+            print("\nSignal received. Cleaning up and Exitting...")
             cleanup_on_exit()
             sys.exit(0)
 
@@ -1319,13 +1322,13 @@ def run_test_file(filename, force_all_family_option, specific_file):
 
     if specific_file:
         if force_all_family_option:
-            print print_result_all(filename, tests, total_warning, total_error,
-                                   total_unit_run)
+            print(print_result_all(filename, tests, total_warning, total_error,
+                                   total_unit_run))
         else:
-            print print_result(filename, tests, total_warning, total_error)
+            print(print_result(filename, tests, total_warning, total_error))
     else:
         if tests == passed and tests > 0:
-            print filename + ": " + Colors.GREEN + "OK" + Colors.ENDC
+            print(filename + ": " + Colors.GREEN + "OK" + Colors.ENDC)
 
     f.close()
     del table_list[:]
@@ -1336,7 +1339,7 @@ def run_test_file(filename, force_all_family_option, specific_file):
 
 
 def main():
-    parser = argparse.ArgumentParser(description='Run nft tests', version='1.0')
+    parser = argparse.ArgumentParser(description='Run nft tests')
 
     parser.add_argument('filenames', nargs='*', metavar='path/to/file.t',
                         help='Run only these tests')
@@ -1359,6 +1362,10 @@ def main():
                         dest='enable_schema',
                         help='verify json input/output against schema')
 
+    parser.add_argument('-v', '--version', action='version',
+                        version='1.0',
+                        help='Print the version information')
+
     args = parser.parse_args()
     global debug_option, need_fix_option, enable_json_option, enable_json_schema
     debug_option = args.debug
@@ -1372,15 +1379,15 @@ def main():
     signal.signal(signal.SIGTERM, signal_handler)
 
     if os.getuid() != 0:
-        print "You need to be root to run this, sorry"
+        print("You need to be root to run this, sorry")
         return
 
     # Change working directory to repository root
     os.chdir(TESTS_PATH + "/../..")
 
     if not os.path.exists('src/.libs/libnftables.so'):
-        print "The nftables library does not exist. " \
-              "You need to build the project."
+        print("The nftables library does not exist. "
+              "You need to build the project.")
         return
 
     if args.enable_schema and not args.enable_json:
@@ -1434,19 +1441,15 @@ def main():
             run_total += file_unit_run
 
     if test_files == 0:
-        print "No test files to run"
+        print("No test files to run")
     else:
         if not specific_file:
             if force_all_family_option:
-                print "%d test files, %d files passed, %d unit tests, " \
-                      "%d total executed, %d error, %d warning" \
-                      % (test_files, files_ok, tests, run_total, errors,
-                         warnings)
+                print("%d test files, %d files passed, %d unit tests, " % (test_files, files_ok, tests))
+                print("%d total executed, %d error, %d warning" % (run_total, errors,warnings))
             else:
-                print "%d test files, %d files passed, %d unit tests, " \
-                      "%d error, %d warning" \
-                      % (test_files, files_ok, tests, errors, warnings)
-
+                print("%d test files, %d files passed, %d unit tests, " % (test_files, files_ok, tests))
+                print("%d error, %d warning" % (errors, warnings))
 
 if __name__ == '__main__':
     main()
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH nft v9]tests: py: fix pyhton3
  2019-06-19 17:57 [PATCH nft v9]tests: py: fix pyhton3 Shekhar Sharma
@ 2019-06-20 14:37 ` Pablo Neira Ayuso
  2019-06-20 17:38   ` shekhar sharma
  2019-06-27 12:37 ` Eric Garver
  1 sibling, 1 reply; 7+ messages in thread
From: Pablo Neira Ayuso @ 2019-06-20 14:37 UTC (permalink / raw)
  To: Shekhar Sharma; +Cc: netfilter-devel

On Wed, Jun 19, 2019 at 11:27:41PM +0530, Shekhar Sharma wrote:
> This patch changes the file to run on both python2 and python3.
> 
> The tempfile module has been imported and used.
> Although the previous replacement of cmp() by eric works, 
> I have replaced cmp(a,b) by ((a>b)-(a<b)) which works correctly.

Any reason not to use Eric's approach? This ((a>b)-(a<b)) is
confusing.

> Thanks!

BTW, strictly place your patch description here.

Things like "Thanks!" to someone specifically and the cmp()
explanation should go below the --- marker, like versioning.

BTW, Cc Eric Garver in your patches, he's helping us with reviewing :-)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH nft v9]tests: py: fix pyhton3
  2019-06-20 14:37 ` Pablo Neira Ayuso
@ 2019-06-20 17:38   ` shekhar sharma
  2019-06-27 12:32     ` Eric Garver
  0 siblings, 1 reply; 7+ messages in thread
From: shekhar sharma @ 2019-06-20 17:38 UTC (permalink / raw)
  To: Pablo Neira Ayuso, Eric Garver; +Cc: Netfilter Development Mailing list

On Thu, Jun 20, 2019 at 8:07 PM Pablo Neira Ayuso <pablo@netfilter.org> wrote:
>
> On Wed, Jun 19, 2019 at 11:27:41PM +0530, Shekhar Sharma wrote:
> > This patch changes the file to run on both python2 and python3.
> >
> > The tempfile module has been imported and used.
> > Although the previous replacement of cmp() by eric works,
> > I have replaced cmp(a,b) by ((a>b)-(a<b)) which works correctly.
>
> Any reason not to use Eric's approach? This ((a>b)-(a<b)) is
> confusing.

No, Eric's approach is also working nicely. I read on a website
that cmp(a,b) of python2 can be replaced by ((a>b)-(a<b)) in python3.

> > Thanks!
>
> BTW, strictly place your patch description here.
>
> Things like "Thanks!" to someone specifically and the cmp()
> explanation should go below the --- marker, like versioning.
>
OK. Will take care of that in the future.

> BTW, Cc Eric Garver in your patches, he's helping us with reviewing :-)
Sure :-)

Shekhar

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH nft v9]tests: py: fix pyhton3
  2019-06-20 17:38   ` shekhar sharma
@ 2019-06-27 12:32     ` Eric Garver
  2019-06-27 15:41       ` shekhar sharma
  0 siblings, 1 reply; 7+ messages in thread
From: Eric Garver @ 2019-06-27 12:32 UTC (permalink / raw)
  To: shekhar sharma; +Cc: Pablo Neira Ayuso, Netfilter Development Mailing list

On Thu, Jun 20, 2019 at 11:08:18PM +0530, shekhar sharma wrote:
> On Thu, Jun 20, 2019 at 8:07 PM Pablo Neira Ayuso <pablo@netfilter.org> wrote:
> >
> > On Wed, Jun 19, 2019 at 11:27:41PM +0530, Shekhar Sharma wrote:
> > > This patch changes the file to run on both python2 and python3.
> > >
> > > The tempfile module has been imported and used.
> > > Although the previous replacement of cmp() by eric works,
> > > I have replaced cmp(a,b) by ((a>b)-(a<b)) which works correctly.
> >
> > Any reason not to use Eric's approach? This ((a>b)-(a<b)) is
> > confusing.
> 
> No, Eric's approach is also working nicely. I read on a website
> that cmp(a,b) of python2 can be replaced by ((a>b)-(a<b)) in python3.

This is true, but as Pablo stated it can be confusing. For this function
we only care if the sets are equivalent so I simplified it.

If you agree, the please drop this change from your next revision and
Pablo can take my patch.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH nft v9]tests: py: fix pyhton3
  2019-06-19 17:57 [PATCH nft v9]tests: py: fix pyhton3 Shekhar Sharma
  2019-06-20 14:37 ` Pablo Neira Ayuso
@ 2019-06-27 12:37 ` Eric Garver
  2019-06-27 15:42   ` shekhar sharma
  1 sibling, 1 reply; 7+ messages in thread
From: Eric Garver @ 2019-06-27 12:37 UTC (permalink / raw)
  To: Shekhar Sharma; +Cc: netfilter-devel

On Wed, Jun 19, 2019 at 11:27:41PM +0530, Shekhar Sharma wrote:
> This patch changes the file to run on both python2 and python3.
> 
> The tempfile module has been imported and used.
> Although the previous replacement of cmp() by eric works, 
> I have replaced cmp(a,b) by ((a>b)-(a<b)) which works correctly.
> 
> Thanks!
> 
> 
> Signed-off-by: Shekhar Sharma <shekhar250198@gmail.com>
> ---

The patch Subject has a typo, "pyhton3". Please fix it on next revision.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH nft v9]tests: py: fix pyhton3
  2019-06-27 12:32     ` Eric Garver
@ 2019-06-27 15:41       ` shekhar sharma
  0 siblings, 0 replies; 7+ messages in thread
From: shekhar sharma @ 2019-06-27 15:41 UTC (permalink / raw)
  To: Eric Garver, shekhar sharma, Pablo Neira Ayuso,
	Netfilter Development Mailing list

On Thu, Jun 27, 2019 at 6:02 PM Eric Garver <eric@garver.life> wrote:
>
> On Thu, Jun 20, 2019 at 11:08:18PM +0530, shekhar sharma wrote:
> > On Thu, Jun 20, 2019 at 8:07 PM Pablo Neira Ayuso <pablo@netfilter.org> wrote:
> > >
> > > On Wed, Jun 19, 2019 at 11:27:41PM +0530, Shekhar Sharma wrote:
> > > > This patch changes the file to run on both python2 and python3.
> > > >
> > > > The tempfile module has been imported and used.
> > > > Although the previous replacement of cmp() by eric works,
> > > > I have replaced cmp(a,b) by ((a>b)-(a<b)) which works correctly.
> > >
> > > Any reason not to use Eric's approach? This ((a>b)-(a<b)) is
> > > confusing.
> >
> > No, Eric's approach is also working nicely. I read on a website
> > that cmp(a,b) of python2 can be replaced by ((a>b)-(a<b)) in python3.
>
> This is true, but as Pablo stated it can be confusing. For this function
> we only care if the sets are equivalent so I simplified it.
>
True.
> If you agree, the please drop this change from your next revision and
> Pablo can take my patch.

OK , I will not include this change in v10.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH nft v9]tests: py: fix pyhton3
  2019-06-27 12:37 ` Eric Garver
@ 2019-06-27 15:42   ` shekhar sharma
  0 siblings, 0 replies; 7+ messages in thread
From: shekhar sharma @ 2019-06-27 15:42 UTC (permalink / raw)
  To: Eric Garver, Shekhar Sharma, Netfilter Development Mailing list

On Thu, Jun 27, 2019 at 6:07 PM Eric Garver <eric@garver.life> wrote:
>
> On Wed, Jun 19, 2019 at 11:27:41PM +0530, Shekhar Sharma wrote:
> > This patch changes the file to run on both python2 and python3.
> >
> > The tempfile module has been imported and used.
> > Although the previous replacement of cmp() by eric works,
> > I have replaced cmp(a,b) by ((a>b)-(a<b)) which works correctly.
> >
> > Thanks!
> >
> >
> > Signed-off-by: Shekhar Sharma <shekhar250198@gmail.com>
> > ---
>
> The patch Subject has a typo, "pyhton3". Please fix it on next revision.
Oops! Will correct it.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2019-06-27 15:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-19 17:57 [PATCH nft v9]tests: py: fix pyhton3 Shekhar Sharma
2019-06-20 14:37 ` Pablo Neira Ayuso
2019-06-20 17:38   ` shekhar sharma
2019-06-27 12:32     ` Eric Garver
2019-06-27 15:41       ` shekhar sharma
2019-06-27 12:37 ` Eric Garver
2019-06-27 15:42   ` shekhar sharma

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.