Commit 31333266 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Some notes about defaults-changing

I was worried we were in trouble since we don't always pass an owned
reference to any arguments filled via defaults, but for Python functions
this is fine since we will end up creating our own reference to it.
For builtin functions, we disallow changing defaults.
parent 6940f10b
......@@ -1683,6 +1683,12 @@ static void functionSetDefaults(Box* b, Box* v, void*) {
// and leave this assert.
RELEASE_ASSERT(func->can_change_defaults, "trying to change the defaults on a non-defaults-changable function.");
// Changing defaults of builtins would be very scary. Python functions are safe since they
// store their arguments into their symbol table, but builtin functions are free to let them
// stay borrowed. If a builtin function has changable defaults, and it can call into arbitrary
// user code, then it could call something that changes its own defaults, and all of a sudden
// one of its arguments gets deallocated from under it.
if (v == None)
v = NULL;
else if (v && !PyTuple_Check(v)) {
......
......@@ -70,3 +70,9 @@ print type(C.__new__), type(C.__dict__['__new__'])
C.__new__.__defaults__ = (1,)
print type(C())
def f(a=1, b=2, c=3):
print a, b, c, type(a), type(b), type(c)
f.func_defaults = (a + 3, b + 3, c + 3)
for i in xrange(1000):
f()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment