From: <pj...@us...> - 2009-10-27 02:54:29
|
Revision: 6910 http://jython.svn.sourceforge.net/jython/?rev=6910&view=rev Author: pjenvey Date: 2009-10-27 02:54:10 +0000 (Tue, 27 Oct 2009) Log Message: ----------- speedup get/hasattr, use __findattr__ and intern the PyString itself, that way we don't have to do it again later if called with the same string Modified Paths: -------------- trunk/jython/src/org/python/core/__builtin__.java Modified: trunk/jython/src/org/python/core/__builtin__.java =================================================================== --- trunk/jython/src/org/python/core/__builtin__.java 2009-10-26 21:15:12 UTC (rev 6909) +++ trunk/jython/src/org/python/core/__builtin__.java 2009-10-27 02:54:10 UTC (rev 6910) @@ -633,24 +633,23 @@ public static PyObject getattr(PyObject obj, PyObject name, PyObject def) { String nameStr; if (name instanceof PyUnicode) { - nameStr = ((PyUnicode)name).encode(); + nameStr = ((PyUnicode)name).encode().intern(); } else if (name instanceof PyString) { - nameStr = name.asString(); + nameStr = ((PyString)name).internedString(); } else { throw Py.TypeError("getattr(): attribute name must be string"); } - PyObject result; - try { - result = obj.__getattr__(nameStr.intern()); - } catch (PyException pye) { - if (pye.match(Py.AttributeError) && def != null) { - result = def; - } else { - throw pye; - } + PyObject result = obj.__findattr__(nameStr.intern()); + if (result != null) { + return result; } - return result; + if (def != null) { + return def; + } + // throws AttributeError + obj.noAttributeError(nameStr); + return null; } public static PyObject globals() { @@ -662,13 +661,13 @@ if (name instanceof PyUnicode) { nameStr = ((PyUnicode)name).encode().intern(); } else if (name instanceof PyString) { - nameStr = name.asString(); + nameStr = ((PyString)name).internedString(); } else { throw Py.TypeError("hasattr(): attribute name must be string"); } try { - return obj.__findattr__(nameStr.intern()) != null; + return obj.__findattr__(nameStr) != null; } catch (PyException pye) { // swallow } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |