From 4fd2d569e37268b8c23f3cc45879465f9c269d92 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 27 Jun 2024 06:14:54 -0400 Subject: [PATCH] Extract method for _is_valid_macro. --- distutils/ccompiler.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/distutils/ccompiler.py b/distutils/ccompiler.py index 9974e520..87ca89e1 100644 --- a/distutils/ccompiler.py +++ b/distutils/ccompiler.py @@ -6,6 +6,7 @@ import os import re import sys +import types import warnings from ._itertools import always_iterable @@ -190,7 +191,7 @@ 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): """ @@ -198,17 +199,19 @@ def _check_macro_definition(self, defn): 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):