oracle / oracle/graalpython

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

Aperta
#249 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Python
Stelle
1.6k
Fork
155
Merge medio
9h 42m
PR unite (30g)
36

Descrizione

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'

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo l'esempio usando Context/create, ProxyObject/fromMap e le chiamate eval di Python. Traccia come i membri di ProxyObject vengono esposti alla ricerca dei metodi speciali di Python, quindi verifica che str, len e add funzionino tramite print(), len() e + con l'output previsto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
java, python
Ambito
api, backend
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.