Skip to content

Commit

Permalink
Extract method for _is_valid_macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jun 27, 2024
1 parent 92c9626 commit 4fd2d56
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions distutils/ccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import re
import sys
import types
import warnings

from ._itertools import always_iterable
Expand Down Expand Up @@ -190,25 +191,27 @@ def _find_macro(self, name):
def _check_macro_definitions(self, definitions):
"""Ensure that every element of 'definitions' is valid."""
for defn in definitions:
self._check_macro_definition(defn)
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:
if not isinstance(defn, tuple) or not self._is_valid_macro(*defn):
raise TypeError(
f"invalid macro definition '{defn}': "
"must be tuple (string,), (string, string), or (string, None)"
)

@staticmethod
def _is_valid_macro(name, value=None):
"""
A valid macro is a ``name : str`` and a ``value : str | None``.
"""
return isinstance(name, str) and isinstance(value, (str, types.NoneType))

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

def define_macro(self, name, value=None):
Expand Down

0 comments on commit 4fd2d56

Please sign in to comment.