summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd Gamblin <tgamblin@llnl.gov>2021-02-06 00:22:46 -0800
committerMassimiliano Culpo <massimiliano.culpo@gmail.com>2021-02-11 15:30:57 +0100
commitc81ca37dfccba9dd2863f8ec40414e0453bcc983 (patch)
tree5a1c6c140de2d87a764e12ab61233c9193c91602
parente3e913ef8fc61cc07ba8ff2da3c5f5fb1cd5590e (diff)
downloadspack-c81ca37dfccba9dd2863f8ec40414e0453bcc983.tar.gz
spack-c81ca37dfccba9dd2863f8ec40414e0453bcc983.tar.bz2
spack-c81ca37dfccba9dd2863f8ec40414e0453bcc983.tar.xz
spack-c81ca37dfccba9dd2863f8ec40414e0453bcc983.zip
version: precompile regexes in Version constructor
-rw-r--r--lib/spack/spack/version.py15
1 files changed, 9 insertions, 6 deletions
diff --git a/lib/spack/spack/version.py b/lib/spack/spack/version.py
index d4deee78a9..321a03efc7 100644
--- a/lib/spack/spack/version.py
+++ b/lib/spack/spack/version.py
@@ -37,7 +37,10 @@ from spack.util.spack_yaml import syaml_dict
__all__ = ['Version', 'VersionRange', 'VersionList', 'ver']
# Valid version characters
-VALID_VERSION = r'[A-Za-z0-9_.-]'
+VALID_VERSION = re.compile(r'[A-Za-z0-9_.-]')
+
+# regex for version segments
+SEGMENT_REGEX = re.compile(r'[a-zA-Z]+|[0-9]+')
# Infinity-like versions. The order in the list implies the comparison rules
infinity_versions = ['develop', 'main', 'master', 'head', 'trunk']
@@ -97,9 +100,10 @@ class Version(object):
"""Class to represent versions"""
def __init__(self, string):
- string = str(string)
+ if not isinstance(string, str):
+ string = str(string)
- if not re.match(VALID_VERSION, string):
+ if not VALID_VERSION.match(string):
raise ValueError("Bad characters in version string: %s" % string)
# preserve the original string, but trimmed.
@@ -107,12 +111,11 @@ class Version(object):
self.string = string
# Split version into alphabetical and numeric segments
- segment_regex = r'[a-zA-Z]+|[0-9]+'
- segments = re.findall(segment_regex, string)
+ segments = SEGMENT_REGEX.findall(string)
self.version = tuple(int_if_int(seg) for seg in segments)
# Store the separators from the original version string as well.
- self.separators = tuple(re.split(segment_regex, string)[1:])
+ self.separators = tuple(SEGMENT_REGEX.split(string)[1:])
@property
def dotted(self):