Skip to content

Commit 62135e6

Browse files
committed
Extract method for _is_valid_macro.
1 parent 2c86616 commit 62135e6

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

distutils/ccompiler.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import re
88
import sys
9+
import types
910
import warnings
1011

1112
from ._itertools import always_iterable
@@ -190,25 +191,27 @@ def _find_macro(self, name):
190191
def _check_macro_definitions(self, definitions):
191192
"""Ensure that every element of 'definitions' is valid."""
192193
for defn in definitions:
193-
self._check_macro_definition(defn)
194+
self._check_macro_definition(*defn)
194195

195196
def _check_macro_definition(self, defn):
196197
"""
197198
Raise a TypeError if defn is not valid.
198199
199200
A valid definition is either a (name, value) 2-tuple or a (name,) tuple.
200201
"""
201-
valid = (
202-
isinstance(defn, tuple)
203-
and (len(defn) in (1, 2) and (isinstance(defn[1], str) or defn[1] is None))
204-
and isinstance(defn[0], str)
205-
)
206-
if not valid:
202+
if not isinstance(defn, tuple) or not self._is_valid_macro(*defn):
207203
raise TypeError(
208204
f"invalid macro definition '{defn}': "
209205
"must be tuple (string,), (string, string), or (string, None)"
210206
)
211207

208+
@staticmethod
209+
def _is_valid_macro(name, value=None):
210+
"""
211+
A valid macro is a ``name : str`` and a ``value : str | None``.
212+
"""
213+
return isinstance(name, str) and isinstance(value, (str, types.NoneType))
214+
212215
# -- Bookkeeping methods -------------------------------------------
213216

214217
def define_macro(self, name, value=None):

0 commit comments

Comments
 (0)