Skip to content

Commit b84abc3

Browse files
committed
Add narrowing to constant unit test
1 parent 156b0df commit b84abc3

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
import _opcode
12
import contextlib
3+
import gc
24
import itertools
5+
import os
36
import sys
47
import textwrap
5-
import unittest
6-
import gc
7-
import os
88
import types
9+
import unittest
910

10-
import _opcode
11-
12-
from test.support import (script_helper, requires_specialization,
13-
import_helper, Py_GIL_DISABLED, requires_jit_enabled,
14-
reset_code)
11+
from test.support import (
12+
Py_GIL_DISABLED,
13+
import_helper,
14+
requires_jit_enabled,
15+
requires_specialization,
16+
reset_code,
17+
script_helper,
18+
)
1519

1620
_testinternalcapi = import_helper.import_module("_testinternalcapi")
1721

@@ -3574,6 +3578,25 @@ def return_false():
35743578
# v + 1 should be constant folded
35753579
self.assertNotIn("_BINARY_OP", uops)
35763580

3581+
def test_is_number_narrows_to_constant(self):
3582+
def f(n):
3583+
def return_1():
3584+
return 1
3585+
3586+
hits = 0
3587+
v = return_1()
3588+
for i in range(n):
3589+
if v is 1:
3590+
hits += v + 1
3591+
return hits
3592+
3593+
res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
3594+
self.assertEqual(res, TIER2_THRESHOLD * 2)
3595+
self.assertIsNotNone(ex)
3596+
3597+
# v + 1 should be constant folded
3598+
self.assertLessEqual(count_ops(ex, "_BINARY_OP_ADD_INT"), 1)
3599+
35773600
def test_for_iter_gen_frame(self):
35783601
def f(n):
35793602
for i in range(n):

0 commit comments

Comments
 (0)