全部博文(130)
分类: LINUX
2016-02-26 13:49:17
点击(此处)折叠或打开
The statement is used to wrap the execution of a block with methods defined by a context manager (see section ). This allows common ...... usage patterns to be encapsulated for convenient reuse.
The execution of the statement with one “item” proceeds as follows:
The context expression (the expression given in the ) is evaluated to obtain a context manager.
The context manager’s is loaded for later use.
The context manager’s method is invoked.
If a target was included in the statement, the return value from is assigned to it.
Note
The statement guarantees that if the method returns without an error, then will always be called. Thus, if an error occurs during the assignment to the target list, it will be treated the same as an error occurring within the suite would be. See step 6 below.
The suite is executed.
The context manager’s method is invoked. If an exception caused the suite to be exited, its type, value, and traceback are passed as arguments to . Otherwise, three arguments are supplied.
If the suite was exited due to an exception, and the return value from the method was false, the exception is reraised. If the return value was true, the exception is suppressed, and execution continues with the statement following the statement.
If the suite was exited for any reason other than an exception, the return value from is ignored, and execution proceeds at the normal location for the kind of exit that was taken.
With more than one item, the context managers are processed as if multiple statements were nested:
is equivalent to