1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-14 11:49:01 +00:00

Replace exception-as-flow-control pattern

In general, it's better to use explicit flow control than to throw
and/or catch generic exceptions. Example:

try:
    thing = d["key"]
except:
    pass

This 1. catches an inspecific exception (probably KeyError), 2. can be
replaced with a check for ("key" is in d), and 3. can often be replaced
with d.get("key", some_default_value).
This commit is contained in:
dmiller
2014-01-13 15:37:39 +00:00
parent 0b2a445ddd
commit 8b70dfa0a4
2 changed files with 31 additions and 78 deletions

View File

@@ -328,12 +328,8 @@ class OptionTab(object):
self.notscripttab = False # assume every tab is scripting tab self.notscripttab = False # assume every tab is scripting tab
self.widgets_list = [] self.widgets_list = []
for option_element in root_tab.childNodes: for option_element in root_tab.childNodes:
try: if (hasattr(option_element, "tagName") and
option_element.tagName option_element.tagName in actions.keys()):
except:
pass
else:
if option_element.tagName in actions.keys():
parse_func = actions[option_element.tagName] parse_func = actions[option_element.tagName]
widget = parse_func(option_element) widget = parse_func(option_element)
self.widgets_list.append(widget) self.widgets_list.append(widget)

View File

@@ -248,54 +248,33 @@ class HostDetails(HIGVBox):
self.host_status_expander.set_expanded(True) self.host_status_expander.set_expanded(True)
table, hbox = self.create_table_hbox() table, hbox = self.create_table_hbox()
try: if ('state' in status and
if status['state'] == '': status['state'] != ''):
raise Exception
self.info_host_state_label.set_text(status['state']) self.info_host_state_label.set_text(status['state'])
except:
pass
try: if ('open' in status and
if status['open'] == '': status['open'] != ''):
raise Exception
self.info_open_ports.set_text(status['open']) self.info_open_ports.set_text(status['open'])
except:
pass
try: if ('filtered' in status and
if status['filtered'] == '': status['filtered'] != ''):
raise Exception
self.info_filtered_label.set_text(status['filtered']) self.info_filtered_label.set_text(status['filtered'])
except:
pass
try: if ('closed' in status and
if status['closed'] == '': status['closed'] != ''):
raise Exception
self.info_closed_ports.set_text(status['closed']) self.info_closed_ports.set_text(status['closed'])
except:
pass
try: if ('scanned' in status and
if status['scanned'] == '': status['scanned'] != ''):
raise Exception
self.info_scanned_label.set_text(status['scanned']) self.info_scanned_label.set_text(status['scanned'])
except:
pass
try: if ('uptime' in status and
if status['uptime'] == '': status['uptime'] != ''):
raise Exception
self.info_uptime_label.set_text(status['uptime']) self.info_uptime_label.set_text(status['uptime'])
except:
pass
try: if ('lastboot' in status and
if status['lastboot'] == '': status['lastboot'] != ''):
raise Exception
self.info_lastboot_label.set_text(status['lastboot']) self.info_lastboot_label.set_text(status['lastboot'])
except:
pass
table.attach(self.host_state_label, 0, 1, 0, 1) table.attach(self.host_state_label, 0, 1, 0, 1)
table.attach(self.info_host_state_label, 1, 2, 0, 1) table.attach(self.info_host_state_label, 1, 2, 0, 1)
@@ -339,26 +318,17 @@ class HostDetails(HIGVBox):
self.address_expander.set_expanded(True) self.address_expander.set_expanded(True)
#print '>>> Address:', address #print '>>> Address:', address
try: if ('ipv4' in address and
if address['ipv4'] == 1: address['ipv4'] != 1):
raise Exception
self.info_ipv4_label.set_text(address['ipv4']) self.info_ipv4_label.set_text(address['ipv4'])
except:
pass
try: if ('ipv6' in address and
if address['ipv6'] == 1: address['ipv6'] != 1):
raise Exception
self.info_ipv6_label.set_text(address['ipv6']) self.info_ipv6_label.set_text(address['ipv6'])
except:
pass
try: if ('mac' in address and
if address['mac'] == 1: address['mac'] != 1):
raise Exception
self.info_mac_label.set_text(address['mac']) self.info_mac_label.set_text(address['mac'])
except:
pass
table.attach(self.ipv4_label, 0, 1, 0, 1) table.attach(self.ipv4_label, 0, 1, 0, 1)
table.attach(self.info_ipv4_label, 1, 2, 0, 1) table.attach(self.info_ipv4_label, 1, 2, 0, 1)
@@ -382,17 +352,8 @@ class HostDetails(HIGVBox):
y2 = 2 y2 = 2
for h in hostname: for h in hostname:
name = na name = h.get('hostname', na)
try: type = h.get('hostname_type', na)
name = h['hostname']
except:
pass
type = na
try:
type = h['hostname_type']
except:
pass
table.attach(HIGEntryLabel(_('Name - Type:')), 0, 1, y1, y2) table.attach(HIGEntryLabel(_('Name - Type:')), 0, 1, y1, y2)
table.attach(HIGEntryLabel(name + ' - ' + type), 1, 2, y1, y2) table.attach(HIGEntryLabel(name + ' - ' + type), 1, 2, y1, y2)
@@ -409,10 +370,10 @@ class HostDetails(HIGVBox):
table, hbox = self.create_table_hbox() table, hbox = self.create_table_hbox()
progress = gtk.ProgressBar() progress = gtk.ProgressBar()
try: if 'accuracy' in os:
progress.set_fraction(float(os['accuracy']) / 100.0) progress.set_fraction(float(os['accuracy']) / 100.0)
progress.set_text(os['accuracy'] + '%') progress.set_text(os['accuracy'] + '%')
except: else:
progress.set_text(_('Not Available')) progress.set_text(_('Not Available'))
table.attach(HIGEntryLabel(_('Name:')), 0, 1, 0, 1) table.attach(HIGEntryLabel(_('Name:')), 0, 1, 0, 1)
@@ -424,20 +385,16 @@ class HostDetails(HIGVBox):
y1 = 2 y1 = 2
y2 = 3 y2 = 3
try: if 'portsused' in os:
self.set_ports_used(os['portsused']) self.set_ports_used(os['portsused'])
table.attach(self.portsused_expander, 0, 2, y1, y2) table.attach(self.portsused_expander, 0, 2, y1, y2)
y1 += 1 y1 += 1
y2 += 1 y2 += 1
except:
pass
try: if 'osclasses' in os:
self.set_osclass(os['osclasses']) self.set_osclass(os['osclasses'])
self.osclass_expander.set_use_markup(True) self.osclass_expander.set_use_markup(True)
table.attach(self.osclass_expander, 0, 2, y1, y2) table.attach(self.osclass_expander, 0, 2, y1, y2)
except:
pass
self.os_expander.add(hbox) self.os_expander.add(hbox)
self._pack_noexpand_nofill(self.os_expander) self._pack_noexpand_nofill(self.os_expander)