Skip to content

Commit

Permalink
cpu: Optimizing the Method of Obtaining CPU Version Information
Browse files Browse the repository at this point in the history
1.By using the get() method to obtain matching patterns, it avoids using try exception to capture KeyError exceptions.
2.Immediately return the result after finding the version number to avoid unnecessary iteration.
3.Use an empty string as the default return result to avoid returning None

Signed-off-by: ut003460 <[email protected]>
  • Loading branch information
ut003460 committed Jul 12, 2023
1 parent d57d4ae commit a0ed2b8
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions avocado/utils/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ def get_version():
}
cpu_info = _get_info()
arch = get_arch()
try:
version_pattern[arch]
except KeyError as Err:
LOG.warning("No pattern string for arch: %s\n Error: %s", arch, Err)
return None
pattern = version_pattern.get(arch)
if not pattern:
LOG.warning("No pattern string for arch: %s", arch)
return ""

for line in cpu_info:
version_out = re.findall(version_pattern[arch], line)
version_out = re.findall(pattern, line)
if version_out:
return version_out[0].decode("utf-8")
return ""
Expand Down

0 comments on commit a0ed2b8

Please sign in to comment.