1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
Test constructors via special methods
=====================================
>>> from gmpy2 import mpz, mpq, mpfr, mpc
>>> class A:
... def __mpz__(self): return mpz(1)
... def __mpq__(self): return mpq(3,2)
... def __mpfr__(self): return mpfr(1.5)
... def __mpc__(self): return mpc(42,67)
>>> class B:
... def __mpz__(self): return 'hello'
... def __mpq__(self): return 'hello'
... def __mpfr__(self): return 'hello'
... def __mpc__(self): return 'hello'
>>> class C:
... pass
>>> class D:
... def __mpz__(self): raise TypeError
... def __mpq__(self): raise TypeError
... def __mpfr__(self): raise TypeError
... def __mpc__(self): raise TypeError
>>> a = A()
>>> b = B()
>>> c = C()
>>> d = D()
Test mpz conversion
-------------------
>>> x = mpz(a)
>>> isinstance(x, mpz)
True
>>> x == 1
True
>>> mpz(b)
Traceback (most recent call last):
...
TypeError: object of type 'str' can not be interpreted as mpz
>>> mpz(c)
Traceback (most recent call last):
...
TypeError: mpz() requires numeric or string argument
Test mpq conversion
-------------------
>>> x = mpq(a)
>>> isinstance(x, mpq)
True
>>> 2*x == 3
True
>>> mpq(b)
Traceback (most recent call last):
...
TypeError: object of type 'str' can not be interpreted as mpq
>>> mpq(c)
Traceback (most recent call last):
...
TypeError: mpq() requires numeric or string argument
Test mpfr conversion
--------------------
>>> x = mpfr(a)
>>> isinstance(x, mpfr)
True
>>> x == 1.5
True
>>> mpfr(b)
Traceback (most recent call last):
...
TypeError: object of type 'str' can not be interpreted as mpc
>>> mpfr(c)
Traceback (most recent call last):
...
TypeError: mpfr() requires numeric or string argument
Test mpc conversion
--------------------
>>> x = mpc(a)
>>> isinstance(x, mpc)
True
>>> x == 42+67j
True
>>> mpc(b)
Traceback (most recent call last):
...
TypeError: object of type 'str' can not be interpreted as mpc
>>> mpc(c)
Traceback (most recent call last):
...
TypeError: mpc() requires numeric or string argument
Test special methods raising errors
--------------------
>>> mpz(d)
Traceback (most recent call last):
...
TypeError
>>> mpq(d)
Traceback (most recent call last):
...
TypeError
>>> mpfr(d)
Traceback (most recent call last):
...
TypeError
>>> mpc(d)
Traceback (most recent call last):
...
TypeError
|