Issue1644

classification
Title: Result of jarray __str__ cannot be eval'ed to a jarray
Type: behaviour Severity: normal
Components: Core Versions: 2.5.1
Milestone:
process
Status: closed Resolution: invalid
Dependencies: Superseder:
Assigned To: Nosy List: brice.fernandes, zyasoft
Priority: Keywords:

Created on 2010-08-17.10:01:52 by brice.fernandes, last changed 2010-08-17.17:07:08 by brice.fernandes.

Messages
msg5977 (view) Author: brice (brice.fernandes) Date: 2010-08-17.10:01:51
When using the __str__() method of a java array, the string returned is not evaluable. ie:

arr.__str__() = "array(java.lang.String, [u'hello'])"

but the the array() function actually takes the arguments the other way around: array([u'hello'], java.lang.String)

This means that the __str__() cant be evaled.
msg5978 (view) Author: brice (brice.fernandes) Date: 2010-08-17.10:14:28
to fix, 

Change the repr_array() method on line 60 of Lib/repr.py from:

> def repr_array(self, x, level):
>     header = "array('%s', [" % x.typecode
>     return self._repr_iterable(x, level, header, '])', self.maxarray)

To:

> def repr_array(self, x, level):
>     head = "array(["
>     foot = "], '%s')"% x.typecode
>     return self._repr_iterable(x, level, head, foot, self.maxarray)

and repackage.
msg5979 (view) Author: Jim Baker (zyasoft) Date: 2010-08-17.15:35:07
In general you cannot eval the string representation of a Python object. Why should this be different?
msg5980 (view) Author: brice (brice.fernandes) Date: 2010-08-17.15:51:45
Hey Jim, from the python docs:

If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <...some useful description...> should be returned.
msg5981 (view) Author: brice (brice.fernandes) Date: 2010-08-17.15:52:25
PS: From http://docs.python.org/reference/datamodel.html
msg5982 (view) Author: Jim Baker (zyasoft) Date: 2010-08-17.16:24:55
I see the problem. You're using jarray, instead of the standard Python array package. jarray maintains the old interface, but it's just a thin wrapper of array. Because it's just for backwards compatibility, we haven't deprecated jarray, but we're not going to change it either.

So do this:

>>> import array
>>> import java
>>> x = array.array(java.lang.String, [u'hello', u'world'])
>>> x
array(java.lang.String, [u'hello', u'world'])

But if you do this:
>>> import jarray
>>> y = jarray.array(['upside', 'down'], java.lang.String)
>>> y
array(java.lang.String, [u'upside', u'down'])

But I would not rely too much on eval'ing such string representations. For certain situations, it will work, but not often. Even in this case, it's not directly evaluable since I didn't import array from array. In general, and unlike other languages like Perl and I believe Ruby, eval is just not commonly used in Python.

I believe we can now close this bug as a support issue. Changing the title to reflect this as well.
msg5983 (view) Author: brice (brice.fernandes) Date: 2010-08-17.17:07:08
Thank you for Figuring that out Jim.

As you mentioned, importing array from array gives the desired behaviour. As for 'eval' I'm using it perfectly reasonably. I'm not being lazy, honest :-)

Thanks again for your time.
History
Date User Action Args
2010-08-17 17:07:08brice.fernandessetmessages: + msg5983
2010-08-17 16:24:56zyasoftsetstatus: open -> closed
resolution: invalid
messages: + msg5982
title: Array __str__() error. -> Result of jarray __str__ cannot be eval'ed to a jarray
2010-08-17 15:52:25brice.fernandessetmessages: + msg5981
2010-08-17 15:51:46brice.fernandessetmessages: + msg5980
2010-08-17 15:35:09zyasoftsetnosy: + zyasoft
messages: + msg5979
2010-08-17 10:14:28brice.fernandessetmessages: + msg5978
2010-08-17 10:01:52brice.fernandescreate