py/with macro doesn't bind the return value of __enter__ method

Offen
#273 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
58/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Veraltet
Tech-Stack
clojure, python
Bereich
tooling

Rechercherichtung

Beginne mit src/libpython_clj2/python/with.clj#L45-L63 und dem bestehenden Test in test/libpython_clj2/python_test.clj#L167. Füge Abdeckung hinzu, indem du einen Context Manager verwendest, dessen enter ein anderes Objekt zurückgibt, und überprüfe anschließend, dass der gebundene Wert dieser Rückgabewert ist und das bestehende with-Verhalten weiterhin besteht.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

The py/with macro implementation doesn't correctly follow Python's with statement semantics. According to the Python documentation, the value bound in a with statement should be the return value of calling __enter__() on the context manager:

manager = (EXPRESSION)
enter = type(manager).__enter__
exit = type(manager).__exit__
value = enter(manager)  # This value should be bound to TARGET
hit_except = False

try:
    TARGET = value  # TARGET gets the return value of enter(manager)
    SUITE
...

Current Implementation

The current macro (with.clj#L45-L63) calls __enter__ but discards its return value:

(py-fn/call-attr ~varname "__enter__" nil)  ; Return value is ignored
(try
  (let [retval# (do ~@body)]
    ...

Expected Behavior

The bound variable should receive the return value of __enter__(), not the context manager itself:

(let [enter-result# (py-fn/call-attr ~varname "__enter__" nil)]
  (try
    (let [~varname enter-result#  ; Rebind to __enter__'s return value
          retval# (do ~@body)]
      ...

Why Current Tests Pass

The existing test in python_test.clj#L167 uses a WithObjClass where __enter__() returns None (implicitly), and the test methods are called on the original object. This masks the bug.

Real-World Example

A common Python pattern that fails with the current implementation:

with open('file.txt', 'r') as f:
    content = f.read()  # f should be the file object returned by __enter__

With the current macro, f would be bound to the unopened file path/manager rather than the file object returned by __enter__().

Next Steps

  1. Add test cases that verify this behavior (e.g., update WithObjClass.__enter__ to return a different object, or test with Python's built-in file context manager)
  2. Update the macro to correctly bind the return value of __enter__()
Vorherrschende Sprache
Clojure
Sterne
1.2k
Forks
74
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus clj-python/libpython-clj

Alle Issues in clj-python/libpython-clj

Ähnliche Issues

Weitere Issues zu Clojure

Neue Issues direkt in Ihr Postfach

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