Skip to content

Commit ec5c674

Browse files
committed
Add tests for weird method patterns
1 parent 873829d commit ec5c674

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

Lib/test/test_opcache.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,58 @@ def instantiate():
550550
with self.assertRaises(TypeError):
551551
instantiate()
552552

553+
def test_call_specialized_non_method_on_class(self):
554+
class C:
555+
pass
556+
o = C()
557+
for callable in (isinstance, len, str, tuple, type):
558+
setattr(C, callable.__name__, callable)
559+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
560+
self.assertIs(o.isinstance("Spam", str), True)
561+
self.assertEqual(o.len("Spam"), 4)
562+
self.assertEqual(o.str("Spam"), "Spam")
563+
self.assertEqual(o.tuple("Spam"), ("S", "p", "a", "m"))
564+
self.assertIs(o.type("Spam"), str)
565+
566+
def test_call_specialized_non_method_on_instance(self):
567+
class C:
568+
pass
569+
o = C()
570+
for callable in (isinstance, len, str, tuple, type):
571+
setattr(o, callable.__name__, callable)
572+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
573+
self.assertIs(o.isinstance("Spam", str), True)
574+
self.assertEqual(o.len("Spam"), 4)
575+
self.assertEqual(o.str("Spam"), "Spam")
576+
self.assertEqual(o.tuple("Spam"), ("S", "p", "a", "m"))
577+
self.assertIs(o.type("Spam"), str)
578+
579+
def test_call_specialized_method_on_class(self):
580+
class C:
581+
pass
582+
o = C()
583+
for callable in (isinstance, len, str, tuple, type):
584+
setattr(C, callable.__name__, types.MethodType(callable, "Spam"))
585+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
586+
self.assertIs(o.isinstance(str), True)
587+
self.assertEqual(o.len(), 4)
588+
self.assertEqual(o.str(), "Spam")
589+
self.assertEqual(o.tuple(), ("S", "p", "a", "m"))
590+
self.assertIs(o.type(), str)
591+
592+
def test_call_specialized_method_on_instance(self):
593+
class C:
594+
pass
595+
o = C()
596+
for callable in (isinstance, len, str, tuple, type):
597+
setattr(o, callable.__name__, types.MethodType(callable, "Spam"))
598+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
599+
self.assertIs(o.isinstance(str), True)
600+
self.assertEqual(o.len(), 4)
601+
self.assertEqual(o.str(), "Spam")
602+
self.assertEqual(o.tuple(), ("S", "p", "a", "m"))
603+
self.assertIs(o.type(), str)
604+
553605

554606
def make_deferred_ref_count_obj():
555607
"""Create an object that uses deferred reference counting.

0 commit comments

Comments
 (0)