From: <cg...@us...> - 2008-10-20 02:03:52
|
Revision: 5478 http://jython.svn.sourceforge.net/jython/?rev=5478&view=rev Author: cgroves Date: 2008-10-20 02:03:44 +0000 (Mon, 20 Oct 2008) Log Message: ----------- Cleanup, modernization Modified Paths: -------------- trunk/jython/src/org/python/core/Py.java trunk/jython/src/org/python/core/PyObject.java trunk/jython/src/org/python/core/PyType.java Modified: trunk/jython/src/org/python/core/Py.java =================================================================== --- trunk/jython/src/org/python/core/Py.java 2008-10-20 01:41:32 UTC (rev 5477) +++ trunk/jython/src/org/python/core/Py.java 2008-10-20 02:03:44 UTC (rev 5478) @@ -5,6 +5,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.ObjectStreamException; import java.io.OutputStream; @@ -19,12 +20,12 @@ import java.util.HashSet; import java.util.Set; +import org.python.antlr.ast.modType; import org.python.compiler.Module; import org.python.core.adapter.ClassicPyObjectAdapter; import org.python.core.adapter.ExtensiblePyObjectAdapter; import org.python.core.util.StringUtil; import org.python.modules.errno; -import org.python.antlr.ast.modType; public final class Py { @@ -80,7 +81,7 @@ public static PyString Space; /** Set if the type object is dynamically allocated */ public static long TPFLAGS_HEAPTYPE; - + /** Builtin types that are used to setup PyObject. */ static final Set<Class> BOOTSTRAP_TYPES = new HashSet<Class>(4); static { @@ -353,7 +354,7 @@ public static void FutureWarning(String message) { warning(FutureWarning, message); } - + public static PyObject ImportWarning; public static void ImportWarning(String message) { warning(ImportWarning, message); @@ -569,17 +570,17 @@ } static PyObject newUnicode(int codepoint) { - return (PyUnicode) makeCharacter(codepoint, true); + return makeCharacter(codepoint, true); } public static PyUnicode newUnicode(String s) { return new PyUnicode(s); } - + public static PyUnicode newUnicode(String s, boolean isBasic) { return new PyUnicode(s, isBasic); } - + public static PyBoolean newBoolean(boolean t) { return t ? Py.True : Py.False; } @@ -752,7 +753,7 @@ // Pre-initialize the PyJavaClass for OutOfMemoryError so when we need // it it creating the pieces for it won't cause an additional out of // memory error. Fix for bug #1654484 - PyJavaClass.lookup(java.lang.OutOfMemoryError.class); + PyJavaClass.lookup(OutOfMemoryError.class); } public static PySystemState defaultSystemState; // This is a hack to get initializations to work in proper order @@ -889,11 +890,11 @@ } instance.__init__(pargs, Py.NoKeywords); } - + /** * Initializes a default PythonInterpreter and runs the code from * {@link PyRunnable#getMain} as __main__ - * + * * Called by the code generated in {@link Module#addMain()} */ public static void runMain(PyRunnable main, String[] args) throws Exception { @@ -1136,14 +1137,14 @@ // java.io.IOExceptions. This is a hack for 1.0.x until I can do // it right in 1.1 if (exc == Py.IOError) { - if (__builtin__.isinstance(pye.value, PyJavaClass.lookup(java.io.IOException.class))) { + if (__builtin__.isinstance(pye.value, PyJavaClass.lookup(IOException.class))) { return true; } } // FIXME too, same approach for OutOfMemoryError if (exc == Py.MemoryError) { if (__builtin__.isinstance(pye.value, - PyJavaClass.lookup(java.lang.OutOfMemoryError.class))) { + PyJavaClass.lookup(OutOfMemoryError.class))) { return true; } } @@ -1226,7 +1227,7 @@ } Py.runCode(code, locals, globals); } - + private final static ThreadStateMapping threadStateMapping = new ThreadStateMapping(); public static final ThreadState getThreadState() { @@ -1464,7 +1465,7 @@ /** * Uses the PyObjectAdapter passed to {@link PySystemState#initialize} to turn o into a PyObject. - * + * * @see ClassicPyObjectAdapter - default PyObjectAdapter type */ public static PyObject java2py(Object o) { @@ -1483,7 +1484,7 @@ /** * Set the ExtensiblePyObjectAdapter used by java2py. - * + * * @param adapter The new ExtensiblePyObjectAdapter */ protected static void setAdapter(ExtensiblePyObjectAdapter adapter) { @@ -1530,7 +1531,7 @@ /** * Create a new Python class. - * + * * @param name the String name of the class * @param bases an array of PyObject base classes * @param dict the class's namespace, containing the class body @@ -1677,8 +1678,8 @@ } return Py.compile_flags(node, getName(), filename, true, printResults, cflags); } - - public static PyObject compile_flags(modType node, String filename, + + public static PyObject compile_flags(modType node, String filename, String kind, CompilerFlags cflags) { boolean printResults = false; if (kind.equals("single")) { @@ -1686,16 +1687,16 @@ } return Py.compile_flags(node, getName(), filename, true, printResults, cflags); } - + public static PyObject compile_flags(String data, String filename, String kind, CompilerFlags cflags) { - + if (data.contains("\0")) { throw Py.TypeError("compile() expected string without null bytes"); } - + byte[] bytes; if (cflags != null && cflags.dont_imply_dedent) { bytes = StringUtil.toBytes(data + "\n"); @@ -1894,7 +1895,7 @@ return abstract_issubclass(icls, cls); } } - + public static boolean isSubClass(PyObject derived,PyObject cls) { return isSubClass(derived, cls, 0); } Modified: trunk/jython/src/org/python/core/PyObject.java =================================================================== --- trunk/jython/src/org/python/core/PyObject.java 2008-10-20 01:41:32 UTC (rev 5477) +++ trunk/jython/src/org/python/core/PyObject.java 2008-10-20 02:03:44 UTC (rev 5478) @@ -21,18 +21,18 @@ */ @ExposedType(name = "object") public class PyObject implements Serializable { - + public static final PyType TYPE = PyType.fromClass(PyObject.class); - + @ExposedNew @ExposedMethod final void object___init__(PyObject[] args, String[] keywords) { -// XXX: attempted fix for object(foo=1), etc -// XXX: this doesn't work for metaclasses, for some reason +// XXX: attempted fix for object(foo=1), etc +// XXX: this doesn't work for metaclasses, for some reason // if (args.length > 0) { // throw Py.TypeError("default __new__ takes no parameters"); // } - + } private PyType objtype; @@ -77,16 +77,17 @@ this.objtype = objtype; } - // A package private constructor used by PyJavaClass - // xxx will need variants for PyType of PyType and still PyJavaClass of PyJavaClass - PyObject(boolean dummy) { - objtype = (PyType) this; + /** + * Creates the PyObject for the base type. The argument only exists to make the constructor + * distinct. + */ + PyObject(boolean ignored) { + objtype = (PyType)this; } /** - * The standard constructor for a <code>PyObject</code>. It will set - * the <code>objtype</code> field to correspond to the specific - * subclass of <code>PyObject</code> being instantiated. + * The standard constructor for a <code>PyObject</code>. It will set the <code>objtype</code> + * field to correspond to the specific subclass of <code>PyObject</code> being instantiated. **/ public PyObject() { objtype = PyType.fromClass(getClass()); @@ -145,7 +146,7 @@ public PyUnicode __unicode__() { return new PyUnicode(__str__()); } - + /** * Equivalent to the standard Python __hash__ method. This method can * not be overridden. Instead, you should override the standard Java @@ -342,13 +343,13 @@ if(keys == null) throw Py.TypeError(name + "argument after ** must be a mapping"); - for (int i = 0; i < keywords.length; i++) - if (kwargs.__finditem__(keywords[i]) != null) + for (String keyword : keywords) + if (kwargs.__finditem__(keyword) != null) throw Py.TypeError( name + "got multiple values for " + "keyword argument '" - + keywords[i] + + keyword + "'"); argslen += kwargs.__len__(); } @@ -648,21 +649,21 @@ * <p> * If a PyObject subclass should support iteration based in the __finditem__() method, it must * supply an implementation of __iter__() like this: - * + * * <pre> * public PyObject __iter__() { * return new PySequenceIter(this); * } * </pre> - * + * * When iterating over a python sequence from java code, it should be done with code like this: - * + * * <pre> * for (PyObject item : seq.asIterable()) { * // Do somting with item * } * </pre> - * + * * @since 2.2 */ public PyObject __iter__() { @@ -729,13 +730,13 @@ * Very similar to the standard Python __getattr__ method. Instead of * throwing a AttributeError if the item isn't found, this just returns * null. - * + * * By default, this method will call * <code>__findattr__(name.internedString)</code> with the appropriate - * args. - * + * args. + * * @param name the name to lookup in this namespace - * + * * @return the value corresponding to name or null if name is not found */ public final PyObject __findattr__(PyString name) { @@ -748,7 +749,7 @@ /** * A variant of the __findattr__ method which accepts a Java * <code>String</code> as the name. - * + * * <b>Warning: name must be an interned string!</b> * * @param name the name to lookup in this namespace @@ -763,22 +764,22 @@ return null; } throw exc; - } + } } - + /** - * Attribute lookup hook. If the attribute is not found, null may be - * returned or a Py.AttributeError can be thrown, whatever is more + * Attribute lookup hook. If the attribute is not found, null may be + * returned or a Py.AttributeError can be thrown, whatever is more * correct, efficient and/or convenient for the implementing class. - * - * Client code should use {@link #__getattr__(String)} or - * {@link #__findattr__(String)}. Both methods have a clear policy for - * failed lookups. - * - * @return The looked up value. May return null if the attribute is not found + * + * Client code should use {@link #__getattr__(String)} or + * {@link #__findattr__(String)}. Both methods have a clear policy for + * failed lookups. + * + * @return The looked up value. May return null if the attribute is not found * @throws PyException(AttributeError) if the attribute is not found. This - * is not mandatory, null can be returned if it fits the implementation - * better, or for performance reasons. + * is not mandatory, null can be returned if it fits the implementation + * better, or for performance reasons. */ public PyObject __findattr_ex__(String name) { return object___findattr__(name); @@ -789,7 +790,7 @@ * * By default, this method will call * <code>__getattr__(name.internedString)</code> with the appropriate - * args. + * args. * * @param name the name to lookup in this namespace * @return the value corresponding to name @@ -1002,9 +1003,9 @@ * * This method can not be overridden. * To implement __coerce__ functionality, override __coerce_ex__ instead. - * - * Also, <b>do not</b> call this method from exposed 'coerce' methods. - * Instead, Use adaptToCoerceTuple over the result of the overriden + * + * Also, <b>do not</b> call this method from exposed 'coerce' methods. + * Instead, Use adaptToCoerceTuple over the result of the overriden * __coerce_ex__. * * @param pyo the other object involved in the coercion. @@ -1022,13 +1023,13 @@ /** * Adapts the result of __coerce_ex__ to a tuple of two elements, with the - * resulting coerced values, or to Py.NotImplemented, if o is Py.None. - * - * This is safe to be used from subclasses exposing '__coerce__' + * resulting coerced values, or to Py.NotImplemented, if o is Py.None. + * + * This is safe to be used from subclasses exposing '__coerce__' * (as opposed to {@link #__coerce__(PyObject)}, which calls the virtual * method {@link #__coerce_ex__(PyObject)}) - * - * @param o either a PyObject[2] or a PyObject, as given by + * + * @param o either a PyObject[2] or a PyObject, as given by * {@link #__coerce_ex__(PyObject)}. */ protected final PyObject adaptToCoerceTuple(Object o) { @@ -1292,7 +1293,7 @@ PyObject token = null; PyType t1 = this.getType(); PyType t2 = o.getType(); - + if (t1 != t2 && t2.isSubType(t1)) { return o._eq(this); } @@ -1331,7 +1332,7 @@ PyObject token = null; PyType t1 = this.getType(); PyType t2 = o.getType(); - + if (t1 != t2 && t2.isSubType(t1)) { return o._ne(this); } @@ -1365,7 +1366,7 @@ PyObject token = null; PyType t1 = this.getType(); PyType t2 = o.getType(); - + if (t1 != t2 && t2.isSubType(t1)) { return o._ge(this); } @@ -1399,7 +1400,7 @@ PyObject token = null; PyType t1 = this.getType(); PyType t2 = o.getType(); - + if (t1 != t2 && t2.isSubType(t1)) { return o._gt(this); } @@ -1433,7 +1434,7 @@ PyObject token = null; PyType t1 = this.getType(); PyType t2 = o.getType(); - + if (t1 != t2 && t2.isSubType(t1)) { return o._le(this); } @@ -1467,7 +1468,7 @@ PyObject token = null; PyType t1 = this.getType(); PyType t2 = o.getType(); - + if (t1 != t2 && t2.isSubType(t1)) { return o._lt(this); } @@ -1678,7 +1679,7 @@ throw Py.TypeError(String.format("'%.200s' object cannot be interpreted as an index", getType().fastGetName())); } - + /** * @param op the String form of the op (e.g. "+") * @param o2 the right operand @@ -1705,7 +1706,7 @@ protected String unsupportedopMessage(String op, PyObject o2) { return null; } - + /** * Should return an error message suitable for substitution where. * @@ -3523,7 +3524,7 @@ // backward comp impls. /** * Get descriptor for this PyObject. - * + * * @param obj - * the instance accessing this descriptor. Can be null if this is * being accessed by a type. @@ -3544,7 +3545,7 @@ public void __delete__(PyObject obj) { throw Py.AttributeError("object internal __delete__ impl is abstract"); } - + @ExposedMethod final PyObject object___getattribute__(PyObject arg0) { String name = asName(arg0); @@ -3553,7 +3554,7 @@ noAttributeError(name); return ret; } - + // name must be interned final PyObject object___findattr__(String name) { @@ -3581,12 +3582,12 @@ return null; } - + @ExposedMethod final void object___setattr__(PyObject name, PyObject value) { object___setattr__(asName(name), value); } - + private final void object___setattr__(String name, PyObject value) { PyObject descr = objtype.lookup(name); @@ -3616,7 +3617,7 @@ noAttributeError(name); } - + @ExposedMethod final void object___delattr__(PyObject name) { object___delattr__(asName(name)); @@ -3684,7 +3685,7 @@ /** Used for pickling. If the subclass specifies __reduce__, it will * override __reduce_ex__ in the base-class, even if __reduce_ex__ was * called with an argument. - * + * * @param arg PyInteger specifying reduce algorithm (method without this * argument defaults to 0). * @return a tuple of (class, tuple) @@ -3853,7 +3854,7 @@ throw new ConversionException(index); } - // TODO - remove when all generated users are migrated to @Exposed and asInt() + // TODO - remove when all generated users are migrated to @Exposed and asInt() public int asInt(int index) throws ConversionException { throw new ConversionException(index); } Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2008-10-20 01:41:32 UTC (rev 5477) +++ trunk/jython/src/org/python/core/PyType.java 2008-10-20 02:03:44 UTC (rev 5478) @@ -27,7 +27,6 @@ /** * The Python Type object implementation. - * */ @ExposedType(name = "type") public class PyType extends PyObject implements Serializable { @@ -54,7 +53,7 @@ private long tp_flags; /** The underlying java class or null. */ - private Class underlying_class; + private Class<?> underlying_class; /** Whether it's a builtin type. */ boolean builtin = false; @@ -79,20 +78,23 @@ private ReferenceQueue<PyType> subclasses_refq = new ReferenceQueue<PyType>(); private HashSet<WeakReference<PyType>> subclasses = new HashSet<WeakReference<PyType>>(); - private final static Class[] O = {PyObject.class}; - private final static Class[] OO = {PyObject.class, PyObject.class}; + private final static Class<?>[] O = {PyObject.class}; + private final static Class<?>[] OO = {PyObject.class, PyObject.class}; /** Mapping of Java classes to their PyTypes. */ - private static HashMap<Class, PyType> class_to_type; + private static Map<Class<?>, PyType> class_to_type; /** Mapping of Java classes to their TypeBuilders. */ - private static HashMap<Class, TypeBuilder> classToBuilder; + private static Map<Class<?>, TypeBuilder> classToBuilder; - private PyType() { - } + private PyType() {} - private PyType(boolean dummy) { - super(true); + /** + * Creates the PyType instance for type itself. The argument just exists to make the constructor + * distinct. + */ + private PyType(boolean ignored) { + super(ignored); } PyType(PyType subtype) { @@ -288,7 +290,7 @@ return newobj; } - private static void fillFromClass(PyType newtype, String name, Class c, Class base, + private static void fillFromClass(PyType newtype, String name, Class<?> c, Class<?> base, TypeBuilder tb) { if (base == null) { base = c.getSuperclass(); @@ -333,7 +335,7 @@ newtype.dict = dict; } - private static void fillInClassic(Class c, Class<?> base, PyObject dict) { + private static void fillInClassic(Class<?> c, Class<?> base, PyObject dict) { if (Py.BOOTSTRAP_TYPES.contains(c)) { // BOOTSTRAP_TYPES will be filled in by addBuilder later return; @@ -341,7 +343,7 @@ Map<String, Object> propnames = new HashMap<String, Object>(); Method[] methods = c.getMethods(); for (Method meth : methods) { - Class declaring = meth.getDeclaringClass(); + Class<?> declaring = meth.getDeclaringClass(); if (declaring != base && base.isAssignableFrom(declaring) && !ignore(meth)) { String methname = meth.getName(); String nmethname = normalize_name(methname); @@ -377,11 +379,11 @@ } Field[] fields = c.getFields(); for (Field field : fields) { - Class declaring = field.getDeclaringClass(); + Class<?> declaring = field.getDeclaringClass(); if (declaring != base && base.isAssignableFrom(declaring)) { String fldname = field.getName(); int fldmods = field.getModifiers(); - Class fldtype = field.getType(); + Class<?> fldtype = field.getType(); if (Modifier.isStatic(fldmods)) { if (fldname.startsWith("__doc__") && fldname.length() > 7 && fldtype == PyString.class) { @@ -409,7 +411,7 @@ } Method getter = null; Method setter = null; - Class proptype = null; + Class<?> proptype = null; getter = get_non_static_method(c, "get" + propname, new Class[] {}); if (getter == null) getter = get_non_static_method(c, "is" + propname, new Class[] {}); @@ -430,10 +432,10 @@ // XXX error } } - Constructor[] ctrs = c.getConstructors(); + Constructor<?>[] ctrs = c.getConstructors(); if (ctrs.length != 0) { final PyReflectedConstructor reflctr = new PyReflectedConstructor("_new_impl"); - for (Constructor ctr : ctrs) { + for (Constructor<?> ctr : ctrs) { reflctr.addConstructor(ctr); } PyObject new_ = new PyNewWrapper(c, "__new__", -1, -1) { @@ -446,16 +448,15 @@ } if (ClassDictInit.class.isAssignableFrom(c) && c != ClassDictInit.class) { try { - @SuppressWarnings("unchecked") Method m = c.getMethod("classDictInit", PyObject.class); - m.invoke(null, new Object[] {dict}); + m.invoke(null, dict); } catch (Exception exc) { throw error(exc); } } } - private static void fillInMRO(PyType type, Class base) { + private static void fillInMRO(PyType type, Class<?> base) { PyType[] mro; if (base == Object.class) { mro = new PyType[] {type}; @@ -564,7 +565,7 @@ } } } catch (PyException t) { - for (Iterator it = savedSubMros.iterator(); it.hasNext();) { + for (Iterator<Object> it = savedSubMros.iterator(); it.hasNext();) { PyType subtype = (PyType)it.next(); PyObject[] subtypeSavedMro = (PyObject[])it.next(); subtype.mro = subtypeSavedMro; @@ -633,7 +634,7 @@ } private void cleanup_subclasses() { - Reference ref; + Reference<?> ref; while ((ref = subclasses_refq.poll()) != null) { subclasses.remove(ref); } @@ -1017,7 +1018,7 @@ return Py.JavaError(e); } - private static Method get_non_static_method(Class<?> c, String name, Class[] parmtypes) { + private static Method get_non_static_method(Class<?> c, String name, Class<?>[] parmtypes) { try { Method meth = c.getMethod(name, parmtypes); if (!Modifier.isStatic(meth.getModifiers())) { @@ -1029,7 +1030,7 @@ return null; } - private static Method get_descr_method(Class c, String name, Class[] parmtypes) { + private static Method get_descr_method(Class<?> c, String name, Class<?>[] parmtypes) { Method meth = get_non_static_method(c, name, parmtypes); if (meth != null && meth.getDeclaringClass() != PyObject.class) { return meth; @@ -1038,8 +1039,8 @@ } private static boolean ignore(Method meth) { - Class[] exceptions = meth.getExceptionTypes(); - for (Class exception : exceptions) { + Class<?>[] exceptions = meth.getExceptionTypes(); + for (Class<?> exception : exceptions) { if (exception == PyIgnoreMethodTag.class) { return true; } @@ -1047,9 +1048,9 @@ return false; } - public static void addBuilder(Class forClass, TypeBuilder builder) { + public static void addBuilder(Class<?> forClass, TypeBuilder builder) { if (classToBuilder == null) { - classToBuilder = new HashMap<Class, TypeBuilder>(); + classToBuilder = new HashMap<Class<?>, TypeBuilder>(); } classToBuilder.put(forClass, builder); @@ -1063,7 +1064,7 @@ PyType objType = fromClass(builder.getTypeClass()); objType.name = builder.getName(); objType.dict = builder.getDict(objType); - Class base = builder.getBase(); + Class<?> base = builder.getBase(); if (base == Object.class) { base = forClass.getSuperclass(); } @@ -1072,13 +1073,13 @@ } } - private static PyType addFromClass(Class c) { + private static PyType addFromClass(Class<?> c) { if (ExposeAsSuperclass.class.isAssignableFrom(c)) { PyType exposedAs = fromClass(c.getSuperclass()); class_to_type.put(c, exposedAs); return exposedAs; } - Class base = null; + Class<?> base = null; String name = null; TypeBuilder tb = classToBuilder == null ? null : classToBuilder.get(c); if (tb != null) { @@ -1089,16 +1090,16 @@ } PyType newtype = class_to_type.get(c); if (newtype == null) { - newtype = c == PyType.class ? new PyType(true) : new PyType(); + newtype = c == PyType.class ? new PyType(false) : new PyType(); class_to_type.put(c, newtype); fillFromClass(newtype, name, c, base, tb); } return newtype; } - public static synchronized PyType fromClass(Class c) { + public static synchronized PyType fromClass(Class<?> c) { if (class_to_type == null) { - class_to_type = new HashMap<Class, PyType>(); + class_to_type = new HashMap<Class<?>, PyType>(); addFromClass(PyType.class); } PyType type = class_to_type.get(c); @@ -1333,7 +1334,7 @@ return doc; } - public Object __tojava__(Class c) { + public Object __tojava__(Class<?> c) { if (underlying_class != null && (c == Object.class || c == Class.class || c == Serializable.class)) { return underlying_class; @@ -1429,19 +1430,20 @@ return new TypeResolver(underlying_class, getModule().toString(), name); } - public static interface Newstyle { - } + private interface OnType { - private interface OnType { boolean onType(PyType type); } static class TypeResolver implements Serializable { - private Class underlying_class; - private String module; + + private Class<?> underlying_class; + + String module; + private String name; - TypeResolver(Class underlying_class, String module, String name) { + TypeResolver(Class<?> underlying_class, String module, String name) { this.underlying_class = underlying_class; this.module = module; this.name = name; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |