Skip to contentSkip to conversation

tests: iptables-test, xlate-test: use `os.unshare` Python function

PATCH In review Jeremy Sowden 2 participants 3 messages Mar 4 – Mar 5 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests: iptables-test, xlate-test: use `os.unshare` Python function

Since Python 3.12 the standard library has included an os.unshare function. Use it if it is available.

---
 iptables-test.py | 9 ++++++++-
 xlate-test.py    | 9 ++++++++-
 2 files changed, 16 insertions(+), 2 deletions(-)
iptables-test.py +8 -1
index 66db552185bc..40ed77035c02 100755
@@ -572,7 +572,14 @@ def show_missing():
     print('\n'.join(missing))
 
 def spawn_netns():
-    # prefer unshare module
+    # prefer stdlib unshare function ...
+    try:
+        os.unshare(os.CLONE_NEWNET)
+        return True
+    except Exception as e:
+        pass
+
+    # ... or unshare module
     try:
         import unshare
         unshare.unshare(unshare.CLONE_NEWNET)
xlate-test.py +8 -1
index 1c8cfe71ffd4..247852939c9e 100755
@@ -203,7 +203,14 @@ def load_test_files():
 
 
 def spawn_netns():
-    # prefer unshare module
+    # prefer stdlib unshare function ...
+    try:
+        os.unshare(os.CLONE_NEWNET)
+        return True
+    except Exception as e:
+        pass
+
+    # ... or unshare module
     try:
         import unshare
         unshare.unshare(unshare.CLONE_NEWNET)
Signature · 1 line
2.51.0
reviewer
#1 lore raw
Quoted 4 lines · Jeremy Sowden

Since Python 3.12 the standard library has included an os.unshare function. Use it if it is available.

Patch applied, thanks!

We have the same pattern in nftables' tests/py/nft-test.py, could you please send a patch for that as well?

Thanks, Phil

#2 lore raw
Quoted 10 lines · Phil Sutter
Quoted 4 lines · Jeremy Sowden

Since Python 3.12 the standard library has included an os.unshare function. Use it if it is available.

Patch applied, thanks!

We have the same pattern in nftables' tests/py/nft-test.py, could you please send a patch for that as well?

Yup.

J.