From: <pj...@us...> - 2008-11-23 04:18:54
|
Revision: 5609 http://jython.svn.sourceforge.net/jython/?rev=5609&view=rev Author: pjenvey Date: 2008-11-23 04:18:48 +0000 (Sun, 23 Nov 2008) Log Message: ----------- ((PyInteger)obj.__int__()).getValue() -> obj.asInt() to avoid potential ClassCastException mayhem now that __int__ can return longs. as a side effect TypeErrors are raised on conversion failure rather than AttributeErrors (which is usually more appropriate anyway) fixes #1052 Modified Paths: -------------- trunk/jython/src/com/ziclix/python/sql/Fetch.java trunk/jython/src/com/ziclix/python/sql/Procedure.java trunk/jython/src/com/ziclix/python/sql/PyCursor.java trunk/jython/src/com/ziclix/python/sql/PyStatement.java trunk/jython/src/org/python/core/PyArray.java trunk/jython/src/org/python/core/PyString.java trunk/jython/src/org/python/modules/imp.java trunk/jython/src/org/python/modules/struct.java trunk/jython/src/org/python/modules/thread/thread.java trunk/jython/src/org/python/modules/time/Time.java Modified: trunk/jython/src/com/ziclix/python/sql/Fetch.java =================================================================== --- trunk/jython/src/com/ziclix/python/sql/Fetch.java 2008-11-23 04:15:40 UTC (rev 5608) +++ trunk/jython/src/com/ziclix/python/sql/Fetch.java 2008-11-23 04:18:48 UTC (rev 5609) @@ -304,7 +304,7 @@ for (int i = 0, len = procedure.columns.__len__(); i < len; i++) { PyObject column = procedure.columns.__getitem__(i); - int colType = ((PyInteger)column.__getitem__(Procedure.COLUMN_TYPE).__int__()).getValue(); + int colType = column.__getitem__(Procedure.COLUMN_TYPE).asInt(); switch (colType) { @@ -316,7 +316,7 @@ a[2] = Py.newInteger(-1); a[3] = column.__getitem__(Procedure.LENGTH); - switch (((PyInteger)a[1].__int__()).getValue()) { + switch (a[1].asInt()) { case Types.BIGINT: case Types.BIT: @@ -335,7 +335,7 @@ break; } - int nullable = ((PyInteger)column.__getitem__(Procedure.NULLABLE).__int__()).getValue(); + int nullable = column.__getitem__(Procedure.NULLABLE).asInt(); a[6] = (nullable == DatabaseMetaData.procedureNullable) ? Py.One : Py.Zero; @@ -363,8 +363,8 @@ for (int i = 0, j = 0, len = procedure.columns.__len__(); i < len; i++) { PyObject obj = Py.None; PyObject column = procedure.columns.__getitem__(i); - int colType = ((PyInteger)column.__getitem__(Procedure.COLUMN_TYPE).__int__()).getValue(); - int dataType = ((PyInteger)column.__getitem__(Procedure.DATA_TYPE).__int__()).getValue(); + int colType = column.__getitem__(Procedure.COLUMN_TYPE).asInt(); + int dataType = column.__getitem__(Procedure.DATA_TYPE).asInt(); switch (colType) { Modified: trunk/jython/src/com/ziclix/python/sql/Procedure.java =================================================================== --- trunk/jython/src/com/ziclix/python/sql/Procedure.java 2008-11-23 04:15:40 UTC (rev 5608) +++ trunk/jython/src/com/ziclix/python/sql/Procedure.java 2008-11-23 04:18:48 UTC (rev 5609) @@ -165,8 +165,8 @@ if (normal) { statement = cursor.connection.connection.prepareCall(sqlString); } else { - int t = ((PyInteger)rsType.__int__()).getValue(); - int c = ((PyInteger)rsConcur.__int__()).getValue(); + int t = rsType.asInt(); + int c = rsConcur.asInt(); statement = cursor.connection.connection.prepareCall(sqlString, t, c); } @@ -202,7 +202,7 @@ // do nothing with params at the moment for (int i = 0, len = this.columns.__len__(), binding = 0; i < len; i++) { PyObject column = this.columns.__getitem__(i); - int colType = ((PyInteger)column.__getitem__(COLUMN_TYPE).__int__()).getValue(); + int colType = column.__getitem__(COLUMN_TYPE).asInt(); switch (colType) { @@ -213,7 +213,7 @@ PyInteger key = Py.newInteger(binding++); if (bindings.__finditem__(key) == null) { - int dataType = ((PyInteger)column.__getitem__(DATA_TYPE).__int__()).getValue(); + int dataType = column.__getitem__(DATA_TYPE).asInt(); bindings.__setitem__(key, Py.newInteger(dataType)); } @@ -256,7 +256,7 @@ if (this.columns != Py.None) { for (int i = 0, len = this.columns.__len__(); i < len; i++) { PyObject column = this.columns.__getitem__(i); - int colType = ((PyInteger)column.__getitem__(COLUMN_TYPE).__int__()).getValue(); + int colType = column.__getitem__(COLUMN_TYPE).asInt(); switch (colType) { @@ -326,8 +326,8 @@ for (int i = 0, len = this.columns.__len__(); i < len; i++) { PyObject column = this.columns.__getitem__(i); - int colType = ((PyInteger)column.__getitem__(COLUMN_TYPE).__int__()).getValue(); - int dataType = ((PyInteger)column.__getitem__(DATA_TYPE).__int__()).getValue(); + int colType = column.__getitem__(COLUMN_TYPE).asInt(); + int dataType = column.__getitem__(DATA_TYPE).asInt(); String dataTypeName = column.__getitem__(DATA_TYPE_NAME).toString(); switch (colType) { Modified: trunk/jython/src/com/ziclix/python/sql/PyCursor.java =================================================================== --- trunk/jython/src/com/ziclix/python/sql/PyCursor.java 2008-11-23 04:15:40 UTC (rev 5608) +++ trunk/jython/src/com/ziclix/python/sql/PyCursor.java 2008-11-23 04:18:48 UTC (rev 5609) @@ -180,9 +180,9 @@ public void __setattr__(String name, PyObject value) { if ("arraysize".equals(name)) { - this.arraysize = ((PyInteger)value.__int__()).getValue(); + this.arraysize = value.asInt(); } else if ("softspace".equals(name)) { - this.softspace = ((PyInteger)value.__int__()).getValue(); + this.softspace = value.asInt(); } else if ("datahandler".equals(name)) { this.datahandler = (DataHandler)value.__tojava__(DataHandler.class); } else { @@ -398,8 +398,8 @@ sqlStatement = this.connection.connection.createStatement(); } } else { - int t = ((PyInteger)this.rsType.__int__()).getValue(); - int c = ((PyInteger)this.rsConcur.__int__()).getValue(); + int t = this.rsType.asInt(); + int c = this.rsConcur.asInt(); if (prepared) { sqlStatement = this.connection.connection.prepareStatement(sqlString, t, c); @@ -414,7 +414,7 @@ } if (maxRows != Py.None) { - stmt.statement.setMaxRows(((PyInteger)maxRows.__int__()).getValue()); + stmt.statement.setMaxRows(maxRows.asInt()); } } catch (AbstractMethodError e) { throw zxJDBC.makeException(zxJDBC.NotSupportedError, zxJDBC.getString("nodynamiccursors")); @@ -457,7 +457,7 @@ Statement stmt = procedure.prepareCall(this.rsType, this.rsConcur); if (maxRows != Py.None) { - stmt.setMaxRows(((PyInteger)maxRows.__int__()).getValue()); + stmt.setMaxRows(maxRows.asInt()); } // get the bindings per the stored proc spec @@ -897,7 +897,7 @@ PyCursor cursor = (PyCursor)__self__; switch (index) { case 0 : - return cursor.fetchmany(((PyInteger)arg.__int__()).getValue()); + return cursor.fetchmany(arg.asInt()); case 5 : cursor.execute(arg, Py.None, Py.None, Py.None); return Py.None; @@ -911,7 +911,7 @@ cursor.executemany(arg, Py.None, Py.None, Py.None); return Py.None; case 10 : - cursor.scroll(((PyInteger)arg.__int__()).getValue(), "relative"); + cursor.scroll(arg.asInt(), "relative"); return Py.None; case 11 : cursor.execute(arg, Py.None, Py.None, Py.None); @@ -938,7 +938,7 @@ cursor.executemany(arga, argb, Py.None, Py.None); return Py.None; case 10 : - cursor.scroll(((PyInteger)arga.__int__()).getValue(), argb.toString()); + cursor.scroll(arga.asInt(), argb.toString()); return Py.None; default : throw info.unexpectedCall(2, false); Modified: trunk/jython/src/com/ziclix/python/sql/PyStatement.java =================================================================== --- trunk/jython/src/com/ziclix/python/sql/PyStatement.java 2008-11-23 04:15:40 UTC (rev 5608) +++ trunk/jython/src/com/ziclix/python/sql/PyStatement.java 2008-11-23 04:18:48 UTC (rev 5609) @@ -300,7 +300,7 @@ if (binding != null) { try { - int bindingValue = ((PyInteger)binding.__int__()).getValue(); + int bindingValue = binding.asInt(); datahandler.setJDBCObject(preparedStatement, column, param, bindingValue); } catch (PyException e) { Modified: trunk/jython/src/org/python/core/PyArray.java =================================================================== --- trunk/jython/src/org/python/core/PyArray.java 2008-11-23 04:15:40 UTC (rev 5608) +++ trunk/jython/src/org/python/core/PyArray.java 2008-11-23 04:18:48 UTC (rev 5609) @@ -438,7 +438,7 @@ return s.toString().charAt(0); } else if (obj.__nonzero__()) { - return ((PyInteger)obj.__int__()).getValue(); + return obj.asInt(); } else { return -1; Modified: trunk/jython/src/org/python/core/PyString.java =================================================================== --- trunk/jython/src/org/python/core/PyString.java 2008-11-23 04:15:40 UTC (rev 5608) +++ trunk/jython/src/org/python/core/PyString.java 2008-11-23 04:18:48 UTC (rev 5609) @@ -2953,7 +2953,9 @@ } int val; try { - val = ((PyInteger)arg.__int__()).getValue(); + // Explicitly __int__ so we can look for an AttributeError (which is + // less invasive to mask than a TypeError) + val = arg.__int__().asInt(); } catch (PyException e){ if (Py.matchException(e, Py.AttributeError)) { throw Py.TypeError("%c requires int or char"); Modified: trunk/jython/src/org/python/modules/imp.java =================================================================== --- trunk/jython/src/org/python/modules/imp.java 2008-11-23 04:15:40 UTC (rev 5608) +++ trunk/jython/src/org/python/modules/imp.java 2008-11-23 04:18:48 UTC (rev 5609) @@ -180,7 +180,7 @@ public static PyObject load_module(String name, PyObject file, PyObject filename, PyTuple data) { PyObject mod = Py.None; PySystemState sys = Py.getSystemState(); - int type = ((PyInteger)data.__getitem__(2).__int__()).getValue(); + int type = data.__getitem__(2).asInt(); while(mod == Py.None) { Object o = file.__tojava__(InputStream.class); if (o == Py.NoConversion) { Modified: trunk/jython/src/org/python/modules/struct.java =================================================================== --- trunk/jython/src/org/python/modules/struct.java 2008-11-23 04:15:40 UTC (rev 5608) +++ trunk/jython/src/org/python/modules/struct.java 2008-11-23 04:18:48 UTC (rev 5609) @@ -329,7 +329,7 @@ int get_int(PyObject value) { try { - return ((PyInteger)value.__int__()).getValue(); + return value.asInt(); } catch (PyException ex) { throw StructError("required argument is not an integer"); } Modified: trunk/jython/src/org/python/modules/thread/thread.java =================================================================== --- trunk/jython/src/org/python/modules/thread/thread.java 2008-11-23 04:15:40 UTC (rev 5608) +++ trunk/jython/src/org/python/modules/thread/thread.java 2008-11-23 04:18:48 UTC (rev 5609) @@ -101,7 +101,7 @@ return stack_size; case 1: long old_stack_size = stack_size; - int proposed_stack_size = ((PyInteger)args[0].__int__()).getValue(); + int proposed_stack_size = args[0].asInt(); if (proposed_stack_size != 0 && proposed_stack_size < 32768) { // as specified by Python, Java quietly ignores what // it considers are too small Modified: trunk/jython/src/org/python/modules/time/Time.java =================================================================== --- trunk/jython/src/org/python/modules/time/Time.java 2008-11-23 04:15:40 UTC (rev 5608) +++ trunk/jython/src/org/python/modules/time/Time.java 2008-11-23 04:18:48 UTC (rev 5609) @@ -154,7 +154,7 @@ private static int item(PyTuple tup, int i) { // knows about and asserts format on tuple items. See // documentation for Python's time module for details. - int val = ((PyInteger)tup.__getitem__(i).__int__()).getValue(); + int val = tup.__getitem__(i).asInt(); boolean valid = true; switch (i) { case 0: break; // year This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |