File tree Expand file tree Collapse file tree 4 files changed +25
-10
lines changed Expand file tree Collapse file tree 4 files changed +25
-10
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
7
7
## [ Unreleased]
8
8
9
+ ### Fixed
10
+ * Fix issue with ` (count nil) ` throwing an exception (#759 ).
11
+
9
12
## [ v0.1.0b0]
10
13
### Added
11
14
* Added rudimentary support for ` clojure.stacktrace ` with ` print-cause-trace ` (part of #721 )
Original file line number Diff line number Diff line change 265
265
;;;;;;;;;;;; full support for syntax quote begins here ;;;;;;;;;;;;
266
266
267
267
(def
268
- ^{:doc "Return the length of ``coll`` as by Python's ``len`` builtin. If the
269
- collection does not respond to ``__len__``\\, then count it manually."
268
+ ^{:doc "Return the length of ``coll`` as by Python's ``len`` builtin, or 0 if
269
+ ``coll`` is nil. If the collection does not respond to ``__len__``\\,
270
+ then count it manually."
270
271
:arglists '([coll])
271
272
:inline true}
272
273
count
Original file line number Diff line number Diff line change @@ -1115,15 +1115,18 @@ def apply_kw(f, args):
1115
1115
1116
1116
1117
1117
def count (coll ) -> int :
1118
- try :
1119
- return len ( coll )
1120
- except ( AttributeError , TypeError ) :
1118
+ if coll is None :
1119
+ return 0
1120
+ else :
1121
1121
try :
1122
- return sum (1 for _ in coll )
1123
- except TypeError as e :
1124
- raise TypeError (
1125
- f"count not supported on object of type { type (coll )} "
1126
- ) from e
1122
+ return len (coll )
1123
+ except (AttributeError , TypeError ):
1124
+ try :
1125
+ return sum (1 for _ in coll )
1126
+ except TypeError as e :
1127
+ raise TypeError (
1128
+ f"count not supported on object of type { type (coll )} "
1129
+ ) from e
1127
1130
1128
1131
1129
1132
__nth_sentinel = object ()
Original file line number Diff line number Diff line change @@ -260,6 +260,14 @@ def test_apply():
260
260
)
261
261
262
262
263
+ def test_count ():
264
+ assert 0 == runtime .count (None )
265
+ assert 0 == runtime .count (vec .v ())
266
+ assert 0 == runtime .count ("" )
267
+ assert 3 == runtime .count (vec .v (1 , 2 , 3 ))
268
+ assert 3 == runtime .count ("123" )
269
+
270
+
263
271
def test_nth ():
264
272
assert None is runtime .nth (None , 1 )
265
273
assert "not found" == runtime .nth (None , 4 , "not found" )
You can’t perform that action at this time.
0 commit comments