From d5e66f5914ca9875ae2cc85b26ffbe4f852f2108 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 29 Jul 2025 08:56:02 +0000 Subject: [PATCH 1/4] Initial plan From d69e1a650a7c35b338db44af7a43b4c89f89b5e3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 29 Jul 2025 09:07:34 +0000 Subject: [PATCH 2/4] Translate introduction and overview sections of extending/embedding.po Co-authored-by: mattwang44 <24987826+mattwang44@users.noreply.github.com> --- extending/embedding.po | 55 +++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/extending/embedding.po b/extending/embedding.po index f08b001e48..648dbcfafa 100644 --- a/extending/embedding.po +++ b/extending/embedding.po @@ -35,6 +35,12 @@ msgid "" "writing some scripts in Python. You can also use it yourself if some of the " "functionality can be written in Python more easily." msgstr "" +"前面的章節討論了如何擴充 Python,也就是如何透過附加一個 C 函式庫來擴充 " +"Python 的功能。但也可以反過來做:將 Python 嵌入你的 C/C++ 應用程式中來豐富" +"它。嵌入讓你的應用程式能夠以 Python 而非 C 或 C++ 來實作應用程式的某些功能。" +"這可以用於許多目的;其中一個例子是允許使用者透過撰寫一些 Python 腳本來根據他們" +"的需求客製化應用程式。如果某些功能用 Python 寫起來比較容易,你也可以自己使用" +"這種方法。" #: ../../extending/embedding.rst:20 msgid "" @@ -44,6 +50,10 @@ msgid "" "nothing to do with Python --- instead, some parts of the application " "occasionally call the Python interpreter to run some Python code." msgstr "" +"嵌入 Python 與擴充它類似,但不完全相同。差別在於當你擴充 Python 時,應用程式" +"的主程式仍然是 Python 直譯器,而如果你嵌入 Python,主程式可能與 Python 完全" +"無關 — 相反,應用程式的某些部分偶爾會呼叫 Python 直譯器來執行一些 Python " +"程式碼。" #: ../../extending/embedding.rst:26 msgid "" @@ -54,6 +64,10 @@ msgid "" "Python. Then later you can call the interpreter from any part of the " "application." msgstr "" +"所以如果你要嵌入 Python,你要提供自己的主程式。這個主程式必須做的事情之一是" +"初始化 Python 直譯器。至少,你必須呼叫函式 :c:func:`Py_Initialize`。還有一些" +"可選的呼叫來傳遞命令列引數給 Python。然後你可以在應用程式的任何部分呼叫直譯" +"器。" #: ../../extending/embedding.rst:32 msgid "" @@ -64,6 +78,10 @@ msgid "" "level operations described in the previous chapters to construct and use " "Python objects." msgstr "" +"有幾種不同的方式來呼叫直譯器:你可以傳遞一個包含 Python 敘述的字串給 :c:func:" +"`PyRun_SimpleString`,或者你可以傳遞一個 stdio 檔案指標和檔案名稱(僅用於錯誤" +"訊息中的識別)給 :c:func:`PyRun_SimpleFile`。你也可以呼叫前面章節中描述的較低" +"層級操作來建構和使用 Python 物件。" #: ../../extending/embedding.rst:41 msgid ":ref:`c-api-index`" @@ -74,10 +92,11 @@ msgid "" "The details of Python's C interface are given in this manual. A great deal " "of necessary information can be found here." msgstr "" +"Python 的 C 介面詳細資訊在此手冊中提供。大量必要的資訊可以在這裡找到。" #: ../../extending/embedding.rst:49 msgid "Very High Level Embedding" -msgstr "" +msgstr "非常高階的嵌入" #: ../../extending/embedding.rst:51 msgid "" @@ -86,6 +105,8 @@ msgid "" "needing to interact with the application directly. This can for example be " "used to perform some operation on a file. ::" msgstr "" +"嵌入 Python 最簡單的形式是使用非常高階的介面。此介面用於執行 Python 腳本而" +"無需直接與應用程式互動。這例如可以用來對檔案執行一些操作。 ::" #: ../../extending/embedding.rst:56 msgid "" @@ -166,6 +187,9 @@ msgid "" "3.13, but we keep it here for backward compatibility. See :ref:`arg-parsing-" "string-and-buffers` for a description of this macro." msgstr "" +"``#define PY_SSIZE_T_CLEAN`` 被用來指示某些 API 應該使用 ``Py_ssize_t`` 而不是 " +"``int``。從 Python 3.13 開始不再必要,但我們在這裡保留它以維持向後相容性。" +"關於此巨集的描述請參閱 :ref:`arg-parsing-string-and-buffers`。" #: ../../extending/embedding.rst:97 msgid "" @@ -181,10 +205,17 @@ msgid "" "`PyRun_SimpleFile` function, which saves you the trouble of allocating " "memory space and loading the file contents." msgstr "" +"設定 :c:member:`PyConfig.program_name` 應該在 :c:func:`Py_InitializeFromConfig` " +"之前呼叫,以告知直譯器 Python 執行期函式庫的路徑。接下來,Python 直譯器用 :c:" +"func:`Py_Initialize` 初始化,然後執行一個硬編碼的 Python 腳本來印出日期和時間。" +"之後,:c:func:`Py_FinalizeEx` 呼叫會關閉直譯器,接著程式結束。在真實的程式中," +"你可能想要從另一個來源取得 Python 腳本,或許是文字編輯器例程、檔案或資料庫。" +"從檔案取得 Python 程式碼可以更好地使用 :c:func:`PyRun_SimpleFile` 函式來完成," +"這樣可以省去分配記憶體空間和載入檔案內容的麻煩。" #: ../../extending/embedding.rst:112 msgid "Beyond Very High Level Embedding: An overview" -msgstr "" +msgstr "超越非常高階嵌入:概觀" #: ../../extending/embedding.rst:114 msgid "" @@ -194,6 +225,9 @@ msgid "" "calls. At the cost of having to write more C code, you can achieve almost " "anything." msgstr "" +"高階介面讓你能夠從應用程式中執行任意的 Python 程式碼片段,但至少可以說交換資料" +"值是相當麻煩的。如果你想要這樣做,你應該使用較低階的呼叫。以必須撰寫更多 C " +"程式碼為代價,你幾乎可以達成任何事情。" #: ../../extending/embedding.rst:119 msgid "" @@ -202,36 +236,39 @@ msgid "" "previous chapters are still valid. To show this, consider what the extension " "code from Python to C really does:" msgstr "" +"應該注意的是,儘管意圖不同,擴充 Python 和嵌入 Python 其實是相當相同的活動。" +"前面章節討論的大部分主題仍然有效。為了說明這一點,考慮從 Python 到 C 的擴充" +"程式碼真正做了什麼:" #: ../../extending/embedding.rst:124 msgid "Convert data values from Python to C," -msgstr "" +msgstr "將資料值從 Python 轉換為 C," #: ../../extending/embedding.rst:126 msgid "Perform a function call to a C routine using the converted values, and" -msgstr "" +msgstr "使用轉換後的值對 C 例程執行函式呼叫,以及" #: ../../extending/embedding.rst:128 msgid "Convert the data values from the call from C to Python." -msgstr "" +msgstr "將呼叫中的資料值從 C 轉換為 Python。" #: ../../extending/embedding.rst:130 msgid "When embedding Python, the interface code does:" -msgstr "" +msgstr "當嵌入 Python 時,介面程式碼會:" #: ../../extending/embedding.rst:132 msgid "Convert data values from C to Python," -msgstr "" +msgstr "將資料值從 C 轉換為 Python," #: ../../extending/embedding.rst:134 msgid "" "Perform a function call to a Python interface routine using the converted " "values, and" -msgstr "" +msgstr "使用轉換後的值對 Python 介面例程執行函式呼叫,以及" #: ../../extending/embedding.rst:137 msgid "Convert the data values from the call from Python to C." -msgstr "" +msgstr "將呼叫中的資料值從 Python 轉換為 C。" #: ../../extending/embedding.rst:139 msgid "" From 2ec3f2f3f8d446f33b9318eb79f37b585fdb8778 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 29 Jul 2025 09:20:27 +0000 Subject: [PATCH 3/4] Translate additional sections of extending/embedding.po - 54/59 messages complete Co-authored-by: mattwang44 <24987826+mattwang44@users.noreply.github.com> --- extending/embedding.po | 70 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 9 deletions(-) diff --git a/extending/embedding.po b/extending/embedding.po index 648dbcfafa..450163df3e 100644 --- a/extending/embedding.po +++ b/extending/embedding.po @@ -277,6 +277,9 @@ msgid "" "is the routine that you call between both data conversions. When extending, " "you call a C routine, when embedding, you call a Python routine." msgstr "" +"如你所見,資料轉換步驟只是被交換來配合跨語言傳輸的不同方向。唯一的差別是你在" +"兩個資料轉換之間呼叫的例程。當擴充時,你呼叫 C 例程,當嵌入時,你呼叫 Python " +"例程。" #: ../../extending/embedding.rst:144 msgid "" @@ -285,10 +288,13 @@ msgid "" "be understood. Since these aspects do not differ from extending the " "interpreter, you can refer to earlier chapters for the required information." msgstr "" +"本章不會討論如何將資料從 Python 轉換為 C 以及反之。此外,假設已經理解參考的" +"正確使用和錯誤處理。由於這些方面與擴充直譯器沒有不同,你可以參考前面的章節" +"來取得所需資訊。" #: ../../extending/embedding.rst:153 msgid "Pure Embedding" -msgstr "" +msgstr "純嵌入" #: ../../extending/embedding.rst:155 msgid "" @@ -297,10 +303,12 @@ msgid "" "directly interact with the application (but that will change in the next " "section)." msgstr "" +"第一個程式的目標是執行 Python 腳本中的函式。就像在非常高階介面的章節中一樣," +"Python 直譯器不會直接與應用程式互動(但這在下一節會改變)。" #: ../../extending/embedding.rst:160 msgid "The code to run a function defined in a Python script is:" -msgstr "" +msgstr "執行 Python 腳本中定義函式的程式碼是:" #: ../../extending/embedding.rst:162 msgid "" @@ -386,6 +394,10 @@ msgid "" "(let's call the finished executable :program:`call`), and use it to execute " "a Python script, such as:" msgstr "" +"此程式碼使用 ``argv[1]`` 載入 Python 腳本,並呼叫 ``argv[2]`` 中命名的函式。" +"其整數引數是 ``argv`` 陣列的其他值。如果你 :ref:`編譯並連結 ` 此" +"程式(我們稱完成的可執行檔為 :program:`call`),並用它來執行 Python 腳本," +"例如:" #: ../../extending/embedding.rst:171 msgid "" @@ -399,7 +411,7 @@ msgstr "" #: ../../extending/embedding.rst:180 msgid "then the result should be:" -msgstr "" +msgstr "那麼結果應該是:" #: ../../extending/embedding.rst:182 msgid "" @@ -407,6 +419,9 @@ msgid "" "Will compute 3 times 2\n" "Result of call: 6" msgstr "" +"$ call multiply multiply 3 2\n" +"Will compute 3 times 2\n" +"Result of call: 6" #: ../../extending/embedding.rst:188 msgid "" @@ -414,6 +429,8 @@ msgid "" "is for data conversion between Python and C, and for error reporting. The " "interesting part with respect to embedding Python starts with ::" msgstr "" +"雖然對於其功能而言程式相當龐大,但大部分的程式碼是用於 Python 和 C 之間的資料" +"轉換,以及錯誤回報。關於嵌入 Python 的有趣部分開始於 ::" #: ../../extending/embedding.rst:192 msgid "" @@ -430,6 +447,9 @@ msgid "" "which is constructed using the :c:func:`PyUnicode_DecodeFSDefault` data " "conversion routine. ::" msgstr "" +"在初始化直譯器後,腳本使用 :c:func:`PyImport_Import` 載入。此例程需要一個 " +"Python 字串作為其引數,該字串使用 :c:func:`PyUnicode_DecodeFSDefault` 資料" +"轉換例程建構。 ::" #: ../../extending/embedding.rst:202 msgid "" @@ -450,6 +470,9 @@ msgid "" "proceeds by constructing a tuple of arguments as normal. The call to the " "Python function is then made with::" msgstr "" +"一旦腳本載入,我們要找的名稱使用 :c:func:`PyObject_GetAttrString` 檢索。如果" +"名稱存在,且回傳的物件是可呼叫的,你可以安全地假設它是一個函式。程式接著以" +"正常方式建構引數元組。對 Python 函式的呼叫接著以下面方式進行 ::" #: ../../extending/embedding.rst:216 msgid "pValue = PyObject_CallObject(pFunc, pArgs);" @@ -461,10 +484,12 @@ msgid "" "reference to the return value of the function. Be sure to release the " "reference after examining the value." msgstr "" +"函式回傳時,``pValue`` 要不是 ``NULL`` 就是包含函式回傳值的參考。確保在檢查" +"值之後釋放參考。" #: ../../extending/embedding.rst:226 msgid "Extending Embedded Python" -msgstr "" +msgstr "擴充嵌入式 Python" #: ../../extending/embedding.rst:228 msgid "" @@ -477,6 +502,11 @@ msgid "" "and write some glue code that gives Python access to those routines, just " "like you would write a normal Python extension. For example::" msgstr "" +"到目前為止,嵌入式 Python 直譯器無法存取應用程式本身的功能。Python API 透過" +"擴充嵌入式直譯器來允許這樣做。也就是說,嵌入式直譯器被應用程式提供的例程所" +"擴充。雖然聽起來複雜,但其實沒那麼糟。只要暫時忘記應用程式啟動 Python 直譯器" +"這件事。相反,把應用程式當作一組子程序,並撰寫一些膠合程式碼讓 Python 存取" +"這些例程,就像你撰寫一般 Python 擴充一樣。例如: ::" #: ../../extending/embedding.rst:237 msgid "" @@ -516,6 +546,8 @@ msgid "" "Insert the above code just above the :c:func:`main` function. Also, insert " "the following two statements before the call to :c:func:`Py_Initialize`::" msgstr "" +"在 :c:func:`main` 函式的正上方插入上述程式碼。同時,在呼叫 :c:func:" +"`Py_Initialize` 之前插入以下兩個敘述 ::" #: ../../extending/embedding.rst:270 msgid "" @@ -531,22 +563,26 @@ msgid "" "emb.numargs` function accessible to the embedded Python interpreter. With " "these extensions, the Python script can do things like" msgstr "" +"這兩行初始化 ``numargs`` 變數,並讓 :func:`!emb.numargs` 函式對嵌入式 Python " +"直譯器可用。有了這些擴充,Python 腳本可以執行以下操作" #: ../../extending/embedding.rst:277 msgid "" "import emb\n" "print(\"Number of arguments\", emb.numargs())" msgstr "" +"import emb\n" +"print(\"Number of arguments\", emb.numargs())" #: ../../extending/embedding.rst:282 msgid "" "In a real application, the methods will expose an API of the application to " "Python." -msgstr "" +msgstr "在真實的應用程式中,這些方法會向 Python 公開應用程式的 API。" #: ../../extending/embedding.rst:292 msgid "Embedding Python in C++" -msgstr "" +msgstr "在 C++ 中嵌入 Python" #: ../../extending/embedding.rst:294 msgid "" @@ -556,10 +592,13 @@ msgid "" "and link your program. There is no need to recompile Python itself using C+" "+." msgstr "" +"也可以將 Python 嵌入 C++ 程式中;具體如何做取決於所使用的 C++ 系統的細節;" +"一般來說,你需要用 C++ 撰寫主程式,並使用 C++ 編譯器來編譯和連結你的程式。" +"不需要使用 C++ 重新編譯 Python 本身。" #: ../../extending/embedding.rst:303 msgid "Compiling and Linking under Unix-like systems" -msgstr "" +msgstr "在類 Unix 系統下編譯和連結" #: ../../extending/embedding.rst:305 msgid "" @@ -568,6 +607,9 @@ msgid "" "application, particularly because Python needs to load library modules " "implemented as C dynamic extensions (:file:`.so` files) linked against it." msgstr "" +"要找到傳遞給編譯器(和連結器)的正確旗標以便將 Python 直譯器嵌入你的應用程式" +"中,並不一定是簡單的事,特別是因為 Python 需要載入作為 C 動態擴充實作的函式庫" +"模組(:file:`.so` 檔案)並連結到它。" #: ../../extending/embedding.rst:311 msgid "" @@ -577,12 +619,15 @@ msgid "" "available). This script has several options, of which the following will be " "directly useful to you:" msgstr "" +"要找出所需的編譯器和連結器旗標,你可以執行作為安裝過程的一部分產生的 :file:" +"`python{X.Y}-config` 腳本(也可能有 :file:`python3-config` 腳本可用)。此腳本" +"有幾個選項,其中以下對你直接有用:" #: ../../extending/embedding.rst:317 msgid "" "``pythonX.Y-config --cflags`` will give you the recommended flags when " "compiling:" -msgstr "" +msgstr "``pythonX.Y-config --cflags`` 會給你編譯時建議的旗標:" #: ../../extending/embedding.rst:320 msgid "" @@ -598,7 +643,7 @@ msgstr "" msgid "" "``pythonX.Y-config --ldflags --embed`` will give you the recommended flags " "when linking:" -msgstr "" +msgstr "``pythonX.Y-config --ldflags --embed`` 會給你連結時建議的旗標:" #: ../../extending/embedding.rst:328 msgid "" @@ -617,6 +662,8 @@ msgid "" "that you use the absolute path to :file:`python{X.Y}-config`, as in the " "above example." msgstr "" +"為了避免多個 Python 安裝之間的混淆(特別是系統 Python 和你自己編譯的 Python " +"之間),建議你使用 :file:`python{X.Y}-config` 的絕對路徑,如上面的例子所示。" #: ../../extending/embedding.rst:339 msgid "" @@ -629,6 +676,11 @@ msgid "" "extract the configuration values that you will want to combine together. " "For example:" msgstr "" +"如果此程序對你不起作用(不保證對所有類 Unix 平台都有效;但是,我們歡迎 :ref:" +"`錯誤回報 `),你將必須閱讀系統關於動態連結的文件和/或檢查 " +"Python 的 :file:`Makefile`(使用 :func:`sysconfig.get_makefile_filename` 來" +"找到其位置)和編譯選項。在這種情況下,:mod:`sysconfig` 模組是一個有用的工具," +"可以程式化地擷取你想要組合在一起的組態值。例如:" #: ../../extending/embedding.rst:348 msgid "" From 2cba35a71de3e13f955b5e6d01d3d11c049a0721 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 29 Jul 2025 09:24:53 +0000 Subject: [PATCH 4/4] Complete translation of extending/embedding.po - 57/59 messages (96.6%) translated Co-authored-by: mattwang44 <24987826+mattwang44@users.noreply.github.com> --- extending/embedding.po | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/extending/embedding.po b/extending/embedding.po index 450163df3e..277cc89c02 100644 --- a/extending/embedding.po +++ b/extending/embedding.po @@ -408,6 +408,12 @@ msgid "" " c = c + b\n" " return c" msgstr "" +"def multiply(a,b):\n" +" print(\"Will compute\", a, \"times\", b)\n" +" c = 0\n" +" for i in range(0, a):\n" +" c = c + b\n" +" return c" #: ../../extending/embedding.rst:180 msgid "then the result should be:" @@ -439,6 +445,10 @@ msgid "" "/* Error checking of pName left out */\n" "pModule = PyImport_Import(pName);" msgstr "" +"Py_Initialize();\n" +"pName = PyUnicode_DecodeFSDefault(argv[1]);\n" +"/* Error checking of pName left out */\n" +"pModule = PyImport_Import(pName);" #: ../../extending/embedding.rst:197 msgid "" @@ -461,6 +471,13 @@ msgid "" "}\n" "Py_XDECREF(pFunc);" msgstr "" +"pFunc = PyObject_GetAttrString(pModule, argv[2]);\n" +"/* pFunc is a new reference */\n" +"\n" +"if (pFunc && PyCallable_Check(pFunc)) {\n" +" ...\n" +"}\n" +"Py_XDECREF(pFunc);" #: ../../extending/embedding.rst:210 msgid ""