MCPcopy Create free account
hub / github.com/numpy/numpy / __new__

Method __new__

numpy/core/getlimits.py:484–538  ·  view source on GitHub ↗
(cls, dtype)

Source from the content-addressed store, hash-verified

482 _finfo_cache = {}
483
484 def __new__(cls, dtype):
485 try:
486 obj = cls._finfo_cache.get(dtype) # most common path
487 if obj is not None:
488 return obj
489 except TypeError:
490 pass
491
492 if dtype is None:
493 # Deprecated in NumPy 1.25, 2023-01-16
494 warnings.warn(
495 "finfo() dtype cannot be None. This behavior will "
496 "raise an error in the future. (Deprecated in NumPy 1.25)",
497 DeprecationWarning,
498 stacklevel=2
499 )
500
501 try:
502 dtype = numeric.dtype(dtype)
503 except TypeError:
504 # In case a float instance was given
505 dtype = numeric.dtype(type(dtype))
506
507 obj = cls._finfo_cache.get(dtype)
508 if obj is not None:
509 return obj
510 dtypes = [dtype]
511 newdtype = numeric.obj2sctype(dtype)
512 if newdtype is not dtype:
513 dtypes.append(newdtype)
514 dtype = newdtype
515 if not issubclass(dtype, numeric.inexact):
516 raise ValueError("data type %r not inexact" % (dtype))
517 obj = cls._finfo_cache.get(dtype)
518 if obj is not None:
519 return obj
520 if not issubclass(dtype, numeric.floating):
521 newdtype = _convert_to_float[dtype]
522 if newdtype is not dtype:
523 # dtype changed, for example from complex128 to float64
524 dtypes.append(newdtype)
525 dtype = newdtype
526
527 obj = cls._finfo_cache.get(dtype, None)
528 if obj is not None:
529 # the original dtype was not in the cache, but the new
530 # dtype is in the cache. we add the original dtypes to
531 # the cache and return the result
532 for dt in dtypes:
533 cls._finfo_cache[dt] = obj
534 return obj
535 obj = object.__new__(cls)._init(dtype)
536 for dt in dtypes:
537 cls._finfo_cache[dt] = obj
538 return obj
539
540 def _init(self, dtype):
541 self.dtype = numeric.dtype(dtype)

Callers

nothing calls this directly

Calls 4

warnMethod · 0.80
getMethod · 0.45
dtypeMethod · 0.45
_initMethod · 0.45

Tested by

no test coverage detected