|
65 | 65 | import sys
|
66 | 66 | import re
|
67 | 67 | import traceback
|
68 |
| -def slots_tester(Klass): |
| 68 | +def slots_tester(Klass, other_klasses): |
69 | 69 | def test(fun, name):
|
70 | 70 | try:
|
71 | 71 | print(f'{name}:', end='')
|
@@ -148,6 +148,11 @@ def del_descr(self): del self.descr
|
148 | 148 | test_dunder(obj, '__getitem__', -1)
|
149 | 149 | test_dunder(obj, '__getitem__', 'hello')
|
150 | 150 |
|
| 151 | + other_objs = [K() for K in other_klasses] + [42, 3.14, 'string', (1,2,3)] |
| 152 | + for obj2 in other_objs: |
| 153 | + obj1 = Klass() |
| 154 | + test(lambda: obj1 + obj2, f"{Klass} + {type(obj2)}") |
| 155 | + test(lambda: obj2 + obj1, f"{type(obj2)} + {Klass}") |
151 | 156 |
|
152 | 157 | '''
|
153 | 158 |
|
@@ -222,8 +227,10 @@ def tp_decl(self, name_prefix):
|
222 | 227 |
|
223 | 228 | SLOTS = [
|
224 | 229 | Slot('tp_as_number', 'nb_bool', 'int $name$(PyObject* self)', ['1', '0', None]),
|
| 230 | + Slot('tp_as_number', 'nb_add', 'PyObject* $name$(PyObject* self, PyObject *other)', ['Py_NewRef(self)', 'PyLong_FromLong(0)', None]), |
225 | 231 | Slot('tp_as_sequence', 'sq_length', 'Py_ssize_t $name$(PyObject* self)', ['0', '1', '42', None]),
|
226 |
| - Slot('tp_as_sequence', 'sq_item', 'PyObject* $name$(PyObject* self, Py_ssize_t index)', ['0', 'PyLong_FromSsize_t(index + 1)', None]), |
| 232 | + Slot('tp_as_sequence', 'sq_item', 'PyObject* $name$(PyObject* self, Py_ssize_t index)', ['Py_NewRef(self)', 'PyLong_FromSsize_t(index + 1)', None]), |
| 233 | + Slot('tp_as_sequence', 'sq_concat', 'PyObject* $name$(PyObject* self, PyObject *other)', ['Py_NewRef(self)', 'PyLong_FromLong(10)', None]), |
227 | 234 | Slot('tp_as_mapping', 'mp_length', 'Py_ssize_t $name$(PyObject* self)', ['0', '1', '42', None]),
|
228 | 235 | Slot('tp_as_mapping', 'mp_subscript', 'PyObject* $name$(PyObject* self, PyObject* key)', ['Py_RETURN_FALSE', 'Py_NewRef(key)', None]),
|
229 | 236 | Slot(NO_GROUP, 'tp_getattr', 'PyObject* $name$(PyObject* self, char *name)', ['Py_RETURN_NONE', 'Py_RETURN_FALSE', 'Py_NewRef(self)', None,
|
@@ -275,6 +282,8 @@ def tp_decl(self, name_prefix):
|
275 | 282 |
|
276 | 283 | MAGIC = {
|
277 | 284 | '__bool__(self)': ['True', 'False', None],
|
| 285 | + '__add__(self)': ['self', '"__add__result"', "NotImplemented", None], |
| 286 | + '__radd__(self)': ['self', '"__add__result"', "NotImplemented", None], |
278 | 287 | '__len__(self)': ['0', '1', '42', None],
|
279 | 288 | '__getattribute__(self,name)': ['name', '42', 'global_dict1[name]', None],
|
280 | 289 | '__getattr__(self,name)': ['name+"abc"', 'False', 'global_dict1[name]', None],
|
@@ -517,9 +526,10 @@ def choose_random(l):
|
517 | 526 |
|
518 | 527 | py_source += '\n\n# ===========\n'
|
519 | 528 | py_source += '# Tests:\n\n'
|
| 529 | + py_source += 'all_classes = ' + ','.join(classes) + '\n\n' |
520 | 530 | for klass in classes:
|
521 | 531 | py_source += f'print("\\n\\n\\nTesting {klass}\\n")\n'
|
522 |
| - py_source += f'slots_tester({klass})\n' |
| 532 | + py_source += f'slots_tester({klass}, all_classes)\n' |
523 | 533 |
|
524 | 534 | py_filename = f"test{test_case_idx}.py"
|
525 | 535 | write_all(os.path.join(output_dir, py_filename), py_source)
|
|
0 commit comments