Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Cc: python@g.o, "Michał Górny" <mgorny@g.o>
Subject: [gentoo-dev] [PATCH] python*-r1.eclass: Check for invalid impl patterns
Date: Tue, 17 Mar 2020 12:03:43
Message-Id: 20200317120311.94407-1-mgorny@gentoo.org
Check for invalid implementation patterns passed to python_gen*
functions, python_setup, etc.  Currently the functions silently ignore
pattern mismatches which is fine for patterns matching historical
implementations but also hides errors in patterns.

After this change, each pattern must match at least one current or
historical implementation.  If no match is found, the function dies
indicating developer's mistake.

Signed-off-by: Michał Górny <mgorny@g.o>
---
 eclass/python-r1.eclass        |  6 ++++++
 eclass/python-single-r1.eclass |  4 ++++
 eclass/python-utils-r1.eclass  | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 44 insertions(+)

diff --git a/eclass/python-r1.eclass b/eclass/python-r1.eclass
index 960fed8c451a..5eebd3c79750 100644
--- a/eclass/python-r1.eclass
+++ b/eclass/python-r1.eclass
@@ -293,6 +293,7 @@ _python_gen_usedep() {
 
 	local impl matches=()
 
+	_python_verify_patterns "${@}"
 	for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
 		if _python_impl_matches "${impl}" "${@}"; then
 			matches+=(
@@ -376,6 +377,7 @@ python_gen_useflags() {
 
 	local impl matches=()
 
+	_python_verify_patterns "${@}"
 	for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
 		if _python_impl_matches "${impl}" "${@}"; then
 			matches+=( "python_targets_${impl}" )
@@ -424,6 +426,7 @@ python_gen_cond_dep() {
 	local dep=${1}
 	shift
 
+	_python_verify_patterns "${@}"
 	for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
 		if _python_impl_matches "${impl}" "${@}"; then
 			# substitute ${PYTHON_USEDEP} if used
@@ -482,6 +485,7 @@ python_gen_impl_dep() {
 	local PYTHON_REQ_USE=${1}
 	shift
 
+	_python_verify_patterns "${@}"
 	for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
 		if _python_impl_matches "${impl}" "${@}"; then
 			local PYTHON_PKG_DEP
@@ -560,6 +564,7 @@ python_gen_any_dep() {
 	shift
 
 	local i PYTHON_PKG_DEP out=
+	_python_verify_patterns "${@}"
 	for i in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
 		if _python_impl_matches "${i}" "${@}"; then
 			local PYTHON_USEDEP="python_targets_${i}(-),python_single_target_${i}(+)"
@@ -745,6 +750,7 @@ python_setup() {
 
 	# (reverse iteration -- newest impl first)
 	local found
+	_python_verify_patterns "${@}"
 	for (( i = ${#_PYTHON_SUPPORTED_IMPLS[@]} - 1; i >= 0; i-- )); do
 		local impl=${_PYTHON_SUPPORTED_IMPLS[i]}
 
diff --git a/eclass/python-single-r1.eclass b/eclass/python-single-r1.eclass
index f9e26e7c334f..98ab761f39b8 100644
--- a/eclass/python-single-r1.eclass
+++ b/eclass/python-single-r1.eclass
@@ -280,6 +280,7 @@ _python_gen_usedep() {
 
 	local impl matches=()
 
+	_python_verify_patterns "${@}"
 	for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
 		if _python_impl_matches "${impl}" "${@}"; then
 			matches+=(
@@ -322,6 +323,7 @@ python_gen_useflags() {
 
 	local impl matches=()
 
+	_python_verify_patterns "${@}"
 	for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
 		if _python_impl_matches "${impl}" "${@}"; then
 			matches+=( "python_single_target_${impl}" )
@@ -371,6 +373,7 @@ python_gen_cond_dep() {
 	local dep=${1}
 	shift
 
+	_python_verify_patterns "${@}"
 	for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
 		if _python_impl_matches "${impl}" "${@}"; then
 			# substitute ${PYTHON_SINGLE_USEDEP} if used
@@ -433,6 +436,7 @@ python_gen_impl_dep() {
 	local PYTHON_REQ_USE=${1}
 	shift
 
+	_python_verify_patterns "${@}"
 	for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
 		if _python_impl_matches "${impl}" "${@}"; then
 			local PYTHON_PKG_DEP
diff --git a/eclass/python-utils-r1.eclass b/eclass/python-utils-r1.eclass
index f144cbbb1279..a3257260f7f0 100644
--- a/eclass/python-utils-r1.eclass
+++ b/eclass/python-utils-r1.eclass
@@ -45,6 +45,18 @@ _PYTHON_ALL_IMPLS=(
 )
 readonly _PYTHON_ALL_IMPLS
 
+# @ECLASS-VARIABLE: _PYTHON_HISTORICAL_IMPLS
+# @INTERNAL
+# @DESCRIPTION:
+# All historical Python implementations that are no longer supported.
+_PYTHON_HISTORICAL_IMPLS=(
+	jython2_7
+	pypy pypy1_{8,9} pypy2_0
+	python2_{5,6}
+	python3_{1,2,3,4,5}
+)
+readonly _PYTHON_HISTORICAL_IMPLS
+
 # @ECLASS-VARIABLE: PYTHON_COMPAT_NO_STRICT
 # @INTERNAL
 # @DESCRIPTION:
@@ -89,6 +101,28 @@ _python_impl_supported() {
 	esac
 }
 
+# @FUNCTION: _python_verify_patterns
+# @USAGE: <pattern>...
+# @INTERNAL
+# @DESCRIPTION:
+# Verify whether the patterns passed to the eclass function are correct
+# (i.e. can match any valid implementation).  Dies on wrong pattern.
+_python_verify_patterns() {
+	debug-print-function ${FUNCNAME} "${@}"
+
+	local impl pattern
+	for pattern; do
+		[[ ${pattern} == -[23] ]] && continue
+
+		for impl in "${_PYTHON_ALL_IMPLS[@]}" "${_PYTHON_HISTORICAL_IMPLS[@]}"
+		do
+			[[ ${impl} == ${pattern/./_} ]] && continue 2
+		done
+
+		die "Invalid implementation pattern: ${pattern}"
+	done
+}
+
 # @FUNCTION: _python_set_impls
 # @INTERNAL
 # @DESCRIPTION:
-- 
2.25.1