@@ -852,6 +852,14 @@ msgid ""
852852" def __exit__(self, *exc):\n"
853853" return False"
854854msgstr ""
855+ "from contextlib import ContextDecorator\n"
856+ "\n"
857+ "class mycontext(ContextBaseClass, ContextDecorator):\n"
858+ " def __enter__(self):\n"
859+ " return self\n"
860+ "\n"
861+ " def __exit__(self, *exc):\n"
862+ " return False"
855863
856864#: ../../library/contextlib.rst:464
857865msgid ""
8000
div>
@@ -886,6 +894,17 @@ msgid ""
886894" print('Finishing')\n"
887895" return False"
888896msgstr ""
897+ "from asyncio import run\n"
898+ "from contextlib import AsyncContextDecorator\n"
899+ "\n"
900+ "class mycontext(AsyncContextDecorator):\n"
901+ " async def __aenter__(self):\n"
902+ " print('Starting')\n"
903+ " return self\n"
904+ "\n"
905+ " async def __aexit__(self, *exc):\n"
906+ " print('Finishing')\n"
907+ " return False"
889908
890909#: ../../library/contextlib.rst:492
891910msgid ""
@@ -907,6 +926,23 @@ msgid ""
907926"The bit in the middle\n"
908927"Finishing"
909928msgstr ""
929+ ">>> @mycontext()\n"
930+ "... async def function():\n"
931+ "... print('The bit in the middle')\n"
932+ "...\n"
933+ ">>> run(function())\n"
934+ "Starting\n"
935+ "The bit in the middle\n"
936+ "Finishing\n"
937+ "\n"
938+ ">>> async def function():\n"
939+ "... async with mycontext():\n"
940+ "... print('The bit in the middle')\n"
941+ "...\n"
942+ ">>> run(function())\n"
943+ "Starting\n"
944+ "The bit in the middle\n"
945+ "Finishing"
910946
911947#: ../../library/contextlib.rst:515
912948msgid ""
@@ -929,6 +965,11 @@ msgid ""
929965" # the with statement, even if attempts to open files later\n"
930966" # in the list raise an exception"
931967msgstr ""
968+ "with ExitStack() as stack:\n"
969+ " files = [stack.enter_context(open(fname)) for fname in filenames]\n"
970+ " # 所有已打开的文件都将在 with 语句结束时\n"
971+ " # 自动被关闭,即使此后打开列表中文件的\n"
972+ " # 尝试引发了异常"
932973
933974#: ../../library/contextlib.rst:528
934975msgid ""
@@ -1077,6 +1118,14 @@ msgid ""
10771118" # they will remain open even after the with statement ends.\n"
10781119" # close_files() can then be invoked explicitly to close them all."
10791120msgstr ""
1121+ "with ExitStack() as stack:\n"
1122+ " files = [stack.enter_context(open(fname)) for fname in filenames]\n"
1123+ " # 持有 close 方法,但暂时不调用它。\n"
1124+ " close_files = stack.pop_all().close\n"
1125+ " # 如果任何文件打开失败,则所有之前打开的文件\n"
1126+ " # 将自动被关闭。 如果所有文件都被成功地打开,\n"
1127+ " # 即使在 with 语句结束后它们仍将保持打开状态。\n"
1128+ " # 随后可以显式唤起 close_files() 来全部关闭它们。"
10801129
10811130#: ../../library/contextlib.rst:617
10821131msgid ""
@@ -1142,6 +1191,12 @@ msgid ""
11421191" # the async with statement, even if attempts to open a connection\n"
11431192" # later in the list raise an exception."
11441193msgstr ""
1194+ "async with AsyncExitStack() as stack:\n"
1195+ " connections = [await stack.enter_async_context(get_connection())\n"
1196+ " for i in range(5)]\n"
1197+ " # 所有已打开连接都将在 async with 语句结束时\n"
1198+ " # 自动被关闭,即使此后打开列表中连接的尝试\n"
1199+ " # 引发了异常。"
11451200
11461201#: ../../library/contextlib.rst:666
11471202msgid "Examples and Recipes"
@@ -1180,6 +1235,13 @@ msgid ""
11801235" stack.callback(release_special_resource, special)\n"
11811236" # Perform operations that use the acquired resources"
11821237msgstr ""
1238+ "with ExitStack() as stack:\n"
1239+ " for resource in resources:\n"
1240+ " stack.enter_context(resource)\n"
1241+ " if need_special_resource():\n"
1242+ " special = acquire_special_resource()\n"
1243+ " stack.callback(release_special_resource, special)\n"
1244+ " # 执行使用了所获取资源的操作"
11831245
11841246#: ../../library/contextlib.rst:690
11851247msgid ""
@@ -1215,6 +1277,14 @@ msgid ""
12151277" with stack:\n"
12161278" # Handle normal case"
12171279msgstr ""
1280+ "stack = ExitStack()\n"
1281+ "try:\n"
1282+ " x = stack.enter_context(cm)\n"
1283+ "except Exception:\n"
1284+ " # 处理 __enter__ 异常\n"
1285+ "else:\n"
1286+ " with stack:\n"
1287+ " # 处理正常情况"
1218<
9E88
/code>1288
12191289#: ../../library/contextlib.rst:713
12201290msgid ""
@@ -1313,6 +1383,14 @@ msgid ""
13131383" if cleanup_needed:\n"
13141384" cleanup_resources()"
13151385msgstr ""
1386+ "cleanup_needed = True\n"
1387+ "try:\n"
1388+ " result = perform_operation()\n"
1389+ " if result:\n"
1390+ " cleanup_needed = False\n"
1391+ "finally:\n"
1392+ " if cleanup_needed:\n"
1393+ " cleanup_resources()"
13161394
13171395#: ../../library/contextlib.rst:785
13181396msgid ""
@@ -1339,6 +1417,13 @@ msgid ""
13391417" if result:\n"
13401418" stack.pop_all()"
13411419msgstr ""
1420+ "from contextlib import ExitStack\n"
1421+ "\n"
1422+ "with ExitStack() as stack:\n"
1423+ " stack.callback(cleanup_resources)\n"
1424+ " result = perform_operation()\n"
1425+ " if result:\n"
1426+ " stack.pop_all()"
13421427
13431428#: ../../library/contextlib.rst:801
13441429msgid ""
0 commit comments