From: <th...@us...> - 2009-03-31 23:54:02
|
Revision: 6142 http://jython.svn.sourceforge.net/jython/?rev=6142&view=rev Author: thobes Date: 2009-03-31 23:53:50 +0000 (Tue, 31 Mar 2009) Log Message: ----------- Fixed a problem with functions being bound to a name before decorators are applied. Also added a test case that checks for it. Modified Paths: -------------- trunk/jython/src/org/python/compiler/CodeCompiler.java Added Paths: ----------- trunk/jython/Lib/test/test_decorators_jy.py Added: trunk/jython/Lib/test/test_decorators_jy.py =================================================================== --- trunk/jython/Lib/test/test_decorators_jy.py (rev 0) +++ trunk/jython/Lib/test/test_decorators_jy.py 2009-03-31 23:53:50 UTC (rev 6142) @@ -0,0 +1,24 @@ +"""Misc decorator related tests + +Made for Jython. +""" +from test import test_support +import unittest + +class TestDecorators(unittest.TestCase): + + def test_lookup_order(self): + class Foo(object): + foo = 'bar' + @property + def property(self): + return self.foo + self.assertEqual(Foo().property, 'bar') + + +def test_main(): + test_support.run_unittest(TestDecorators) + + +if __name__ == '__main__': + test_main() Modified: trunk/jython/src/org/python/compiler/CodeCompiler.java =================================================================== --- trunk/jython/src/org/python/compiler/CodeCompiler.java 2009-03-31 23:30:57 UTC (rev 6141) +++ trunk/jython/src/org/python/compiler/CodeCompiler.java 2009-03-31 23:53:50 UTC (rev 6142) @@ -420,22 +420,29 @@ } else { code.invokespecial( "org/python/core/PyFunction", "<init>", "(" + $pyObj + $pyObjArr + $pyCode + $pyObj + $pyObjArr + ")V"); } + + applyDecorators(node.getInternalDecorator_list()); set(new Name(node,node.getInternalName(), expr_contextType.Store)); - doDecorators(node,node.getInternalDecorator_list(), node.getInternalName()); + //doDecorators(node,node.getInternalDecorator_list(), node.getInternalName()); return null; } - private void doDecorators(stmt node, java.util.List<expr> decs, String name) throws Exception { - if (decs.size() > 0) { - expr currentExpr = new Name(node, name, expr_contextType.Load); - for (int i=decs.size() - 1;i > -1;i--) { - java.util.List args = new ArrayList(); - args.add(currentExpr); - currentExpr = new Call(node, decs.get(i), args, new ArrayList<keyword>(), null, null); + private void applyDecorators(java.util.List<expr> decorators) throws Exception { + if (decorators != null && !decorators.isEmpty()) { + int res = storeTop(); + for (expr decorator : decorators) { + visit(decorator); stackProduce(); } - visit(currentExpr); - set(new Name(node, name, expr_contextType.Store)); + for (int i = decorators.size(); i > 0; i--) { + stackConsume(); + loadThreadState(); + code.aload(res); + code.invokevirtual("org/python/core/PyObject", "__call__", "(" + $threadState + $pyObj + ")" + $pyObj); + code.astore(res); + } + code.aload(res); + code.freeLocal(res); } } @@ -2023,10 +2030,12 @@ } else { code.invokestatic("org/python/core/Py", "makeClass", "(" + $str + $pyObjArr + $pyCode + $pyObj + $pyObjArr + ")" + $pyObj); } + + applyDecorators(node.getInternalDecorator_list()); //Assign this new class to the given name set(new Name(node,node.getInternalName(), expr_contextType.Store)); - doDecorators(node,node.getInternalDecorator_list(), node.getInternalName()); + //doDecorators(node,node.getInternalDecorator_list(), node.getInternalName()); return null; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |