oracle / graalpython

A Python 3 implementation built on GraalVM
Other
1.17k stars 101 forks source link

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

Open aeriksson opened 2 years ago

aeriksson commented 2 years ago

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 commented 2 years ago

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.