mirror of
https://github.com/Nuitka/Nuitka.git
synced 2026-11-26 13:14:23 +01:00
* Pass "needs_check" from "mayRaiseException" for "enumerate" and "zip" calls, so calls known not to raise no longer jump to an exception exit the function does not have, which failed to compile. * Consider "enumerate" start values and "zip" strict truth checks as potentially raising, and add "mayRaiseExceptionIndex" and "mayRaiseExceptionIter" shape methods for the required checks. * Fix, "next" with a default value crashing for values that are not iterators. Check "HAS_ITERNEXT" and raise the same "TypeError" as CPython instead, and use the optimized iterator next path when the value is known to be an iterator. * Solve TODO about optimizing "next" with special helper for known good iterator values.
110 lines
2.9 KiB
Python
110 lines
2.9 KiB
Python
# Copyright 2026, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
|
|
|
|
|
|
"""Calls of iterator related built-ins, with values known and unknown at compile time."""
|
|
|
|
from __future__ import print_function
|
|
|
|
import sys
|
|
|
|
|
|
class BadBool(object):
|
|
def __bool__(self):
|
|
raise ValueError("no truth value")
|
|
|
|
__nonzero__ = __bool__
|
|
|
|
|
|
def enumerateConstant():
|
|
return enumerate("ab")
|
|
|
|
|
|
def enumerateConstantStart():
|
|
return enumerate("ab", 2)
|
|
|
|
|
|
def enumerateBadStart():
|
|
return enumerate("ab", b"x")
|
|
|
|
|
|
def zipEmpty():
|
|
return zip()
|
|
|
|
|
|
def zipConstants():
|
|
return zip("ab", (1, 2))
|
|
|
|
|
|
def zipStrictBadBool(value):
|
|
return zip("ab", strict=value)
|
|
|
|
|
|
def nextNotIterator(value):
|
|
return next(value, "default")
|
|
|
|
|
|
def nextIterator(value):
|
|
return next(iter(value), "default")
|
|
|
|
|
|
def nextKnownIterator():
|
|
return next(iter("ab"))
|
|
|
|
|
|
def nextKnownEmptyIterator():
|
|
return next(iter(()))
|
|
|
|
|
|
def nextKnownIteratorWithDefault():
|
|
return next(iter("ab"), "default")
|
|
|
|
|
|
def nextKnownEmptyIteratorWithDefault():
|
|
return next(iter(()), "default")
|
|
|
|
|
|
def nextNotIteratorNoDefault(value):
|
|
return next(value)
|
|
|
|
|
|
def check(description, func, *args):
|
|
try:
|
|
result = func(*args)
|
|
if not isinstance(result, str):
|
|
result = list(result)
|
|
except Exception as e: # pylint: disable=broad-except
|
|
result = type(e).__name__
|
|
print(description, result)
|
|
|
|
|
|
check("enumerate constant", enumerateConstant)
|
|
check("enumerate constant start", enumerateConstantStart)
|
|
check("enumerate bad start", enumerateBadStart)
|
|
check("zip empty", zipEmpty)
|
|
check("zip constants", zipConstants)
|
|
if sys.version_info >= (3, 10):
|
|
check("zip strict bad bool", zipStrictBadBool, BadBool())
|
|
check("next of int with default", nextNotIterator, int("5"))
|
|
check("next of list with default", nextNotIterator, list("ab"))
|
|
check("next of empty iterator with default", nextIterator, ())
|
|
check("next of known iterator", nextKnownIterator)
|
|
check("next of empty known iterator", nextKnownEmptyIterator)
|
|
check("next of known iterator with default", nextKnownIteratorWithDefault)
|
|
check("next of empty known iterator with default", nextKnownEmptyIteratorWithDefault)
|
|
check("next of int", nextNotIteratorNoDefault, int("5"))
|
|
|
|
# Python tests originally created or extracted from other peoples work. The
|
|
# parts were too small to be protected.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|