Skip to content

Commit 4838f4b

Browse files
committed
Improvements in the slots fuzzer
1 parent 7739539 commit 4838f4b

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

scripts/slot_fuzzer.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,6 @@ def compile_ext(name):
169169
)
170170
'''
171171

172-
# language=C
173-
C_SOURCE_HEADER = '''
174-
#include <Python.h>
175-
176-
PyObject *global_stash1;
177-
PyObject *global_stash2;
178-
'''
179-
180172

181173
def write_all(filename, text):
182174
with open(filename, 'w+') as f:
@@ -212,6 +204,14 @@ def tp_decl(self, name_prefix):
212204

213205
NO_GROUP = 'top'
214206

207+
# language=C
208+
C_SOURCE_HEADER = '''
209+
#include <Python.h>
210+
211+
PyObject *global_stash1;
212+
PyObject *global_stash2;
213+
'''
214+
215215
SLOTS = [
216216
Slot('tp_as_number', 'nb_bool', 'int $name$(PyObject* self)', ['1', '0', None]),
217217
Slot('tp_as_sequence', 'sq_length', 'Py_ssize_t $name$(PyObject* self)', ['0', '1', '42', None]),
@@ -235,13 +235,14 @@ def tp_decl(self, name_prefix):
235235
global_stash1 = value;
236236
return 0;
237237
''']),
238-
Slot(NO_GROUP, 'tp_setattr', 'int $name$(PyObject* self, char *name, PyObject *value)', ['0', None,
239-
'''
240-
Py_IncRef(value);
241-
Py_XDECREF(global_stash1);
242-
global_stash1 = value;
243-
return 0;
244-
''']),
238+
# Disabled due to incompatibilities with Carlo Verre hack in some very specific corner cases
239+
# Slot(NO_GROUP, 'tp_setattr', 'int $name$(PyObject* self, char *name, PyObject *value)', ['0', None,
240+
# '''
241+
# Py_IncRef(value);
242+
# Py_XDECREF(global_stash1);
243+
# global_stash1 = value;
244+
# return 0;
245+
# ''']),
245246
Slot(NO_GROUP, 'tp_descr_get', 'PyObject* $name$(PyObject* self, PyObject* key, PyObject* type)', ['Py_RETURN_NONE', 'Py_NewRef(key)', None,
246247
'''
247248
if (global_stash2 == NULL) Py_RETURN_NONE;
@@ -257,26 +258,33 @@ def tp_decl(self, name_prefix):
257258
'''])
258259
]
259260

261+
PY_GLOBALS = '''
262+
global_dict1 = dict()
263+
global_descr_val = None
264+
'''
260265

261266
MAGIC = {
262267
'__bool__(self)': ['True', 'False', None],
263268
'__len__(self)': ['0', '1', '42', None],
264-
'__getattribute__(self,name)': ['name', '42', 'self._dict[name]', None],
265-
'__getattr__(self,name)': ['name+"abc"', 'False', 'self._dict[name]', None],
266-
'__get__(self,obj,objtype=None)': ['obj', 'True', 'self.descr_value', None],
267-
'__set__(self,obj,value)': ['self.descr_value = value\nreturn None', None],
269+
'__getattribute__(self,name)': ['name', '42', 'global_dict1[name]', None],
270+
'__getattr__(self,name)': ['name+"abc"', 'False', 'global_dict1[name]', None],
271+
'__get__(self,obj,objtype=None)': ['obj', 'True', 'global_descr_val', None],
272+
'__set__(self,obj,value)': [None,
273+
'''
274+
global global_descr_val
275+
global_descr_val = value
276+
return None
277+
'''],
268278
'__setattr__(self,name,value)': [None,
269279
'''
270-
if not self._dict:
271-
self._dict = dict()
272-
self._dict[name] = value
280+
global global_dict1 # not using self._dict, because attr lookup can be something funky...
281+
global_dict1[name] = value
273282
return None
274283
'''],
275284
'__delattr__(self,name,value)': [None,
276285
'''
277-
if not self._dict:
278-
self._dict = dict()
279-
del self._dict[name]
286+
global global_dict1
287+
del global_dict1[name]
280288
return None
281289
'''],
282290
}
@@ -446,7 +454,7 @@ def choose_random(l):
446454
classes = []
447455
test_module_name = f"test{test_case_idx}"
448456
c_source = C_SOURCE_HEADER
449-
py_source = SLOTS_TESTER
457+
py_source = SLOTS_TESTER + PY_GLOBALS
450458
native_classes = []
451459
for i in range(classes_count):
452460
base = choose_random(classes) if classes else 'object'

0 commit comments

Comments
 (0)