Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue14369] make __closure__ writable

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

Apr 23, 2012, 9:54 PM

Post #1 of 6 (134 views)
Permalink
[issue14369] make __closure__ writable

Nick Coghlan <ncoghlan [at] gmail> added the comment:

Another use case for a writeable __closure__ attribute is to make it possible to manually break reference cycles:
http://blog.ccpgames.com/kristjan/2012/04/23/reference-cycles-with-closures/

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14369>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 24, 2012, 11:19 AM

Post #2 of 6 (124 views)
Permalink
[issue14369] make __closure__ writable [In reply to]

Richard Oudkerk <shibturn [at] gmail> added the comment:

Shouldn't test___closure__() also test what happens when the closure is replaced with None, or a tuple which is too long or too short or contains non-cell objects?

All of these things seem to be checked when you create a new function using types.FunctionType:

>>> h = types.FunctionType(g.__code__, g.__globals__, "h", g.__defaults__, None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: arg 5 (closure) must be tuple
>>> h = types.FunctionType(g.__code__, g.__globals__, "h", g.__defaults__, ())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: g requires closure of length 2, not 0
>>> h = types.FunctionType(g.__code__, g.__globals__, "h", g.__defaults__, (1,2))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: arg 5 (closure) expected cell, found int

I think the setter should make similar checks. Maybe there is C code which assumes "broken" closures never happen.

----------
nosy: +sbt

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14369>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 24, 2012, 4:37 PM

Post #3 of 6 (125 views)
Permalink
[issue14369] make __closure__ writable [In reply to]

Richard Oudkerk <shibturn [at] gmail> added the comment:

The patch causes crashes. If I define

def cell(o):
def f(): o
return f.__closure__[0]

def f():
a = 1
b = 2
def g():
return a + b
return g

g = f()

then I find

g.__closure__ = None; g() -> crash
g.__closure__ = (cell(3),); g() -> crash
g.__closure__ = (1, 2); g() -> SystemError *
g.__closure__ = (cell(3), cell(4), cell(5)); g() -> returns 7

* SystemError: ..\Objects\cellobject.c:24: bad argument to internal function

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14369>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 24, 2012, 4:52 PM

Post #4 of 6 (125 views)
Permalink
[issue14369] make __closure__ writable [In reply to]

Yury Selivanov <yselivanov [at] gmail> added the comment:

> The patch causes crashes.

Yes, that's known.

First, we need to check, that we can only write tuple of cell objects or None in __closure__ (that's easy to add). Secondly, perhaps, we can check __closure__ correctness each time we start evaluating a code object. The latter would offer us better stability, but may also introduce some slowdowns -- need to find some time to implement and benchmark this.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14369>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 25, 2012, 7:33 AM

Post #5 of 6 (126 views)
Permalink
[issue14369] make __closure__ writable [In reply to]

Richard Oudkerk <shibturn [at] gmail> added the comment:

Version of patch which checks invariants in the setter and adds tests.

----------
Added file: http://bugs.python.org/file25363/writable_closure_with_checking.patch

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14369>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 28, 2012, 3:56 AM

Post #6 of 6 (127 views)
Permalink
[issue14369] make __closure__ writable [In reply to]

Andrew Svetlov <andrew.svetlov [at] gmail> added the comment:

sbt, looks good for me.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14369>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.