<div>Thanks for your reply.</div><div>But the answer is not I except, I will show you some examples to explain what result I except:</div><div><br></div><div>@contextmanager</div><div>def cm():</div><div>    print('open file')</div><div>    yield</div><div>    print('close file')</div><div>with cm():</div><div>    1/0</div><div><br></div><div>If I use a contextmanager ,I except it can help me to close the file anytime,even raise an error,</div><div>but if I define a function with @contextmanager like the example which I have showed for you, </div><div>it will never print('close file')</div><div><br></div><div>I can only modify it like this:</div><div>@contextmanager</div><div>def cm():</div><div>    try:</div><div>        print('open file')</div><div>        yield</div><div>    except Exception as e:</div><div>        print('Error',e)</div><div>    finally:</div><div>        print('close file')</div><div><br></div><div>It is not friendly for us to use it, so I modify the contextlib to fix it,you can catch it from the e-mail attachment.</div><div>It's in the line 79 and line 97</div><div id="origbody"><div style="background: #f2f2f2;"><span style="background-color: rgb(255, 255, 255);">----------------------------------------------------------------------</span><br>发件人:python-ideas-request@python.org<br>收件人:python-ideas@python.org<br>主题:Python-ideas Digest, Vol 146, Issue 13<br>日期:2019年01月06日 01点05分<br></div><br><br>Send Python-ideas mailing list submissions to<br>      python-ideas@python.org<br>To subscribe or unsubscribe via the World Wide Web, visit<br>    https://mail.python.org/mailman/listinfo/python-ideas<br>or, via email, send a message with subject or body 'help' to<br>   python-ideas-request@python.org<br>You can reach the person managing the list at<br>        python-ideas-owner@python.org<br>When replying, please edit your Subject line so it is more specific<br>than "Re: Contents of Python-ideas digest..."<br>Today's Topics:<br>   1. Re: Make the @contextmanager of contextlib to be a real<br>      contextmanager (Serhiy Storchaka)<br>   2. Re: Fixed point format for numbers with locale based<br>      separators (?ukasz Stelmach)<br>----------------------------------------------------------------------<br>Message: 1<br>Date: Sat, 5 Jan 2019 16:45:34 +0200<br>From: Serhiy Storchaka <storchaka@gmail.com><br>To: python-ideas@python.org<br>Subject: Re: [Python-ideas] Make the @contextmanager of contextlib to<br>        be a real contextmanager<br>Message-ID: <q0qfqd$q1q$1@blaine.gmane.org><br>Content-Type: text/plain; charset=UTF-8; format=flowed<br>05.01.19 14:52, Moon?sun ????:<br>> As we know,when we import the module--'contextlib',we can use the <br>> decorator '@contextmanager' and keyword ?yield? to make a 'instance' of <br>> Class '_GeneratorContextManager' in 'contextlib' module,then we can use <br>> it like:<br>> with 'instance' as 'xx':<br>>  ? ? 'code block'<br>>  ? ? pass<br>> But there is a little bug,when the code block raise a error,the instance <br>> cannot run the code which after the keyword 'yield'.<br>This is not a bug.<br>Consider the following example:<br>     @contextmanager<br>     def cm():<br>         try:<br>             yield<br>         except BaseException as err:<br>             print('Fail:', err)<br>             raise<br>         else:<br>             print('Success')<br>     with cm():<br>         1/0<br>What result would you expect? Test it with the stdlib implementation and <br>with your implementation.<br>------------------------------<br>Message: 2<br>Date: Sat, 05 Jan 2019 16:41:20 +0100<br>From: ?ukasz Stelmach <steelman@post.pl><br>To: Steven D'Aprano <steve@pearwood.info><br>Cc: python-ideas@python.org<br>Subject: Re: [Python-ideas] Fixed point format for numbers with locale<br>   based separators<br>Message-ID: <87y37z9knz.fsf%steelman@post.pl><br>Content-Type: text/plain; charset="utf-8"<br>Steven D'Aprano <steve@pearwood.info> writes:<br>> On Fri, Jan 04, 2019 at 03:57:53PM +0100, ?ukasz Stelmach wrote:<br>>> Hi,<br>>> <br>>> I would like to present two pull requests[1][2] implementing fixed point<br>>> presentation of numbers and ask for comments. The first is mine. I<br>>> learnt about the second after publishing mine.<br>><br>> Before I look at the implementation, can you explain the functional <br>> requirements please?<br>As I stated in the original message below the table:<br>>> In the application I want to create I am going to present users numbers<br>>> ranging up to 3 orders of magnitude and I (my users) want them to be<br>>> presented consistently with regards to number of decimal digits AND I<br>>> want to conform to rules of languages of my users. And I would like to<br>>> avoid the exponent notation by all means.<br>The pint[1] library I use, implements formatting of physical quantities<br>using the format()/__format__ code. As far as I can tell my patch for<br>Python is shorter and more straightforward than a patch for pint to use<br>locale.format().<br>Because the "g" based "n" formatter has been present since the advanced<br>string formatting was described in PEP-3101, I think it is necessary to<br>add the "m" formatter based on "f". The advanced string formatting<br>facility in Python is very convenient and programmers shouldn't forced<br>to use locale.format() like this<br>    "The total length of {} sticks is {} meters.".format(n_sticks, locale.format(".2f", l_sticks))<br>instead of<br>    "The total length of {} sticks is {:.2f} meters.".format(n_sticks, l_sticks)<br>> In other words, what is the new feature you hope to have excepted? <br>> Explain the intention and the API (the interface). The implementation is <br>> the least important part :-)<br>I wish to add a new formatter "m" for float/complex/decimal numbers,<br>which behaves like the existing "f", but uses the decimal separator from<br>the locale database. There is "n" formmatter which behaves like "g" but<br>it does not fit my needs.<br>> [...]<br>>> Formatting 1.23456789 * n (LC_ALL=3Dpl_PL.UTF-8)<br>>>     | n |    ".2f" |    ".3n" |<br>>>     |---+----------+----------|<br>>>     | 1 |     1.23 |     1,23 |<br>>>     | 2 |    12.35 |     12,3 |<br>>>     | 3 |   123.46 |      123 |<br>>>     | 4 |  1234.57 | 1,23e+03 |<br>><br>> I'm afraid I cannot work out what that table means. You say "Formatting <br>> 1.23... * n" (multiplying by n) but the results shown aren't multiplied <br>> by n=2, n=3, n=4 as the table suggests.<br>><br>> Can you show what Python code you expect will produce the expected <br>> output?<br>    for n in range(1,5): print("| {} | {:8.2f} | {:8.3n} |".format(n,1.23456789 * 10**n, 1.23456789 * 10**n))<br>[1] http://pint.readthedocs.io/<br>-- <br>By?o mi bardzo mi?o.                                  --- Rurku. --- ...<br>>?ukasz<                                --- To dobrze, ?e mnie s?uchasz.<br>-------------- next part --------------<br>A non-text attachment was scrubbed...<br>Name: signature.asc<br>Type: application/pgp-signature<br>Size: 617 bytes<br>Desc: not available<br>URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190105/9a2fc15d/attachment-0001.sig><br>------------------------------<br>Subject: Digest Footer<br>_______________________________________________<br>Python-ideas mailing list<br>Python-ideas@python.org<br>https://mail.python.org/mailman/listinfo/python-ideas<br>------------------------------<br>End of Python-ideas Digest, Vol 146, Issue 13<br>*********************************************<br></div>