Skip to content
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

Special methods (__foo__) don't work as expected on ProxyObject methods #249

Open
aeriksson opened this issue Dec 10, 2021 · 1 comment
Open

Comments

@aeriksson
Copy link

Considering Python's duck-typing semantics, it'd be really useful if it were possible to define custom 'special functions' (like __add__, __str__, etc) on ProxyObject, in order to build custom objects that mesh nicely with the rest of Python.

Defining these methods is currently possible, but Python doesn't pick them up properly. For instance, consider the following code (pardon the Clojure ;)):

(import '[org.graalvm.polyglot Context] '[org.graalvm.polyglot.proxy ProxyObject ProxyExecutable])
(let [ctx (Context/create (into-array String ["python"]))
      obj (ProxyObject/fromMap {"__len__" (reify ProxyExecutable (execute [_ _] 1))
                                "__add__" (reify ProxyExecutable (execute [_ _] 2))
                                "__str__" (reify ProxyExecutable (execute [_ _] "hi"))})]
  (.putMember (.getBindings ctx "python") "x" obj)
  (.eval ctx "python" "print(x.__len__(), x.__add__(1), x.__str__())")
  (.eval ctx "python" "print(x)")
  (try (.eval ctx "python" "print(len(x))") (catch Exception e (println (ex-message e))))
  (try (.eval ctx "python" "print(x + 1)") (catch Exception e (println (ex-message e)))))

The expected output here should be

1 2 hi
hi
1
2

but in reality, what we get is:

1 2 hi
<foreign object at 0x67b613f9>
AttributeError: foreign object has no attribute '__len__'
TypeError: unsupported operand type(s) for +: 'foreign' and 'int'
@timfel
Copy link
Member

timfel commented Dec 10, 2021

Am I reading this correctly that you create an object with a property __len__? In that case, this is expected and according to Python semantics. Consider this:

>>> class X():
...   pass
...
>>> x = X()
>>> x.__len__ = lambda self: 12
>>> x.__len__(x)
12
>>> len(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'X' has no len()

Magic methods in Python are always only looked up on the type, never on the object itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants