| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,9 @@ | |||
| 1 | 1 | # Copyright 2007 Google, Inc. All Rights Reserved. | |
| 2 | 2 | # Licensed to PSF under a Contributor Agreement. | |
| 3 | 3 | ||
| 4 | - """Abstract Base Classes (ABCs) for numbers, according to PEP 3141.""" | ||
| 4 | + """Abstract Base Classes (ABCs) for numbers, according to PEP 3141. | ||
| 5 | + | ||
| 6 | + TODO: Fill out more detailed documentation on the operators.""" | ||
| 5 | 7 | ||
| 6 | 8 | from abc import ABCMeta, abstractmethod, abstractproperty | |
| 7 | 9 | ||
@@ -56,10 +58,10 @@ class Complex(Number): | |||
| 56 | 58 | ||
| 57 | 59 | @abstractmethod | |
| 58 | 60 | def __complex__(self): | |
| 59 | - """Return a builtin complex instance.""" | ||
| 61 | + """Return a builtin complex instance. Called for complex(self).""" | ||
| 60 | 62 | ||
| 61 | 63 | def __bool__(self): | |
| 62 | - """True if self != 0.""" | ||
| 64 | + """True if self != 0. Called for bool(self).""" | ||
| 63 | 65 | return self != 0 | |
| 64 | 66 | ||
| 65 | 67 | @abstractproperty | |
@@ -80,53 +82,64 @@ def imag(self): | |||
| 80 | 82 | ||
| 81 | 83 | @abstractmethod | |
| 82 | 84 | def __add__(self, other): | |
| 85 | + """self + other""" | ||
| 83 | 86 | raise NotImplementedError | |
| 84 | 87 | ||
| 85 | 88 | @abstractmethod | |
| 86 | 89 | def __radd__(self, other): | |
| 90 | + """other + self""" | ||
| 87 | 91 | raise NotImplementedError | |
| 88 | 92 | ||
| 89 | 93 | @abstractmethod | |
| 90 | 94 | def __neg__(self): | |
| 95 | + """-self""" | ||
| 91 | 96 | raise NotImplementedError | |
| 92 | 97 | ||
| 93 | 98 | def __pos__(self): | |
| 99 | + """+self""" | ||
| 94 | 100 | return self | |
| 95 | 101 | ||
| 96 | 102 | def __sub__(self, other): | |
| 103 | + """self - other""" | ||
| 97 | 104 | return self + -other | |
| 98 | 105 | ||
| 99 | 106 | def __rsub__(self, other): | |
| 107 | + """other - self""" | ||
| 100 | 108 | return -self + other | |
| 101 | 109 | ||
| 102 | 110 | @abstractmethod | |
| 103 | 111 | def __mul__(self, other): | |
| 112 | + """self * other""" | ||
| 104 | 113 | raise NotImplementedError | |
| 105 | 114 | ||
| 106 | 115 | @abstractmethod | |
| 107 | 116 | def __rmul__(self, other): | |
| 117 | + """other * self""" | ||
| 108 | 118 | raise NotImplementedError | |
| 109 | 119 | ||
| 110 | 120 | @abstractmethod | |
| 111 | 121 | def __div__(self, other): | |
| 122 | + """self / other""" | ||
| 112 | 123 | raise NotImplementedError | |
| 113 | 124 | ||
| 114 | 125 | @abstractmethod | |
| 115 | 126 | def __rdiv__(self, other): | |
| 127 | + """other / self""" | ||
| 116 | 128 | raise NotImplementedError | |
| 117 | 129 | ||
| 118 | 130 | @abstractmethod | |
| 119 | 131 | def __pow__(self, exponent): | |
| 120 | - """Like division, a**b should promote to complex when necessary.""" | ||
| 132 | + """Like division, self**exponent should promote to complex when necessary.""" | ||
| 121 | 133 | raise NotImplementedError | |
| 122 | 134 | ||
| 123 | 135 | @abstractmethod | |
| 124 | 136 | def __rpow__(self, base): | |
| 137 | + """base ** self""" | ||
| 125 | 138 | raise NotImplementedError | |
| 126 | 139 | ||
| 127 | 140 | @abstractmethod | |
| 128 | 141 | def __abs__(self): | |
| 129 | - """Returns the Real distance from 0.""" | ||
| 142 | + """Returns the Real distance from 0. Called for abs(self).""" | ||
| 130 | 143 | raise NotImplementedError | |
| 131 | 144 | ||
| 132 | 145 | @abstractmethod | |
@@ -136,9 +149,11 @@ def conjugate(self): | |||
| 136 | 149 | ||
| 137 | 150 | @abstractmethod | |
| 138 | 151 | def __eq__(self, other): | |
| 152 | + """self == other""" | ||
| 139 | 153 | raise NotImplementedError | |
| 140 | 154 | ||
| 141 | 155 | def __ne__(self, other): | |
| 156 | + """self != other""" | ||
| 142 | 157 | return not (self == other) | |
| 143 | 158 | ||
| 144 | 159 | Complex.register(complex) | |
@@ -155,12 +170,14 @@ class Real(Complex): | |||
| 155 | 170 | ||
| 156 | 171 | @abstractmethod | |
| 157 | 172 | def __float__(self): | |
| 158 | - """Any Real can be converted to a native float object.""" | ||
| 173 | + """Any Real can be converted to a native float object. | ||
| 174 | + | ||
| 175 | + Called for float(self).""" | ||
| 159 | 176 | raise NotImplementedError | |
| 160 | 177 | ||
| 161 | 178 | @abstractmethod | |
| 162 | 179 | def __trunc__(self): | |
| 163 | - """Truncates self to an Integral. | ||
| 180 | + """trunc(self): Truncates self to an Integral. | ||
| 164 | 181 | ||
| 165 | 182 | Returns an Integral i such that: | |
| 166 | 183 | * i>0 iff self>0 | |
@@ -169,15 +186,15 @@ def __trunc__(self): | |||
| 169 | 186 | raise NotImplementedError | |
| 170 | 187 | ||
| 171 | 188 | def __divmod__(self, other): | |
| 172 | - """The pair (self // other, self % other). | ||
| 189 | + """divmod(self, other): The pair (self // other, self % other). | ||
| 173 | 190 | ||
| 174 | 191 | Sometimes this can be computed faster than the pair of | |
| 175 | 192 | operations. | |
| 176 | 193 | """ | |
| 177 | 194 | return (self // other, self % other) | |
| 178 | 195 | ||
| 179 | 196 | def __rdivmod__(self, other): | |
| 180 | - """The pair (self // other, self % other). | ||
| 197 | + """divmod(other, self): The pair (self // other, self % other). | ||
| 181 | 198 | ||
| 182 | 199 | Sometimes this can be computed faster than the pair of | |
| 183 | 200 | operations. | |
@@ -186,40 +203,49 @@ def __rdivmod__(self, other): | |||
| 186 | 203 | ||
| 187 | 204 | @abstractmethod | |
| 188 | 205 | def __floordiv__(self, other): | |
| 189 | - """The floor() of self/other.""" | ||
| 206 | + """self // other: The floor() of self/other.""" | ||
| 190 | 207 | raise NotImplementedError | |
| 191 | 208 | ||
| 192 | 209 | @abstractmethod | |
| 193 | 210 | def __rfloordiv__(self, other): | |
| 194 | - """The floor() of other/self.""" | ||
| 211 | + """other // self: The floor() of other/self.""" | ||
| 195 | 212 | raise NotImplementedError | |
| 196 | 213 | ||
| 197 | 214 | @abstractmethod | |
| 198 | 215 | def __mod__(self, other): | |
| 216 | + """self % other""" | ||
| 199 | 217 | raise NotImplementedError | |
| 200 | 218 | ||
| 201 | 219 | @abstractmethod | |
| 202 | 220 | def __rmod__(self, other): | |
| 221 | + """other % self""" | ||
| 203 | 222 | raise NotImplementedError | |
| 204 | 223 | ||
| 205 | 224 | @abstractmethod | |
| 206 | 225 | def __lt__(self, other): | |
| 207 | - """< on Reals defines a total ordering, except perhaps for NaN.""" | ||
| 226 | + """self < other | ||
| 227 | + | ||
| 228 | + < on Reals defines a total ordering, except perhaps for NaN.""" | ||
| 208 | 229 | raise NotImplementedError | |
| 209 | 230 | ||
| 231 | + @abstractmethod | ||
| 210 | 232 | def __le__(self, other): | |
| 233 | + """self <= other""" | ||
| 211 | 234 | raise NotImplementedError | |
| 212 | 235 | ||
| 213 | 236 | # Concrete implementations of Complex abstract methods. | |
| 214 | 237 | def __complex__(self): | |
| 238 | + """complex(self) == complex(float(self), 0)""" | ||
| 215 | 239 | return complex(float(self)) | |
| 216 | 240 | ||
| 217 | 241 | @property | |
| 218 | 242 | def real(self): | |
| 243 | + """Real numbers are their real component.""" | ||
| 219 | 244 | return self | |
| 220 | 245 | ||
| 221 | 246 | @property | |
| 222 | 247 | def imag(self): | |
| 248 | + """Real numbers have no imaginary component.""" | ||
| 223 | 249 | return 0 | |
| 224 | 250 | ||
| 225 | 251 | def conjugate(self): | |
@@ -242,6 +268,7 @@ def denominator(self): | |||
| 242 | 268 | ||
| 243 | 269 | # Concrete implementation of Real's conversion to float. | |
| 244 | 270 | def __float__(self): | |
| 271 | + """float(self) = self.numerator / self.denominator""" | ||
| 245 | 272 | return self.numerator / self.denominator | |
| 246 | 273 | ||
| 247 | 274 | ||
@@ -250,76 +277,92 @@ class Integral(Rational): | |||
| 250 | 277 | ||
| 251 | 278 | @abstractmethod | |
| 252 | 279 | def __int__(self): | |
| 280 | + """int(self)""" | ||
| 253 | 281 | raise NotImplementedError | |
| 254 | 282 | ||
| 255 | 283 | def __index__(self): | |
| 284 | + """index(self)""" | ||
| 256 | 285 | return int(self) | |
| 257 | 286 | ||
| 258 | 287 | @abstractmethod | |
| 259 | - def __pow__(self, exponent, modulus): | ||
| 288 | + def __pow__(self, exponent, modulus=None): | ||
| 260 | 289 | """self ** exponent % modulus, but maybe faster. | |
| 261 | 290 | ||
| 262 | - Implement this if you want to support the 3-argument version | ||
| 263 | - of pow(). Otherwise, just implement the 2-argument version | ||
| 264 | - described in Complex. Raise a TypeError if exponent < 0 or any | ||
| 265 | - argument isn't Integral. | ||
| 291 | + Accept the modulus argument if you want to support the | ||
| 292 | + 3-argument version of pow(). Raise a TypeError if exponent < 0 | ||
| 293 | + or any argument isn't Integral. Otherwise, just implement the | ||
| 294 | + 2-argument version described in Complex. | ||
| 266 | 295 | """ | |
| 267 | 296 | raise NotImplementedError | |
| 268 | 297 | ||
| 269 | 298 | @abstractmethod | |
| 270 | 299 | def __lshift__(self, other): | |
| 300 | + """self << other""" | ||
| 271 | 301 | raise NotImplementedError | |
| 272 | 302 | ||
| 273 | 303 | @abstractmethod | |
| 274 | 304 | def __rlshift__(self, other): | |
| 305 | + """other << self""" | ||
| 275 | 306 | raise NotImplementedError | |
| 276 | 307 | ||
| 277 | 308 | @abstractmethod | |
| 278 | 309 | def __rshift__(self, other): | |
| 310 | + """self >> other""" | ||
| 279 | 311 | raise NotImplementedError | |
| 280 | 312 | ||
| 281 | 313 | @abstractmethod | |
| 282 | 314 | def __rrshift__(self, other): | |
| 315 | + """other >> self""" | ||
| 283 | 316 | raise NotImplementedError | |
| 284 | 317 | ||
| 285 | 318 | @abstractmethod | |
| 286 | 319 | def __and__(self, other): | |
| 320 | + """self & other""" | ||
| 287 | 321 | raise NotImplementedError | |
| 288 | 322 | ||
| 289 | 323 | @abstractmethod | |
| 290 | 324 | def __rand__(self, other): | |
| 325 | + """other & self""" | ||
| 291 | 326 | raise NotImplementedError | |
| 292 | 327 | ||
| 293 | 328 | @abstractmethod | |
| 294 | 329 | def __xor__(self, other): | |
| 330 | + """self ^ other""" | ||
| 295 | 331 | raise NotImplementedError | |
| 296 | 332 | ||
| 297 | 333 | @abstractmethod | |
| 298 | 334 | def __rxor__(self, other): | |
| 335 | + """other ^ self""" | ||
| 299 | 336 | raise NotImplementedError | |
| 300 | 337 | ||
| 301 | 338 | @abstractmethod | |
| 302 | 339 | def __or__(self, other): | |
| 340 | + """self | other""" | ||
| 303 | 341 | raise NotImplementedError | |
| 304 | 342 | ||
| 305 | 343 | @abstractmethod | |
| 306 | 344 | def __ror__(self, other): | |
| 345 | + """other | self""" | ||
| 307 | 346 | raise NotImplementedError | |
| 308 | 347 | ||
| 309 | 348 | @abstractmethod | |
| 310 | 349 | def __invert__(self): | |
| 350 | + """~self""" | ||
| 311 | 351 | raise NotImplementedError | |
| 312 | 352 | ||
| 313 | 353 | # Concrete implementations of Rational and Real abstract methods. | |
| 314 | 354 | def __float__(self): | |
| 355 | + """float(self) == float(int(self))""" | ||
| 315 | 356 | return float(int(self)) | |
| 316 | 357 | ||
| 317 | 358 | @property | |
| 318 | 359 | def numerator(self): | |
| 360 | + """Integers are their own numerators.""" | ||
| 319 | 361 | return self | |
| 320 | 362 | ||
| 321 | 363 | @property | |
| 322 | 364 | def denominator(self): | |
| 365 | + """Integers have a denominator of 1.""" | ||
| 323 | 366 | return 1 | |
| 324 | 367 | ||
| 325 | 368 | Integral.register(int) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1358,11 +1358,13 @@ def test_pow(self): | |||
| 1358 | 1358 | else: | |
| 1359 | 1359 | self.assertAlmostEqual(pow(x, y, z), 24.0) | |
| 1360 | 1360 | ||
| 1361 | + self.assertAlmostEqual(pow(-1, 0.5), 1j) | ||
| 1362 | + self.assertAlmostEqual(pow(-1, 1/3), 0.5 + 0.8660254037844386j) | ||
| 1363 | + | ||
| 1361 | 1364 | self.assertRaises(TypeError, pow, -1, -2, 3) | |
| 1362 | 1365 | self.assertRaises(ValueError, pow, 1, 2, 0) | |
| 1363 | 1366 | self.assertRaises(TypeError, pow, -1, -2, 3) | |
| 1364 | 1367 | self.assertRaises(ValueError, pow, 1, 2, 0) | |
| 1365 | - self.assertRaises(ValueError, pow, -342.43, 0.234) | ||
| 1366 | 1368 | ||
| 1367 | 1369 | self.assertRaises(TypeError, pow) | |
| 1368 | 1370 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,13 +1,6 @@ | |||
| 1 | 1 | import unittest, os | |
| 2 | 2 | from test import test_support | |
| 3 | 3 | ||
| 4 | - import warnings | ||
| 5 | - warnings.filterwarnings( | ||
| 6 | - "ignore", | ||
| 7 | - category=DeprecationWarning, | ||
| 8 | - message=".*complex divmod.*are deprecated" | ||
| 9 | - ) | ||
| 10 | - | ||
| 11 | 4 | from random import random | |
| 12 | 5 | ||
| 13 | 6 | # These tests ensure that complex math does the right thing | |
@@ -108,6 +101,7 @@ def test_mod(self): | |||
| 108 | 101 | # % is no longer supported on complex numbers | |
| 109 | 102 | self.assertRaises(TypeError, (1+1j).__mod__, 0+0j) | |
| 110 | 103 | self.assertRaises(TypeError, lambda: (3.33+4.43j) % 0) | |
| 104 | + self.assertRaises(TypeError, (1+1j).__mod__, 4.3j) | ||
| 111 | 105 | ||
| 112 | 106 | def test_divmod(self): | |
| 113 | 107 | self.assertRaises(TypeError, divmod, 1+1j, 1+0j) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,13 +3,8 @@ | |||
| 3 | 3 | from test.test_support import verify, vereq, verbose, TestFailed, TESTFN | |
| 4 | 4 | from test.test_support import get_original_stdout | |
| 5 | 5 | from copy import deepcopy | |
| 6 | - import warnings | ||
| 7 | 6 | import types | |
| 8 | 7 | ||
| 9 | - warnings.filterwarnings("ignore", | ||
| 10 | - r'complex divmod\(\), // and % are deprecated$', | ||
| 11 | - DeprecationWarning, r'(<string>|%s)$' % __name__) | ||
| 12 | - | ||
| 13 | 8 | def veris(a, b): | |
| 14 | 9 | if a is not b: | |
| 15 | 10 | raise TestFailed("%r is %r" % (a, b)) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -680,9 +680,10 @@ float_pow(PyObject *v, PyObject *w, PyObject *z) | |||
| 680 | 680 | * bugs so we have to figure it out ourselves. | |
| 681 | 681 | */ | |
| 682 | 682 | if (iw != floor(iw)) { | |
| 683 | - PyErr_SetString(PyExc_ValueError, "negative number " | ||
| 684 | - "cannot be raised to a fractional power"); | ||
| 685 | - return NULL; | ||
| 683 | + /* Negative numbers raised to fractional powers | ||
| 684 | + * become complex. | ||
| 685 | + */ | ||
| 686 | + return PyComplex_Type.tp_as_number->nb_power(v, w, z); | ||
| 686 | 687 | } | |
| 687 | 688 | /* iw is an exact integer, albeit perhaps a very large one. | |
| 688 | 689 | * -1 raised to an exact integer should never be exceptional. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments