From 70a414c1e8d300f75be822577fa3042ab5dc56bb Mon Sep 17 00:00:00 2001 From: Simon Gomizelj Date: Sun, 25 Mar 2018 02:34:17 -0400 Subject: [PATCH] Improve `replace` on HyObject so it does less work Calling `replace` can trigger a call to `replace_hy_obj`, which will try to also wrap values inside a `HyObject`. However, this means when we try to recusively replace inside a nested object, like a `HyList`, we should try and capture these new wrapped objects. Hy still works if we don't, but it results in the object getting rewrapped multiple times during a compile. Closes #1519 --- hy/models.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/hy/models.py b/hy/models.py index 35ff55acb..11700a63d 100644 --- a/hy/models.py +++ b/hy/models.py @@ -226,11 +226,8 @@ class HyList(HyObject, list): """ def replace(self, other): - for x in self: - replace_hy_obj(x, other) - - HyObject.replace(self, other) - return self + self[:] = [replace_hy_obj(x, other) for x in self] + return HyObject.replace(self, other) def __add__(self, other): return self.__class__(super(HyList, self).__add__(other)) @@ -387,11 +384,11 @@ def __iter__(self): def replace(self, other): if self.car is not None: - replace_hy_obj(self.car, other) + self.car = replace_hy_obj(self.car, other) if self.cdr is not None: - replace_hy_obj(self.cdr, other) + self.cdr = replace_hy_obj(self.cdr, other) - HyObject.replace(self, other) + return HyObject.replace(self, other) def __repr__(self): if PRETTY: