Author: Maciej Fijalkowski <fij...@gmail.com> Branch: Changeset: r67223:6441df636735 Date: 2013-10-08 17:13 +0200 http://bitbucket.org/pypy/pypy/changeset/6441df636735/
Log: merge diff --git a/lib_pypy/numpypy/core/numerictypes.py b/lib_pypy/numpypy/core/numerictypes.py --- a/lib_pypy/numpypy/core/numerictypes.py +++ b/lib_pypy/numpypy/core/numerictypes.py @@ -1,1 +1,75 @@ from _numpypy.numerictypes import * +import numpypy + +def issubclass_(arg1, arg2): + """ + Determine if a class is a subclass of a second class. + + `issubclass_` is equivalent to the Python built-in ``issubclass``, + except that it returns False instead of raising a TypeError is one + of the arguments is not a class. + + Parameters + ---------- + arg1 : class + Input class. True is returned if `arg1` is a subclass of `arg2`. + arg2 : class or tuple of classes. + Input class. If a tuple of classes, True is returned if `arg1` is a + subclass of any of the tuple elements. + + Returns + ------- + out : bool + Whether `arg1` is a subclass of `arg2` or not. + + See Also + -------- + issubsctype, issubdtype, issctype + + Examples + -------- + >>> np.issubclass_(np.int32, np.int) + True + >>> np.issubclass_(np.int32, np.float) + False + + """ + try: + return issubclass(arg1, arg2) + except TypeError: + return False + +def issubdtype(arg1, arg2): + """ + Returns True if first argument is a typecode lower/equal in type hierarchy. + + Parameters + ---------- + arg1, arg2 : dtype_like + dtype or string representing a typecode. + + Returns + ------- + out : bool + + See Also + -------- + issubsctype, issubclass_ + numpy.core.numerictypes : Overview of numpy type hierarchy. + + Examples + -------- + >>> np.issubdtype('S1', str) + True + >>> np.issubdtype(np.float64, np.float32) + False + + """ + if issubclass_(arg2, generic): + return issubclass(numpypy.dtype(arg1).type, arg2) + mro = numpypy.dtype(arg2).type.mro() + if len(mro) > 1: + val = mro[1] + else: + val = mro[0] + return issubclass(numpypy.dtype(arg1).type, val) _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit