Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,13 @@ def test_teedataobject_requires_three_arguments(self):
def test_teedataobject_list_values(self):
t = itertools._tee_dataobject(iter([]), [1, 2], None)
self.assertIs(type(t), itertools._tee_dataobject)


class CombinationsArgumentOrderTests(unittest.TestCase):
def test_combinations_reads_iterable_before_checking_r(self):
self.assertRaises(TypeError, combinations, 1, -1)
self.assertRaises(ValueError, combinations, [1], -1)

def test_combinations_with_replacement_reads_iterable_before_checking_r(self):
self.assertRaises(TypeError, combinations_with_replacement, 1, -1)
self.assertRaises(ValueError, combinations_with_replacement, [1], -1)
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,13 @@ static Object construct(VirtualFrame frame, Object cls, Object iterable, int r,
if (!wrongTypeProfile.profile(inliningTarget, isTypeNode.execute(inliningTarget, cls))) {
throw raiseNode.raise(inliningTarget, TypeError, ErrorMessages.IS_NOT_TYPE_OBJ, "'cls'", cls);
}
Object[] pool = toArrayNode.execute(frame, iterable);
if (negativeProfile.profile(inliningTarget, r < 0)) {
throw raiseNode.raise(inliningTarget, ValueError, MUST_BE_NON_NEGATIVE, "r");
}

PCombinations self = PFactory.createCombinations(cls, getInstanceShape.execute(cls));
self.setPool(toArrayNode.execute(frame, iterable));
self.setPool(pool);

int[] indices = new int[r];
indicesLoopProfile.profileCounted(r);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2025, 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
Expand Down Expand Up @@ -106,11 +106,12 @@ static Object construct(VirtualFrame frame, Object cls, Object iterable, int r,
if (!wrongTypeProfile.profile(inliningTarget, isTypeNode.execute(inliningTarget, cls))) {
throw raiseNode.raise(inliningTarget, TypeError, ErrorMessages.IS_NOT_TYPE_OBJ, "'cls'", cls);
}
Object[] pool = toArrayNode.execute(frame, iterable);
if (negativeProfile.profile(inliningTarget, r < 0)) {
throw raiseNode.raise(inliningTarget, ValueError, MUST_BE_NON_NEGATIVE, "r");
}
PCombinationsWithReplacement self = PFactory.createCombinationsWithReplacement(cls, getInstanceShape.execute(cls));
self.setPool(toArrayNode.execute(frame, iterable));
self.setPool(pool);
self.setR(r);

self.setIndices(new int[r]);
Expand Down