From: <cg...@us...> - 2009-01-26 03:10:39
|
Revision: 5982 http://jython.svn.sourceforge.net/jython/?rev=5982&view=rev Author: cgroves Date: 2009-01-26 03:10:36 +0000 (Mon, 26 Jan 2009) Log Message: ----------- Try to assign to a static field for a given name in __setattr__ on a Java type before filling something in on the dict. Fixes bug #1241 Modified Paths: -------------- trunk/jython/Lib/test/test_java_visibility.py trunk/jython/src/org/python/core/PyJavaType.java trunk/jython/src/org/python/core/PyObject.java trunk/jython/src/org/python/core/PySystemState.java trunk/jython/src/org/python/core/PyType.java trunk/jython/tests/java/org/python/tests/Visible.java Modified: trunk/jython/Lib/test/test_java_visibility.py =================================================================== --- trunk/jython/Lib/test/test_java_visibility.py 2009-01-26 02:21:05 UTC (rev 5981) +++ trunk/jython/Lib/test/test_java_visibility.py 2009-01-26 03:10:36 UTC (rev 5982) @@ -68,6 +68,11 @@ v = Visible() self.assertEquals(Results.PUBLIC_FIELD, v.visibleField) self.assertEquals(Results.PUBLIC_STATIC_FIELD, Visible.visibleStaticField) + Visible.visibleStaticField = Results.PUBLIC_STATIC_FIELD + 1 + self.assertEquals(Results.PUBLIC_STATIC_FIELD + 1, Visible.visibleStaticField) + self.assertEquals(Results.PUBLIC_STATIC_FIELD + 1, Visible.getVisibleStaticField()) + Visible.setVisibleStaticField(Results.PUBLIC_STATIC_FIELD) + self.assertEquals(Results.PUBLIC_STATIC_FIELD, Visible.visibleStaticField) self.assertEquals(Results.PUBLIC_METHOD, v.visibleInstance(0)) self.assertEquals(Results.OVERLOADED_PUBLIC_METHOD, v.visibleInstance('a')) self.assertEquals(Results.EXTRA_ARG_PUBLIC_METHOD, v.visibleInstance(0, 'b')) Modified: trunk/jython/src/org/python/core/PyJavaType.java =================================================================== --- trunk/jython/src/org/python/core/PyJavaType.java 2009-01-26 02:21:05 UTC (rev 5981) +++ trunk/jython/src/org/python/core/PyJavaType.java 2009-01-26 03:10:36 UTC (rev 5982) @@ -56,45 +56,51 @@ super(TYPE == null ? fromClass(PyType.class) : TYPE); } - // Java types are ok with things being added and removed from their dicts as long as there isn't - // something there, so let these checks through - @Override - protected void checkDelattr() {} - - @Override - protected void checkSetattr() {} - protected boolean useMetatypeFirst(PyObject attr) { return !(attr instanceof PyReflectedField || attr instanceof PyReflectedFunction); } + // Java types are ok with things being added and removed from their dicts as long as there isn't @Override - public void addMethod(PyBuiltinMethod meth) { + void type___setattr__(String name, PyObject value) { + PyObject field = lookup(name);// If we have a static field that takes this, go with that + if (field != null) { + if (field._doset(null, value)) { + return; + } + } if (modified == null) { modified = Generic.set(); } - if (modified.add(meth.info.getName())) { + if (modified.add(name)) { if (conflicted != null) { for (PyJavaType conflict : conflicted) { - if (conflict.modified != null - && conflict.modified.contains(meth.info.getName())) { + if (conflict.modified != null && conflict.modified.contains(name)) { throw Py.TypeError(getName() - + " does not have a consistent method resolution" + " order with " - + conflict.getName() + ", and it already has " - + meth.info.getName() + " added for Python"); + + " does not have a consistent method resolution order with " + + conflict.getName() + ", and it already has " + name + + " added for Python"); } } } } - super.addMethod(meth); + object___setattr__(name, value); + postSetattr(name); } @Override - public void removeMethod(PyBuiltinMethod meth) { - super.removeMethod(meth); + void type___delattr__(String name) { + PyObject field = lookup(name); + if (field == null) { + throw Py.NameError("attribute not found: "+name); + } + if (!field.jdontdel()) { + object___delattr__(name); + } if (modified != null) { - modified.remove(meth.info.getName()); + modified.remove(name); } + postDelattr(name); } @Override @@ -136,8 +142,8 @@ types.add(othertype); } } - throw Py.TypeError(String.format("Supertypes that share a method " - + " have an MRO conflict[method=%s, types=%s]", method, types)); + throw Py.TypeError(String.format("Supertypes that share a modified attribute " + + " have an MRO conflict[attribute=%s, types=%s]", method, types)); } } } Modified: trunk/jython/src/org/python/core/PyObject.java =================================================================== --- trunk/jython/src/org/python/core/PyObject.java 2009-01-26 02:21:05 UTC (rev 5981) +++ trunk/jython/src/org/python/core/PyObject.java 2009-01-26 03:10:36 UTC (rev 5982) @@ -1033,10 +1033,6 @@ return false; } - boolean jtryset(PyObject container, PyObject value) { - return _doset(container, value); - } - boolean jdontdel() { return false; } @@ -3682,7 +3678,7 @@ object___setattr__(asName(name), value); } - private final void object___setattr__(String name, PyObject value) { + final void object___setattr__(String name, PyObject value) { PyObject descr = objtype.lookup(name); boolean set = false; Modified: trunk/jython/src/org/python/core/PySystemState.java =================================================================== --- trunk/jython/src/org/python/core/PySystemState.java 2009-01-26 02:21:05 UTC (rev 5981) +++ trunk/jython/src/org/python/core/PySystemState.java 2009-01-26 03:10:36 UTC (rev 5982) @@ -327,7 +327,7 @@ setWarnoptions(value); } else { PyObject ret = getType().lookup(name); // xxx fix fix fix - if (ret != null && ret.jtryset(this, value)) { + if (ret != null && ret._doset(this, value)) { return; } __dict__.__setitem__(name, value); @@ -338,7 +338,7 @@ checkMustExist(name); PyObject ret = getType().lookup(name); // xxx fix fix fix if (ret != null) { - ret.jtryset(this, PyAttributeDeleted.INSTANCE); + ret._doset(this, PyAttributeDeleted.INSTANCE); } try { __dict__.__delitem__(name); Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2009-01-26 02:21:05 UTC (rev 5981) +++ trunk/jython/src/org/python/core/PyType.java 2009-01-26 03:10:36 UTC (rev 5982) @@ -1092,26 +1092,26 @@ */ public void addMethod(PyBuiltinMethod meth) { PyMethodDescr pmd = meth.makeDescriptor(this); - dict.__setitem__(pmd.getName(), pmd); + __setattr__(pmd.getName(), pmd); } /** * Removes the given method from this type's dict or raises a KeyError. */ public void removeMethod(PyBuiltinMethod meth) { - dict.__delitem__(meth.info.getName()); + __delattr__(meth.info.getName()); } - protected void checkSetattr() { + void type___setattr__(String name, PyObject value) { if (builtin) { throw Py.TypeError(String.format("can't set attributes of built-in/extension type " + "'%s'", this.name)); } + super.__setattr__(name, value); + postSetattr(name); } - final void type___setattr__(String name, PyObject value) { - checkSetattr(); - super.__setattr__(name, value); + void postSetattr(String name) { if (name == "__set__") { if (!has_set && lookup("__set__") != null) { traverse_hierarchy(false, new OnType() { @@ -1145,15 +1145,19 @@ } protected void checkDelattr() { + } + + void type___delattr__(String name) { if (builtin) { throw Py.TypeError(String.format("can't set attributes of built-in/extension type " + "'%s'", this.name)); } + super.__delattr__(name); + postDelattr(name); } - final void type___delattr__(String name) { - checkDelattr(); - super.__delattr__(name); + + void postDelattr(String name) { if (name == "__set__") { if (has_set && lookup("__set__") == null) { traverse_hierarchy(false, new OnType() { @@ -1168,7 +1172,7 @@ }); } } else if (name == "__delete__") { - if (has_set && lookup("__delete__") == null) { + if (has_delete && lookup("__delete__") == null) { traverse_hierarchy(false, new OnType() { public boolean onType(PyType type) { boolean absent = type.getDict().__finditem__("__delete__") == null; Modified: trunk/jython/tests/java/org/python/tests/Visible.java =================================================================== --- trunk/jython/tests/java/org/python/tests/Visible.java 2009-01-26 02:21:05 UTC (rev 5981) +++ trunk/jython/tests/java/org/python/tests/Visible.java 2009-01-26 03:10:36 UTC (rev 5982) @@ -58,4 +58,12 @@ public static int visibleStatic(int iinput, String input) { return EXTRA_ARG_PUBLIC_STATIC_METHOD; } + + public static int getVisibleStaticField() { + return visibleStaticField; + } + + public static void setVisibleStaticField(int newValue) { + visibleStaticField = newValue; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |