Пример #1
0
 def test_errcall(self):
     def foo(*args):
         print(args)
     olderrcall = np.geterrcall()
     with np.errstate(call=foo):
         assert_(np.geterrcall() is foo, 'call is not foo')
         with np.errstate(call=None):
             assert_(np.geterrcall() is None, 'call is not None')
     assert_(np.geterrcall() is olderrcall, 'call is not olderrcall')
Пример #2
0
 def test_errcall(self):
     def foo(*args):
         print(args)
     olderrcall = np.geterrcall()
     with np.errstate(call=foo):
         assert_(np.geterrcall() is foo, 'call is not foo')
         with np.errstate(call=None):
             assert_(np.geterrcall() is None, 'call is not None')
     assert_(np.geterrcall() is olderrcall, 'call is not olderrcall')
Пример #3
0
    def push_err(self, arg):
        """ Set numpy numerical error handling via a stack.

    """
        try:
            import numpy
        except ImportError:
            raise UsageError("could not import numpy.")

        sentinel = object()

        args = parse_argstring(self.push_err, arg)
        kwds = {}
        errcall = sentinel
        for key in ['all', 'divide', 'over', 'under', 'invalid']:
            value = getattr(args, key)
            if value is not None:
                kwds[key] = value
        if args.call_func is not None:
            if args.no_call_func:
                raise UsageError("You cannot specify both a --call-func and "
                                 "--no-call-func at the same time.")
            global_ns = self.shell.user_global_ns
            local_ns = self.shell.user_ns
            try:
                errcall = eval(args.call_func, global_ns, local_ns)
            except Exception as e:
                raise UsageError(
                    "Could not find function {0!r}\n{1}: {2}".format(
                        args.call_func, e.__class__.__name__, e))
        elif args.no_call_func:
            errcall = None

        old_options = numpy.geterr()
        old_errcall = numpy.geterrcall()
        numpy.seterr(**kwds)
        if errcall is not sentinel:
            try:
                numpy.seterrcall(errcall)
            except ValueError as e:
                raise UsageError(str(e))
        stack = getattr(self, '_numpy_err_stack', [])
        stack.append((old_options, old_errcall))
        self._numpy_err_stack = stack
        if not args.quiet:
            print_numpy_err(numpy.geterr(), numpy.geterrcall())
Пример #4
0
    def pop_err(self, arg):
        """ Pop the last set of numpy numerical error handling settings from the
        stack.

    """
        try:
            import numpy
        except ImportError:
            raise UsageError("could not import numpy.")
        args = parse_argstring(self.pop_err, arg)

        stack = getattr(self, '_numpy_err_stack', [])
        if stack:
            kwds, errcall = stack.pop()
            numpy.seterr(**kwds)
            numpy.seterrcall(errcall)
        elif not args.quiet:
            print("At the end of the stack.\n")
        self._numpy_err_stack = stack
        if not args.quiet:
            print_numpy_err(numpy.geterr(), numpy.geterrcall())
Пример #5
0
def magic_pop_err(self, arg):
    """ Pop the last set of numpy numerical error handling settings from the
    stack.

"""
    try:
        import numpy
    except ImportError:
        raise UsageError("could not import numpy.")
    args = parse_argstring(magic_pop_err, arg)

    stack = getattr(self, '_numpy_err_stack', [])
    if stack:
        kwds, errcall = stack.pop()
        numpy.seterr(**kwds)
        numpy.seterrcall(errcall)
    elif not args.quiet:
        print "At the end of the stack."
        print
    self._numpy_err_stack = stack
    if not args.quiet:
        print_numpy_err(numpy.geterr(), numpy.geterrcall())
Пример #6
0
import numpy as np

np.geterrcall()  # we did not yet set a handler, returns None
oldsettings = np.seterr(all='call')


def err_handler(err_type, flag):
    print
    "Floating point error (%s), with flag %s" % (err_type, flag)


