problem with closures

alain alainpoint at yahoo.fr
Wed Dec 6 12:23:02 EST 2006


Hi,

I have a problem with closures.
I am trying to implement yet another design by contract decorator which
would look like the following:
<pre>
def contract(f):
	def newf(*args, **kw):
		import new
	        precondition =  new.function(f.func_code.co_consts[1],
							f.func_globals,'pre',
							f.func_defaults,
							f.func_closure)
		precondition()
		result=f(*args, **kw)
		postcondition=new.function(f.func_code.co_consts[2],globals())
		postcondition(result)
		return result
	return newf
@contract
def foo(x,y,g=2,z=1):
	def pre():
		assert x>1 and 0<y<100
	def post(result):
		assert result >0
	print 'main'
	return x+y+z*g

print foo(2,5,4,69)
<pre>

The problem is that i get the following error message on line 7:
TypeError: arg 5 (closure) must be tuple

f.func_closure is indeed empty while
f.func_code.co_consts[1].co_freevars is logically equal to ('x','y').

Thanks for responding

Alain




More information about the Python-list mailing list