◐ Shell
clean mode source ↗

Issue 3537: dict creation failure causes crash

If the first item can't be inserted the interpreter will crash 
eventually.

while 1:
	try:
		d = { 'a':a,
		  'b':'b',
		  'c':'c',
		  'd':'d',
		  'e':'e',
		  'f':'f',
		  'g':'g',
		  'h':'h',
		  'i':'i',
		  'j':'j',
		  'k':'k',
		  'l':'l',
		  'm':'m',
		  'n':'n',
		  'o':'o'
		  }
	except:
		pass

As best I can tell, this only happens for the first item.
In a debug build, this assert fails on the second time thru
the loop (dictobject.c, line 247):
		assert (mp->ma_table == mp->ma_smalltable);

Apparently something is leaving one of the entries in the list
of preallocated dict objects in an inconsistent state.
The problem is that PyDict_New doesn't reinitialize the fields of a dict
from the free list when the number of entries is zero. For a
preconstructed dict (like created by BUILD_MAP) of size >=8, however,
there will be an allocated ma_table and ma_mask will be 16-1, not 8-1.

I propose the attached patch.