-
Notifications
You must be signed in to change notification settings - Fork 255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Objects in Map<String,Object> and other complex containers sometimes disappear #217
Comments
If you change the line: map.put(str(key), mapObject(value)) to: k = mapObject(key)
v = mapObject(value)
map.put(k, v) Then it works. Here is a more minimal example demonstrating the issue: HashMap = autoclass('java.util.HashMap')
def new_key():
return 'stuff'
def new_val():
map_value = HashMap()
map_value.put('foo', 'bar')
return map_value
hm_good = HashMap()
k = new_key()
v = new_val()
hm_good.put(k, v)
print('good: ' + hm_good.toString())
hm_mid = HashMap()
k = new_key()
hm_mid.put(k, new_val())
print('val only: ' + hm_mid.toString())
hm_bad = HashMap()
hm_bad.put(new_key(), new_val())
print('bad: ' + hm_bad.toString()) Which outputs:
If you change My Python-fu is not strong enough to understand why there could be a difference between including the function calls within the expression, versus assigning them to intermediate output variables. (I thought it might be a race condition, but adding a sleep call before returning the newly populated |
I ran into this issue multiple times now, and it is really annoying. Could we get this resolved somehow? |
@AKuederle I implemented conversion methods similar to those discussed here, and published them on PyPI as part of the scyjava Python module. Could you give it a try and see if any of your conversions suffer from this issue? I wrote quite a few unit tests and was unable to find any failing cases. I avoided the issue by splitting the function calls across multiple lines of code, as shown by the above minimal example; additionally, I use I'd love to hear any ideas from others on why the following works: HashMap = autoclass('java.util.HashMap')
hm_good = HashMap()
k = new_key()
v = new_val()
hm_good.put(k, v)
print('good: ' + hm_good.toString()) But this does not: hm_bad = HashMap()
hm_bad.put(new_key(), new_val())
print('bad: ' + hm_bad.toString()) I am not a Python expert, but I find it hard to believe that a bug like this could really be on the Pyjnius side. |
That's awesome! I will have a look.
From my naive knowledge about Python, it appears to an issue with the
garbage collection. Basically, the second you leave the current scope the
object gets deleted.
It feels like this might be connected to the bug with the refcounter
increase reported in another bug. I am currently at mobile but I will link
the issue tomorrow.
…On Wed, Nov 28, 2018, 21:03 Curtis Rueden ***@***.*** wrote:
@AKuederle <https://github.com/AKuederle> I implemented conversion
methods similar to those discussed here, and published them on PyPI as part
of the scyjava <https://pypi.org/project/scyjava/> Python module. Could
you give it a try and see if any of your conversions suffer from this
issue? I wrote quite a few unit tests and was unable to find any failing
cases. I avoided the issue by splitting the function calls across multiple
lines of code, as shown by the above minimal example; additionally, I use
LinkedHashMap rather than HashMap, which does not seem to suffer from the
problem regardless.
I'd love to hear any ideas from others on why the following works:
HashMap = autoclass('java.util.HashMap')
hm_good = HashMap()
k = new_key()
v = new_val()
hm_good.put(k, v)print('good: ' + hm_good.toString())
But this does not:
hm_bad = HashMap()
hm_bad.put(new_key(), new_val())print('bad: ' + hm_bad.toString())
I am not a Python expert, but I find it hard to believe that a bug like
this could really be on the Pyjnius side.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#217 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AKRwuyFVEMSsNRfmQFdHRX7F24x_MaaJks5uzuwNgaJpZM4IACro>
.
|
here is the reference to the other issue: #345 |
@AKuederle Ahh, that makes sense! Then probably this issue should be closed in favor of #345, no? |
I am not 100% sure. I have no way of testing it and also I think there is no proper solution. When passing things to a java method, there is no way of knowing if you should keep a reference to the object or not, right? If the method is just a simple function, then you don't need to increase the ref counter, but if the method actually stores the object on java side, you should increase the refcounter. But I could be totally wrong here. For sure not my field of expertise. |
Btw. This might be related as well: #59 |
I am using pyjnius to pass nested Python dictionaries and lists to Java
Sometimes the content of Map<String,Object> and ArrayList disappears.
It usually goes okay with simple one-dimensional lists and dictionaries filled with strings and integers. But when structures get more complex sometimes data goes missing. It doesn't always go wrong, one day a certain Python object maps fine to Java, the other day the resulting Java-objects misses items or is completely empty.
I'm using the following versions:
Python 2.7.6
Cython 0.23.4
java version "1.7.0_95"
OpenJDK Runtime Environment (IcedTea 2.6.4) (7u95-2.6.4-0ubuntu0.14.04.1)
OpenJDK 64-Bit Server VM (build 24.95-b01, mixed mode)
Tried jnius 1.0.2 and 1.1-dev
The text was updated successfully, but these errors were encountered: