Thanks for your reply.
But the answer is not I except, I will show you some examples to explain what result I except:

def cm():
    print('open file')
    print('close file')
with cm():

If I use a contextmanager ,I except it can help me to close the file anytime,even raise an error,
but if I define a function with @contextmanager like the example which I have showed for you, 
it will never print('close file')

I can only modify it like this:
def cm():
        print('open file')
    except Exception as e:
        print('close file')

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.
It's in the line 79 and line 97
主题:Python-ideas Digest, Vol 146, Issue 13
日期:2019年01月06日 01点05分

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