Skip to contentSkip to conversation

py: return boolean value from Nftables.__[gs]et_output_flag()

PATCH In review Thomas Haller 2 participants 2 messages Jul 18, 2023 – Jul 19, 2023 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 py: return boolean value from Nftables.__[gs]et_output_flag()

The callers of __get_output_flag() and __set_output_flag(), for example get_reversedns_output(), are all documented to return a "boolean" value.

Instead, they returned the underlying, non-zero flags value. That number is not obviously useful to the caller, because there is no API so that the caller could do anything with it (except evaluating it in a boolean context). Adjust that, to match the documentation.

The alternative would be to update the documentation, to indicate that the functions return a non-zero integer when the flag is set. That would preserve the previous behavior and maybe the number could be useful somehow(?).

---
 py/nftables.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
py/nftables.py +2 -2
index 6daeafc231f4..68fcd7dd103c 100644
@@ -154,7 +154,7 @@ class Nftables:
 
     def __get_output_flag(self, name):
         flag = self.output_flags[name]
-        return self.nft_ctx_output_get_flags(self.__ctx) & flag
+        return (self.nft_ctx_output_get_flags(self.__ctx) & flag) != 0
 
     def __set_output_flag(self, name, val):
         flag = self.output_flags[name]
@@ -164,7 +164,7 @@ class Nftables:
         else:
             new_flags = flags & ~flag
         self.nft_ctx_output_set_flags(self.__ctx, new_flags)
-        return flags & flag
+        return (flags & flag) != 0
 
     def get_reversedns_output(self):
         """Get the current state of reverse DNS output.
Signature · 1 line
2.41.0
reviewer
#1 lore raw
Quoted 8 lines · Thomas Haller

The callers of __get_output_flag() and __set_output_flag(), for example get_reversedns_output(), are all documented to return a "boolean" value.

Instead, they returned the underlying, non-zero flags value. That number is not obviously useful to the caller, because there is no API so that the caller could do anything with it (except evaluating it in a boolean context). Adjust that, to match the documentation.

Quoted 6 lines

The alternative would be to update the documentation, to indicate that the functions return a non-zero integer when the flag is set. That would preserve the previous behavior and maybe the number could be useful somehow(?).

Patch applied, thanks!

I wasn't aware Python is not intuitive (from a C programmer's point of view) in that regard:

| >>> 0 == True
| False
| >>> 1 == True
| True
| >>> 2 == True
| False
| >>> 3 == True
| False
| >>> if 3:
| ...   print("ok")
| ... 
| ok