oracle / oracle/graalpython

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

Offen
#249 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Python
Sterne
1.6k
Forks
155
Ø Merge
9 Std. 42 Min.
Gemergte PRs (30 T.)
36

Beschreibung

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'

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das Beispiel mit Context/create, ProxyObject/fromMap und den Python-eval-Aufrufen zu reproduzieren. Verfolge, wie ProxyObject-Mitglieder für die Suche nach speziellen Methoden von Python verfügbar gemacht werden, und überprüfe dann, dass str, len und add über print(), len() und + mit der erwarteten Ausgabe funktionieren.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
java, python
Bereich
api, backend
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.