On 10/23/05, Phillip J. Eby <[EMAIL PROTECTED]> wrote: > Actually, you've just pointed out a new complication introduced by having > __context__. The return value of __context__ is supposed to have an > __enter__ and an __exit__. Is it a type error if it doesn't? How do we > handle that, exactly? > > That is, assuming generators don't have enter/exit/context methods, then > the above code is broken because its __context__ returns an object without > enter/exit, sort of like an __iter__ that returns something without a > 'next()'.
I would have thought that the parallel with __iter__ would be the right way to go: >>> class C: ... def __iter__(self): ... return 12 ... >>> c = C() >>> iter(c) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: __iter__ returned non-iterator of type 'int' >>> So, when you try calling __context__ in a with statement (or I guess in a context() builtin if one were to be added), raise a TypeError if the resulting object doesn't have __enter__ and __exit__ methods. (Or maybe just if it has neither - I can't recall if the methods are optional, but certainly having neither is wrong). Paul. _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com