Skip to content

Commit

Permalink
Don't mix connections for value_of
Browse files Browse the repository at this point in the history
This can't work so let the user know, similarly to how we avoid mixing
connections elsewhere. Also allow value_of() to be called in a static
context, since some users want this.
  • Loading branch information
iamsrp-deshaw committed Mar 29, 2024
1 parent 5088128 commit 2a3516f
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion python/pjrmi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,11 @@ def value_of(self,
``ndarray`` makes for vastly more efficient array access patterns on the
Python side.
If this method is called in a static context, with ``self`` being
explicitly passed as ``None``::
PJRmi.value_of(None, java_object)
then it will infer the `PJRmi` instance to use from the object.
By default the Java object must be wholly converted to its Python
equivalent, otherwise an ``UnsupportedOperationException`` will be
thrown. However, setting the ``best_effort`` kwarg to ``True`` will mean
Expand Down Expand Up @@ -873,10 +878,24 @@ def value_of(self,
if obj is None:
return None

# Sanity
# Check user inputs
if not issubclass(obj.__class__, _JavaObject):
raise TypeError("Given a non-JavaObject %s (%s)" %
(str(obj), str(obj.__class__)))
if self is not None:
if obj._pjrmi_inst is not self:
raise KeyError(
"Attempt to use Java object (%s: %s) from connection %s with %s" %
(str(obj.__class__), str(obj),
obj._pjrmi_inst._transport, self._transport)
)
elif obj._pjrmi_inst is None:
raise ValueError("Object has no associated PJRmi instance")
else:
# We are being called in a static context, as opposed to from the
# PJRmi instance. We can use the object's instance as self for the
# call.
self = obj._pjrmi_inst

if compress:
if best_effort:
Expand Down

0 comments on commit 2a3516f

Please sign in to comment.