@@ -1988,6 +1988,10 @@ msgid ""
1988
1988
"graph. Buffers accumulated by the *buffer_callback* will not see their data "
1989
1989
"copied into the pickle stream, only a cheap marker will be inserted."
1990
1990
msgstr ""
1991
+ "傳送端需要傳遞一個調用緩衝區的回呼函數給 :class:`Pickler`(或 :func:`dump` "
1992
+ "或 :func:`dumps` 函數)的 *buffer_callback* 引數,使每次生成 :class:"
1993
+ "`PickleBuffer` 時,該物件在處理物件圖時能被呼叫。除了一個簡易標記以外,由 "
1994
+ "*buffer_callback* 累積的緩衝區資料不會被複製到 pickle 串流中。"
1991
1995
1992
1996
#: ../../library/pickle.rst:991
1993
1997
msgid ""
@@ -1999,6 +2003,12 @@ msgid ""
1999
2003
"reconstructors of the objects whose pickling produced the original :class:"
2000
2004
"`PickleBuffer` objects."
2001
2005
msgstr ""
2006
+ "接收端需要傳遞一個緩衝區物件給 :class:`Unpickler`(或 :func:`load` 或 :func:"
2007
+ "`loads` 函式)的 *buffers* 引數。該物件須是一個可疊代的(iterable)緩衝區"
2008
+ "(buffer)物件,其中包含傳遞給 *buffer_callback* 的緩衝區物件。這個可疊代物件"
2009
+ "的緩衝區順序應該與它們當初被封裝時傳遞給 *buffer_callback* 的順序相同。這些緩"
2010
+ "衝區將提供物件重建所需的資料,以使重建器能還原出那個當時產生了 :class:"
2011
+ "`PickleBuffer` 的物件。"
2002
2012
2003
2013
#: ../../library/pickle.rst:999
2004
2014
msgid ""
@@ -2007,6 +2017,8 @@ msgid ""
2007
2017
"Potential optimizations include the use of shared memory or datatype-"
2008
2018
"dependent compression."
2009
2019
msgstr ""
2020
+ "在傳送與接收端之間,通訊系統可以自由實作轉移帶外緩衝區資料的機制。該機制可能"
2021
+ "可以利用共用記憶體機制或根據資料類型特定的壓縮方式來最佳化執行速度。"
2010
2022
2011
2023
#: ../../library/pickle.rst:1005
2012
2024
msgid "Example"
@@ -2017,6 +2029,8 @@ msgid ""
2017
2029
"Here is a trivial example where we implement a :class:`bytearray` subclass "
2018
2030
"able to participate in out-of-band buffer pickling::"
2019
2031
msgstr ""
2032
+ "這一個簡單的範例展示了如何實作一個可以參與帶外緩衝區封裝的 :class:"
2033
+ "`bytearray` 子類別:::"
2020
2034
2021
2035
#: ../../library/pickle.rst:1010
2022
2036
msgid ""
@@ -2041,19 +2055,42 @@ msgid ""
2041
2055
" else:\n"
2042
2056
" return cls(obj)"
2043
2057
msgstr ""
2058
+ "class ZeroCopyByteArray(bytearray):\n"
2059
+ "\n"
2060
+ " def __reduce_ex__(self, protocol):\n"
2061
+ " if protocol >= 5:\n"
2062
+ " return type(self)._reconstruct, (PickleBuffer(self),), None\n"
2063
+ " else:\n"
2064
+ " # PickleBuffer 在 pickle 協定 <= 4 時禁止使用。\n"
2065
+ " return type(self)._reconstruct, (bytearray(self),)\n"
2066
+ "\n"
2067
+ " @classmethod\n"
2068
+ " def _reconstruct(cls, obj):\n"
2069
+ " with memoryview(obj) as m:\n"
2070
+ " # 取得對原始緩衝區物件的控制\n"
2071
+ " obj = m.obj\n"
2072
+ " if type(obj) is cls:\n"
2073
+ " # 若原本的緩衝區物件是 ZeroCopyByteArray,則直接回傳。\n"
2074
+ " return obj\n"
2075
+ " else:\n"
2076
+ " return cls(obj)"
2044
2077
2045
2078
#: ../../library/pickle.rst:1031
2046
2079
msgid ""
2047
2080
"The reconstructor (the ``_reconstruct`` class method) returns the buffer's "
2048
2081
"providing object if it has the right type. This is an easy way to simulate "
2049
2082
"zero-copy behaviour on this toy example."
2050
2083
msgstr ""
2084
+ "如果型別正確,重建器(``_reconstruct`` 類別方法)會返回當時提供緩衝區的物件。"
2085
+ "這個簡易實作可以模擬一個無複製行為的重建器。"
2051
2086
2052
2087
#: ../../library/pickle.rst:1035
2053
2088
msgid ""
2054
2089
"On the consumer side, we can pickle those objects the usual way, which when "
2055
2090
"unserialized will give us a copy of the original object::"
2056
2091
msgstr ""
2092
+ "在使用端,我們可以用一般的方式封裝這些物件,當我們拆封時會得到一個原始物件的"
2093
+ "副本:::"
2057
2094
2058
2095
#: ../../library/pickle.rst:1038
2059
2096
msgid ""
@@ -2063,12 +2100,19 @@ msgid ""
2063
2100
"print(b == new_b) # True\n"
2064
2101
"print(b is new_b) # False: a copy was made"
2065
2102
msgstr ""
2103
+ "b = ZeroCopyByteArray(b\" abc\" )\n"
2104
+ "data = pickle.dumps(b, protocol=5)\n"
2105
+ "new_b = pickle.loads(data)\n"
2106
+ "print(b == new_b) # True\n"
2107
+ "print(b is new_b) # False: 曾進行過複製運算"
2066
2108
2067
2109
#: ../../library/pickle.rst:1044
2068
2110
msgid ""
2069
2111
"But if we pass a *buffer_callback* and then give back the accumulated "
2070
2112
"buffers when unserializing, we are able to get back the original object::"
2071
2113
msgstr ""
2114
+ "但如果我們傳一個 *buffer_callback* 並在去序列化時正確返回積累的緩衝資料,我們"
2115
+ "就能拿回原始的物件:::"
2072
2116
2073
2117
#: ../../library/pickle.rst:1047
2074
2118
msgid ""
@@ -2079,6 +2123,12 @@ msgid ""
2079
2123
"print(b == new_b) # True\n"
2080
2124
"print(b is new_b) # True: no copy was made"
2081
2125
msgstr ""
2126
+ "b = ZeroCopyByteArray(b\" abc\" )\n"
2127
+ "buffers = []\n"
2128
+ "data = pickle.dumps(b, protocol=5, buffer_callback=buffers.append)\n"
2129
+ "new_b = pickle.loads(data, buffers=buffers)\n"
2130
+ "print(b == new_b) # True\n"
2131
+ "print(b is new_b) # True: 沒有進行過複製"
2082
2132
2083
2133
#: ../../library/pickle.rst:1054
2084
2134
msgid ""
@@ -2089,14 +2139,18 @@ msgid ""
2089
2139
"making as few copies as possible) when transferring between distinct "
2090
2140
"processes or systems."
2091
2141
msgstr ""
2142
+ "此範例是因為受限於 :class:`bytearray` 會自行分配記憶體:您無法創建以其他物件"
2143
+ "的記憶體為基礎的 :class:`bytearray` 實例。不過第三方資料型態(如 NumPy 陣列)"
2144
+ "則可能沒有這個限制,而允許在不同程序或系統之間傳輸資料時使用零拷貝封裝(或儘"
2145
+ "可能地減少拷貝次數)。"
2092
2146
2093
2147
#: ../../library/pickle.rst:1061
2094
2148
msgid ":pep:`574` -- Pickle protocol 5 with out-of-band data"
2095
- msgstr ""
2149
+ msgstr ":pep:`574` -- 第 5 版 Pickle 協定的帶外資料(out-of-band data)處裡 "
2096
2150
2097
2151
#: ../../library/pickle.rst:1067
2098
2152
msgid "Restricting Globals"
2099
- msgstr ""
2153
+ msgstr "限制全域物件 "
2100
2154
2101
2155
#: ../../library/pickle.rst:1072
2102
2156
msgid ""
@@ -2105,6 +2159,9 @@ msgid ""
2105
2159
"it permits the unpickler to import and invoke arbitrary code. Just consider "
2106
2160
"what this hand-crafted pickle data stream does when loaded::"
2107
2161
msgstr ""
2162
+ "預設情況下,拆封過程將會引入任何在 pickle 資料中找到的類別或函式。對於許多應"
2163
+ "用程式來說,這種行為是不可接受的,因為它讓拆封器能夠引入並執行任意程式碼。請"
2164
+ "參見以下 pickle 資料流在載入時的行為:::"
2108
2165
2109
2166
#: ../../library/pickle.rst:1077
2110
2167
msgid ""
@@ -2125,6 +2182,9 @@ msgid ""
2125
2182
"is inoffensive, it is not difficult to imagine one that could damage your "
2126
2183
"system."
2127
2184
msgstr ""
2185
+ "在這個例子中,拆封器會引入 :func:`os.system` 函式,然後執行命令「echo hello "
2186
+ "world」。雖然這個例子是無害的,但不難想像可以這個方式輕易執行任意可能對系統造"
2187
+ "成損害的命令。"
2128
2188
2129
2189
#: ../../library/pickle.rst:1086
2130
2190
msgid ""
@@ -2134,12 +2194,18 @@ msgid ""
2134
2194
"requested. Thus it is possible to either completely forbid globals or "
2135
2195
"restrict them to a safe subset."
2136
2196
msgstr ""
2197
+ "基於以上原因,您可能會希望透過自訂 :meth:`Unpickler.find_class` 來控制哪些是"
2198
+ "能夠被拆封的內容。與其名稱字面意義暗示的不同,實際上每當你請求一個全域物件"
2199
+ "(例如,類別或函式)時,就會調用 :meth:`Unpickler.find_class`。因此,可以透過"
2200
+ "這個方法完全禁止全域物件或將其限制在安全的子集合。"
2137
2201
2138
2202
#: ../../library/pickle.rst:1092
2139
2203
msgid ""
2140
2204
"Here is an example of an unpickler allowing only few safe classes from the :"
2141
2205
"mod:`builtins` module to be loaded::"
2142
2206
msgstr ""
2207
+ "以下是一個僅允許從 :mod:`builtins` 模組中載入少數安全類別的拆封器"
2208
+ "(unpickler)的例子:::"
2143
2209
2144
2210
#: ../../library/pickle.rst:1095
2145
2211
msgid ""
@@ -2169,10 +2235,35 @@ msgid ""
2169
2235
"\"\"\" Helper function analogous to pickle.loads().\"\"\" \n"
2170
2236
" return RestrictedUnpickler(io.BytesIO(s)).load()"
2171
2237
msgstr ""
2238
+ "import builtins\n"
2239
+ "import io\n"
2240
+ "import pickle\n"
2241
+ "\n"
2242
+ "safe_builtins = {\n"
2243
+ " 'range',\n"
2244
+ " 'complex',\n"
2245
+ " 'set',\n"
2246
+ " 'frozenset',\n"
2247
+ " 'slice',\n"
2248
+ "}\n"
2249
+ "\n"
2250
+ "class RestrictedUnpickler(pickle.Unpickler):\n"
2251
+ "\n"
2252
+ " def find_class(self, module, name):\n"
2253
+ " # 只允許幾個內建的安全類別\n"
2254
+ " if module ==\" builtins\" and name in safe_builtins:\n"
2255
+ " return getattr(builtins, name)\n"
2256
+ " # 完全禁止任何其他類別\n"
2257
+ " raise pickle.UnpicklingError(\" global '%s.%s' is forbidden\" %\n"
2258
+ " (module, name))\n"
2259
+ "\n"
2260
+ "def restricted_loads(s):\n"
2261
+ "\"\"\" 一個模擬 pickle.loads() 的輔助函數\"\"\" \n"
2262
+ " return RestrictedUnpickler(io.BytesIO(s)).load()"
2172
2263
2173
2264
#: ../../library/pickle.rst:1121
2174
2265
msgid "A sample usage of our unpickler working as intended::"
2175
- msgstr ""
2266
+ msgstr "我們剛才實作的的拆封器範例正常運作的樣子::: "
2176
2267
2177
2268
#: ../../library/pickle.rst:1123
2178
2269
msgid ""
@@ -2209,17 +2300,23 @@ msgid ""
2209
2300
"alternatives such as the marshalling API in :mod:`xmlrpc.client` or third-"
2210
2301
"party solutions."
2211
2302
msgstr ""
2303
+ "正如我們的範例所示,必須謹慎審視能被拆封的內容。因此,如果您的應用場景非常關"
2304
+ "心安全性,您可能需要考慮其他選擇,例如 :mod:`xmlrpc.client` 中的 marshalling "
2305
+ "API 或其他第三方解決方案。"
2212
2306
2213
2307
#: ../../library/pickle.rst:1147
2214
2308
msgid "Performance"
2215
- msgstr ""
2309
+ msgstr "效能 "
2216
2310
2217
2311
#: ../../library/pickle.rst:1149
2218
2312
msgid ""
2219
2313
"Recent versions of the pickle protocol (from protocol 2 and upwards) feature "
2220
2314
"efficient binary encodings for several common features and built-in types. "
2221
2315
"Also, the :mod:`pickle` module has a transparent optimizer written in C."
2222
2316
msgstr ""
2317
+ "較近期的 pickle 協定版本(從 2 版協定開始)為多種常見功能和內建型別提供了高效"
2318
+ "率的二進位編碼。此外,:mod:`pickle` 模組還具備一個透明化的、以 C 語言編寫的最"
2319
+ "佳化工具。"
2223
2320
2224
2321
#: ../../library/pickle.rst:1157
2225
2322
msgid "Examples"
@@ -2228,7 +2325,7 @@ msgstr "範例"
2228
2325
#: ../../library/pickle.rst:1159
2229
2326
msgid ""
2230
2327
"For the simplest code, use the :func:`dump` and :func:`load` functions. ::"
2231
- msgstr ""
2328
+ msgstr "最簡單的使用方式,調用 :func:`dump` 和 :func:`load` 函式。:: "
2232
2329
2233
2330
#: ../../library/pickle.rst:1161
2234
2331
msgid ""
@@ -2245,10 +2342,22 @@ msgid ""
2245
2342
" # Pickle the 'data' dictionary using the highest protocol available.\n"
2246
2343
" pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)"
2247
2344
msgstr ""
2345
+ "import pickle\n"
2346
+ "\n"
2347
+ "# 任意 pickle 支援的物件。\n"
2348
+ "data = {\n"
2349
+ " 'a': [1, 2.0, 3+4j],\n"
2350
+ " 'b': (\" string\" , b\" byte string\" ),\n"
2351
+ " 'c': {None, True, False}\n"
2352
+ "}\n"
2353
+ "\n"
2354
+ "with open('data.pickle', 'wb') as f:\n"
2355
+ " # 使用可用的最高協定來封裝 'data' 字典。\n"
2356
+ " pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)\n"
2248
2357
2249
2358
#: ../../library/pickle.rst:1175
2250
2359
msgid "The following example reads the resulting pickled data. ::"
2251
- msgstr ""
2360
+ msgstr "以下範例可以讀取前述程式所封裝的 pickle 資料。:: "
2252
2361
2253
2362
#: ../../library/pickle.rst:1177
2254
2363
msgid ""
@@ -2259,72 +2368,81 @@ msgid ""
2259
2368
" # have to specify it.\n"
2260
2369
" data = pickle.load(f)"
2261
2370
msgstr ""
2371
+ "import pickle\n"
2372
+ "\n"
2373
+ "with open('data.pickle', 'rb') as f:\n"
2374
+ " # 會自動檢測資料使用的協定版本,因此我們不需要手動指定。\n"
2375
+ " data = pickle.load(f)"
2262
2376
2263
2377
#: ../../library/pickle.rst:1191
2264
2378
msgid "Module :mod:`copyreg`"
2265
2379
msgstr ":mod:`copyreg` 模組"
2266
2380
2267
2381
#: ../../library/pickle.rst:1192
2268
2382
msgid "Pickle interface constructor registration for extension types."
2269
- msgstr ""
2383
+ msgstr "註冊擴充型別的 Pickle 介面建構子。 "
2270
2384
2271
2385
#: ../../library/pickle.rst:1194
2272
2386
msgid "Module :mod:`pickletools`"
2273
2387
msgstr ":mod:`pickletools` 模組"
2274
2388
2275
2389
#: ../../library/pickle.rst:1195
2276
2390
msgid "Tools for working with and analyzing pickled data."
2277
- msgstr ""
2391
+ msgstr "用於分析或處裡被封裝資料的工具。 "
2278
2392
2279
2393
#: ../../library/pickle.rst:1197
2280
2394
msgid "Module :mod:`shelve`"
2281
2395
msgstr ":mod:`shelve` 模組"
2282
2396
2283
2397
#: ../../library/pickle.rst:1198
2284
2398
msgid "Indexed databases of objects; uses :mod:`pickle`."
2285
- msgstr ""
2399
+ msgstr "索引式資料庫;使用 :mod:`pickle` 實作。 "
2286
2400
2287
2401
#: ../../library/pickle.rst:1200
2288
2402
msgid "Module :mod:`copy`"
2289
2403
msgstr ":mod:`copy` 模組"
2290
2404
2291
2405
#: ../../library/pickle.rst:1201
2292
2406
msgid "Shallow and deep object copying."
2293
- msgstr ""
2407
+ msgstr "物件的淺層或深度拷貝。 "
2294
2408
2295
2409
#: ../../library/pickle.rst:1203
2296
2410
msgid "Module :mod:`marshal`"
2297
2411
msgstr ":mod:`marshal` 模組"
2298
2412
2299
2413
#: ../../library/pickle.rst:1204
2300
2414
msgid "High-performance serialization of built-in types."
2301
- msgstr ""
2415
+ msgstr "內建型別的高效能序列化。 "
2302
2416
2303
2417
#: ../../library/pickle.rst:1208
2304
2418
msgid "Footnotes"
2305
2419
msgstr "註解"
2306
2420
2307
2421
#: ../../library/pickle.rst:1209
2308
2422
msgid "Don't confuse this with the :mod:`marshal` module"
2309
- msgstr ""
2423
+ msgstr "不要將此模組與 :mod:`marshal` 模組混淆 "
2310
2424
2311
2425
#: ../../library/pickle.rst:1211
2312
2426
msgid ""
2313
2427
"This is why :keyword:`lambda` functions cannot be pickled: all :keyword:`!"
2314
2428
"lambda` functions share the same name: ``<lambda>``."
2315
2429
msgstr ""
2430
+ "這就是為什麼 :keyword:`lambda` 函式無法被封裝:所有 :keyword:`!lambda` 函式共"
2431
+ "享相同的名稱:``<lambda>``。"
2316
2432
2317
2433
#: ../../library/pickle.rst:1214
2318
2434
msgid ""
2319
2435
"The exception raised will likely be an :exc:`ImportError` or an :exc:"
2320
2436
"`AttributeError` but it could be something else."
2321
2437
msgstr ""
2438
+ "拋出的例外應該是 :exc:`ImportError` 或 :exc:`AttributeError`,但也可能是其他"
2439
+ "例外。"
2322
2440
2323
2441
#: ../../library/pickle.rst:1217
2324
2442
msgid ""
2325
2443
"The :mod:`copy` module uses this protocol for shallow and deep copying "
2326
2444
"operations."
2327
- msgstr ""
2445
+ msgstr ":mod:`copy` 模組使用此協定進行淺層及深層複製操作。 "
2328
2446
2329
2447
#: ../../library/pickle.rst:1220
2330
2448
msgid ""
@@ -2333,6 +2451,9 @@ msgid ""
2333
2451
"kind of newline characters occurs in persistent IDs, the resulting pickled "
2334
2452
"data will become unreadable."
2335
2453
msgstr ""
2454
+ "協定 0 中限制僅能使用英文字母或數字字元來分配持久化 ID 是因為持久化 ID 是由換"
2455
+ "行符號所分隔的。因此,如果持久化 ID 中出現任何形式的換行字元,將導致封裝資料"
2456
+ "變得無法讀取。"
2336
2457
2337
2458
#: ../../library/pickle.rst:12
2338
2459
msgid "persistence"
@@ -2364,7 +2485,7 @@ msgstr "pickling"
2364
2485
2365
2486
#: ../../library/pickle.rst:123
2366
2487
msgid "External Data Representation"
2367
- msgstr "External Data Representation(外部資料表示法 )"
2488
+ msgstr "External Data Representation(外部資料表現 )"
2368
2489
2369
2490
#: ../../library/pickle.rst:664
2370
2491
msgid "copy"