|
1 | 1 | "Test the functionality of Python classes implementing operators." |
2 | 2 |
|
3 | 3 | import unittest |
4 | | -import test.support |
| 4 | +from test.support import cpython_only |
| 5 | +from test.support import import_helper, script_helper |
5 | 6 |
|
6 | 7 | testmeths = [ |
7 | 8 |
|
@@ -933,20 +934,36 @@ class C: |
933 | 934 | C.a = X() |
934 | 935 | C.a = X() |
935 | 936 |
|
| 937 | + @cpython_only |
936 | 938 | def test_detach_materialized_dict_no_memory(self): |
937 | | - import _testcapi |
938 | | - class A: |
939 | | - def __init__(self): |
940 | | - self.a = 1 |
941 | | - self.b = 2 |
942 | | - a = A() |
943 | | - d = a.__dict__ |
944 | | - with test.support.catch_unraisable_exception() as ex: |
945 | | - _testcapi.set_nomemory(0, 1) |
946 | | - del a |
947 | | - self.assertEqual(ex.unraisable.exc_type, MemoryError) |
948 | | - with self.assertRaises(KeyError): |
949 | | - d["a"] |
| 939 | + # Skip test if _testcapi is not available: |
| 940 | + import_helper.import_module('_testcapi') |
| 941 | + |
| 942 | + code = """if 1: |
| 943 | + import test.support |
| 944 | + import _testcapi |
| 945 | +
|
| 946 | + class A: |
| 947 | + def __init__(self): |
| 948 | + self.a = 1 |
| 949 | + self.b = 2 |
| 950 | + a = A() |
| 951 | + d = a.__dict__ |
| 952 | + with test.support.catch_unraisable_exception() as ex: |
| 953 | + _testcapi.set_nomemory(0, 1) |
| 954 | + del a |
| 955 | + assert ex.unraisable.exc_type is MemoryError |
| 956 | + try: |
| 957 | + d["a"] |
| 958 | + except KeyError: |
| 959 | + pass |
| 960 | + else: |
| 961 | + assert False, "KeyError not raised" |
| 962 | + """ |
| 963 | + rc, out, err = script_helper.assert_python_ok("-c", code) |
| 964 | + self.assertEqual(rc, 0) |
| 965 | + self.assertFalse(out, msg=out.decode('utf-8')) |
| 966 | + self.assertFalse(err, msg=err.decode('utf-8')) |
950 | 967 |
|
951 | 968 | if __name__ == '__main__': |
952 | 969 | unittest.main() |
0 commit comments