@@ -1988,6 +1988,10 @@ msgid ""
19881988"graph. Buffers accumulated by the *buffer_callback* will not see their data "
19891989"copied into the pickle stream, only a cheap marker will be inserted."
19901990msgstr ""
1991+ "傳送端需要傳遞一個調用緩衝區的回呼函數給 :class:`Pickler`(或 :func:`dump` "
1992+ "或 :func:`dumps` 函數)的 *buffer_callback* 引數,使每次生成 :class:"
1993+ "`PickleBuffer` 時,該物件在處理物件圖時能被呼叫。除了一個簡易標記以外,由 "
1994+ "*buffer_callback* 累積的緩衝區資料不會被複製到 pickle 串流中。"
19911995
19921996#: ../../library/pickle.rst:991
19931997msgid ""
@@ -1999,6 +2003,12 @@ msgid ""
19992003"reconstructors of the objects whose pickling produced the original :class:"
20002004"`PickleBuffer` objects."
20012005msgstr ""
2006+ "接收端需要傳遞一個緩衝區物件給 :class:`Unpickler`(或 :func:`load` 或 :func:"
2007+ "`loads` 函式)的 *buffers* 引數。該物件須是一個可疊代的(iterable)緩衝區"
2008+ "(buffer)物件,其中包含傳遞給 *buffer_callback* 的緩衝區物件。這個可疊代物件"
2009+ "的緩衝區順序應該與它們當初被封裝時傳遞給 *buffer_callback* 的順序相同。這些緩"
2010+ "衝區將提供物件重建所需的資料,以使重建器能還原出那個當時產生了 :class:"
2011+ "`PickleBuffer` 的物件。"
20022012
20032013#: ../../library/pickle.rst:999
20042014msgid ""
@@ -2007,6 +2017,8 @@ msgid ""
20072017"Potential optimizations include the use of shared memory or datatype-"
20082018"dependent compression."
20092019msgstr ""
2020+ "在傳送與接收端之間,通訊系統可以自由實作轉移帶外緩衝區資料的機制。該機制可能"
2021+ "可以利用共用記憶體機制或根據資料類型特定的壓縮方式來最佳化執行速度。"
20102022
20112023#: ../../library/pickle.rst:1005
20122024msgid "Example"
@@ -2017,6 +2029,8 @@ msgid ""
20172029"Here is a trivial example where we implement a :class:`bytearray` subclass "
20182030"able to participate in out-of-band buffer pickling::"
20192031msgstr ""
2032+ "這一個簡單的範例展示了如何實作一個可以參與帶外緩衝區封裝的 :class:"
2033+ "`bytearray` 子類別:::"
20202034
20212035#: ../../library/pickle.rst:1010
20222036msgid ""
@@ -2041,19 +2055,42 @@ msgid ""
20412055" else:\n"
20422056" return cls(obj)"
20432057msgstr ""
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)"
20442077
20452078#: ../../library/pickle.rst:1031
20462079msgid ""
20472080"The reconstructor (the ``_reconstruct`` class method) returns the buffer's "
20482081"providing object if it has the right type. This is an easy way to simulate "
20492082"zero-copy behaviour on this toy example."
20502083msgstr ""
2084+ "如果型別正確,重建器(``_reconstruct`` 類別方法)會返回當時提供緩衝區的物件。"
2085+ "這個簡易實作可以模擬一個無複製行為的重建器。"
20512086
20522087#: ../../library/pickle.rst:1035
20532088msgid ""
20542089"On the consumer side, we can pickle those objects the usual way, which when "
20552090"unserialized will give us a copy of the original object::"
20562091msgstr ""
2092+ "在使用端,我們可以用一般的方式封裝這些物件,當我們拆封時會得到一個原始物件的"
2093+ "副本:::"
20572094
20582095#: ../../library/pickle.rst:1038
20592096msgid ""
@@ -2063,12 +2100,19 @@ msgid ""
20632100"print(b == new_b) # True\n"
20642101"print(b is new_b) # False: a copy was made"
20652102msgstr ""
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: 曾進行過複製運算"
20662108
20672109#: ../../library/pickle.rst:1044
20682110msgid ""
20692111"But if we pass a *buffer_callback* and then give back the accumulated "
20702112"buffers when unserializing, we are able to get back the original object::"
20712113msgstr ""
2114+ "但如果我們傳一個 *buffer_callback* 並在去序列化時正確返回積累的緩衝資料,我們"
2115+ "就能拿回原始的物件:::"
20722116
20732117#: ../../library/pickle.rst:1047
20742118msgid ""
@@ -2079,6 +2123,12 @@ msgid ""
20792123"print(b == new_b) # True\n"
20802124"print(b is new_b) # True: no copy was made"
20812125msgstr ""
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: 沒有進行過複製"
20822132
20832133#: ../../library/pickle.rst:1054
20842134msgid ""
@@ -2089,14 +2139,18 @@ msgid ""
20892139"making as few copies as possible) when transferring between distinct "
20902140"processes or systems."
20912141msgstr ""
2142+ "此範例是因為受限於 :class:`bytearray` 會自行分配記憶體:您無法創建以其他物件"
2143+ "的記憶體為基礎的 :class:`bytearray` 實例。不過第三方資料型態(如 NumPy 陣列)"
2144+ "則可能沒有這個限制,而允許在不同程序或系統之間傳輸資料時使用零拷貝封裝(或儘"
2145+ "可能地減少拷貝次數)。"
20922146
20932147#: ../../library/pickle.rst:1061
20942148msgid ":pep:`574` -- Pickle protocol 5 with out-of-band data"
2095- msgstr ""
2149+ msgstr ":pep:`574` -- 第 5 版 Pickle 協定的帶外資料(out-of-band data)處裡 "
20962150
20972151#: ../../library/pickle.rst:1067
20982152msgid "Restricting Globals"
2099- msgstr ""
2153+ msgstr "限制全域物件 "
21002154
21012155#: ../../library/pickle.rst:1072
21022156msgid ""
@@ -2105,6 +2159,9 @@ msgid ""
21052159"it permits the unpickler to import and invoke arbitrary code. Just consider "
21062160"what this hand-crafted pickle data stream does when loaded::"
21072161msgstr ""
2162+ "預設情況下,拆封過程將會引入任何在 pickle 資料中找到的類別或函式。對於許多應"
2163+ "用程式來說,這種行為是不可接受的,因為它讓拆封器能夠引入並執行任意程式碼。請"
2164+ "參見以下 pickle 資料流在載入時的行為:::"
21082165
21092166#: ../../library/pickle.rst:1077
21102167msgid ""
@@ -2125,6 +2182,9 @@ msgid ""
21252182"is inoffensive, it is not difficult to imagine one that could damage your "
21262183"system."
21272184msgstr ""
2185+ "在這個例子中,拆封器會引入 :func:`os.system` 函式,然後執行命令「echo hello "
2186+ "world」。雖然這個例子是無害的,但不難想像可以這個方式輕易執行任意可能對系統造"
2187+ "成損害的命令。"
21282188
21292189#: ../../library/pickle.rst:1086
21302190msgid ""
@@ -2134,12 +2194,18 @@ msgid ""
21342194"requested. Thus it is possible to either completely forbid globals or "
21352195"restrict them to a safe subset."
21362196msgstr ""
2197+ "基於以上原因,您可能會希望透過自訂 :meth:`Unpickler.find_class` 來控制哪些是"
2198+ "能夠被拆封的內容。與其名稱字面意義暗示的不同,實際上每當你請求一個全域物件"
2199+ "(例如,類別或函式)時,就會調用 :meth:`Unpickler.find_class`。因此,可以透過"
2200+ "這個方法完全禁止全域物件或將其限制在安全的子集合。"
21372201
21382202#: ../../library/pickle.rst:1092
21392203msgid ""
21402204"Here is an example of an unpickler allowing only few safe classes from the :"
21412205"mod:`builtins` module to be loaded::"
21422206msgstr ""
2207+ "以下是一個僅允許從 :mod:`builtins` 模組中載入少數安全類別的拆封器"
2208+ "(unpickler)的例子:::"
21432209
21442210#: ../../library/pickle.rst:1095
21452211msgid ""
@@ -2169,10 +2235,35 @@ msgid ""
21692235"\"\"\" Helper function analogous to pickle.loads().\"\"\" \n"
21702236" return RestrictedUnpickler(io.BytesIO(s)).load()"
21712237msgstr ""
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()"
21722263
21732264#: ../../library/pickle.rst:1121
21742265msgid "A sample usage of our unpickler working as intended::"
2175- msgstr ""
2266+ msgstr "我們剛才實作的的拆封器範例正常運作的樣子::: "
21762267
21772268#: ../../library/pickle.rst:1123
21782269msgid ""
@@ -2209,17 +2300,23 @@ msgid ""
22092300"alternatives such as the marshalling API in :mod:`xmlrpc.client` or third-"
22102301"party solutions."
22112302msgstr ""
2303+ "正如我們的範例所示,必須謹慎審視能被拆封的內容。因此,如果您的應用場景非常關"
2304+ "心安全性,您可能需要考慮其他選擇,例如 :mod:`xmlrpc.client` 中的 marshalling "
2305+ "API 或其他第三方解決方案。"
22122306
22132307#: ../../library/pickle.rst:1147
22142308msgid "Performance"
2215- msgstr ""
2309+ msgstr "效能 "
22162310
22172311#: ../../library/pickle.rst:1149
22182312msgid ""
22192313"Recent versions of the pickle protocol (from protocol 2 and upwards) feature "
22202314"efficient binary encodings for several common features and built-in types. "
22212315"Also, the :mod:`pickle` module has a transparent optimizer written in C."
22222316msgstr ""
2317+ "較近期的 pickle 協定版本(從 2 版協定開始)為多種常見功能和內建型別提供了高效"
2318+ "率的二進位編碼。此外,:mod:`pickle` 模組還具備一個透明化的、以 C 語言編寫的最"
2319+ "佳化工具。"
22232320
22242321#: ../../library/pickle.rst:1157
22252322msgid "Examples"
@@ -2228,7 +2325,7 @@ msgstr "範例"
22282325#: ../../library/pickle.rst:1159
22292326msgid ""
22302327"For the simplest code, use the :func:`dump` and :func:`load` functions. ::"
2231- msgstr ""
2328+ msgstr "最簡單的使用方式,調用 :func:`dump` 和 :func:`load` 函式。:: "
22322329
22332330#: ../../library/pickle.rst:1161
22342331msgid ""
@@ -2245,10 +2342,22 @@ msgid ""
22452342" # Pickle the 'data' dictionary using the highest protocol available.\n"
22462343" pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)"
22472344msgstr ""
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"
22482357
22492358#: ../../library/pickle.rst:1175
22502359msgid "The following example reads the resulting pickled data. ::"
2251- msgstr ""
2360+ msgstr "以下範例可以讀取前述程式所封裝的 pickle 資料。:: "
22522361
22532362#: ../../library/pickle.rst:1177
22542363msgid ""
@@ -2259,72 +2368,81 @@ msgid ""
22592368" # have to specify it.\n"
22602369" data = pickle.load(f)"
22612370msgstr ""
2371+ "import pickle\n"
2372+ "\n"
2373+ "with open('data.pickle', 'rb') as f:\n"
2374+ " # 會自動檢測資料使用的協定版本,因此我們不需要手動指定。\n"
2375+ " data = pickle.load(f)"
22622376
22632377#: ../../library/pickle.rst:1191
22642378msgid "Module :mod:`copyreg`"
22652379msgstr ":mod:`copyreg` 模組"
22662380
22672381#: ../../library/pickle.rst:1192
22682382msgid "Pickle interface constructor registration for extension types."
2269- msgstr ""
2383+ msgstr "註冊擴充型別的 Pickle 介面建構子。 "
22702384
22712385#: ../../library/pickle.rst:1194
22722386msgid "Module :mod:`pickletools`"
22732387msgstr ":mod:`pickletools` 模組"
22742388
22752389#: ../../library/pickle.rst:1195
22762390msgid "Tools for working with and analyzing pickled data."
2277- msgstr ""
2391+ msgstr "用於分析或處裡被封裝資料的工具。 "
22782392
22792393#: ../../library/pickle.rst:1197
22802394msgid "Module :mod:`shelve`"
22812395msgstr ":mod:`shelve` 模組"
22822396
22832397#: ../../library/pickle.rst:1198
22842398msgid "Indexed databases of objects; uses :mod:`pickle`."
2285- msgstr ""
2399+ msgstr "索引式資料庫;使用 :mod:`pickle` 實作。 "
22862400
22872401#: ../../library/pickle.rst:1200
22882402msgid "Module :mod:`copy`"
22892403msgstr ":mod:`copy` 模組"
22902404
22912405#: ../../library/pickle.rst:1201
22922406msgid "Shallow and deep object copying."
2293- msgstr ""
2407+ msgstr "物件的淺層或深度拷貝。 "
22942408
22952409#: ../../library/pickle.rst:1203
22962410msgid "Module :mod:`marshal`"
22972411msgstr ":mod:`marshal` 模組"
22982412
22992413#: ../../library/pickle.rst:1204
23002414msgid "High-performance serialization of built-in types."
2301- msgstr ""
2415+ msgstr "內建型別的高效能序列化。 "
23022416
23032417#: ../../library/pickle.rst:1208
23042418msgid "Footnotes"
23052419msgstr "註解"
23062420
23072421#: ../../library/pickle.rst:1209
23082422msgid "Don't confuse this with the :mod:`marshal` module"
2309- msgstr ""
2423+ msgstr "不要將此模組與 :mod:`marshal` 模組混淆 "
23102424
23112425#: ../../library/pickle.rst:1211
23122426msgid ""
23132427"This is why :keyword:`lambda` functions cannot be pickled: all :keyword:`!"
23142428"lambda` functions share the same name: ``<lambda>``."
23152429msgstr ""
2430+ "這就是為什麼 :keyword:`lambda` 函式無法被封裝:所有 :keyword:`!lambda` 函式共"
2431+ "享相同的名稱:``<lambda>``。"
23162432
23172433#: ../../library/pickle.rst:1214
23182434msgid ""
23192435"The exception raised will likely be an :exc:`ImportError` or an :exc:"
23202436"`AttributeError` but it could be something else."
23212437msgstr ""
2438+ "拋出的例外應該是 :exc:`ImportError` 或 :exc:`AttributeError`,但也可能是其他"
2439+ "例外。"
23222440
23232441#: ../../library/pickle.rst:1217
23242442msgid ""
23252443"The :mod:`copy` module uses this protocol for shallow and deep copying "
23262444"operations."
2327- msgstr ""
2445+ msgstr ":mod:`copy` 模組使用此協定進行淺層及深層複製操作。 "
23282446
23292447#: ../../library/pickle.rst:1220
23302448msgid ""
@@ -2333,6 +2451,9 @@ msgid ""
23332451"kind of newline characters occurs in persistent IDs, the resulting pickled "
23342452"data will become unreadable."
23352453msgstr ""
2454+ "協定 0 中限制僅能使用英文字母或數字字元來分配持久化 ID 是因為持久化 ID 是由換"
2455+ "行符號所分隔的。因此,如果持久化 ID 中出現任何形式的換行字元,將導致封裝資料"
2456+ "變得無法讀取。"
23362457
23372458#: ../../library/pickle.rst:12
23382459msgid "persistence"
@@ -2364,7 +2485,7 @@ msgstr "pickling"
23642485
23652486#: ../../library/pickle.rst:123
23662487msgid "External Data Representation"
2367- msgstr "External Data Representation(外部資料表示法 )"
2488+ msgstr "External Data Representation(外部資料表現 )"
23682489
23692490#: ../../library/pickle.rst:664
23702491msgid "copy"