Skip to content

Commit

Permalink
Attempt to wrap iterables
Browse files Browse the repository at this point in the history
Instead of crashing when trying to unwrap an iterable, first expand it
and then try to wrap the resulting list.

This allows `unquote` to work with iterators and generators, which
previously didn't work unless you used `unquote-splice` or explicitly
expanded a list first.
  • Loading branch information
vodik committed Mar 25, 2018
1 parent 70a414c commit 8dfbfd5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 5 additions & 2 deletions hy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# license. See the LICENSE.

from __future__ import unicode_literals
import collections
from contextlib import contextmanager
from math import isnan, isinf
from hy._compat import PY3, str_type, bytes_type, long_type, string_types
Expand Down Expand Up @@ -62,9 +63,11 @@ def wrap_value(x):

wrapper = _wrappers.get(type(x))
if wrapper is None:
if (not isinstance(x, HyObject)
and isinstance(x, collections.Iterable)):
return wrap_value(list(x))
return x
else:
return wrapper(x)
return wrapper(x)


def replace_hy_obj(obj, other):
Expand Down
9 changes: 9 additions & 0 deletions tests/native_tests/quote.hy
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,12 @@
[1 2 3]
[4 5 6])
[4 5 6])))

(defn test-unquote-with-generators []
(defn values []
(yield '1)
(yield '2)
(yield '3))

(assert (= (eval `[~@(values)]) [1 2 3]))
(assert (= (eval `[~(values)]) [[1 2 3]])))

0 comments on commit 8dfbfd5

Please sign in to comment.