Skip to content

Instantly share code, notes, and snippets.

@Arachnid
Created April 2, 2012 09:43
Show Gist options
  • Save Arachnid/2282182 to your computer and use it in GitHub Desktop.
Save Arachnid/2282182 to your computer and use it in GitHub Desktop.
def _make_skel_func(code, num_closures):
""" Creates a skeleton function object that contains just the provided
code and the correct number of cells in func_closure. All other
func attributes (e.g. func_globals) are empty.
"""
#build closure (cells):
cellnew = ctypes.pythonapi.PyCell_New
cellnew.restype = ctypes.py_object
cellnew.argtypes = (ctypes.py_object,)
dummy_closure = tuple(map(lambda i: cellnew(None), range(num_closures)))
return types.FunctionType(code, {'__builtins__': __builtins__},
None, None, dummy_closure)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment