From: <zy...@us...> - 2010-08-21 03:42:56
|
Revision: 7099 http://jython.svn.sourceforge.net/jython/?rev=7099&view=rev Author: zyasoft Date: 2010-08-21 03:42:48 +0000 (Sat, 21 Aug 2010) Log Message: ----------- Standard refactoring of some core Python types so that their implementation (including subclasses) do not access the underlying backing field, but uses an accessor. More fixes for #1645. In addition, there's support for defaultdict providing atomic construction of values for associated keys, using Guava MapMaker's makeComputingMap. This is a rather cool example of the additional abstraction provided by the refactoring (namely PyDictionary#getMap). Some tests are needed to demo how sweet this really is :) Note that we cannot, given our constructors calling super in a chain, readily remove extraneous fields that are not used, either through the use of abstract classes to share implementation; or violate safe publication by calling it later, outside the constructor. (There are in fact tests that fail without safe publication.) But this is a minor concern. Modified Paths: -------------- trunk/jython/src/org/python/core/FilelikeInputStream.java trunk/jython/src/org/python/core/JavaImportHelper.java trunk/jython/src/org/python/core/PyDictionary.java trunk/jython/src/org/python/core/PyFile.java trunk/jython/src/org/python/core/PyFileReader.java trunk/jython/src/org/python/core/PyFileWriter.java trunk/jython/src/org/python/core/PyFloat.java trunk/jython/src/org/python/core/PyInteger.java trunk/jython/src/org/python/core/PyObject.java trunk/jython/src/org/python/core/PyString.java trunk/jython/src/org/python/core/PyUnicode.java trunk/jython/src/org/python/core/StdoutWrapper.java trunk/jython/src/org/python/core/__builtin__.java trunk/jython/src/org/python/core/codecs.java trunk/jython/src/org/python/modules/_collections/PyDefaultDict.java Modified: trunk/jython/src/org/python/core/FilelikeInputStream.java =================================================================== --- trunk/jython/src/org/python/core/FilelikeInputStream.java 2010-08-21 03:30:41 UTC (rev 7098) +++ trunk/jython/src/org/python/core/FilelikeInputStream.java 2010-08-21 03:42:48 UTC (rev 7099) @@ -31,8 +31,8 @@ } else if(len == 0) { return 0; } - String result = ((PyString)filelike.__getattr__("read") - .__call__(new PyInteger(len))).string; + String result = ((PyString) filelike.__getattr__("read") + .__call__(new PyInteger(len))).getString(); if(result.length() == 0) { return -1; } Modified: trunk/jython/src/org/python/core/JavaImportHelper.java =================================================================== --- trunk/jython/src/org/python/core/JavaImportHelper.java 2010-08-21 03:30:41 UTC (rev 7098) +++ trunk/jython/src/org/python/core/JavaImportHelper.java 2010-08-21 03:42:48 UTC (rev 7099) @@ -117,7 +117,7 @@ while (iterator.hasNext()) { Object obj = iterator.next(); if (obj instanceof PyString) { - obj = ((PyString)obj).string; + obj = ((PyString) obj).getString(); } if (obj instanceof String) { String fromName = (String) obj; Modified: trunk/jython/src/org/python/core/PyDictionary.java =================================================================== --- trunk/jython/src/org/python/core/PyDictionary.java 2010-08-21 03:30:41 UTC (rev 7098) +++ trunk/jython/src/org/python/core/PyDictionary.java 2010-08-21 03:42:48 UTC (rev 7099) @@ -31,8 +31,12 @@ public static final PyType TYPE = PyType.fromClass(PyDictionary.class); - protected final ConcurrentMap<PyObject, PyObject> table; + private final ConcurrentMap<PyObject, PyObject> map; + public ConcurrentMap<PyObject, PyObject> getMap() { + return map; + } + /** * Create an empty dictionary. */ @@ -45,7 +49,7 @@ */ public PyDictionary(PyType type, int capacity) { super(type); - table = new ConcurrentHashMap<PyObject, PyObject>(capacity, Generic.CHM_LOAD_FACTOR, + map = new ConcurrentHashMap<PyObject, PyObject>(capacity, Generic.CHM_LOAD_FACTOR, Generic.CHM_CONCURRENCY_LEVEL); } @@ -54,7 +58,7 @@ */ public PyDictionary(PyType type) { super(type); - table = Generic.concurrentMap(); + map = Generic.concurrentMap(); } /** @@ -65,14 +69,27 @@ } /** - * Create a new derived dictionary which is based on the given map. + * Create a new dictionary which is populated with entries the given map. */ public PyDictionary(PyType type, Map<PyObject, PyObject> map) { this(type, Math.max((int) (map.size() / Generic.CHM_LOAD_FACTOR) + 1, Generic.CHM_INITIAL_CAPACITY)); - table.putAll(map); + this.map.putAll(map); } + /** + * Create a new dictionary without initializing table. Used for dictionary + * factories, with different backing maps, at the cost that it prevents us from making table be final. + */ + // TODO we may want to revisit this API, but our chain calling of super makes this tough + protected PyDictionary(PyType type, boolean initializeBacking) { + super(type); + if (initializeBacking) { + map = Generic.concurrentMap(); + } else { + map = null; // for later initialization + } + } /** * Create a new dictionary with the element as content. @@ -84,7 +101,7 @@ public PyDictionary(PyObject elements[]) { this(); for (int i = 0; i < elements.length; i += 2) { - table.put(elements[i], elements[i + 1]); + map.put(elements[i], elements[i + 1]); } } @@ -103,7 +120,7 @@ } @ExposedClassMethod(defaults = "Py.None", doc = BuiltinDocs.dict_fromkeys_doc) - final static PyObject dict_fromkeys(PyType type, PyObject keys, PyObject value) { + static PyObject dict_fromkeys(PyType type, PyObject keys, PyObject value) { PyObject d = type.__call__(); for (PyObject o : keys.asIterable()) { d.__setitem__(o, value); @@ -118,12 +135,12 @@ @ExposedMethod(doc = BuiltinDocs.dict___len___doc) final int dict___len__() { - return table.size(); + return getMap().size(); } @Override public boolean __nonzero__() { - return table.size() != 0; + return getMap().size() != 0; } @Override @@ -133,12 +150,12 @@ @Override public PyObject __finditem__(PyObject key) { - return table.get(key); + return getMap().get(key); } @ExposedMethod(doc = BuiltinDocs.dict___getitem___doc) protected final PyObject dict___getitem__(PyObject key) { - PyObject result = table.get(key); + PyObject result = getMap().get(key); if (result != null) { return result; } @@ -161,7 +178,7 @@ @ExposedMethod(doc = BuiltinDocs.dict___setitem___doc) final void dict___setitem__(PyObject key, PyObject value) { - table.put(key, value); + getMap().put(key, value); } @Override @@ -171,7 +188,7 @@ @ExposedMethod(doc = BuiltinDocs.dict___delitem___doc) final void dict___delitem__(PyObject key) { - Object ret = table.remove(key); + Object ret = getMap().remove(key); if (ret == null) { throw Py.KeyError(key.toString()); } @@ -200,7 +217,7 @@ } StringBuilder buf = new StringBuilder("{"); - for (Entry<PyObject, PyObject> entry : table.entrySet()) { + for (Entry<PyObject, PyObject> entry : getMap().entrySet()) { buf.append((entry.getKey()).__repr__().toString()); buf.append(": "); buf.append((entry.getValue()).__repr__().toString()); @@ -229,8 +246,8 @@ return null; } PyDictionary other = (PyDictionary)otherObj; - int an = table.size(); - int bn = other.table.size(); + int an = getMap().size(); + int bn = other.getMap().size(); if (an != bn) { return Py.False; } @@ -314,8 +331,8 @@ return -2; } PyDictionary other = (PyDictionary)otherObj; - int an = table.size(); - int bn = other.table.size(); + int an = getMap().size(); + int bn = other.getMap().size(); if (an < bn) { return -1; } @@ -364,7 +381,7 @@ @ExposedMethod(doc = BuiltinDocs.dict_has_key_doc) final boolean dict_has_key(PyObject key) { - return table.containsKey(key); + return getMap().containsKey(key); } @Override @@ -390,7 +407,7 @@ @ExposedMethod(defaults = "Py.None", doc = BuiltinDocs.dict_get_doc) final PyObject dict_get(PyObject key, PyObject defaultObj) { - PyObject o = table.get(key); + PyObject o = getMap().get(key); return o == null ? defaultObj : o; } @@ -413,7 +430,7 @@ @ExposedMethod(doc = BuiltinDocs.dict_copy_doc) final PyDictionary dict_copy() { - return new PyDictionary(table); // no need to clone() + return new PyDictionary(getMap()); // no need to clone() } /** @@ -425,7 +442,7 @@ @ExposedMethod(doc = BuiltinDocs.dict_clear_doc) final void dict_clear() { - table.clear(); + getMap().clear(); } /** @@ -479,7 +496,7 @@ */ private void merge(PyObject other) { if (other instanceof PyDictionary) { - table.putAll(((PyDictionary)other).table); + getMap().putAll(((PyDictionary) other).getMap()); } else if (other instanceof PyStringMap) { mergeFromKeys(other, ((PyStringMap)other).keys()); } else { @@ -552,14 +569,14 @@ @ExposedMethod(defaults = "Py.None", doc = BuiltinDocs.dict_setdefault_doc) final PyObject dict_setdefault(PyObject key, PyObject failobj) { - PyObject oldValue = table.putIfAbsent(key, failobj); + PyObject oldValue = getMap().putIfAbsent(key, failobj); return oldValue == null ? failobj : oldValue; } // XXX: needs __doc__ but CPython does not define setifabsent @ExposedMethod(defaults = "Py.None") final PyObject dict_setifabsent(PyObject key, PyObject failobj) { - PyObject oldValue = table.putIfAbsent(key, failobj); + PyObject oldValue = getMap().putIfAbsent(key, failobj); return oldValue == null ? Py.None : oldValue; } @@ -582,13 +599,13 @@ @ExposedMethod(defaults = "null", doc = BuiltinDocs.dict_pop_doc) final PyObject dict_pop(PyObject key, PyObject defaultValue) { - if (!table.containsKey(key)) { + if (!map.containsKey(key)) { if (defaultValue == null) { throw Py.KeyError("popitem(): dictionary is empty"); } return defaultValue; } - return table.remove(key); + return getMap().remove(key); } @@ -602,7 +619,7 @@ @ExposedMethod(doc = BuiltinDocs.dict_popitem_doc) final PyObject dict_popitem() { - Iterator<Entry<PyObject, PyObject>> it = table.entrySet().iterator(); + Iterator<Entry<PyObject, PyObject>> it = getMap().entrySet().iterator(); if (!it.hasNext()) { throw Py.KeyError("popitem(): dictionary is empty"); } @@ -622,8 +639,8 @@ @ExposedMethod(doc = BuiltinDocs.dict_items_doc) final PyList dict_items() { - List<PyObject> list = new ArrayList<PyObject>(table.size()); - for (Entry<PyObject, PyObject> entry : table.entrySet()) { + List<PyObject> list = new ArrayList<PyObject>(getMap().size()); + for (Entry<PyObject, PyObject> entry : getMap().entrySet()) { list.add(new PyTuple(entry.getKey(), entry.getValue())); } return PyList.fromList(list); @@ -638,12 +655,12 @@ @ExposedMethod(doc = BuiltinDocs.dict_keys_doc) final PyList dict_keys() { - return PyList.fromList(new ArrayList<PyObject>(table.keySet())); + return PyList.fromList(new ArrayList<PyObject>(getMap().keySet())); } @ExposedMethod(doc = BuiltinDocs.dict_values_doc) final PyList dict_values() { - return PyList.fromList(new ArrayList<PyObject>(table.values())); + return PyList.fromList(new ArrayList<PyObject>(getMap().values())); } /** @@ -655,7 +672,7 @@ @ExposedMethod(doc = BuiltinDocs.dict_iteritems_doc) final PyObject dict_iteritems() { - return new ItemsIter(table.entrySet()); + return new ItemsIter(getMap().entrySet()); } /** @@ -667,7 +684,7 @@ @ExposedMethod(doc = BuiltinDocs.dict_iterkeys_doc) final PyObject dict_iterkeys() { - return new ValuesIter(table.keySet()); + return new ValuesIter(getMap().keySet()); } /** @@ -679,7 +696,7 @@ @ExposedMethod(doc = BuiltinDocs.dict_itervalues_doc) final PyObject dict_itervalues() { - return new ValuesIter(table.values()); + return new ValuesIter(getMap().values()); } @Override @@ -687,6 +704,21 @@ return dict___hash__(); } + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final PyDictionary other = (PyDictionary) obj; + if (this.map != other.map && (this.map == null || !this.map.equals(other.map))) { + return false; + } + return true; + } + @ExposedMethod(doc = BuiltinDocs.dict___hash___doc) final int dict___hash__() { throw Py.TypeError(String.format("unhashable type: '%.200s'", getType().fastGetName())); @@ -715,7 +747,7 @@ @Override public PyObject __iternext__() { - if (table.size() != size) { + if (getMap().size() != size) { throw Py.RuntimeError("dictionary changed size during iteration"); } if (!iterator.hasNext()) { @@ -738,7 +770,7 @@ @Override public PyObject __iternext__() { - if (table.size() != size) { + if (getMap().size() != size) { throw Py.RuntimeError("dictionary changed size during iteration"); } if (!iterator.hasNext()) { @@ -757,81 +789,81 @@ */ /** @see java.util.Map#entrySet() */ public Set entrySet() { - return new PyMapEntrySet(table.entrySet()); + return new PyMapEntrySet(getMap().entrySet()); } /** @see java.util.Map#keySet() */ public Set keySet() { - return new PyMapKeyValSet(table.keySet()); + return new PyMapKeyValSet(getMap().keySet()); } /** @see java.util.Map#values() */ public Collection values() { - return new PyMapKeyValSet(table.values()); + return new PyMapKeyValSet(getMap().values()); } /** @see java.util.Map#putAll(Map map) */ public void putAll(Map map) { for (Object o : map.entrySet()) { Entry entry = (Entry)o; - table.put(Py.java2py(entry.getKey()), Py.java2py(entry.getValue())); + getMap().put(Py.java2py(entry.getKey()), Py.java2py(entry.getValue())); } } /** @see java.util.Map#remove(Object key) */ public Object remove(Object key) { - return tojava(table.remove(Py.java2py(key))); + return tojava(getMap().remove(Py.java2py(key))); } /** @see java.util.Map#put(Object key, Object value) */ public Object put(Object key, Object value) { - return tojava(table.put(Py.java2py(key), Py.java2py(value))); + return tojava(getMap().put(Py.java2py(key), Py.java2py(value))); } /** @see java.util.Map#get(Object key) */ public Object get(Object key) { - return tojava(table.get(Py.java2py(key))); + return tojava(getMap().get(Py.java2py(key))); } /** @see java.util.Map#containsValue(Object key) */ public boolean containsValue(Object value) { - return table.containsValue(Py.java2py(value)); + return getMap().containsValue(Py.java2py(value)); } /** @see java.util.Map#containsValue(Object key) */ public boolean containsKey(Object key) { - return table.containsKey(Py.java2py(key)); + return getMap().containsKey(Py.java2py(key)); } /** @see java.util.Map#isEmpty() */ public boolean isEmpty() { - return table.isEmpty(); + return getMap().isEmpty(); } /** @see java.util.Map#size() */ public int size() { - return table.size(); + return getMap().size(); } /** Convert return values to java objects */ - final static Object tojava(Object val) { + static Object tojava(Object val) { return val == null ? null : ((PyObject)val).__tojava__(Object.class); } public Object putIfAbsent(Object key, Object value) { - return tojava(table.putIfAbsent(Py.java2py(key), Py.java2py(value))); + return tojava(getMap().putIfAbsent(Py.java2py(key), Py.java2py(value))); } public boolean remove(Object key, Object value) { - return table.remove(Py.java2py(key), Py.java2py(value)); + return getMap().remove(Py.java2py(key), Py.java2py(value)); } public boolean replace(Object key, Object oldValue, Object newValue) { - return table.replace(Py.java2py(key), Py.java2py(oldValue), Py.java2py(newValue)); + return getMap().replace(Py.java2py(key), Py.java2py(oldValue), Py.java2py(newValue)); } public Object replace(Object key, Object value) { - return tojava(table.replace(Py.java2py(key), Py.java2py(value))); + return tojava(getMap().replace(Py.java2py(key), Py.java2py(value))); } } @@ -926,6 +958,11 @@ return new SimpleEntry(key, value); } + @Override + public int hashCode() { + return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode()); + } + } /** Modified: trunk/jython/src/org/python/core/PyFile.java =================================================================== --- trunk/jython/src/org/python/core/PyFile.java 2010-08-21 03:30:41 UTC (rev 7098) +++ trunk/jython/src/org/python/core/PyFile.java 2010-08-21 03:42:48 UTC (rev 7099) @@ -389,7 +389,7 @@ if (obj instanceof PyUnicode) { return ((PyUnicode)obj).encode(); } else if (obj instanceof PyString) { - return ((PyString)obj).string; + return ((PyString) obj).getString(); } else if (binary && obj instanceof PyArray) { return ((PyArray)obj).tostring(); } Modified: trunk/jython/src/org/python/core/PyFileReader.java =================================================================== --- trunk/jython/src/org/python/core/PyFileReader.java 2010-08-21 03:30:41 UTC (rev 7098) +++ trunk/jython/src/org/python/core/PyFileReader.java 2010-08-21 03:42:48 UTC (rev 7099) @@ -155,7 +155,7 @@ int size = 0; do { final PyString line = readline(-1); - int len = line.string.length(); + int len = line.getString().length(); if (len == 0) { // EOF break; Modified: trunk/jython/src/org/python/core/PyFileWriter.java =================================================================== --- trunk/jython/src/org/python/core/PyFileWriter.java 2010-08-21 03:30:41 UTC (rev 7098) +++ trunk/jython/src/org/python/core/PyFileWriter.java 2010-08-21 03:42:48 UTC (rev 7099) @@ -51,9 +51,9 @@ public void write(PyObject o) { if (o instanceof PyUnicode) { - write(((PyUnicode) o).string); + write(((PyUnicode) o).getString()); } else if (o instanceof PyString) { - write(((PyString) o).string); + write(((PyString) o).getString()); } else { throw Py.TypeError("write requires a string as its argument"); } Modified: trunk/jython/src/org/python/core/PyFloat.java =================================================================== --- trunk/jython/src/org/python/core/PyFloat.java 2010-08-21 03:30:41 UTC (rev 7098) +++ trunk/jython/src/org/python/core/PyFloat.java 2010-08-21 03:42:48 UTC (rev 7099) @@ -268,11 +268,11 @@ } } - private static final boolean canCoerce(PyObject other) { + private static boolean canCoerce(PyObject other) { return other instanceof PyFloat || other instanceof PyInteger || other instanceof PyLong; } - private static final double coerce(PyObject other) { + private static double coerce(PyObject other) { if (other instanceof PyFloat) { return ((PyFloat) other).getValue(); } else if (other instanceof PyInteger) { Modified: trunk/jython/src/org/python/core/PyInteger.java =================================================================== --- trunk/jython/src/org/python/core/PyInteger.java 2010-08-21 03:30:41 UTC (rev 7098) +++ trunk/jython/src/org/python/core/PyInteger.java 2010-08-21 03:42:48 UTC (rev 7099) @@ -33,7 +33,7 @@ @Deprecated public static final BigInteger maxInt = MAX_INT; - private int value; + private final int value; public PyInteger(PyType subType, int v) { super(subType); Modified: trunk/jython/src/org/python/core/PyObject.java =================================================================== --- trunk/jython/src/org/python/core/PyObject.java 2010-08-21 03:30:41 UTC (rev 7098) +++ trunk/jython/src/org/python/core/PyObject.java 2010-08-21 03:42:48 UTC (rev 7099) @@ -26,7 +26,7 @@ public static final PyType TYPE = PyType.fromClass(PyObject.class); /** The type of this object. */ - PyType objtype; + protected PyType objtype; /** * An underlying Java instance that this object is wrapping or is a subclass Modified: trunk/jython/src/org/python/core/PyString.java =================================================================== --- trunk/jython/src/org/python/core/PyString.java 2010-08-21 03:30:41 UTC (rev 7098) +++ trunk/jython/src/org/python/core/PyString.java 2010-08-21 03:42:48 UTC (rev 7099) @@ -17,9 +17,13 @@ public class PyString extends PyBaseString { public static final PyType TYPE = PyType.fromClass(PyString.class); - protected String string; + protected String string; // cannot make final because of Python intern support protected transient boolean interned=false; + public String getString() { + return string; + } + // for PyJavaClass.init() public PyString() { this(TYPE, ""); @@ -57,7 +61,7 @@ } @ExposedNew - final static PyObject str_new(PyNewWrapper new_, boolean init, PyType subtype, + static PyObject str_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords) { ArgParser ap = new ArgParser("str", args, keywords, new String[] { "object" }, 0); PyObject S = ap.getPyObject(0, null); @@ -75,18 +79,19 @@ } public int[] toCodePoints() { - int n = string.length(); + int n = getString().length(); int[] codePoints = new int[n]; for (int i = 0; i < n; i++) { - codePoints[i] = string.charAt(i); + codePoints[i] = getString().charAt(i); } return codePoints; } public String substring(int start, int end) { - return string.substring(start, end); + return getString().substring(start, end); } + @Override public PyString __str__() { return str___str__(); } @@ -96,43 +101,47 @@ if (getClass() == PyString.class) { return this; } - return new PyString(string); + return new PyString(getString()); } + @Override public PyUnicode __unicode__() { return new PyUnicode(this); } + @Override public int __len__() { return str___len__(); } @ExposedMethod(doc = BuiltinDocs.str___len___doc) final int str___len__() { - return string.length(); + return getString().length(); } + @Override public String toString() { - return string; + return getString(); } public String internedString() { if (interned) - return string; + return getString(); else { - string = string.intern(); + string = getString().intern(); interned = true; - return string; + return getString(); } } + @Override public PyString __repr__() { return str___repr__(); } @ExposedMethod(doc = BuiltinDocs.str___repr___doc) final PyString str___repr__() { - return new PyString(encode_UnicodeEscape(string, true)); + return new PyString(encode_UnicodeEscape(getString(), true)); } private static char[] hexdigit = "0123456789abcdef".toCharArray(); @@ -475,6 +484,7 @@ return seq___getslice__(start, stop, step); } + @Override public int __cmp__(PyObject other) { return str___cmp__(other); } @@ -484,10 +494,11 @@ if (!(other instanceof PyString)) return -2; - int c = string.compareTo(((PyString)other).string); + int c = getString().compareTo(((PyString) other).getString()); return c < 0 ? -1 : c > 0 ? 1 : 0; } + @Override public PyObject __eq__(PyObject other) { return str___eq__(other); } @@ -497,9 +508,10 @@ String s = coerce(other); if (s == null) return null; - return string.equals(s) ? Py.True : Py.False; + return getString().equals(s) ? Py.True : Py.False; } + @Override public PyObject __ne__(PyObject other) { return str___ne__(other); } @@ -509,9 +521,10 @@ String s = coerce(other); if (s == null) return null; - return string.equals(s) ? Py.False : Py.True; + return getString().equals(s) ? Py.False : Py.True; } + @Override public PyObject __lt__(PyObject other) { return str___lt__(other); } @@ -521,9 +534,10 @@ String s = coerce(other); if (s == null) return null; - return string.compareTo(s) < 0 ? Py.True : Py.False; + return getString().compareTo(s) < 0 ? Py.True : Py.False; } + @Override public PyObject __le__(PyObject other) { return str___le__(other); } @@ -533,9 +547,10 @@ String s = coerce(other); if (s == null) return null; - return string.compareTo(s) <= 0 ? Py.True : Py.False; + return getString().compareTo(s) <= 0 ? Py.True : Py.False; } + @Override public PyObject __gt__(PyObject other) { return str___gt__(other); } @@ -545,9 +560,10 @@ String s = coerce(other); if (s == null) return null; - return string.compareTo(s) > 0 ? Py.True : Py.False; + return getString().compareTo(s) > 0 ? Py.True : Py.False; } + @Override public PyObject __ge__(PyObject other) { return str___ge__(other); } @@ -557,7 +573,7 @@ String s = coerce(other); if (s == null) return null; - return string.compareTo(s) >= 0 ? Py.True : Py.False; + return getString().compareTo(s) >= 0 ? Py.True : Py.False; } private static String coerce(PyObject o) { @@ -566,13 +582,29 @@ return null; } + @Override public int hashCode() { return str___hash__(); } +// @Override +// public boolean equals(Object obj) { +// if (obj == null) { +// return false; +// } +// if (getClass() != obj.getClass()) { +// return false; +// } +// final PyString other = (PyString) obj; +// if ((this.string == null) ? (other.string != null) : !this.string.equals(other.string)) { +// return false; +// } +// return true; +// } + @ExposedMethod(doc = BuiltinDocs.str___hash___doc) final int str___hash__() { - return string.hashCode(); + return getString().hashCode(); } /** @@ -581,23 +613,24 @@ * corresponding char. */ public byte[] toBytes() { - return StringUtil.toBytes(string); + return StringUtil.toBytes(getString()); } + @Override public Object __tojava__(Class<?> c) { if (c.isAssignableFrom(String.class)) { - return string; + return getString(); } if (c == Character.TYPE || c == Character.class) - if (string.length() == 1) - return new Character(string.charAt(0)); + if (getString().length() == 1) + return new Character(getString().charAt(0)); if (c.isArray()) { if (c.getComponentType() == Byte.TYPE) return toBytes(); if (c.getComponentType() == Character.TYPE) - return string.toCharArray(); + return getString().toCharArray(); } if (c.isInstance(this)) @@ -607,7 +640,7 @@ } protected PyObject pyget(int i) { - return Py.newString(string.charAt(i)); + return Py.newString(getString().charAt(i)); } protected PyObject getslice(int start, int stop, int step) { @@ -620,7 +653,7 @@ char new_chars[] = new char[n]; int j = 0; for (int i=start; j<n; i+=step) - new_chars[j++] = string.charAt(i); + new_chars[j++] = getString().charAt(i); return createInstance(new String(new_chars), true); } @@ -635,6 +668,7 @@ return new PyString(str); } + @Override public boolean __contains__(PyObject o) { return str___contains__(o); } @@ -644,14 +678,14 @@ if (!(o instanceof PyString)) throw Py.TypeError("'in <string>' requires string as left operand"); PyString other = (PyString) o; - return string.indexOf(other.string) >= 0; + return getString().indexOf(other.getString()) >= 0; } protected PyObject repeat(int count) { if(count < 0) { count = 0; } - int s = string.length(); + int s = getString().length(); if((long)s * count > Integer.MAX_VALUE) { // Since Strings store their data in an array, we can't make one // longer than Integer.MAX_VALUE. Without this check we get @@ -661,7 +695,7 @@ } char new_chars[] = new char[s * count]; for(int i = 0; i < count; i++) { - string.getChars(0, s, new_chars, i * s); + getString().getChars(0, s, new_chars, i * s); } return createInstance(new String(new_chars)); } @@ -692,6 +726,7 @@ return repeat(o.asIndex(Py.OverflowError)); } + @Override public PyObject __add__(PyObject other) { return str___add__(other); } @@ -703,30 +738,33 @@ } if (other instanceof PyString) { PyString otherStr = (PyString)other; - return new PyString(string.concat(otherStr.string)); + return new PyString(getString().concat(otherStr.getString())); } return null; } @ExposedMethod(doc = BuiltinDocs.str___getnewargs___doc) final PyTuple str___getnewargs__() { - return new PyTuple(new PyString(this.string)); + return new PyTuple(new PyString(this.getString())); } + @Override public PyTuple __getnewargs__() { return str___getnewargs__(); } + @Override public PyObject __mod__(PyObject other) { return str___mod__(other); } @ExposedMethod(doc = BuiltinDocs.str___mod___doc) public PyObject str___mod__(PyObject other){ - StringFormatter fmt = new StringFormatter(string, false); + StringFormatter fmt = new StringFormatter(getString(), false); return fmt.format(other); } + @Override public PyObject __int__() { try { @@ -739,27 +777,33 @@ } } + @Override public PyObject __long__() { return atol(10); } + @Override public PyFloat __float__() { return new PyFloat(atof()); } + @Override public PyObject __pos__() { throw Py.TypeError("bad operand type for unary +"); } + @Override public PyObject __neg__() { throw Py.TypeError("bad operand type for unary -"); } + @Override public PyObject __invert__() { throw Py.TypeError("bad operand type for unary ~"); } @SuppressWarnings("fallthrough") + @Override public PyComplex __complex__() { boolean got_re = false; boolean got_im = false; @@ -767,8 +811,8 @@ boolean sw_error = false; int s = 0; - int n = string.length(); - while (s < n && Character.isSpaceChar(string.charAt(s))) + int n = getString().length(); + while (s < n && Character.isSpaceChar(getString().charAt(s))) s++; if (s == n) { @@ -781,7 +825,7 @@ int sign = 1; do { - char c = string.charAt(s); + char c = getString().charAt(s); switch (c) { case '-': sign = -1; @@ -793,7 +837,7 @@ } // a character is guaranteed, but it better be a digit // or J or j - c = string.charAt(++s); // eat the sign character + c = getString().charAt(++s); // eat the sign character // and check the next if (!Character.isDigit(c) && c!='J' && c!='j') sw_error = true; @@ -817,7 +861,7 @@ break; case ' ': - while (s < n && Character.isSpaceChar(string.charAt(s))) + while (s < n && Character.isSpaceChar(getString().charAt(s))) s++; if (s != n) sw_error = true; @@ -829,15 +873,15 @@ sw_error = true; break; } - int end = endDouble(string, s); - z = Double.valueOf(string.substring(s, end)).doubleValue(); + int end = endDouble(getString(),s); + z = Double.valueOf(getString().substring(s, end)).doubleValue(); if (z == Double.POSITIVE_INFINITY) { - throw Py.ValueError(String.format("float() out of range: %.150s", string)); + throw Py.ValueError(String.format("float() out of range: %.150s", getString())); } s=end; if (s < n) { - c = string.charAt(s); + c = getString().charAt(s); if (c == 'J' || c == 'j') { break; } @@ -861,7 +905,7 @@ if (sw_error) { throw Py.ValueError("malformed string for complex() " + - string.substring(s)); + getString().substring(s)); } return new PyComplex(x,y); @@ -895,7 +939,7 @@ @ExposedMethod(doc = BuiltinDocs.str_lower_doc) final String str_lower() { - return string.toLowerCase(); + return getString().toLowerCase(); } public String upper() { @@ -904,7 +948,7 @@ @ExposedMethod(doc = BuiltinDocs.str_upper_doc) final String str_upper() { - return string.toUpperCase(); + return getString().toUpperCase(); } public String title() { @@ -913,7 +957,7 @@ @ExposedMethod(doc = BuiltinDocs.str_title_doc) final String str_title() { - char[] chars = string.toCharArray(); + char[] chars = getString().toCharArray(); int n = chars.length; boolean previous_is_cased = false; @@ -940,7 +984,7 @@ @ExposedMethod(doc = BuiltinDocs.str_swapcase_doc) final String str_swapcase() { - char[] chars = string.toCharArray(); + char[] chars = getString().toCharArray(); int n=chars.length; for (int i=0; i<n; i++) { char c = chars[i]; @@ -964,7 +1008,7 @@ @ExposedMethod(defaults = "null", doc = BuiltinDocs.str_strip_doc) final String str_strip(String sep) { - char[] chars = string.toCharArray(); + char[] chars = getString().toCharArray(); int n=chars.length; int start=0; if (sep == null) @@ -984,7 +1028,7 @@ if (end >= start) { return (end < n-1 || start > 0) - ? string.substring(start, end+1) : string; + ? getString().substring(start, end+1) : getString(); } else { return ""; } @@ -1000,7 +1044,7 @@ @ExposedMethod(defaults = "null", doc = BuiltinDocs.str_lstrip_doc) final String str_lstrip(String sep) { - char[] chars = string.toCharArray(); + char[] chars = getString().toCharArray(); int n=chars.length; int start=0; if (sep == null) @@ -1010,7 +1054,7 @@ while (start < n && sep.indexOf(chars[start]) >= 0) start++; - return (start > 0) ? string.substring(start, n) : string; + return (start > 0) ? getString().substring(start, n) : getString(); } public String rstrip(String sep) { @@ -1019,7 +1063,7 @@ @ExposedMethod(defaults = "null", doc = BuiltinDocs.str_rstrip_doc) final String str_rstrip(String sep) { - char[] chars = string.toCharArray(); + char[] chars = getString().toCharArray(); int n=chars.length; int end=n-1; if (sep == null) @@ -1029,7 +1073,7 @@ while (end >= 0 && sep.indexOf(chars[end]) >= 0) end--; - return (end < n-1) ? string.substring(0, end+1) : string; + return (end < n-1) ? getString().substring(0, end+1) : getString(); } @@ -1056,7 +1100,7 @@ PyList list = new PyList(); - char[] chars = string.toCharArray(); + char[] chars = getString().toCharArray(); int n=chars.length; if (maxsplit < 0) @@ -1108,7 +1152,7 @@ } PyList list = new PyList(); - char[] chars = string.toCharArray(); + char[] chars = getString().toCharArray(); if (maxsplit < 0) { maxsplit = chars.length; @@ -1165,7 +1209,7 @@ if (sepObj instanceof PyUnicode) { return unicodePartition(sepObj); } else if (sepObj instanceof PyString) { - sep = ((PyString)sepObj).string; + sep = ((PyString) sepObj).getString(); } else { throw Py.TypeError("expected a character buffer object"); } @@ -1174,10 +1218,10 @@ throw Py.ValueError("empty separator"); } - int index = string.indexOf(sep); + int index = getString().indexOf(sep); if (index != -1) { return new PyTuple(fromSubstring(0, index), sepObj, - fromSubstring(index + sep.length(), string.length())); + fromSubstring(index + sep.length(), getString().length())); } else { return new PyTuple(this, Py.EmptyString, Py.EmptyString); } @@ -1185,7 +1229,7 @@ final PyTuple unicodePartition(PyObject sepObj) { PyUnicode strObj = __unicode__(); - String str = strObj.string; + String str = strObj.getString(); // Will throw a TypeError if not a basestring String sep = sepObj.asString(); @@ -1216,7 +1260,7 @@ if (sepObj instanceof PyUnicode) { return unicodePartition(sepObj); } else if (sepObj instanceof PyString) { - sep = ((PyString)sepObj).string; + sep = ((PyString) sepObj).getString(); } else { throw Py.TypeError("expected a character buffer object"); } @@ -1225,10 +1269,10 @@ throw Py.ValueError("empty separator"); } - int index = string.lastIndexOf(sep); + int index = getString().lastIndexOf(sep); if (index != -1) { return new PyTuple(fromSubstring(0, index), sepObj, - fromSubstring(index + sep.length(), string.length())); + fromSubstring(index + sep.length(), getString().length())); } else { return new PyTuple(Py.EmptyString, Py.EmptyString, this); } @@ -1236,7 +1280,7 @@ final PyTuple unicodeRpartition(PyObject sepObj) { PyUnicode strObj = __unicode__(); - String str = strObj.string; + String str = strObj.getString(); // Will throw a TypeError if not a basestring String sep = sepObj.asString(); @@ -1259,7 +1303,7 @@ private PyList splitfields(String sep, int maxsplit) { PyList list = new PyList(); - int length = string.length(); + int length = getString().length(); if (maxsplit < 0) maxsplit = length + 1; @@ -1268,12 +1312,12 @@ int sepLength = sep.length(); int index; if((sep.length() == 0) && (maxsplit != 0)) { - index = string.indexOf(sep, lastbreak); + index = getString().indexOf(sep, lastbreak); list.append(fromSubstring(lastbreak, index)); splits++; } while (splits < maxsplit) { - index = string.indexOf(sep, lastbreak); + index = getString().indexOf(sep, lastbreak); if (index == -1) break; if(sep.length() == 0) @@ -1291,7 +1335,7 @@ private PyList rsplitfields(String sep, int maxsplit) { PyList list = new PyList(); - int length = string.length(); + int length = getString().length(); if (maxsplit < 0) { maxsplit = length + 1; } @@ -1302,7 +1346,7 @@ int sepLength = sep.length(); while (index > 0 && splits < maxsplit) { - int i = string.lastIndexOf(sep, index - sepLength); + int i = getString().lastIndexOf(sep, index - sepLength); if (i == index) { i -= sepLength; } @@ -1331,7 +1375,7 @@ final PyList str_splitlines(boolean keepends) { PyList list = new PyList(); - char[] chars = string.toCharArray(); + char[] chars = getString().toCharArray(); int n=chars.length; int j = 0; @@ -1361,7 +1405,7 @@ } protected PyString fromSubstring(int begin, int end) { - return createInstance(string.substring(begin, end), true); + return createInstance(getString().substring(begin, end), true); } public int index(String sub) { @@ -1421,14 +1465,14 @@ int[] indices = translateIndices(start, end); int n = sub.length(); if(n == 0) { - if (start > string.length()) { + if (start > getString().length()) { return 0; } return indices[1] - indices[0] + 1; } int count = 0; while(true){ - int index = string.indexOf(sub, indices[0]); + int index = getString().indexOf(sub, indices[0]); indices[0] = index + n; if(indices[0] > indices[1] || index == -1) { break; @@ -1453,7 +1497,7 @@ @ExposedMethod(defaults = {"0", "null"}, doc = BuiltinDocs.str_find_doc) final int str_find(String sub, int start, PyObject end) { int[] indices = translateIndices(start, end); - int index = string.indexOf(sub, indices[0]); + int index = getString().indexOf(sub, indices[0]); if (index < start || index > indices[1]) { return -1; } @@ -1475,7 +1519,7 @@ @ExposedMethod(defaults = {"0", "null"}, doc = BuiltinDocs.str_rfind_doc) final int str_rfind(String sub, int start, PyObject end) { int[] indices = translateIndices(start, end); - int index = string.lastIndexOf(sub, indices[1] - sub.length()); + int index = getString().lastIndexOf(sub, indices[1] - sub.length()); if (index < start) { return -1; } @@ -1484,20 +1528,20 @@ public double atof() { StringBuilder s = null; - int n = string.length(); + int n = getString().length(); for (int i = 0; i < n; i++) { - char ch = string.charAt(i); + char ch = getString().charAt(i); if (ch == '\u0000') { throw Py.ValueError("null byte in argument for float()"); } if (Character.isDigit(ch)) { if (s == null) - s = new StringBuilder(string); + s = new StringBuilder(getString()); int val = Character.digit(ch, 10); s.setCharAt(i, Character.forDigit(val, 10)); } } - String sval = string; + String sval = getString(); if (s != null) sval = s.toString(); try { @@ -1513,7 +1557,7 @@ return Double.valueOf(sval).doubleValue(); } catch (NumberFormatException exc) { - throw Py.ValueError("invalid literal for __float__: "+string); + throw Py.ValueError("invalid literal for __float__: "+getString()); } } @@ -1527,26 +1571,26 @@ } int b = 0; - int e = string.length(); + int e = getString().length(); - while (b < e && Character.isWhitespace(string.charAt(b))) + while (b < e && Character.isWhitespace(getString().charAt(b))) b++; - while (e > b && Character.isWhitespace(string.charAt(e-1))) + while (e > b && Character.isWhitespace(getString().charAt(e-1))) e--; char sign = 0; if (b < e) { - sign = string.charAt(b); + sign = getString().charAt(b); if (sign == '-' || sign == '+') { b++; - while (b < e && Character.isWhitespace(string.charAt(b))) b++; + while (b < e && Character.isWhitespace(getString().charAt(b))) b++; } if (base == 0 || base == 16) { - if (string.charAt(b) == '0') { + if (getString().charAt(b) == '0') { if (b < e-1 && - Character.toUpperCase(string.charAt(b+1)) == 'X') { + Character.toUpperCase(getString().charAt(b+1)) == 'X') { base = 16; b += 2; } else { @@ -1560,9 +1604,9 @@ if (base == 0) base = 10; - String s = string; - if (b > 0 || e < string.length()) - s = string.substring(b, e); + String s = getString(); + if (b > 0 || e < getString().length()) + s = getString().substring(b, e); try { BigInteger bi; @@ -1575,9 +1619,9 @@ } return bi.intValue(); } catch (NumberFormatException exc) { - throw Py.ValueError("invalid literal for int() with base " + base + ": " + string); + throw Py.ValueError("invalid literal for int() with base " + base + ": " + getString()); } catch (StringIndexOutOfBoundsException exc) { - throw Py.ValueError("invalid literal for int() with base " + base + ": " + string); + throw Py.ValueError("invalid literal for int() with base " + base + ": " + getString()); } } @@ -1586,7 +1630,7 @@ } public PyLong atol(int base) { - String str = string; + String str = getString(); int b = 0; int e = str.length(); @@ -1599,7 +1643,7 @@ char sign = 0; if (b < e) { - sign = string.charAt(b); + sign = getString().charAt(b); if (sign == '-' || sign == '+') { b++; while (b < e && Character.isWhitespace(str.charAt(b))) b++; @@ -1607,9 +1651,9 @@ if (base == 0 || base == 16) { - if (string.charAt(b) == '0') { + if (getString().charAt(b) == '0') { if (b < e-1 && - Character.toUpperCase(string.charAt(b+1)) == 'X') { + Character.toUpperCase(getString().charAt(b+1)) == 'X') { base = 16; b += 2; } else { @@ -1649,10 +1693,10 @@ 0,0, "invalid decimal Unicode string"); } else { - throw Py.ValueError("invalid literal for long() with base " + base + ": " + string); + throw Py.ValueError("invalid literal for long() with base " + base + ": " + getString()); } } catch (StringIndexOutOfBoundsException exc) { - throw Py.ValueError("invalid literal for long() with base " + base + ": " + string); + throw Py.ValueError("invalid literal for long() with base " + base + ": " + getString()); } } @@ -1682,10 +1726,10 @@ @ExposedMethod(defaults="null", doc = BuiltinDocs.str_ljust_doc) final String str_ljust(int width, String fillchar) { char pad = parse_fillchar("ljust", fillchar); - int n = width-string.length(); + int n = width-getString().length(); if (n <= 0) - return string; - return string+padding(n, pad); + return getString(); + return getString()+padding(n, pad); } public String rjust(int width) { @@ -1695,10 +1739,10 @@ @ExposedMethod(defaults="null", doc = BuiltinDocs.str_rjust_doc) final String str_rjust(int width, String fillchar) { char pad = parse_fillchar("rjust", fillchar); - int n = width-string.length(); + int n = width-getString().length(); if (n <= 0) - return string; - return padding(n, pad)+string; + return getString(); + return padding(n, pad)+getString(); } public String center(int width) { @@ -1708,14 +1752,14 @@ @ExposedMethod(defaults="null", doc = BuiltinDocs.str_center_doc) final String str_center(int width, String fillchar) { char pad = parse_fillchar("center", fillchar); - int n = width-string.length(); + int n = width-getString().length(); if (n <= 0) - return string; + return getString(); int half = n/2; if (n%2 > 0 && width%2 > 0) half += 1; - return padding(half, pad)+string+padding(n-half, pad); + return padding(half, pad)+getString()+padding(n-half, pad); } public String zfill(int width) { @@ -1724,7 +1768,7 @@ @ExposedMethod(doc = BuiltinDocs.str_zfill_doc) final String str_zfill(int width) { - String s = string; + String s = getString(); int n = s.length(); if (n >= width) return s; @@ -1758,7 +1802,7 @@ @ExposedMethod(defaults = "8", doc = BuiltinDocs.str_expandtabs_doc) final String str_expandtabs(int tabsize) { - String s = string; + String s = getString(); StringBuilder buf = new StringBuilder((int)(s.length()*1.5)); char[] chars = s.toCharArray(); int n = chars.length; @@ -1789,10 +1833,10 @@ @ExposedMethod(doc = BuiltinDocs.str_capitalize_doc) final String str_capitalize() { - if (string.length() == 0) - return string; - String first = string.substring(0,1).toUpperCase(); - return first.concat(string.substring(1).toLowerCase()); + if (getString().length() == 0) + return getString(); + String first = getString().substring(0,1).toUpperCase(); + return first.concat(getString().substring(1).toLowerCase()); } @ExposedMethod(defaults = "null", doc = BuiltinDocs.str_replace_doc) @@ -1805,25 +1849,25 @@ } protected PyString replace(PyString oldPiece, PyString newPiece, int maxsplit) { - int len = string.length(); - int old_len = oldPiece.string.length(); + int len = getString().length(); + int old_len = oldPiece.getString().length(); if (len == 0) { if (maxsplit == -1 && old_len == 0) { - return createInstance(newPiece.string, true); + return createInstance(newPiece.getString(), true); } - return createInstance(string, true); + return createInstance(getString(), true); } - if (old_len == 0 && newPiece.string.length() != 0 && maxsplit !=0) { + if (old_len == 0 && newPiece.getString().length() != 0 && maxsplit !=0) { // old="" and new != "", interleave new piece with each char in original, taking in effect maxsplit StringBuilder buffer = new StringBuilder(); int i = 0; - buffer.append(newPiece.string); + buffer.append(newPiece.getString()); for (; i < len && (i < maxsplit-1 || maxsplit == -1); i++) { - buffer.append(string.charAt(i)); - buffer.append(newPiece.string); + buffer.append(getString().charAt(i)); + buffer.append(newPiece.getString()); } - buffer.append(string.substring(i)); + buffer.append(getString().substring(i)); return createInstance(buffer.toString(), true); } @@ -1835,7 +1879,7 @@ } } - return newPiece.join(splitfields(oldPiece.string, maxsplit)); + return newPiece.join(splitfields(oldPiece.getString(), maxsplit)); } public PyString join(PyObject seq) { @@ -1864,7 +1908,7 @@ // join if appropriate int i = 0; long size = 0; - int sepLen = string.length(); + int sepLen = getString().length(); for (; i < seqLen; i++) { item = seq.pyget(i); if (!(item instanceof PyString)) { @@ -1880,7 +1924,7 @@ if (i != 0) { size += sepLen; } - size += ((PyString)item).string.length(); + size += ((PyString) item).getString().length(); if (size > Integer.MAX_VALUE) { throw Py.OverflowError("join() result is too long for a Python string"); } @@ -1891,9 +1935,9 @@ for (i = 0; i < seqLen; i++) { item = seq.pyget(i); if (i != 0) { - buf.append(string); + buf.append(getString()); } - buf.append(((PyString)item).string); + buf.append(((PyString) item).getString()); } return new PyString(buf.toString()); } @@ -1923,9 +1967,9 @@ String sep = null; if (seqLen > 1) { if (this instanceof PyUnicode) { - sep = string; + sep = getString(); } else { - sep = ((PyUnicode)decode()).string; + sep = ((PyUnicode) decode()).getString(); // In case decode()'s codec mutated seq seqLen = seq.__len__(); } @@ -1933,7 +1977,7 @@ // At least two items to join, or one that isn't exact Unicode long size = 0; - int sepLen = string.length(); + int sepLen = getString().length(); StringBuilder buf = new StringBuilder(); String itemString; for (int i = 0; i < seqLen; i++) { @@ -1949,7 +1993,7 @@ // In case decode()'s codec mutated seq seqLen = seq.__len__(); } - itemString = ((PyUnicode)item).string; + itemString = ((PyUnicode) item).getString(); if (i != 0) { size += sepLen; @@ -1981,11 +2025,11 @@ int[] indices = translateIndices(start, end); if (prefix instanceof PyString) { - String strPrefix = ((PyString)prefix).string; + String strPrefix = ((PyString) prefix).getString(); if (indices[1] - indices[0] < strPrefix.length()) return false; - return string.startsWith(strPrefix, indices[0]); + return getString().startsWith(strPrefix, indices[0]); } else if (prefix instanceof PyTuple) { PyObject[] prefixes = ((PyTuple)prefix).getArray(); @@ -1993,11 +2037,11 @@ if (!(prefixes[i] instanceof PyString)) throw Py.TypeError("expected a character buffer object"); - String strPrefix = ((PyString)prefixes[i]).string; + String strPrefix = ((PyString) prefixes[i]).getString(); if (indices[1] - indices[0] < strPrefix.length()) continue; - if (string.startsWith(strPrefix, indices[0])) + if (getString().startsWith(strPrefix, indices[0])) return true; } return false; @@ -2022,9 +2066,9 @@ final boolean str_endswith(PyObject suffix, int start, PyObject end) { int[] indices = translateIndices(start, end); - String substr = string.substring(indices[0], indices[1]); + String substr = getString().substring(indices[0], indices[1]); if (suffix instanceof PyString) { - return substr.endsWith(((PyString)suffix).string); + return substr.endsWith(((PyString) suffix).getString()); } else if (suffix instanceof PyTuple) { PyObject[] suffixes = ((PyTuple)suffix).getArray(); @@ -2032,7 +2076,7 @@ if (!(suffixes[i] instanceof PyString)) throw Py.TypeError("expected a character buffer object"); - if (substr.endsWith(((PyString)suffixes[i]).string)) + if (substr.endsWith(((PyString) suffixes[i]).getString())) return true; } return false; @@ -2053,11 +2097,11 @@ protected int[] translateIndices(int start, PyObject end) { int iEnd; if(end == null) { - iEnd = string.length(); + iEnd = getString().length(); } else { iEnd = end.asInt(); } - int n = string.length(); + int n = getString().length(); if(iEnd < 0) { iEnd = n + iEnd; if(iEnd < 0) { @@ -2092,9 +2136,9 @@ throw Py.ValueError( "translation table must be 256 characters long"); - StringBuilder buf = new StringBuilder(string.length()); - for (int i=0; i < string.length(); i++) { - char c = string.charAt(i); + StringBuilder buf = new StringBuilder(getString().length()); + for (int i=0; i < getString().length(); i++) { + char c = getString().charAt(i); if (deletechars != null && deletechars.indexOf(c) >= 0) continue; try { @@ -2110,9 +2154,9 @@ //XXX: is this needed? public String translate(PyObject table) { - StringBuilder v = new StringBuilder(string.length()); - for (int i=0; i < string.length(); i++) { - char ch = string.charAt(i); + StringBuilder v = new StringBuilder(getString().length()); + for (int i=0; i < getString().length(); i++) { + char ch = getString().charAt(i); PyObject w = Py.newInteger(ch); PyObject x = table.__finditem__(w); @@ -2152,15 +2196,15 @@ @ExposedMethod(doc = Builti... [truncated message content] |