Fix copy behaviour of HyComplex
This commit is contained in:
parent
7e5d4830eb
commit
274f5e9002
19
hy/models.py
19
hy/models.py
@ -171,18 +171,17 @@ class HyComplex(HyObject, complex):
|
|||||||
complex(foo) was called, given HyComplex(foo).
|
complex(foo) was called, given HyComplex(foo).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __new__(cls, num, *args, **kwargs):
|
def __new__(cls, real, imag=0, *args, **kwargs):
|
||||||
value = super(HyComplex, cls).__new__(cls, strip_digit_separators(num))
|
if isinstance(real, string_types):
|
||||||
if isinstance(num, string_types):
|
value = super(HyComplex, cls).__new__(
|
||||||
p1, _, p2 = num.lstrip("+-").replace("-", "+").partition("+")
|
cls, strip_digit_separators(real)
|
||||||
|
)
|
||||||
|
p1, _, p2 = real.lstrip("+-").replace("-", "+").partition("+")
|
||||||
|
check_inf_nan_cap(p1, value.imag if "j" in p1 else value.real)
|
||||||
if p2:
|
if p2:
|
||||||
check_inf_nan_cap(p1, value.real)
|
|
||||||
check_inf_nan_cap(p2, value.imag)
|
check_inf_nan_cap(p2, value.imag)
|
||||||
elif "j" in p1:
|
return value
|
||||||
check_inf_nan_cap(p1, value.imag)
|
return super(HyComplex, cls).__new__(cls, real, imag)
|
||||||
else:
|
|
||||||
check_inf_nan_cap(p1, value.real)
|
|
||||||
return value
|
|
||||||
|
|
||||||
_wrappers[complex] = HyComplex
|
_wrappers[complex] = HyComplex
|
||||||
|
|
||||||
|
@ -2,9 +2,10 @@
|
|||||||
# This file is part of Hy, which is free software licensed under the Expat
|
# This file is part of Hy, which is free software licensed under the Expat
|
||||||
# license. See the LICENSE.
|
# license. See the LICENSE.
|
||||||
|
|
||||||
|
import copy
|
||||||
from hy._compat import long_type, str_type
|
from hy._compat import long_type, str_type
|
||||||
from hy.models import (wrap_value, replace_hy_obj, HyString, HyInteger, HyList,
|
from hy.models import (wrap_value, replace_hy_obj, HyString, HyInteger, HyList,
|
||||||
HyDict, HySet, HyExpression, HyCons)
|
HyDict, HySet, HyExpression, HyCons, HyComplex, HyFloat)
|
||||||
|
|
||||||
|
|
||||||
def test_wrap_long_type():
|
def test_wrap_long_type():
|
||||||
@ -125,3 +126,17 @@ def test_cons_replacing():
|
|||||||
assert True is False
|
assert True is False
|
||||||
except IndexError:
|
except IndexError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def test_number_model_copy():
|
||||||
|
i = HyInteger(42)
|
||||||
|
assert (i == copy.copy(i))
|
||||||
|
assert (i == copy.deepcopy(i))
|
||||||
|
|
||||||
|
f = HyFloat(42.)
|
||||||
|
assert (f == copy.copy(f))
|
||||||
|
assert (f == copy.deepcopy(f))
|
||||||
|
|
||||||
|
c = HyComplex(42j)
|
||||||
|
assert (c == copy.copy(c))
|
||||||
|
assert (c == copy.deepcopy(c))
|
||||||
|
Loading…
Reference in New Issue
Block a user