MCPcopy Index your code
hub / github.com/RustPython/RustPython / localcontext

Function localcontext

Lib/_pydecimal.py:376–419  ·  view source on GitHub ↗

Return a context manager for a copy of the supplied context Uses a copy of the current context if no context is specified The returned context manager creates a local decimal context in a with statement: def sin(x): with localcontext() as ctx: ctx.p

(ctx=None, **kwargs)

Source from the content-addressed store, hash-verified

374del contextvars # Don't contaminate the namespace
375
376def localcontext(ctx=None, **kwargs):
377 """Return a context manager for a copy of the supplied context
378
379 Uses a copy of the current context if no context is specified
380 The returned context manager creates a local decimal context
381 in a with statement:
382 def sin(x):
383 with localcontext() as ctx:
384 ctx.prec += 2
385 # Rest of sin calculation algorithm
386 # uses a precision 2 greater than normal
387 return +s # Convert result to normal precision
388
389 def sin(x):
390 with localcontext(ExtendedContext):
391 # Rest of sin calculation algorithm
392 # uses the Extended Context from the
393 # General Decimal Arithmetic Specification
394 return +s # Convert result to normal context
395
396 >>> setcontext(DefaultContext)
397 >>> print(getcontext().prec)
398 28
399 >>> with localcontext():
400 ... ctx = getcontext()
401 ... ctx.prec += 2
402 ... print(ctx.prec)
403 ...
404 30
405 >>> with localcontext(ExtendedContext):
406 ... print(getcontext().prec)
407 ...
408 9
409 >>> print(getcontext().prec)
410 28
411 """
412 if ctx is None:
413 ctx = getcontext()
414 ctx_manager = _ContextManager(ctx)
415 for key, value in kwargs.items():
416 if key not in _context_attributes:
417 raise TypeError(f"'{key}' is an invalid keyword argument for this function")
418 setattr(ctx_manager.new_context, key, value)
419 return ctx_manager
420
421
422def IEEEContext(bits, /):

Callers 15

test_nan_comparisonsMethod · 0.85
thfunc1Function · 0.85
thfunc2Function · 0.85
test_hash_methodMethod · 0.85
test_none_argsMethod · 0.85
test_named_parametersMethod · 0.85
test_localcontextMethod · 0.85
test_localcontextargMethod · 0.85

Calls 4

getcontextFunction · 0.85
_ContextManagerClass · 0.85
setattrFunction · 0.85
itemsMethod · 0.45

Tested by 15

test_nan_comparisonsMethod · 0.68
thfunc1Function · 0.68
thfunc2Function · 0.68
test_hash_methodMethod · 0.68
test_none_argsMethod · 0.68
test_named_parametersMethod · 0.68
test_localcontextMethod · 0.68
test_localcontextargMethod · 0.68