From 086037a9c1413da1480bc8a548e95b4dffa82f20 Mon Sep 17 00:00:00 2001 From: Jeongseop Lim Date: Fri, 30 Oct 2026 02:45:10 +0900 Subject: [PATCH] Reject None for itertools.count() start and step Both arguments were declared with useDefaultForNone, so an explicit None was replaced by the default and count(None) and count(0, None) returned count(0). CPython requires a number for any argument that is passed and raises TypeError for None. Signed-off-by: Jeongseop Lim --- .../src/tests/test_itertools_count.py | 67 +++++++++++++++++++ .../objects/itertools/CountBuiltins.java | 6 +- 2 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 graalpython/com.oracle.graal.python.test/src/tests/test_itertools_count.py diff --git a/graalpython/com.oracle.graal.python.test/src/tests/test_itertools_count.py b/graalpython/com.oracle.graal.python.test/src/tests/test_itertools_count.py new file mode 100644 index 0000000000..f2761133d4 --- /dev/null +++ b/graalpython/com.oracle.graal.python.test/src/tests/test_itertools_count.py @@ -0,0 +1,67 @@ +# Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# The Universal Permissive License (UPL), Version 1.0 +# +# Subject to the condition set forth below, permission is hereby granted to any +# person obtaining a copy of this software, associated documentation and/or +# data (collectively the "Software"), free of charge and under any and all +# copyright rights in the Software, and any and all patent rights owned or +# freely licensable by each licensor hereunder covering either (i) the +# unmodified Software as contributed to or provided by such licensor, or (ii) +# the Larger Works (as defined below), to deal in both +# +# (a) the Software, and +# +# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if +# one is included with the Software each a "Larger Work" to which the Software +# is contributed by such licensors), +# +# without restriction, including without limitation the rights to copy, create +# derivative works of, display, perform, and distribute the Software and make, +# use, sell, offer for sale, import, export, have made, and have sold the +# Software and the Larger Work(s), and to sublicense the foregoing rights on +# either these or other terms. +# +# This license is subject to the following condition: +# +# The above copyright notice and either this complete permission notice or at a +# minimum a reference to the UPL must be included in all copies or substantial +# portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +import itertools + + +def assert_raises(err, fn, *args, **kwargs): + raised = False + try: + fn(*args, **kwargs) + except err: + raised = True + assert raised + + +def test_count_rejects_none_start(): + assert_raises(TypeError, itertools.count, None) + assert_raises(TypeError, itertools.count, start=None) + + +def test_count_rejects_none_step(): + assert_raises(TypeError, itertools.count, 0, None) + assert_raises(TypeError, itertools.count, step=None) + + +def test_count_defaults(): + c = itertools.count() + assert next(c) == 0 + assert next(c) == 1 + c = itertools.count(5) + assert next(c) == 5 + assert next(c) == 6 diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/itertools/CountBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/itertools/CountBuiltins.java index b3cd281f78..33efaff3f1 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/itertools/CountBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/itertools/CountBuiltins.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 @@ -100,8 +100,8 @@ protected List> getNodeFa @Slot(value = SlotKind.tp_new, isComplex = true) @SlotSignature(name = "count", minNumOfPositionalArgs = 1, parameterNames = {"cls", "start", "step"}) - @ArgumentClinic(name = "start", defaultValue = "0", useDefaultForNone = true) - @ArgumentClinic(name = "step", defaultValue = "1", useDefaultForNone = true) + @ArgumentClinic(name = "start", defaultValue = "0") + @ArgumentClinic(name = "step", defaultValue = "1") @GenerateNodeFactory public abstract static class CountNode extends PythonTernaryClinicBuiltinNode {