Skip to content

Commit 5373531

Browse files
authored
Fix issue with with failing with traceback error. (#715)
Hi, could you please fix for `with` to not fail with traceback error when an exception is thrown. It fixes #714 . It seems that the `with` macro expected the `__traceback__` field of an exception to be a method, but it is rather a __field__ https://docs.python.org/3/library/traceback.html > The module uses traceback objects — these are objects of type [types.TracebackType](https://docs.python.org/3/library/types.html#types.TracebackType), which are assigned to the __traceback__ field of [BaseException](https://docs.python.org/3/library/exceptions.html#BaseException) instances. For some reason this issue does not manifest with `python/open` resources, as if the `__enter__` magic method is not hit (or perhaps it is lazy gc and it will eventually happen). I've added a test for the same. Thanks, Co-authored-by: ikappaki <[email protected]>
1 parent 58cff26 commit 5373531

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
* Fix `sort-by` support for maps and boolean comparator fns (#709).
1212
* Fix `sort` support for maps and boolean comparator fns (#711).
1313
* Fix `(is (= exp act))` should only evaluate its args once on failure (#712).
14+
* Fix issue with `with` failing with a traceback error when an exception is thrown (#714).
1415

1516
## [v0.1.0a2]
1617
### Added

src/basilisp/core.lpy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3716,7 +3716,7 @@
37163716
res#)
37173717
(catch python/Exception e#
37183718
(vreset! hit-except# true)
3719-
(when-not (. obj# (~'__exit__ (python/type e#) e# (. e# ~'__traceback__)))
3719+
(when-not (. obj# (~'__exit__ (python/type e#) e# (.- e# ~'__traceback__)))
37203720
(throw e#)))
37213721
(finally
37223722
(when-not @hit-except#

tests/basilisp/test_core_macros.lpy

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(ns tests.basilisp.test-core-macros
2-
(:import inspect os tempfile)
2+
(:import inspect os socket tempfile)
33
(:require
44
[basilisp.test :refer [deftest is testing]]))
55

@@ -450,15 +450,21 @@
450450
(is (= #py ["A" "B" "C"] (.. "a,b,c" upper (split ",")))))
451451

452452
(deftest with-test
453-
(let [[fh filename] (tempfile/mkstemp)]
454-
(try
455-
(with-open [file (python/open filename "w")]
456-
(.write file "Testing text"))
457-
(with-open [file (python/open filename)]
458-
(is (= "Testing text" (.read file))))
459-
(finally
460-
(os/close fh)
461-
(os/unlink filename)))))
453+
(testing "basic"
454+
(let [[fh filename] (tempfile/mkstemp)]
455+
(try
456+
(with-open [file (python/open filename "w")]
457+
(.write file "Testing text"))
458+
(with-open [file (python/open filename)]
459+
(is (= "Testing text" (.read file))))
460+
(finally
461+
(os/close fh)
462+
(os/unlink filename)))))
463+
464+
(testing "exceptions"
465+
(is (thrown? python/FloatingPointError
466+
(with [sock (socket/socket socket/AF_INET socket/SOCK_STREAM)]
467+
(throw python/FloatingPointError))))))
462468

463469
(deftest if-let-test
464470
(is (= :a (if-let [a :a] a :b)))

0 commit comments

Comments
 (0)