Skip to content

Commit

Permalink
Extract method for checking macro definition.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jun 27, 2024
1 parent efc5dfb commit 92c9626
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions distutils/ccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,23 +188,26 @@ def _find_macro(self, name):
return None

def _check_macro_definitions(self, definitions):
"""Ensures that every element of 'definitions' is a valid macro
definition, ie. either (name,value) 2-tuple or a (name,) tuple. Do
nothing if all definitions are OK, raise TypeError otherwise.
"""
"""Ensure that every element of 'definitions' is valid."""
for defn in definitions:
if not (
isinstance(defn, tuple)
and (
len(defn) in (1, 2)
and (isinstance(defn[1], str) or defn[1] is None)
)
and isinstance(defn[0], str)
):
raise TypeError(
f"invalid macro definition '{defn}': "
"must be tuple (string,), (string, string), or (string, None)"
)
self._check_macro_definition(defn)

def _check_macro_definition(self, defn):
"""
Raise a TypeError if defn is not valid.
A valid definition is either a (name, value) 2-tuple or a (name,) tuple.
"""
valid = (
isinstance(defn, tuple)
and (len(defn) in (1, 2) and (isinstance(defn[1], str) or defn[1] is None))
and isinstance(defn[0], str)
)
if not valid:
raise TypeError(
f"invalid macro definition '{defn}': "
"must be tuple (string,), (string, string), or (string, None)"
)

# -- Bookkeeping methods -------------------------------------------

Expand Down

0 comments on commit 92c9626

Please sign in to comment.