@@ -8,7 +8,7 @@ msgstr ""
8
8
"Project-Id-Version : Python 3.12\n "
9
9
"Report-Msgid-Bugs-To : \n "
10
10
"POT-Creation-Date : 2023-09-09 00:03+0000\n "
11
- "PO-Revision-Date : 2024-03-28 22:08 +0800\n "
11
+ "PO-Revision-Date : 2024-04-02 21:55 +0800\n "
12
12
"Last-Translator : Liang-Bo Wang <me@liang2.tw>\n "
13
13
"Language-Team : Chinese - TAIWAN (https://github.com/python/python-docs-zh- "
14
14
"tw)\n "
@@ -1136,14 +1136,16 @@ msgstr ""
1136
1136
1137
1137
#: ../../library/unittest.mock-examples.rst:1128
1138
1138
msgid "Mocking imports with patch.dict"
1139
- msgstr ""
1139
+ msgstr "使用 patch.dict 來 mock import "
1140
1140
1141
1141
#: ../../library/unittest.mock-examples.rst:1130
1142
1142
msgid ""
1143
1143
"One situation where mocking can be hard is where you have a local import "
1144
1144
"inside a function. These are harder to mock because they aren't using an "
1145
1145
"object from the module namespace that we can patch out."
1146
1146
msgstr ""
1147
+ "可能讓 mock 會很困難的一種情況是在函式內部進行區域 import。這些狀況會更難進"
1148
+ "行 mock,因為它們沒有使用我們可以 patch out 的模組命名空間中的物件。"
1147
1149
1148
1150
#: ../../library/unittest.mock-examples.rst:1134
1149
1151
msgid ""
@@ -1154,6 +1156,11 @@ msgid ""
1154
1156
"an unconditional local import (store the module as a class or module "
1155
1157
"attribute and only do the import on first use)."
1156
1158
msgstr ""
1159
+ "一般來說,我們應該避免區域 import 的發生。有時這樣做是為了防止循環相依 "
1160
+ "(circular dependencies),為此\\ *通常*\\ 有更好的方式來解決問題(例如重構程式"
1161
+ "碼)或透過延遲 import 來防止「前期成本 (up front costs)」。這也可以透過比無條"
1162
+ "件的區域 import 更好的方式來解決(例如將模組儲存為類別或模組屬性,並且僅在第"
1163
+ "一次使用時進行 import)。"
1157
1164
1158
1165
#: ../../library/unittest.mock-examples.rst:1141
1159
1166
msgid ""
@@ -1164,6 +1171,10 @@ msgid ""
1164
1171
"in ``sys.modules``, so usually when you import something you get a module "
1165
1172
"back. This need not be the case however."
1166
1173
msgstr ""
1174
+ "除此之外,還有一種方法可以使用 ``mock`` 來影響 import 的結果。Import 會從 :"
1175
+ "data:`sys.modules` 字典中取得一個\\ *物件*。請注意,它會取得一個\\ *物件*,而"
1176
+ "該物件不一定是一個模組。初次 import 模組會導致模組物件被放入 ``sys.modules`` "
1177
+ "中,因此通常當你 import 某些東西時,你會得到一個模組。但情況並非總是如此。"
1167
1178
1168
1179
#: ../../library/unittest.mock-examples.rst:1148
1169
1180
msgid ""
@@ -1173,6 +1184,10 @@ msgid ""
1173
1184
"the with statement body is complete or ``patcher.stop()`` is called) then "
1174
1185
"whatever was there previously will be restored safely."
1175
1186
msgstr ""
1187
+ "這代表你可以使用 :func:`patch.dict` 來\\ *暫時*\\ 在 :data:`sys.modules` 中原"
1188
+ "地放置 mock。在這個 patch 存活時的任何 import 都會取得這個 mock。當 patch 完"
1189
+ "成時(被裝飾的函式結束、with 陳述式主體完成或 ``patcher.stop()`` 被呼叫),那"
1190
+ "麼之前在 ``sys.modules`` 中的任何內容都會被安全地復原。"
1176
1191
1177
1192
#: ../../library/unittest.mock-examples.rst:1154
1178
1193
msgid "Here's an example that mocks out the 'fooble' module."
0 commit comments