From: Pierre T. <pie...@gm...> - 2011-08-12 01:55:16
|
2011/7/22 Dünnebeil Gerhard <Ger...@ai...> > ** ** > > Hello everybody,**** > > ** ** > > being new to this list I first want to send greetings to everyone around > here.**** > > ** ** > > I joined because I ran into a problem with Jython 2.5.2 that I can’t easily > solve on my own.**** > > ** ** > > We embedded Jython into our own application which means we do calls to > Python code from within java.**** > > ** ** > > Within the call we need to make a deep copy of some structures that have > been handed over from the java side so we have to deal with java style > variables here.**** > > Doing this I ran into the problem that a deep copy of an array of String > (String[]) is not working correctly.**** > > ** ** > > A String[] in java is represented as an array.array with a type code of > “java.lang.string” in Jython.**** > > The problem comes up due to the special semantics of the array.array() > constructor which needs either a letter (new typecode) or an object as a > template.**** > > ** ** > > Deepcopy now uses –straight forward—a java.lang.string object as the > template which seems correct for me. Unfortunately the author of > array.array() refuses to accept this kind of argument with the error message > “array() argument 1 must be char, not str”. So deepcopy-ing a String[] > fails.**** > > ** ** > > Unfortunately the String[] is hidden in a structure (HashMap) which is a > map of options which are only partly evaluated in the code in questions, > most simply must be ignored and passed forward.. So it’s not easy to predict > if such an array is there and its existence can not be pinned down to just a > few cases. So replacing deepcopy and doing an own conversion is VERY > inconvenient at the best.**** > > ** ** > > Does anybody know of a clever workaround for this problem? **** > > ** ** > > Best regards**** > > Gerhard**** > > ** ** > > > Yes, this is tricky. You have to use array from jarray. Here is my solution. "source" is a String[] from the Java side. ICopyStringArray is the interface I am using to make my code callable from Java. Sorry to be so late. from main import ICopyStringArray from java.lang import String from jarray import array class StringCopier(ICopyStringArray): def copyStringArray(self, source): result = [] for string in source: result.append(string) result = array(result, String) return result Look here: http://www.prasannatech.net/2008/09/jarray-java-arrays-from-jython.html -- A+ ------------- Pierre My blog and profile (http://pierrethibault.posterous.com)<http://pierrethibault.posterous.com> YouTube page (http://www.youtube.com/user/tubetib)<http://www.youtube.com/user/tubetib> Twitter (http://twitter.com/pierreth2) <http://twitter.com/pierreth2> |