[Python-Dev] PEP 550 v4

Stefan Krah stefan at bytereef.org
Mon Aug 28 07:19:03 EDT 2017


On Sun, Aug 27, 2017 at 11:19:20AM -0400, Yury Selivanov wrote:
> On Sun, Aug 27, 2017 at 6:08 AM, Stefan Krah <stefan at bytereef.org> wrote:
> > On Sat, Aug 26, 2017 at 04:13:24PM -0700, Nathaniel Smith wrote:
> >> It's perfectly reasonable to have a script where you call
> >> decimal.setcontext or np.seterr somewhere at the top to set the
> >> defaults for the rest of the script.
> >
> > +100.  The only thing that makes sense for decimal is to change localcontext()
> > to be automatically async-safe while preserving the rest of the semantics.
> 
> TBH Nathaniel's argument isn't entirely correct.
> 
> With the semantics defined in PEP 550 v4, you still can set decimal
> context on top of your file, in your async functions etc.
> 
> and this:
> 
>     def bar():
>         decimal.setcontext(ctx)
> 
>     def foo():
>          bar()
>          # use decimal with context=ctx

Okay, so if I understand this correctly we actually will not have dynamic
scoping for regular functions:  bar() has returned, so the new context
would not be found on the stack with proper dynamic scoping.


> and this:
> 
>     async def bar():
>         # use decimal with context=ctx
> 
>     async def foo():
>          decimal.setcontext(ctx)
>          await bar()
> 
> The only thing that will not work, is this (ex1):
> 
>     async def bar():
>         decimal.setcontext(ctx)
> 
>     async def foo():
>          await bar()
>          # use decimal with context=ctx

Here we do have dynamic scoping.



> Speaking of (ex1), there's an example that didn't work in any PEP 550 version:
> 
>     def bar():
>         decimal.setcontext(ctx)
>         yield
> 
>     async def foo():
>          list(bar())
>          # use decimal with context=ctx

What about this?

async def bar():
    setcontext(Context(prec=1))
    for i in range(10):
        await asyncio.sleep(1)
        yield i

async def foo():
    async for i in bar():
        # ctx.prec=1?
        print(Decimal(100) / 3)



I'm searching for some abstract model to reason about the scopes.



Stefan Krah






More information about the Python-Dev mailing list