Re: [Python-Dev] Rewrite @contextlib.contextmanager in C

2016-08-10 Thread Chris Angelico
On Wed, Aug 10, 2016 at 4:43 AM, Giampaolo Rodola' wrote: > Chris' SimplerContextManager solution is faster because it avoids the > factory function but that is necessary for supporting the decoration of > methods. Hooking a tangent onto this: I have no idea, based on the current source code, whi

Re: [Python-Dev] Rewrite @contextlib.contextmanager in C

2016-08-10 Thread Nick Coghlan
On 10 August 2016 at 04:43, Giampaolo Rodola' wrote: > > > On Tue, Aug 9, 2016 at 3:30 PM, Nick Coghlan wrote: > >> On 9 August 2016 at 23:26, Nick Coghlan wrote: >> >>> On 9 August 2016 at 06:18, Guido van Rossum wrote: >>> I think Nick would be interested in understanding why this is th

Re: [Python-Dev] Rewrite @contextlib.contextmanager in C

2016-08-09 Thread Serhiy Storchaka
On 09.08.16 00:59, Chris Angelico wrote: class TooSimpleContextManager: """Now this time you've gone too far.""" def __init__(self, func): self.func = func def __call__(self): self.gen = self.func() return self def __enter__(self): next(self.gen)

Re: [Python-Dev] Rewrite @contextlib.contextmanager in C

2016-08-09 Thread Chris Angelico
On Wed, Aug 10, 2016 at 4:43 AM, Giampaolo Rodola' wrote: > -return self.__class__(self.func, self.args, self.kwds) > +func, args, kwds = self.funcak > +return self.__class__(func, args, kwds) return self.__class__(*self.funcak) > @wraps(func) > def helper(*args

Re: [Python-Dev] Rewrite @contextlib.contextmanager in C

2016-08-09 Thread Giampaolo Rodola'
On Tue, Aug 9, 2016 at 3:30 PM, Nick Coghlan wrote: > On 9 August 2016 at 23:26, Nick Coghlan wrote: > >> On 9 August 2016 at 06:18, Guido van Rossum wrote: >> >>> I think Nick would be interested in understanding why this is the case. >>> What does the decorator do that could be so expensive?

Re: [Python-Dev] Rewrite @contextlib.contextmanager in C

2016-08-09 Thread Giampaolo Rodola'
On Mon, Aug 8, 2016 at 11:59 PM, Chris Angelico wrote: > On Tue, Aug 9, 2016 at 7:14 AM, Wolfgang Maier > wrote: > > Right, I think a fairer comparison would be to: > > > > class ctx2: > > def __enter__(self): > > self.it = iter(self) > > return next(self.it) > > > > def

Re: [Python-Dev] Rewrite @contextlib.contextmanager in C

2016-08-09 Thread Nick Coghlan
On 9 August 2016 at 23:26, Nick Coghlan wrote: > On 9 August 2016 at 06:18, Guido van Rossum wrote: > >> I think Nick would be interested in understanding why this is the case. >> What does the decorator do that could be so expensive? >> > > Reviewing https://hg.python.org/cpython/file/default/L

Re: [Python-Dev] Rewrite @contextlib.contextmanager in C

2016-08-09 Thread Nick Coghlan
On 9 August 2016 at 06:18, Guido van Rossum wrote: > I think Nick would be interested in understanding why this is the case. > What does the decorator do that could be so expensive? > Reviewing https://hg.python.org/cpython/file/default/Lib/contextlib.py#l57, Chris's analysis seems plausible to

Re: [Python-Dev] Rewrite @contextlib.contextmanager in C

2016-08-08 Thread Chris Angelico
On Tue, Aug 9, 2016 at 7:14 AM, Wolfgang Maier wrote: > Right, I think a fairer comparison would be to: > > class ctx2: > def __enter__(self): > self.it = iter(self) > return next(self.it) > > def __exit__(self, *args): > try: > next(self.it) > e

Re: [Python-Dev] Rewrite @contextlib.contextmanager in C

2016-08-08 Thread Wolfgang Maier
On 8/8/2016 22:38, Yury Selivanov wrote: On 2016-08-08 4:18 PM, Guido van Rossum wrote: I think Nick would be interested in understanding why this is the case. What does the decorator do that could be so expensive? From the looks of it it doesn't do anything special. Although with @contextl

Re: [Python-Dev] Rewrite @contextlib.contextmanager in C

2016-08-08 Thread Yury Selivanov
On 2016-08-08 4:18 PM, Guido van Rossum wrote: I think Nick would be interested in understanding why this is the case. What does the decorator do that could be so expensive? From the looks of it it doesn't do anything special. Although with @contextlib.contextmanager we have to instantiate

Re: [Python-Dev] Rewrite @contextlib.contextmanager in C

2016-08-08 Thread Giampaolo Rodola'
On Mon, Aug 8, 2016 at 10:07 PM, Yury Selivanov wrote: > > > On 2016-08-08 3:33 PM, Giampaolo Rodola' wrote: > >> I wanted to give it a try rewriting this in C but since @contextmanager >> has a lot of magic I wanted to ask first whether this 1) is technically >> possible 2) is desirable. >> > >

Re: [Python-Dev] Rewrite @contextlib.contextmanager in C

2016-08-08 Thread Guido van Rossum
I think Nick would be interested in understanding why this is the case. What does the decorator do that could be so expensive? On Mon, Aug 8, 2016 at 1:07 PM, Yury Selivanov wrote: > > > On 2016-08-08 3:33 PM, Giampaolo Rodola' wrote: > >> I wanted to give it a try rewriting this in C but since

Re: [Python-Dev] Rewrite @contextlib.contextmanager in C

2016-08-08 Thread Yury Selivanov
On 2016-08-08 3:33 PM, Giampaolo Rodola' wrote: I wanted to give it a try rewriting this in C but since @contextmanager has a lot of magic I wanted to ask first whether this 1) is technically possible 2) is desirable. It is definitely technologically possible. However, the C implementation

[Python-Dev] Rewrite @contextlib.contextmanager in C

2016-08-08 Thread Giampaolo Rodola'
import timeit import contextlib @contextlib.contextmanager def ctx1(): yield class ctx2: def __enter__(self): pass def __exit__(self, *args): pass t1 = timeit.timeit("with ctx1(): pass", setup="from __main__ import ctx1") t2 = timeit.timeit("with ctx2(): pass", setup=