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,27 @@ 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 CountIntSubclassStartTests(unittest.TestCase):
def test_bool_start_with_default_step(self):
c = count(True)
self.assertEqual(repr(c), 'count(1)')
self.assertIs(type(next(c)), int)

def test_bool_start_with_other_step(self):
c = count(True, 2)
self.assertEqual(repr(c), 'count(True, 2)')
self.assertIs(next(c), True)

def test_int_subclass_step_one_is_not_added(self):
class RaisingAdd(int):
def __add__(self, other):
raise RuntimeError

def __radd__(self, other):
raise RuntimeError

c = count(RaisingAdd(0), RaisingAdd(1))
self.assertEqual(next(c), 0)
self.assertEqual(next(c), 1)
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import com.oracle.graal.python.builtins.objects.type.TpSlots;
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotIterNext.TpIterNextBuiltin;
import com.oracle.graal.python.lib.PyLongAsLongAndOverflowNode;
import com.oracle.graal.python.lib.PyNumberAddNode;
import com.oracle.graal.python.lib.PyNumberCheckNode;
import com.oracle.graal.python.lib.PyObjectGetAttr;
Expand All @@ -78,6 +79,7 @@
import com.oracle.graal.python.nodes.util.CastToJavaLongExactNode;
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
import com.oracle.graal.python.runtime.object.PFactory;
import com.oracle.graal.python.util.OverflowException;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
Expand Down Expand Up @@ -111,9 +113,11 @@ protected ArgumentClinicProvider getArgumentClinic() {
}

@Specialization
static Object construct(Object cls, Object start, Object step,
static Object construct(VirtualFrame frame, Object cls, Object start, Object step,
@Bind Node inliningTarget,
@Cached PyNumberCheckNode checkNode,
@Cached PyObjectTypeCheck typeCheckNode,
@Cached PyLongAsLongAndOverflowNode asLongNode,
@Cached TypeNodes.IsTypeNode isTypeNode,
@Cached TypeNodes.GetInstanceShape getInstanceShape,
@Cached PRaiseNode raiseNode) {
Expand All @@ -126,9 +130,22 @@ static Object construct(Object cls, Object start, Object step,
if (!checkNode.execute(inliningTarget, step)) {
throw raiseNode.raise(inliningTarget, TypeError, NUMBER_IS_REQUIRED);
}
Object cnt = start;
Object countStep = step;
if (typeCheckNode.execute(inliningTarget, start, PythonBuiltinClassType.PInt) && typeCheckNode.execute(inliningTarget, step, PythonBuiltinClassType.PInt)) {
try {
if (asLongNode.execute(frame, inliningTarget, step) == 1) {
// counting from an int by 1 yields exact ints, also for an int subclass
cnt = asLongNode.execute(frame, inliningTarget, start);
countStep = 1;
}
} catch (OverflowException e) {
// does not fit: keep the object
}
}
PCount self = PFactory.createCount(cls, getInstanceShape.execute(cls));
self.setCnt(start);
self.setStep(step);
self.setCnt(cnt);
self.setStep(countStep);
return self;
}
}
Expand Down