Lines Matching refs:self
47 def testfunction(self):
49 return self
58 def test_instancemethod(self):
60 self.assertEqual(id(inst), inst.id())
61 self.assertTrue(inst.testfunction() is inst)
62 self.assertEqual(inst.testfunction.__doc__, testfunction.__doc__)
63 self.assertEqual(InstanceMethod.testfunction.__doc__, testfunction.__doc__)
66 self.assertEqual(testfunction.attribute, "test")
67 self.assertRaises(AttributeError, setattr, inst.testfunction, "attribute", "test")
70 def test_no_FatalError_infinite_loop(self):
78 self.assertEqual(out, b'')
80 self.assertTrue(err.rstrip().startswith(
88 def test_memoryview_from_NULL_pointer(self):
89 self.assertRaises(ValueError, _testcapi.make_memoryview_from_NULL_pointer)
91 def test_exception(self):
103 self.assertEqual(orig_exception, e)
105 self.assertEqual(orig_exception, raised_exception)
106 self.assertEqual(orig_sys_exception, orig_exception)
107 self.assertEqual(reset_sys_exception, orig_exception)
108 self.assertEqual(new_exception, new_exc)
109 self.assertEqual(new_sys_exception, new_exception)
111 self.fail("Exception not raised")
113 def test_exc_info(self):
126 self.assertEqual(orig_exc_info[1], e)
128 self.assertSequenceEqual(orig_exc_info, (raised_exception.__class__, raised_exception, tb))
129 self.assertSequenceEqual(orig_sys_exc_info, orig_exc_info)
130 self.assertSequenceEqual(reset_sys_exc_info, orig_exc_info)
131 self.assertSequenceEqual(new_exc_info, (new_exc.__class__, new_exc, None))
132 self.assertSequenceEqual(new_sys_exc_info, new_exc_info)
134 self.assertTrue(False)
136 def test_set_object(self):
138 with self.assertRaises(ValueError) as e:
140 self.assertEqual(e.exception.args, (42,))
143 with self.assertRaises(ValueError) as e:
146 self.assertIsInstance(wrapped, TypeError)
147 self.assertEqual(wrapped.args, (1, 2, 3))
150 with self.assertRaises(PermissionError) as e:
152 self.assertEqual(e.exception.args, (24,))
161 with self.assertRaises(ZeroDivisionError) as e:
165 def test_seq_bytes_to_charp_array(self):
168 def __len__(self):
170 with self.assertRaisesRegex(TypeError, 'indexing'):
175 def __len__(self):
177 def __getitem__(self, i):
179 self.assertRaises(MemoryError, _posixsubprocess.fork_exec,
183 def test_subprocess_fork_exec(self):
185 def __len__(self):
189 self.assertRaises(TypeError, _posixsubprocess.fork_exec,
194 def test_docstring_signature_parsing(self):
196 self.assertEqual(_testcapi.no_docstring.__doc__, None)
197 self.assertEqual(_testcapi.no_docstring.__text_signature__, None)
199 self.assertEqual(_testcapi.docstring_empty.__doc__, None)
200 self.assertEqual(_testcapi.docstring_empty.__text_signature__, None)
202 self.assertEqual(_testcapi.docstring_no_signature.__doc__,
204 self.assertEqual(_testcapi.docstring_no_signature.__text_signature__, None)
206 self.assertEqual(_testcapi.docstring_with_invalid_signature.__doc__,
211 self.assertEqual(_testcapi.docstring_with_invalid_signature.__text_signature__, None)
213 self.assertEqual(_testcapi.docstring_with_invalid_signature2.__doc__,
220 self.assertEqual(_testcapi.docstring_with_invalid_signature2.__text_signature__, None)
222 self.assertEqual(_testcapi.docstring_with_signature.__doc__,
224 self.assertEqual(_testcapi.docstring_with_signature.__text_signature__, "($module, /, sig)")
226 self.assertEqual(_testcapi.docstring_with_signature_but_no_doc.__doc__, None)
227 self.assertEqual(_testcapi.docstring_with_signature_but_no_doc.__text_signature__,
230 self.assertEqual(_testcapi.docstring_with_signature_and_extra_newlines.__doc__,
232 self.assertEqual(_testcapi.docstring_with_signature_and_extra_newlines.__text_signature__,
235 def test_c_type_with_matrix_multiplication(self):
239 self.assertEqual(m1 @ m2, ("matmul", m1, m2))
240 self.assertEqual(m1 @ 42, ("matmul", m1, 42))
241 self.assertEqual(42 @ m1, ("matmul", 42, m1))
244 self.assertEqual(o, ("imatmul", m1, m2))
247 self.assertEqual(o, ("imatmul", m1, 42))
250 self.assertEqual(o, ("matmul", 42, m1))
252 def test_c_type_with_ipow(self):
256 self.assertEqual(o.__ipow__(1), (1, None))
257 self.assertEqual(o.__ipow__(2, 2), (2, 2))
259 def test_return_null_without_error(self):
272 self.assertRegex(err,
282 with self.assertRaises(SystemError) as cm:
284 self.assertRegex(str(cm.exception),
288 def test_return_result_with_error(self):
300 self.assertRegex(err,
316 with self.assertRaises(SystemError) as cm:
318 self.assertRegex(str(cm.exception),
322 def test_getitem_with_error(self):
336 self.assertRegex(err,
350 self.assertIn('returned a result with an exception set', err)
352 def test_buildvalue_N(self):
355 def test_set_nomemory(self):
385 self.assertIn(b'MemoryError', out)
388 self.assertLessEqual(count, i*5)
389 self.assertGreaterEqual(count, i*5-2)
391 def test_mapping_keys_values_items(self):
393 def keys(self):
395 def values(self):
397 def items(self):
400 def keys(self):
402 def values(self):
404 def items(self):
411 self.assertListEqual(_testcapi.get_mapping_keys(mapping),
413 self.assertListEqual(_testcapi.get_mapping_values(mapping),
415 self.assertListEqual(_testcapi.get_mapping_items(mapping),
418 def test_mapping_keys_values_items_bad_arg(self):
419 self.assertRaises(AttributeError, _testcapi.get_mapping_keys, None)
420 self.assertRaises(AttributeError, _testcapi.get_mapping_values, None)
421 self.assertRaises(AttributeError, _testcapi.get_mapping_items, None)
424 def keys(self):
426 def values(self):
428 def items(self):
431 self.assertRaises(TypeError, _testcapi.get_mapping_keys, bad_mapping)
432 self.assertRaises(TypeError, _testcapi.get_mapping_values, bad_mapping)
433 self.assertRaises(TypeError, _testcapi.get_mapping_items, bad_mapping)
435 def test_mapping_has_key(self):
437 self.assertTrue(_testcapi.mapping_has_key(dct, 'a'))
438 self.assertFalse(_testcapi.mapping_has_key(dct, 'b'))
444 self.assertTrue(_testcapi.mapping_has_key(dct2, 'a'))
445 self.assertFalse(_testcapi.mapping_has_key(dct2, 'b'))
447 def test_sequence_set_slice(self):
454 self.assertEqual(data, data_copy)
455 self.assertEqual(data, [1, 8, 9, 4, 5])
459 def __setitem__(self, index, value):
460 self.index = index
461 self.value = value
465 self.assertEqual(c.index, slice(0, 5))
466 self.assertEqual(c.value, 'abc')
470 with self.assertRaises(TypeError):
472 self.assertEqual(bad_seq1, (1, 2, 3, 4))
475 with self.assertRaises(TypeError):
477 self.assertEqual(bad_seq2, 'abcd')
480 with self.assertRaises(TypeError):
484 with self.assertRaises(TypeError):
486 self.assertEqual(mapping, {1: 'a', 2: 'b', 3: 'c'})
488 def test_sequence_del_slice(self):
495 self.assertEqual(data, data_copy)
496 self.assertEqual(data, [1, 4, 5])
500 def __delitem__(self, index):
501 self.index = index
505 self.assertEqual(c.index, slice(0, 5))
509 with self.assertRaises(TypeError):
511 self.assertEqual(bad_seq1, (1, 2, 3, 4))
514 with self.assertRaises(TypeError):
516 self.assertEqual(bad_seq2, 'abcd')
519 with self.assertRaises(TypeError):
523 with self.assertRaises(TypeError):
525 self.assertEqual(mapping, {1: 'a', 2: 'b', 3: 'c'})
529 def test_negative_refcount(self):
540 self.assertRegex(err,
545 def test_trashcan_subclass(self):
554 def test_trashcan_python_class1(self):
555 self.do_test_trashcan_python_class(list)
558 def test_trashcan_python_class2(self):
560 self.do_test_trashcan_python_class(MyList)
562 def do_test_trashcan_python_class(self, base):
570 def __init__(self, *args):
573 def __del__(self):
586 self.assertGreater(PyList.num, 0)
588 self.assertEqual(PyList.num, 0)
590 def test_heap_ctype_doc_and_text_signature(self):
591 self.assertEqual(_testcapi.HeapDocCType.__doc__, "somedoc")
592 self.assertEqual(_testcapi.HeapDocCType.__text_signature__, "(arg1, arg2)")
594 def test_null_type_doc(self):
595 self.assertEqual(_testcapi.NullTpDocType.__doc__, None)
597 def test_subclass_of_heap_gc_ctype_with_tpdealloc_decrefs_once(self):
599 def __init__(self):
600 self.value2 = 20
607 self.assertEqual(subclass_instance.value, 10)
608 self.assertEqual(subclass_instance.value2, 20)
612 self.assertEqual(type_refcnt - 1, sys.getrefcount(HeapGcCTypeSubclass))
614 def test_subclass_of_heap_gc_ctype_with_del_modifying_dunder_class_only_decrefs_once(self):
616 def __init__(self):
617 self.value2 = 20
621 def __init__(self):
624 def __del__(self):
625 self.__class__ = A
634 self.assertEqual(subclass_instance.value, 10)
635 self.assertEqual(subclass_instance.value2, 20)
643 self.assertEqual(type_refcnt, B.refcnt_in_del)
645 self.assertEqual(type_refcnt - 1, B.refcnt_in_del)
646 self.assertEqual(new_type_refcnt + 1, A.refcnt_in_del)
649 self.assertEqual(type_refcnt - 1, sys.getrefcount(B))
652 self.assertEqual(new_type_refcnt, sys.getrefcount(A))
654 def test_heaptype_with_dict(self):
657 self.assertEqual(inst.foo, 42)
658 self.assertEqual(inst.dictobj, inst.__dict__)
659 self.assertEqual(inst.dictobj, {"foo": 42})
662 self.assertEqual({}, inst.__dict__)
664 def test_heaptype_with_negative_dict(self):
667 self.assertEqual(inst.foo, 42)
668 self.assertEqual(inst.dictobj, inst.__dict__)
669 self.assertEqual(inst.dictobj, {"foo": 42})
672 self.assertEqual({}, inst.__dict__)
674 def test_heaptype_with_weakref(self):
677 self.assertEqual(ref(), inst)
678 self.assertEqual(inst.weakreflist, ref)
680 def test_heaptype_with_buffer(self):
683 self.assertEqual(b, b"1234")
685 def test_c_subclass_of_heap_ctype_with_tpdealloc_decrefs_once(self):
690 self.assertEqual(subclass_instance.value, 10)
691 self.assertEqual(subclass_instance.value2, 20)
695 self.assertEqual(type_refcnt - 1, sys.getrefcount(_testcapi.HeapCTypeSubclass))
697 def test_c_subclass_of_heap_ctype_with_del_modifying_dunder_class_only_decrefs_once(self):
703 self.assertEqual(subclass_instance.value, 10)
704 self.assertEqual(subclass_instance.value2, 20)
713 self.assertEqual(type_refcnt, _testcapi.HeapCTypeSubclassWithFinalizer.refcnt_in_del)
715 self.assertEqual(type_refcnt - 1, _testcapi.HeapCTypeSubclassWithFinalizer.refcnt_in_del)
716 self.assertEqual(new_type_refcnt + 1, _testcapi.HeapCTypeSubclass.refcnt_in_del)
719 self.assertEqual(type_refcnt - 1, sys.getrefcount(_testcapi.HeapCTypeSubclassWithFinalizer))
722 self.assertEqual(new_type_refcnt, sys.getrefcount(_testcapi.HeapCTypeSubclass))
724 def test_heaptype_with_setattro(self):
726 self.assertEqual(obj.pvalue, 10)
728 self.assertEqual(obj.pvalue, 12)
730 self.assertEqual(obj.pvalue, 0)
732 def test_multiple_inheritance_ctypes_with_weakref_or_dict(self):
751 def test_pynumber_tobase(self):
753 self.assertEqual(pynumber_tobase(123, 2), '0b1111011')
754 self.assertEqual(pynumber_tobase(123, 8), '0o173')
755 self.assertEqual(pynumber_tobase(123, 10), '123')
756 self.assertEqual(pynumber_tobase(123, 16), '0x7b')
757 self.assertEqual(pynumber_tobase(-123, 2), '-0b1111011')
758 self.assertEqual(pynumber_tobase(-123, 8), '-0o173')
759 self.assertEqual(pynumber_tobase(-123, 10), '-123')
760 self.assertEqual(pynumber_tobase(-123, 16), '-0x7b')
761 self.assertRaises(TypeError, pynumber_tobase, 123.0, 10)
762 self.assertRaises(TypeError, pynumber_tobase, '123', 10)
763 self.assertRaises(SystemError, pynumber_tobase, 123, 0)
765 def check_fatal_error(self, code, expected, not_expected=()):
770 self.assertIn('Fatal Python error: test_fatal_error: MESSAGE\n',
776 self.fail(f"Cannot find 'Extension modules:' in {err!r}")
781 self.assertIn(name, modules)
783 self.assertNotIn(name, modules)
784 self.assertEqual(len(modules), total)
787 def test_fatal_error(self):
793 self.check_fatal_error(code, expected, not_expected)
803 self.check_fatal_error(code, expected)
805 def test_pyobject_repr_from_null(self):
807 self.assertEqual(s, '<NULL>')
809 def test_pyobject_str_from_null(self):
811 self.assertEqual(s, '<NULL>')
813 def test_pyobject_bytes_from_null(self):
815 self.assertEqual(s, b'<NULL>')
817 def test_Py_CompileString(self):
823 self.assertEqual(result.co_consts, expected.co_consts)
825 def test_export_symbols(self):
845 with self.subTest(name=name):
846 self.assertTrue(hasattr(ctypes.pythonapi, name))
848 def test_eval_get_func_name(self):
852 def method_example(self): ...
854 self.assertEqual(_testcapi.eval_get_func_name(function_example),
856 self.assertEqual(_testcapi.eval_get_func_name(A.method_example),
858 self.assertEqual(_testcapi.eval_get_func_name(A().method_example),
860 self.assertEqual(_testcapi.eval_get_func_name(sum), "sum") # c function
861 self.assertEqual(_testcapi.eval_get_func_name(A), "type")
863 def test_eval_get_func_desc(self):
867 def method_example(self): ...
869 self.assertEqual(_testcapi.eval_get_func_desc(function_example),
871 self.assertEqual(_testcapi.eval_get_func_desc(A.method_example),
873 self.assertEqual(_testcapi.eval_get_func_desc(A().method_example),
875 self.assertEqual(_testcapi.eval_get_func_desc(sum), "()") # c function
876 self.assertEqual(_testcapi.eval_get_func_desc(A), " object")
878 def test_function_get_code(self):
885 self.assertIsInstance(code, types.CodeType)
886 self.assertEqual(code, some.__code__)
888 with self.assertRaises(SystemError):
891 def test_function_get_globals(self):
896 self.assertIsInstance(globals_, dict)
897 self.assertEqual(globals_, some.__globals__)
899 with self.assertRaises(SystemError):
902 def test_function_get_module(self):
907 self.assertIsInstance(module, str)
908 self.assertEqual(module, some.__module__)
910 with self.assertRaises(SystemError):
916 def pendingcalls_submit(self, l, n):
931 def pendingcalls_wait(self, l, n, context = None):
945 self.assertTrue(count < 10000,
951 def test_pendingcalls_threaded(self):
965 threads = [threading.Thread(target=self.pendingcalls_thread,
969 self.pendingcalls_wait(context.l, n, context)
971 def pendingcalls_thread(self, context):
973 self.pendingcalls_submit(context.l, context.n)
983 def test_pendingcalls_non_threaded(self):
990 self.pendingcalls_submit(l, n)
991 self.pendingcalls_wait(l, n)
997 def test_subinterps(self):
1008 self.assertEqual(ret, 0)
1009 self.assertNotEqual(pickle.load(f), id(sys.modules))
1010 self.assertNotEqual(pickle.load(f), id(builtins))
1013 def test_subinterps_recent_language_features(self):
1031 self.assertEqual(ret, 0)
1032 self.assertEqual(pickle.load(f), {'a': '123x', 'b': '123'})
1034 def test_mutate_exception(self):
1045 self.assertFalse(hasattr(binascii.Error, "foobar"))
1048 def test_module_state_shared_in_global(self):
1054 self.addCleanup(os.close, r)
1055 self.addCleanup(os.close, w)
1075 self.assertEqual(ret, 0)
1077 self.assertEqual(main_attr_id, subinterp_attr_id)
1084 def test_thread_state(self):
1096 self.assertEqual(idents.count(threading.get_ident()), 3,
1106 def test_gilstate_ensure_no_deadlock(self):
1117 self.assertIn(b'callback called', ret.out)
1127 def test_widechar(self):
1130 def test_version_api_data(self):
1131 self.assertEqual(_testcapi.Py_Version, sys.hexversion)
1146 def check(self, code):
1150 PYTHONMALLOC=self.PYTHONMALLOC,
1158 def test_buffer_overflow(self):
1159 out = self.check('import _testcapi; _testcapi.pymem_buffer_overflow()')
1174 regex = regex.format(ptr=self.PTR_REGEX)
1176 self.assertRegex(out, regex)
1178 def test_api_misuse(self):
1179 out = self.check('import _testcapi; _testcapi.pymem_api_misuse()')
1190 regex = regex.format(ptr=self.PTR_REGEX)
1191 self.assertRegex(out, regex)
1193 def check_malloc_without_gil(self, code):
1194 out = self.check(code)
1197 self.assertIn(expected, out)
1199 def test_pymem_malloc_without_gil(self):
1203 self.check_malloc_without_gil(code)
1205 def test_pyobject_malloc_without_gil(self):
1209 self.check_malloc_without_gil(code)
1211 def check_pyobject_is_freed(self, func_name):
1226 PYTHONMALLOC=self.PYTHONMALLOC,
1230 def test_pyobject_null_is_freed(self):
1231 self.check_pyobject_is_freed('check_pyobject_null_is_freed')
1233 def test_pyobject_uninitialized_is_freed(self):
1234 self.check_pyobject_is_freed('check_pyobject_uninitialized_is_freed')
1236 def test_pyobject_forbidden_bytes_is_freed(self):
1237 self.check_pyobject_is_freed('check_pyobject_forbidden_bytes_is_freed')
1239 def test_pyobject_freed_is_freed(self):
1240 self.check_pyobject_is_freed('check_pyobject_freed_is_freed')
1266 def setUp(self):
1273 self.module = module
1275 def test_subclass_get_module(self):
1277 class StateAccessType_Subclass(self.module.StateAccessType):
1281 self.assertIs(instance.get_defining_module(), self.module)
1283 def test_subclass_get_module_with_super(self):
1284 class StateAccessType_Subclass(self.module.StateAccessType):
1285 def get_defining_module(self):
1289 self.assertIs(instance.get_defining_module(), self.module)
1291 def test_state_access(self):
1298 a = self.module.StateAccessType()
1299 b = self.module.StateAccessType()
1307 with self.subTest(name):
1308 self.assertEqual(a.get_count(), b.get_count())
1309 self.assertEqual(a.get_count(), 0)
1312 self.assertEqual(a.get_count(), b.get_count())
1313 self.assertEqual(a.get_count(), 1)
1316 self.assertEqual(a.get_count(), b.get_count())
1317 self.assertEqual(a.get_count(), 4)
1320 self.assertEqual(a.get_count(), b.get_count())
1321 self.assertEqual(a.get_count(), 0)
1323 with self.assertRaises(TypeError):
1326 with self.assertRaises(TypeError):
1329 def test_get_module_bad_def(self):
1333 instance = self.module.StateAccessType()
1334 with self.assertRaises(TypeError):
1337 def test_get_module_static_in_mro(self):
1341 class Subclass(BaseException, self.module.StateAccessType):
1343 self.assertIs(Subclass().get_defining_module(), self.module)
1348 def getframe(self):
1351 def getgenframe(self):
1354 def test_frame_getters(self):
1355 frame = self.getframe()
1356 self.assertEqual(frame.f_locals, _testcapi.frame_getlocals(frame))
1357 self.assertIs(frame.f_globals, _testcapi.frame_getglobals(frame))
1358 self.assertIs(frame.f_builtins, _testcapi.frame_getbuiltins(frame))
1359 self.assertEqual(frame.f_lasti, _testcapi.frame_getlasti(frame))
1361 def test_frame_get_generator(self):
1362 gen = self.getgenframe()
1364 self.assertIs(gen, _testcapi.frame_getgenerator(frame))
1366 def test_frame_fback_api(self):
1374 self.assertIsNone(frame.f_back)
1381 def do_test(self, func, names):
1393 self.assertEqual(len(expected_calls), len(actual_calls))
1395 self.assertEqual(expected, actual)
1397 def test_inlined_binary_subscr(self):
1399 def __getitem__(self, other):
1404 self.do_test(func, names)
1406 def test_inlined_call(self):
1413 self.do_test(func, names)