Skip to content

Commit 2cdb749

Browse files
committed
((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
1 parent 4326695 commit 2cdb749

10 files changed

Lines changed: 30 additions & 28 deletions

File tree

src/com/ziclix/python/sql/Fetch.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ protected PyObject createDescription(Procedure procedure) throws SQLException {
304304

305305
for (int i = 0, len = procedure.columns.__len__(); i < len; i++) {
306306
PyObject column = procedure.columns.__getitem__(i);
307-
int colType = ((PyInteger)column.__getitem__(Procedure.COLUMN_TYPE).__int__()).getValue();
307+
int colType = column.__getitem__(Procedure.COLUMN_TYPE).asInt();
308308

309309
switch (colType) {
310310

@@ -316,7 +316,7 @@ protected PyObject createDescription(Procedure procedure) throws SQLException {
316316
a[2] = Py.newInteger(-1);
317317
a[3] = column.__getitem__(Procedure.LENGTH);
318318

319-
switch (((PyInteger)a[1].__int__()).getValue()) {
319+
switch (a[1].asInt()) {
320320

321321
case Types.BIGINT:
322322
case Types.BIT:
@@ -335,7 +335,7 @@ protected PyObject createDescription(Procedure procedure) throws SQLException {
335335
break;
336336
}
337337

338-
int nullable = ((PyInteger)column.__getitem__(Procedure.NULLABLE).__int__()).getValue();
338+
int nullable = column.__getitem__(Procedure.NULLABLE).asInt();
339339

340340
a[6] = (nullable == DatabaseMetaData.procedureNullable) ? Py.One : Py.Zero;
341341

@@ -363,8 +363,8 @@ protected PyObject createResults(CallableStatement callableStatement, Procedure
363363
for (int i = 0, j = 0, len = procedure.columns.__len__(); i < len; i++) {
364364
PyObject obj = Py.None;
365365
PyObject column = procedure.columns.__getitem__(i);
366-
int colType = ((PyInteger)column.__getitem__(Procedure.COLUMN_TYPE).__int__()).getValue();
367-
int dataType = ((PyInteger)column.__getitem__(Procedure.DATA_TYPE).__int__()).getValue();
366+
int colType = column.__getitem__(Procedure.COLUMN_TYPE).asInt();
367+
int dataType = column.__getitem__(Procedure.DATA_TYPE).asInt();
368368

369369
switch (colType) {
370370

src/com/ziclix/python/sql/Procedure.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ public CallableStatement prepareCall(PyObject rsType, PyObject rsConcur) throws
165165
if (normal) {
166166
statement = cursor.connection.connection.prepareCall(sqlString);
167167
} else {
168-
int t = ((PyInteger)rsType.__int__()).getValue();
169-
int c = ((PyInteger)rsConcur.__int__()).getValue();
168+
int t = rsType.asInt();
169+
int c = rsConcur.asInt();
170170

171171
statement = cursor.connection.connection.prepareCall(sqlString, t, c);
172172
}
@@ -202,7 +202,7 @@ public void normalizeInput(PyObject params, PyObject bindings) throws SQLExcepti
202202
// do nothing with params at the moment
203203
for (int i = 0, len = this.columns.__len__(), binding = 0; i < len; i++) {
204204
PyObject column = this.columns.__getitem__(i);
205-
int colType = ((PyInteger)column.__getitem__(COLUMN_TYPE).__int__()).getValue();
205+
int colType = column.__getitem__(COLUMN_TYPE).asInt();
206206

207207
switch (colType) {
208208

@@ -213,7 +213,7 @@ public void normalizeInput(PyObject params, PyObject bindings) throws SQLExcepti
213213
PyInteger key = Py.newInteger(binding++);
214214

215215
if (bindings.__finditem__(key) == null) {
216-
int dataType = ((PyInteger)column.__getitem__(DATA_TYPE).__int__()).getValue();
216+
int dataType = column.__getitem__(DATA_TYPE).asInt();
217217
bindings.__setitem__(key, Py.newInteger(dataType));
218218
}
219219

@@ -256,7 +256,7 @@ public String toSql() throws SQLException {
256256
if (this.columns != Py.None) {
257257
for (int i = 0, len = this.columns.__len__(); i < len; i++) {
258258
PyObject column = this.columns.__getitem__(i);
259-
int colType = ((PyInteger)column.__getitem__(COLUMN_TYPE).__int__()).getValue();
259+
int colType = column.__getitem__(COLUMN_TYPE).asInt();
260260

261261
switch (colType) {
262262

@@ -326,8 +326,8 @@ protected void registerOutParameters(CallableStatement statement) throws SQLExce
326326

327327
for (int i = 0, len = this.columns.__len__(); i < len; i++) {
328328
PyObject column = this.columns.__getitem__(i);
329-
int colType = ((PyInteger)column.__getitem__(COLUMN_TYPE).__int__()).getValue();
330-
int dataType = ((PyInteger)column.__getitem__(DATA_TYPE).__int__()).getValue();
329+
int colType = column.__getitem__(COLUMN_TYPE).asInt();
330+
int dataType = column.__getitem__(DATA_TYPE).asInt();
331331
String dataTypeName = column.__getitem__(DATA_TYPE_NAME).toString();
332332

333333
switch (colType) {

src/com/ziclix/python/sql/PyCursor.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ public String toString() {
180180
public void __setattr__(String name, PyObject value) {
181181

182182
if ("arraysize".equals(name)) {
183-
this.arraysize = ((PyInteger)value.__int__()).getValue();
183+
this.arraysize = value.asInt();
184184
} else if ("softspace".equals(name)) {
185-
this.softspace = ((PyInteger)value.__int__()).getValue();
185+
this.softspace = value.asInt();
186186
} else if ("datahandler".equals(name)) {
187187
this.datahandler = (DataHandler)value.__tojava__(DataHandler.class);
188188
} else {
@@ -398,8 +398,8 @@ private PyStatement prepareStatement(PyObject sql, PyObject maxRows, boolean pre
398398
sqlStatement = this.connection.connection.createStatement();
399399
}
400400
} else {
401-
int t = ((PyInteger)this.rsType.__int__()).getValue();
402-
int c = ((PyInteger)this.rsConcur.__int__()).getValue();
401+
int t = this.rsType.asInt();
402+
int c = this.rsConcur.asInt();
403403

404404
if (prepared) {
405405
sqlStatement = this.connection.connection.prepareStatement(sqlString, t, c);
@@ -414,7 +414,7 @@ private PyStatement prepareStatement(PyObject sql, PyObject maxRows, boolean pre
414414
}
415415

416416
if (maxRows != Py.None) {
417-
stmt.statement.setMaxRows(((PyInteger)maxRows.__int__()).getValue());
417+
stmt.statement.setMaxRows(maxRows.asInt());
418418
}
419419
} catch (AbstractMethodError e) {
420420
throw zxJDBC.makeException(zxJDBC.NotSupportedError, zxJDBC.getString("nodynamiccursors"));
@@ -457,7 +457,7 @@ public void callproc(PyObject name, final PyObject params, PyObject bindings, Py
457457
Statement stmt = procedure.prepareCall(this.rsType, this.rsConcur);
458458

459459
if (maxRows != Py.None) {
460-
stmt.setMaxRows(((PyInteger)maxRows.__int__()).getValue());
460+
stmt.setMaxRows(maxRows.asInt());
461461
}
462462

463463
// get the bindings per the stored proc spec
@@ -897,7 +897,7 @@ public PyObject __call__(PyObject arg) {
897897
PyCursor cursor = (PyCursor)__self__;
898898
switch (index) {
899899
case 0 :
900-
return cursor.fetchmany(((PyInteger)arg.__int__()).getValue());
900+
return cursor.fetchmany(arg.asInt());
901901
case 5 :
902902
cursor.execute(arg, Py.None, Py.None, Py.None);
903903
return Py.None;
@@ -911,7 +911,7 @@ public PyObject __call__(PyObject arg) {
911911
cursor.executemany(arg, Py.None, Py.None, Py.None);
912912
return Py.None;
913913
case 10 :
914-
cursor.scroll(((PyInteger)arg.__int__()).getValue(), "relative");
914+
cursor.scroll(arg.asInt(), "relative");
915915
return Py.None;
916916
case 11 :
917917
cursor.execute(arg, Py.None, Py.None, Py.None);
@@ -938,7 +938,7 @@ public PyObject __call__(PyObject arga, PyObject argb) {
938938
cursor.executemany(arga, argb, Py.None, Py.None);
939939
return Py.None;
940940
case 10 :
941-
cursor.scroll(((PyInteger)arga.__int__()).getValue(), argb.toString());
941+
cursor.scroll(arga.asInt(), argb.toString());
942942
return Py.None;
943943
default :
944944
throw info.unexpectedCall(2, false);

src/com/ziclix/python/sql/PyStatement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ private void prepare(PyCursor cursor, PyObject params, PyObject bindings) throws
300300

301301
if (binding != null) {
302302
try {
303-
int bindingValue = ((PyInteger)binding.__int__()).getValue();
303+
int bindingValue = binding.asInt();
304304

305305
datahandler.setJDBCObject(preparedStatement, column, param, bindingValue);
306306
} catch (PyException e) {

src/org/python/core/PyArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ else if (obj instanceof PyString) {
438438
return s.toString().charAt(0);
439439
}
440440
else if (obj.__nonzero__()) {
441-
return ((PyInteger)obj.__int__()).getValue();
441+
return obj.asInt();
442442
}
443443
else {
444444
return -1;

src/org/python/core/PyString.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2953,7 +2953,9 @@ else if (arg instanceof PyInteger || arg instanceof PyFloat) {
29532953
}
29542954
int val;
29552955
try {
2956-
val = ((PyInteger)arg.__int__()).getValue();
2956+
// Explicitly __int__ so we can look for an AttributeError (which is
2957+
// less invasive to mask than a TypeError)
2958+
val = arg.__int__().asInt();
29572959
} catch (PyException e){
29582960
if (Py.matchException(e, Py.AttributeError)) {
29592961
throw Py.TypeError("%c requires int or char");

src/org/python/modules/imp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public static PyObject find_module(String name, PyObject path) {
180180
public static PyObject load_module(String name, PyObject file, PyObject filename, PyTuple data) {
181181
PyObject mod = Py.None;
182182
PySystemState sys = Py.getSystemState();
183-
int type = ((PyInteger)data.__getitem__(2).__int__()).getValue();
183+
int type = data.__getitem__(2).asInt();
184184
while(mod == Py.None) {
185185
Object o = file.__tojava__(InputStream.class);
186186
if (o == Py.NoConversion) {

src/org/python/modules/struct.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ void doUnpack(ByteStream buf, int count, PyList list) {
329329

330330
int get_int(PyObject value) {
331331
try {
332-
return ((PyInteger)value.__int__()).getValue();
332+
return value.asInt();
333333
} catch (PyException ex) {
334334
throw StructError("required argument is not an integer");
335335
}

src/org/python/modules/thread/thread.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public static long stack_size(PyObject[] args) {
101101
return stack_size;
102102
case 1:
103103
long old_stack_size = stack_size;
104-
int proposed_stack_size = ((PyInteger)args[0].__int__()).getValue();
104+
int proposed_stack_size = args[0].asInt();
105105
if (proposed_stack_size != 0 && proposed_stack_size < 32768) {
106106
// as specified by Python, Java quietly ignores what
107107
// it considers are too small

src/org/python/modules/time/Time.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private static void throwValueError(String msg) {
154154
private static int item(PyTuple tup, int i) {
155155
// knows about and asserts format on tuple items. See
156156
// documentation for Python's time module for details.
157-
int val = ((PyInteger)tup.__getitem__(i).__int__()).getValue();
157+
int val = tup.__getitem__(i).asInt();
158158
boolean valid = true;
159159
switch (i) {
160160
case 0: break; // year

0 commit comments

Comments
 (0)