oldhandler = np.seterrcall(err_handler)
np.array([1, 2, 3]) / 0.0
cur_handler = np.geterrcall()
cur_handler is err_handler
Пример #7
0
    if args.call_func is not None:
        if args.no_call_func:
            raise UsageError("You cannot specify both a --call-func and "
                "--no-call-func at the same time.")
        global_ns = get_shell(self).user_global_ns
        local_ns = get_shell(self).user_ns
        try:
            errcall = eval(args.call_func, global_ns, local_ns)
        except Exception, e:
            raise UsageError('Could not find function %r.\n%s: %s' % 
                (args.call_func, e.__class__.__name__, e))
    elif args.no_call_func:
        errcall = None

    old_options = numpy.geterr()
    old_errcall = numpy.geterrcall()
    numpy.seterr(**kwds)
    if errcall is not sentinel:
        try:
            numpy.seterrcall(errcall)
        except ValueError, e:
            raise UsageError(str(e))
    stack = getattr(self, '_numpy_err_stack', [])
    stack.append((old_options, old_errcall))
    self._numpy_err_stack = stack
    if not args.quiet:
        print_numpy_err(numpy.geterr(), numpy.geterrcall())


@magic_arguments()
@argument('-q', '--quiet', action='store_true',
import numpy as np

old_errobj = np.geterrobj()  # first get the defaults
old_errobj


def err_handler(err_type, flag):
    print
    "Floating point error (%s), with flag %s" % (err_type, flag)


new_errobj = [20000, 12, err_handler]
np.seterrobj(new_errobj)
np.base_repr(12, 8)  # int for divide=4 ('print') and over=1 ('warn')
np.geterr()
np.geterrcall() is err_handler
Пример #9
0
def func2(a: str, b: int, c: float = ...) -> None: ...
def func3(a: str, b: int) -> int: ...

class Write1:
    def write(self, a: str) -> None: ...

class Write2:
    def write(self, a: str, b: int = ...) -> None: ...

class Write3:
    def write(self, a: str) -> int: ...


_err_default = np.geterr()
_bufsize_default = np.getbufsize()
_errcall_default = np.geterrcall()

try:
    np.seterr(all=None)
    np.seterr(divide="ignore")
    np.seterr(over="warn")
    np.seterr(under="call")
    np.seterr(invalid="raise")
    np.geterr()

    np.setbufsize(4096)
    np.getbufsize()

    np.seterrcall(func1)
    np.seterrcall(func2)
    np.seterrcall(func3)
Пример #10
0
reveal_type(
    np.seterr(all=None))  # E: TypedDict('numpy.core._ufunc_config._ErrDict'
reveal_type(np.seterr(
    divide="ignore"))  # E: TypedDict('numpy.core._ufunc_config._ErrDict'
reveal_type(
    np.seterr(over="warn"))  # E: TypedDict('numpy.core._ufunc_config._ErrDict'
reveal_type(np.seterr(
    under="call"))  # E: TypedDict('numpy.core._ufunc_config._ErrDict'
reveal_type(np.seterr(
    invalid="raise"))  # E: TypedDict('numpy.core._ufunc_config._ErrDict'
reveal_type(np.geterr())  # E: TypedDict('numpy.core._ufunc_config._ErrDict'

reveal_type(np.setbufsize(4096))  # E: int
reveal_type(np.getbufsize())  # E: int

reveal_type(
    np.seterrcall(func)
)  # E: Union[None, def (builtins.str, builtins.int) -> Any, numpy.core._ufunc_config._SupportsWrite]
reveal_type(
    np.seterrcall(Write())
)  # E: Union[None, def (builtins.str, builtins.int) -> Any, numpy.core._ufunc_config._SupportsWrite]
reveal_type(
    np.geterrcall()
)  # E: Union[None, def (builtins.str, builtins.int) -> Any, numpy.core._ufunc_config._SupportsWrite]

reveal_type(np.errstate(
    call=func,
    all="call"))  # E: numpy.errstate[def (a: builtins.str, b: builtins.int)]
reveal_type(np.errstate(call=Write(), divide="log",
                        over="log"))  # E: numpy.errstate[ufunc_config.Write]