tools: fix deprecated syntax.

This fixes a similar deprecation warning on various pieces of code:

> DeprecationWarning: Testing an element's truth value will always return True in future versions.  Use specific 'len(elem)' or 'elem is not None' test instead.
>   samples = log.find ("samples") or empty_element
This commit is contained in:
Jehan 2024-09-26 19:49:47 +02:00
parent 05c86dad8a
commit 1726a6f89f
5 changed files with 27 additions and 23 deletions

View file

@ -32,7 +32,7 @@ log = ElementTree.fromstring (sys.stdin.buffer.read ())
address_map = log.find ("address-map") address_map = log.find ("address-map")
if address_map: if address_map is not None:
addresses = [] addresses = []
# Create base addresses dictionary # Create base addresses dictionary

View file

@ -35,8 +35,8 @@ log = ElementTree.fromstring (sys.stdin.buffer.read ())
# Construct state histogram # Construct state histogram
address_states = {} address_states = {}
for sample in (log.find ("samples") or empty_element).iterfind ("sample"): for sample in (log.find ("samples") if log.find ("samples") is not None else empty_element).iterfind ("sample"):
threads = (sample.find ("backtrace") or empty_element).iterfind ("thread") threads = (sample.find ("backtrace") if sample.find ("backtrace") is not None else empty_element).iterfind ("thread")
for thread in threads: for thread in threads:
running = int (thread.get ("running")) running = int (thread.get ("running"))
@ -73,8 +73,8 @@ for address, states in list (address_states.items ()):
del address_states[address] del address_states[address]
# Replace thread states # Replace thread states
for sample in (log.find ("samples") or empty_element).iterfind ("sample"): for sample in (log.find ("samples") if log.find ("samples") is not None else empty_element).iterfind ("sample"):
threads = (sample.find ("backtrace") or empty_element).iterfind ("thread") threads = (sample.find ("backtrace") if sample.find ("backtrace") is not None else empty_element).iterfind ("thread")
for thread in threads: for thread in threads:
frame = thread.find ("frame") frame = thread.find ("frame")

View file

@ -49,17 +49,19 @@ def expand_simple (element, last_values):
last_vars = {} last_vars = {}
last_backtrace = {} last_backtrace = {}
for sample in (log.find ("samples") or empty_element).iterfind ("sample"): for sample in (log.find ("samples") if log.find ("samples") is not None else empty_element).iterfind ("sample"):
# Expand variables # Expand variables
vars = sample.find ("vars") or \ vars = sample.find ("vars")
ElementTree.SubElement (sample, "vars") if vars is None:
vars = ElementTree.SubElement (sample, "vars")
expand_simple (vars, last_vars) expand_simple (vars, last_vars)
# Expand backtrace # Expand backtrace
if has_backtrace: if has_backtrace:
backtrace = sample.find ("backtrace") or \ backtrace = sample.find ("backtrace")
ElementTree.SubElement (sample, "backtrace") if backtrace is None:
backtrace = ElementTree.SubElement (sample, "backtrace")
for thread in backtrace: for thread in backtrace:
id = thread.get ("id") id = thread.get ("id")
@ -100,7 +102,7 @@ for sample in (log.find ("samples") or empty_element).iterfind ("sample"):
# Expand address map # Expand address map
last_address = {} last_address = {}
for address in (log.find ("address-map") or empty_element).iterfind ("address"): for address in (log.find ("address-map") if log.find ("address-map") is not None else empty_element).iterfind ("address"):
expand_simple (address, last_address) expand_simple (address, last_address)
# Write performance log to STDOUT # Write performance log to STDOUT

View file

@ -30,25 +30,27 @@ empty_element = ElementTree.Element ("")
# Read performance log from STDIN # Read performance log from STDIN
log = ElementTree.fromstring (sys.stdin.buffer.read ()) log = ElementTree.fromstring (sys.stdin.buffer.read ())
samples = log.find ("samples") or empty_element samples = log.find ("samples")
if samples is None:
samples = empty_element
address_map = log.find ("address-map") address_map = log.find ("address-map")
if not address_map: if address_map is None:
address_map = ElementTree.Element ("address-map") address_map = ElementTree.Element ("address-map")
# Coalesce partial address maps # Coalesce partial address maps
for partial_address_map in samples.iterfind ("address-map"): for partial_address_map in samples.iterfind ("address-map"):
for element in partial_address_map: for element in partial_address_map:
address_map.append (element) address_map.append (element)
# Remove partial address maps # Remove partial address maps
for partial_address_map in samples.iterfind ("address-map"): for partial_address_map in samples.iterfind ("address-map"):
samples.remove (partial_address_map) samples.remove (partial_address_map)
# Add global address map # Add global address map
if not log.find ("address-map") and len (address_map) > 0: if log.find ("address-map") is None and len (address_map) > 0:
log.append (address_map) log.append (address_map)
# Write performance log to STDOUT # Write performance log to STDOUT
sys.stdout.buffer.write (ElementTree.tostring (log)) sys.stdout.buffer.write (ElementTree.tostring (log))

View file

@ -252,7 +252,7 @@ AddressInfo = namedtuple ("AddressInfo", ("id",
address_map = {} address_map = {}
if log.find ("address-map"): if log.find ("address-map") is not None:
for address in log.find ("address-map").iterfind ("address"): for address in log.find ("address-map").iterfind ("address"):
value = int (address.get ("value"), 0) value = int (address.get ("value"), 0)
@ -308,7 +308,7 @@ for element in log.find ("samples"):
raw = var.text.strip () if var.text else None raw = var.text.strip () if var.text else None
) )
if element.find ("backtrace"): if element.find ("backtrace") is not None:
for thread in element.find ("backtrace").iterfind ("thread"): for thread in element.find ("backtrace").iterfind ("thread"):
id = thread.get ("id") id = thread.get ("id")
name = thread.get ("name") name = thread.get ("name")
@ -1671,12 +1671,12 @@ class InformationViewer (Gtk.ScrolledWindow):
params = log.find ("params") params = log.find ("params")
if params: if params is not None:
add_element (params) add_element (params)
info = log.find ("info") info = log.find ("info")
if info: if info is not None:
for element in info: for element in info:
add_element (element) add_element (element)