Skip to content

Commit b3e3edf

Browse files
author
ldx
committed
Refactor final_check_{match,target}().
1 parent 6be8805 commit b3e3edf

File tree

1 file changed

+51
-47
lines changed

1 file changed

+51
-47
lines changed

iptc/xtables.py

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,29 @@ def _options_fcheck(self, name, xflags, table):
10451045
continue
10461046
# XXX: check for conflicting options
10471047

1048+
def _fcheck_target_old(self, target):
1049+
# old API
1050+
if not target.final_check:
1051+
return
1052+
rv = _wrap_uintfn(target.final_check, target.tflags)
1053+
if rv:
1054+
raise XTablesError("%s.final_check() has failed" %
1055+
(target.name))
1056+
1057+
def _fcheck_target_new(self, target):
1058+
# new API
1059+
cb = xt_fcheck_call()
1060+
cb.ext_name = target.name
1061+
cb.data = ct.cast(target.t[0].data, ct.c_void_p)
1062+
cb.xflags = target.tflags
1063+
cb.udata = target.udata
1064+
rv = _wrap_x6fn(target.x6_fcheck, ct.pointer(cb))
1065+
if rv:
1066+
raise XTablesError("%s.x6_fcheck has failed" % (target.name))
1067+
if target.x6_options:
1068+
self._options_fcheck(target.name, target.tflags,
1069+
target.x6_options)
1070+
10481071
# Dispatch arguments to the appropriate final_check function, based upon
10491072
# the extension's choice of API.
10501073
@preserve_globals
@@ -1058,30 +1081,32 @@ def final_check_target(self, target):
10581081
pass
10591082

10601083
if x6_fcheck:
1061-
# new API
1062-
cb = xt_fcheck_call()
1063-
cb.ext_name = target.name
1064-
cb.data = ct.cast(target.t[0].data, ct.c_void_p)
1065-
cb.xflags = target.tflags
1066-
cb.udata = target.udata
1067-
rv = _wrap_x6fn(target.x6_fcheck, ct.pointer(cb))
1068-
if rv:
1069-
raise XTablesError("%s.x6_fcheck has failed" % (target.name))
1084+
self._fcheck_target_new(target)
10701085
else:
1071-
if target.final_check:
1072-
rv = _wrap_uintfn(target.final_check, target.tflags)
1073-
if rv:
1074-
raise XTablesError("%s.final_check() has failed" %
1075-
(target.name))
1086+
self._fcheck_target_old(target)
10761087

1077-
try:
1078-
# new API
1079-
if target.x6_options:
1080-
self._options_fcheck(target.name, target.tflags,
1081-
target.x6_options)
1082-
except AttributeError:
1083-
# old API
1084-
pass
1088+
def _fcheck_match_old(self, match):
1089+
# old API
1090+
if not match.final_check:
1091+
return
1092+
rv = _wrap_uintfn(match.final_check, match.mflags)
1093+
if rv:
1094+
raise XTablesError("%s.final_check() has failed" %
1095+
(match.name))
1096+
1097+
def _fcheck_match_new(self, match):
1098+
# new API
1099+
cb = xt_fcheck_call()
1100+
cb.ext_name = match.name
1101+
cb.data = ct.cast(match.m[0].data, ct.c_void_p)
1102+
cb.xflags = match.mflags
1103+
cb.udata = match.udata
1104+
rv = _wrap_x6fn(match.x6_fcheck, ct.pointer(cb))
1105+
if rv:
1106+
raise XTablesError("%s.x6_fcheck has failed" % (match.name))
1107+
if match.x6_options:
1108+
self._options_fcheck(match.name, match.mflags,
1109+
match.x6_options)
10851110

10861111
# Dispatch arguments to the appropriate final_check function, based upon
10871112
# the extension's choice of API.
@@ -1091,32 +1116,11 @@ def final_check_match(self, match):
10911116
try:
10921117
# new API?
10931118
x6_fcheck = match.x6_fcheck
1094-
except AttributeError: # old API
1119+
except AttributeError:
1120+
# old API
10951121
pass
10961122

10971123
if x6_fcheck:
1098-
# new API
1099-
cb = xt_fcheck_call()
1100-
cb.ext_name = match.name
1101-
cb.data = ct.cast(match.m[0].data, ct.c_void_p)
1102-
cb.xflags = match.mflags
1103-
cb.udata = match.udata
1104-
rv = _wrap_x6fn(match.x6_fcheck, ct.pointer(cb))
1105-
if rv:
1106-
raise XTablesError("%s.x6_fcheck has failed" % (match.name))
1124+
self._fcheck_match_new(match)
11071125
else:
1108-
# old API
1109-
if match.final_check:
1110-
rv = _wrap_uintfn(match.final_check, match.mflags)
1111-
if rv:
1112-
raise XTablesError("%s.final_check() has failed" %
1113-
(match.name))
1114-
1115-
try:
1116-
# new API
1117-
if match.x6_options:
1118-
self._options_fcheck(match.name, match.mflags,
1119-
match.x6_options)
1120-
except AttributeError:
1121-
# old API
1122-
pass
1126+
self._fcheck_match_old(match)

0 commit comments

Comments
 (0)