diff --git a/python/common/org/python/types/Bytes.java b/python/common/org/python/types/Bytes.java index 2378b2fedf..15b45ce4fa 100644 --- a/python/common/org/python/types/Bytes.java +++ b/python/common/org/python/types/Bytes.java @@ -484,17 +484,21 @@ public org.python.Object __getitem__(org.python.Object index) { } else if (index instanceof org.python.types.Bool || index instanceof org.python.types.Int) { return this.__getitem__index(index); } else { - org.python.Object index_object; + org.python.Object index_object = null; + boolean error_caught = false; try { index_object = index.__index__(); - } catch (org.python.exceptions.TypeError | org.python.exceptions.AttributeError error) { + } catch (org.python.exceptions.TypeError error) { + error_caught = true; + } catch (org.python.exceptions.AttributeError error) { + error_caught = true; + } + if (error_caught) { + String message = "byte indices must be integers or slices, not " + index.typeName(); if (org.Python.VERSION < 0x03050000) { throw new org.python.exceptions.TypeError("byte indices must be integers, not " + index.typeName()); - } else { - throw new org.python.exceptions.TypeError( - "byte indices must be integers or slices, not " + index.typeName() - ); } + throw new org.python.exceptions.TypeError(message); } if (index_object instanceof org.python.types.Int) { return this.__getitem__index(index); diff --git a/python/common/org/python/types/Slice.java b/python/common/org/python/types/Slice.java index 13053abb3b..ac29662ac6 100644 --- a/python/common/org/python/types/Slice.java +++ b/python/common/org/python/types/Slice.java @@ -21,10 +21,16 @@ public Slice(org.python.Object start, org.python.Object stop, org.python.Object } else if (start instanceof org.python.types.NoneType) { this.__dict__.put("start", start); } else { - org.python.Object index_object; + org.python.Object index_object = null; + boolean error_caught = false; try { index_object = start.__index__(); - } catch (org.python.exceptions.TypeError | org.python.exceptions.AttributeError error) { + } catch (org.python.exceptions.TypeError error) { + error_caught = true; + } catch (org.python.exceptions.AttributeError error) { + error_caught = true; + } + if (error_caught) { throw new org.python.exceptions.TypeError("slice indices must be integers or None or have an __index__ method"); } if (index_object instanceof org.python.types.Int) { @@ -41,10 +47,16 @@ public Slice(org.python.Object start, org.python.Object stop, org.python.Object } else if (stop instanceof org.python.types.NoneType) { this.__dict__.put("stop", stop); } else { - org.python.Object index_object; + org.python.Object index_object = null; + boolean error_caught = false; try { index_object = stop.__index__(); - } catch (org.python.exceptions.TypeError | org.python.exceptions.AttributeError error) { + } catch (org.python.exceptions.TypeError error) { + error_caught = true; + } catch (org.python.exceptions.AttributeError error) { + error_caught = true; + } + if (error_caught) { throw new org.python.exceptions.TypeError("slice indices must be integers or None or have an __index__ method"); } if (index_object instanceof org.python.types.Int) { @@ -65,10 +77,16 @@ public Slice(org.python.Object start, org.python.Object stop, org.python.Object } else if (step instanceof org.python.types.NoneType) { this.__dict__.put("step", step); } else { - org.python.Object index_object; + org.python.Object index_object = null; + boolean error_caught = false; try { index_object = step.__index__(); - } catch (org.python.exceptions.TypeError | org.python.exceptions.AttributeError error) { + } catch (org.python.exceptions.TypeError error) { + error_caught = true; + } catch (org.python.exceptions.AttributeError error) { + error_caught = true; + } + if (error_caught) { throw new org.python.exceptions.TypeError("slice indices must be integers or None or have an __index__ method"); } if (index_object instanceof org.python.types.Int) {