py/with macro doesn't bind the return value of __enter__ method
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 58/100
Línea de trabajo
Comienza con src/libpython_clj2/python/with.clj#L45-L63 y la prueba existente en test/libpython_clj2/python_test.clj#L167. Añade cobertura usando un gestor de contexto cuyo enter devuelva un objeto diferente; después, verifica que el valor enlazado sea ese valor devuelto y que el comportamiento existente de with siga pasando.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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
- 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) - Update the macro to correctly bind the return value of
__enter__()
- Lenguaje dominante
- Clojure
- Estrellas
- 1.2k
- Forks
- 74
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de clj-python/libpython-clj
-
upgrade to dtype-next 11.xxx Abierto
Dificultad 3/5 1-2 días Aptitud para principiantes 48/100
clj-python/libpython-clj#280 ·
-
Dificultad 4/5 3-5 días Aptitud para principiantes 38/100
clj-python/libpython-clj#275 · 5 comentarios ·
-
Dificultad 4/5 3-5 días Aptitud para principiantes 25/100
clj-python/libpython-clj#271 · 1 comentario ·
-
"auto flush" tty optionally Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
clj-python/libpython-clj#270 · 1 comentario ·
-
Dificultad 3/5 1-2 días Aptitud para principiantes 42/100
clj-python/libpython-clj#269 · 3 comentarios ·
Todos los issues de clj-python/libpython-clj
Issues similares
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 88/100
-
.Team/Metabot Priority:P3
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
-
needs triage
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
scalar-labs/scalar-jepsen#222 · 1 comentario ·