GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — python-docs-ja/extending/extending.po at 3.14 · python/python-docs-ja Expand file tree/ Copy pathextending.poMore file actions 2337 lines (2171 loc) · 112 KB / Copy pathextending.poFile metadata and controls2337 lines (2171 loc) · 112 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 # SOME DESCRIPTIVE TITLE. # Copyright (C) 2001 Python Software Foundation # This file is distributed under the same license as the Python package. # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. # # Translators: # python-doc bot, 2026 # #, fuzzy msgid "" msgstr "" "Project-Id-Version: Python 3.15\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2026-10-15 17:56+0000\n" "PO-Revision-Date: 2025-09-16 00:00+0000\n" "Last-Translator: python-doc bot, 2026\n" "Language-Team: Japanese (https://app.transifex.com/python-doc/teams/5390/" "ja/)\n" "Language: ja\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=1; plural=0;\n" msgid "Using the C API: Assorted topics" msgstr "C API の使用: その他の話題" msgid "" "The :ref:`tutorial <first-extension-module>` walked you through creating a C " "API extension module, but left many areas unexplained. This document looks " "at several concepts that you'll need to learn in order to write more complex " "extensions." msgstr "" ":ref:`チュートリアル <first-extension-module>` では C API 拡張モジュールの作" "り方を説明しましたが、まだ説明していない領域が数多くあります。\n" "この文書では、より複雑な拡張を書くために必要な概念をいくつか取り上げます。" #: ../../extending/extending.rst:19 msgid "Errors and Exceptions" msgstr "エラーと例外" #: ../../extending/extending.rst:21 msgid "" "An important convention throughout the Python interpreter is the following: " "when a function fails, it should set an exception condition and return an " "error value (usually ``-1`` or a ``NULL`` pointer). Exception information " "is stored in three members of the interpreter's thread state. These are " "``NULL`` if there is no exception. Otherwise they are the C equivalents of " "the members of the Python tuple returned by :meth:`sys.exc_info`. These are " "the exception type, exception instance, and a traceback object. It is " "important to know about them to understand how errors are passed around." msgstr "" #: ../../extending/extending.rst:30 msgid "" "The Python API defines a number of functions to set various types of " "exceptions." msgstr "" "Python API では、様々な型の例外をセットするための関数をいくつか定義していま" "す。" #: ../../extending/extending.rst:32 msgid "" "The most common one is :c:func:`PyErr_SetString`. Its arguments are an " "exception object and a C string. The exception object is usually a " "predefined object like :c:data:`PyExc_ZeroDivisionError`. The C string " "indicates the cause of the error and is converted to a Python string object " "and stored as the \"associated value\" of the exception." msgstr "" "もっともよく用いられるのは :c:func:`PyErr_SetString` です。引数は例外オブジェ" "クトと C 文字列です。例外オブジェクトは通" "常、 :c:data:`PyExc_ZeroDivisionError` のような定義済みのオブジェクトです。 " "C 文字列はエラーの原因を示し、Python 文字列オブジェクトに変換されて例外の " "\"付属値\" に保存されます。" #: ../../extending/extending.rst:38 msgid "" "Another useful function is :c:func:`PyErr_SetFromErrno`, which only takes an " "exception argument and constructs the associated value by inspection of the " "global variable :c:data:`errno`. The most general function " "is :c:func:`PyErr_SetObject`, which takes two object arguments, the " "exception and its associated value. You don't need to :c:func:`Py_INCREF` " "the objects passed to any of these functions." msgstr "" "もう一つ有用な関数として :c:func:`PyErr_SetFromErrno` があります。この関数は" "引数に例外だけをとり、付属値はグローバル変数 :c:data:`errno` から構築します。" "もっとも汎用的な関数は :c:func:`PyErr_SetObject` で、二つのオブジェクト、例外" "と付属値を引数にとります。これら関数に渡すオブジェクトに" "は :c:func:`Py_INCREF` を使う必要はありません。" #: ../../extending/extending.rst:45 msgid "" "You can test non-destructively whether an exception has been set " "with :c:func:`PyErr_Occurred`. This returns the current exception object, " "or ``NULL`` if no exception has occurred. You normally don't need to " "call :c:func:`PyErr_Occurred` to see whether an error occurred in a function " "call, since you should be able to tell from the return value." msgstr "" "例外がセットされているかどうかは、 :c:func:`PyErr_Occurred` を使って非破壊的" "に調べられます。この関数は現在の例外オブジェクトを返します。例外が発生してい" "ない場合には ``NULL`` を返します。通常は、関数の戻り値からエラーが発生したか" "を判別できるはずなので、 :c:func:`PyErr_Occurred` を呼び出す必要はありませ" "ん。" msgid "" "When a function *f* that calls another function *g* detects that the latter " "fails, *f* should itself return an error value (usually ``NULL`` or " "``-1``). It should *not* call one of the ``PyErr_*`` functions --- one has " "already been called by *g*. *f*'s caller is then supposed to also return an " "error indication to *its* caller, again *without* calling ``PyErr_*``, and " "so on --- the most detailed cause of the error was already reported by the " "function that first detected it. Once the error reaches the Python " "interpreter's main loop, this aborts the currently executing Python code and " "tries to find an exception handler specified by the Python programmer." msgstr "" "別の関数 *g* を呼び出した関数 *f* が、その呼び出しの失敗を検出した場合、*f* " "自身もエラー値 (通常は ``NULL`` または ``-1``) を返す必要があります。\n" "このとき ``PyErr_*`` 関数を呼び出しては *いけません*。\n" "*g* がすでに呼び出しているからです。\n" "次に *f* の呼び出し元も、``PyErr_*`` を *呼び出さずに*、*その呼び出し元* へエ" "ラーを示す値を返します。\n" "以下同様に続きます。\n" "エラーの最も詳しい原因は、最初にエラーを検出した関数がすでに報告していま" "す。\n" "エラーが Python インタープリタのメインループに到達すると、実行中の Python " "コードの処理を中断し、Python プログラマーが指定した例外ハンドラーを探します。" msgid "" "(There are situations where a module can actually give a more detailed error " "message by calling another ``PyErr_*`` function, and in such cases it is " "fine to do so. As a general rule, however, this is not necessary, and can " "cause information about the cause of the error to be lost: most operations " "can fail for a variety of reasons.)" msgstr "" "(別の ``PyErr_*`` 関数を呼び出すことで、モジュールがより詳しいエラーメッセー" "ジを提供できる場合は、そうしても構いません。\n" "ただし、一般には不要であり、エラーの原因に関する情報が失われるおそれがありま" "す。\n" "ほとんどの操作は様々な理由で失敗し得るためです。)" #: ../../extending/extending.rst:67 msgid "" "To ignore an exception set by a function call that failed, the exception " "condition must be cleared explicitly by calling :c:func:`PyErr_Clear`. The " "only time C code should call :c:func:`PyErr_Clear` is if it doesn't want to " "pass the error on to the interpreter but wants to handle it completely by " "itself (possibly by trying something else, or pretending nothing went wrong)." msgstr "" "ある関数呼び出しでの処理の失敗によってセットされた例外を無視するに" "は、 :c:func:`PyErr_Clear` を呼び出して例外状態を明示的に消去しなくてはなりま" "せん。エラーをインタプリタには渡したくなく、自前で (何か他の作業を行ったり、" "何も起こらなかったかのように見せかけるような) エラー処理を完全に行う場合にの" "み、 :c:func:`PyErr_Clear` を呼び出すようにすべきです。" #: ../../extending/extending.rst:73 msgid "" "Every failing :c:func:`malloc` call must be turned into an exception --- the " "direct caller of :c:func:`malloc` (or :c:func:`realloc`) must " "call :c:func:`PyErr_NoMemory` and return a failure indicator itself. All " "the object-creating functions (for example, :c:func:`PyLong_FromLong`) " "already do this, so this note is only relevant to those who " "call :c:func:`malloc` directly." msgstr "" ":c:func:`malloc` の呼び出し失敗は、常に例外にしなくてはなりません " "--- :c:func:`malloc` (または :c:func:`realloc`) を直接呼び出しているコード" "は、 :c:func:`PyErr_NoMemory` を呼び出して、失敗を示す値を返さねばなりませ" "ん。オブジェクトを生成する全ての関数 (例えば :c:func:`PyLong_FromLong`) は " "``PyErr_NoMemory()`` の呼び出しを済ませてしまうので、この規則が関係するのは直" "接 :c:func:`malloc` を呼び出すコードだけです。" msgid "" "Also note that, with the important exception of :c:func:`PyArg_ParseTuple` " "and friends, functions that return an integer status usually return a " "positive value or zero for success and ``-1`` for failure, like Unix system " "calls." msgstr "" "また、:c:func:`PyArg_ParseTuple` などの重要な例外を除き、整数の状態コードを返" "す関数は通常、Unix のシステムコールと同じく、成功時に正の値またはゼロを返し、" "失敗時に ``-1`` を返します。" #: ../../extending/extending.rst:83 msgid "" "Finally, be careful to clean up garbage (by making :c:func:`Py_XDECREF` " "or :c:func:`Py_DECREF` calls for objects you have already created) when you " "return an error indicator!" msgstr "" "最後に、エラー標示値を返す際に、(エラーが発生するまでに既に生成してしまったオ" "ブジェクトに対して :c:func:`Py_XDECREF` や :c:func:`Py_DECREF` を呼び出して) " "ごみ処理を注意深く行ってください!" msgid "" "The choice of which exception to raise is entirely yours. There are " "predeclared C objects corresponding to all built-in Python exceptions, such " "as :c:data:`PyExc_ZeroDivisionError`, which you can use directly. Of course, " "you should choose exceptions wisely --- don't use :c:data:`PyExc_TypeError` " "to mean that a file couldn't be opened (that should probably " "be :c:data:`PyExc_OSError`). If something's wrong with the argument list, " "the :c:func:`PyArg_ParseTuple` function usually " "raises :c:data:`PyExc_TypeError`. If you have an argument whose value must " "be in a particular range or must satisfy other " "conditions, :c:data:`PyExc_ValueError` is appropriate." msgstr "" "どの例外を送出するかは自由に選べます。\n" ":c:data:`PyExc_ZeroDivisionError` など、Python のすべての組み込み例外に対応す" "る C オブジェクトが宣言済みで、そのまま利用できます。\n" "もちろん、例外は適切に選ぶ必要があります。\n" "例えば、ファイルを開けなかったことを示すために :c:data:`PyExc_TypeError` を使" "うべきではありません (おそらく :c:data:`PyExc_OSError` が適切です)。\n" "引数リストに問題がある場合、:c:func:`PyArg_ParseTuple` 関数は通" "常 :c:data:`PyExc_TypeError` を送出します。\n" "引数の値が特定の範囲内でなければならない場合や、その他の条件を満たす必要があ" "る場合は、:c:data:`PyExc_ValueError` が適切です。" msgid "" "You can also define a new exception that is unique to your module. The " "simplest way to do this is to declare a static global object variable at the " "beginning of the file::" msgstr "" "モジュール独自の新しい例外を定義することもできます。\n" "最も簡単な方法は、ファイルの先頭で静的なグローバルオブジェクト変数を宣言する" "ことです。 ::" msgid "static PyObject *SpamError = NULL;" msgstr "static PyObject *SpamError = NULL;" msgid "" "and initialize it by calling :c:func:`PyErr_NewException` in the " "module's :c:data:`Py_mod_exec` function (:c:func:`!spam_module_exec`)::" msgstr "" "そして、モジュールの :c:data:`Py_mod_exec` 関数 (:c:func:`!" "spam_module_exec`) で :c:func:`PyErr_NewException` を呼び出して初期化しま" "す。 ::" msgid "SpamError = PyErr_NewException(\"spam.error\", NULL, NULL);" msgstr "SpamError = PyErr_NewException(\"spam.error\", NULL, NULL);" msgid "" "Since :c:data:`!SpamError` is a global variable, it will be overwritten " "every time the module is reinitialized, when the :c:data:`Py_mod_exec` " "function is called." msgstr "" ":c:data:`!SpamError` はグローバル変数なので、モジュールが再初期化され" "て :c:data:`Py_mod_exec` 関数が呼び出されるたびに上書きされます。" msgid "" "For now, let's avoid the issue: we will block repeated initialization by " "raising an :py:exc:`ImportError`::" msgstr "" "ひとまず、この問題は回避しましょう。\n" ":py:exc:`ImportError` を送出して、初期化が繰り返されるのを防ぎます。 ::" msgid "" "static PyObject *SpamError = NULL;\n" "\n" "static int\n" "spam_module_exec(PyObject *m)\n" "{\n" " if (SpamError != NULL) {\n" " PyErr_SetString(PyExc_ImportError,\n" " \"cannot initialize spam module more than once\");\n" " return -1;\n" " }\n" " SpamError = PyErr_NewException(\"spam.error\", NULL, NULL);\n" " if (PyModule_AddObjectRef(m, \"SpamError\", SpamError) < 0) {\n" " return -1;\n" " }\n" "\n" " return 0;\n" "}\n" "\n" "static PyModuleDef_Slot spam_module_slots[] = {\n" " {Py_mod_exec, spam_module_exec},\n" " {0, NULL}\n" "};\n" "\n" "static struct PyModuleDef spam_module = {\n" " .m_base = PyModuleDef_HEAD_INIT,\n" " .m_name = \"spam\",\n" " .m_size = 0, // non-negative\n" " .m_slots = spam_module_slots,\n" "};\n" "\n" "PyMODINIT_FUNC\n" "PyInit_spam(void)\n" "{\n" " return PyModuleDef_Init(&spam_module);\n" "}" msgstr "" "static PyObject *SpamError = NULL;\n" "\n" "static int\n" "spam_module_exec(PyObject *m)\n" "{\n" " if (SpamError != NULL) {\n" " PyErr_SetString(PyExc_ImportError,\n" " \"cannot initialize spam module more than once\");\n" " return -1;\n" " }\n" " SpamError = PyErr_NewException(\"spam.error\", NULL, NULL);\n" " if (PyModule_AddObjectRef(m, \"SpamError\", SpamError) < 0) {\n" " return -1;\n" " }\n" "\n" " return 0;\n" "}\n" "\n" "static PyModuleDef_Slot spam_module_slots[] = {\n" " {Py_mod_exec, spam_module_exec},\n" " {0, NULL}\n" "};\n" "\n" "static struct PyModuleDef spam_module = {\n" " .m_base = PyModuleDef_HEAD_INIT,\n" " .m_name = \"spam\",\n" " .m_size = 0, // 非負の値\n" " .m_slots = spam_module_slots,\n" "};\n" "\n" "PyMODINIT_FUNC\n" "PyInit_spam(void)\n" "{\n" " return PyModuleDef_Init(&spam_module);\n" "}" msgid "" "Note that the Python name for the exception object is :exc:`!spam.error`. " "The :c:func:`PyErr_NewException` function may create a class with the base " "class being :exc:`Exception` (unless another class is passed in instead of " "``NULL``), described in :ref:`bltin-exceptions`." msgstr "" "例外オブジェクトの Python での名前は :exc:`!spam.error` であることに注意して" "ください。\n" ":c:func:`PyErr_NewException` 関数は、``NULL`` の代わりに別のクラスを渡さなけ" "れば、:ref:`bltin-exceptions` で説明する :exc:`Exception` を基底クラスとする" "クラスを作成できます。" msgid "" "Note also that the :c:data:`!SpamError` variable retains a reference to the " "newly created exception class; this is intentional! Since the exception " "could be removed from the module by external code, an owned reference to the " "class is needed to ensure that it will not be discarded, causing :c:data:`!" "SpamError` to become a dangling pointer. Should it become a dangling " "pointer, C code which raises the exception could cause a core dump or other " "unintended side effects." msgstr "" ":c:data:`!SpamError` 変数が新しく作成した例外クラスへの参照を保持している点に" "も注意してください。\n" "これは意図的なものです。\n" "外部のコードがモジュールから例外を削除する可能性があるため、クラスへの参照を" "所有しておく必要があります。\n" "そうすることで、クラスが破棄されて :c:data:`!SpamError` がダングリングポイン" "ターになるのを防ぎます。\n" "ダングリングポインターになると、その例外を送出する C コードがコアダンプなどの" "意図しない副作用を引き起こすおそれがあります。" msgid "" "For now, the :c:func:`Py_DECREF` call to remove this reference is missing. " "Even when the Python interpreter shuts down, the global :c:data:`!SpamError` " "variable will not be garbage-collected. It will \"leak\". We did, however, " "ensure that this will happen at most once per process." msgstr "" "現時点では、この参照を解放する :c:func:`Py_DECREF` 呼び出しがありません。\n" "Python インタープリタが終了しても、グローバル変数 :c:data:`!SpamError` はガ" "ベージコレクションの対象にならず、「リーク」します。\n" "ただし、これが起こるのはプロセスあたり最大 1 回となるようにしてあります。" #: ../../extending/extending.rst:167 msgid "" "We discuss the use of :c:macro:`PyMODINIT_FUNC` as a function return type " "later in this sample." msgstr "" msgid "" "The :exc:`!spam.error` exception can be raised in your extension module " "using a call to :c:func:`PyErr_SetString` as shown below::" msgstr "" "次のように :c:func:`PyErr_SetString` を呼び出すと、拡張モジュールから :exc:`!" "spam.error` 例外を送出できます。 ::" msgid "" "static PyObject *\n" "spam_system(PyObject *self, PyObject *args)\n" "{\n" " const char *command;\n" " int sts;\n" "\n" " if (!PyArg_ParseTuple(args, \"s\", &command))\n" " return NULL;\n" " sts = system(command);\n" " if (sts < 0) {\n" " PyErr_SetString(SpamError, \"System command failed\");\n" " return NULL;\n" " }\n" " return PyLong_FromLong(sts);\n" "}" msgstr "" "static PyObject *\n" "spam_system(PyObject *self, PyObject *args)\n" "{\n" " const char *command;\n" " int sts;\n" "\n" " if (!PyArg_ParseTuple(args, \"s\", &command))\n" " return NULL;\n" " sts = system(command);\n" " if (sts < 0) {\n" " PyErr_SetString(SpamError, \"System command failed\");\n" " return NULL;\n" " }\n" " return PyLong_FromLong(sts);\n" "}" msgid "Embedding an extension" msgstr "拡張モジュールの組み込み" msgid "" "If you want to make your module a permanent part of the Python interpreter, " "you will have to change the configuration setup and rebuild the " "interpreter. On Unix, place your file (:file:`spammodule.c` for example) in " "the :file:`Modules/` directory of an unpacked source distribution, add a " "line to the file :file:`Modules/Setup.local` describing your file:" msgstr "" "モジュールを Python インタープリタの恒久的な構成要素にするには、設定を変更し" "てインタープリタを再ビルドする必要があります。\n" "Unix では、ソース配布物を展開した :file:`Modules/` ディレクトリにファイル (例" "えば :file:`spammodule.c`) を置き、:file:`Modules/Setup.local` にそのファイル" "を記述する行を追加します。" msgid "spam spammodule.o" msgstr "spam spammodule.o" #: ../../extending/extending.rst:206 msgid "" "and rebuild the interpreter by running :program:`make` in the toplevel " "directory. You can also run :program:`make` in the :file:`Modules/` " "subdirectory, but then you must first rebuild :file:`Makefile` there by " "running ':program:`make` Makefile'. (This is necessary each time you change " "the :file:`Setup` file.)" msgstr "" "を追加して、トップレベルのディレクトリで :program:`make` を実行して、インタプ" "リタを再ビルドするだけです。 :file:`Modules/` サブディレクトリで" "も :program:`make` を実行できますが、前もって ':program:`make` Makefile' を実" "行して :file:`Makefile` を再ビルドしておかなければならりません。(この作業" "は :file:`Setup` ファイルを変更するたびに必要です。)" #: ../../extending/extending.rst:212 msgid "" "If your module requires additional libraries to link with, these can be " "listed on the line in the configuration file as well, for instance:" msgstr "" "モジュールが別のライブラリとリンクされている必要がある場合、ライブラリも設定" "ファイルに列挙できます。例えば以下のようにします。" msgid "spam spammodule.o -lX11" msgstr "spam spammodule.o -lX11" #: ../../extending/extending.rst:223 msgid "Calling Python Functions from C" msgstr "C から Python 関数を呼び出す" msgid "" "The tutorial concentrated on making C functions callable from Python. The " "reverse is also useful: calling Python functions from C. This is especially " "the case for libraries that support so-called \"callback\" functions. If a " "C interface makes use of callbacks, the equivalent Python often needs to " "provide a callback mechanism to the Python programmer; the implementation " "will require calling the Python callback functions from a C callback. Other " "uses are also imaginable." msgstr "" "チュートリアルでは、Python から C 関数を呼び出せるようにする方法に注目しまし" "た。\n" "その逆、つまり C から Python 関数を呼び出すことも有用です。\n" "特に、いわゆる「コールバック」関数をサポートするライブラリでは役立ちます。\n" "C のインターフェースがコールバックを利用する場合、それに対応する Python のイ" "ンターフェースでも、Python プログラマーにコールバックの仕組みを提供する必要が" "あることがよくあります。\n" "その実装には、C のコールバックから Python のコールバック関数を呼び出す処理が" "必要です。\n" "ほかの用途も考えられます。" msgid "" "Fortunately, the Python interpreter is easily called recursively, and there " "is a standard interface to call a Python function. (If you're interested in " "how to call the Python parser with a particular string as input, " "see :ref:`veryhigh`.)" msgstr "" "幸い、Python インタープリタは簡単に再帰的に呼び出せますし、Python 関数を呼び" "出すための標準インターフェースがあります。\n" "(特定の文字列を入力として Python のパーサーを呼び出す方法について" "は、:ref:`veryhigh` を参照してください。)" #: ../../extending/extending.rst:237 msgid "" "Calling a Python function is easy. First, the Python program must somehow " "pass you the Python function object. You should provide a function (or some " "other interface) to do this. When this function is called, save a pointer " "to the Python function object (be careful to :c:func:`Py_INCREF` it!) in a " "global variable --- or wherever you see fit. For example, the following " "function might be part of a module definition::" msgstr "" "Python 関数の呼び出しは簡単です。まず、C のコードに対してコールバックを登録し" "ようとする Python プログラムは、何らかの方法で Python の関数オブジェクトを渡" "さねばなりません。このために、コールバック登録関数 (またはその他のインター" "フェース) を提供せねばなりません。このコールバック登録関数が呼び出された際" "に、引き渡された Python 関数オブジェクトへのポインタをグローバル変数に --- あ" "るいは、どこか適切な場所に --- 保存します (関数オブジェクト" "を :c:func:`Py_INCREF` するようよく注意してください!)。例えば、以下のような関" "数がモジュールの一部になっていることでしょう::" msgid "" "static PyObject *my_callback = NULL;\n" "\n" "static PyObject *\n" "my_set_callback(PyObject *dummy, PyObject *args)\n" "{\n" " PyObject *result = NULL;\n" " PyObject *temp;\n" "\n" " if (PyArg_ParseTuple(args, \"O:set_callback\", &temp)) {\n" " if (!PyCallable_Check(temp)) {\n" " PyErr_SetString(PyExc_TypeError, \"parameter must be " "callable\");\n" " return NULL;\n" " }\n" " Py_XINCREF(temp); /* Add a reference to new callback */\n" " Py_XDECREF(my_callback); /* Dispose of previous callback */\n" " my_callback = temp; /* Remember new callback */\n" " /* Boilerplate to return \"None\" */\n" " Py_INCREF(Py_None);\n" " result = Py_None;\n" " }\n" " return result;\n" "}" msgstr "" "static PyObject *my_callback = NULL;\n" "\n" "static PyObject *\n" "my_set_callback(PyObject *dummy, PyObject *args)\n" "{\n" " PyObject *result = NULL;\n" " PyObject *temp;\n" "\n" " if (PyArg_ParseTuple(args, \"O:set_callback\", &temp)) {\n" " if (!PyCallable_Check(temp)) {\n" " PyErr_SetString(PyExc_TypeError, \"parameter must be " "callable\");\n" " return NULL;\n" " }\n" " Py_XINCREF(temp); /* 新しいコールバックへの参照を追加する " "*/\n" " Py_XDECREF(my_callback); /* 以前のコールバックを解放する */\n" " my_callback = temp; /* 新しいコールバックを保存する */\n" " /* \"None\" を返すための定型処理 */\n" " Py_INCREF(Py_None);\n" " result = Py_None;\n" " }\n" " return result;\n" "}" msgid "" "This function must be registered with the interpreter using " "the :c:macro:`METH_VARARGS` flag in :c:type:`PyMethodDef.ml_flags`. " "The :c:func:`PyArg_ParseTuple` function and its arguments are documented in " "section :ref:`parsetuple`." msgstr "" "この関数は、:c:type:`PyMethodDef.ml_flags` に :c:macro:`METH_VARARGS` フラグ" "を指定してインタープリタに登録する必要があります。\n" ":c:func:`PyArg_ParseTuple` 関数とその引数については :ref:`parsetuple` で説明" "しています。" #: ../../extending/extending.rst:272 msgid "" "The macros :c:func:`Py_XINCREF` and :c:func:`Py_XDECREF` increment/decrement " "the reference count of an object and are safe in the presence of ``NULL`` " "pointers (but note that *temp* will not be ``NULL`` in this context). More " "info on them in section :ref:`refcounts`." msgstr "" ":c:func:`Py_XINCREF` および :c:func:`Py_XDECREF` は、オブジェクトに対する参照" "カウントをインクリメント/デクリメントするためのマクロで、 ``NULL`` ポインタが" "渡されても安全に操作できる形式です (とはいえ、上の流れでは *temp* が " "``NULL`` になることはありません)。これらのマクロと参照カウントについて" "は、 :ref:`refcounts` で説明しています。" #: ../../extending/extending.rst:279 msgid "" "Later, when it is time to call the function, you call the C " "function :c:func:`PyObject_CallObject`. This function has two arguments, " "both pointers to arbitrary Python objects: the Python function, and the " "argument list. The argument list must always be a tuple object, whose " "length is the number of arguments. To call the Python function with no " "arguments, pass in ``NULL``, or an empty tuple; to call it with one " "argument, pass a singleton tuple. :c:func:`Py_BuildValue` returns a tuple " "when its format string consists of zero or more format codes between " "parentheses. For example::" msgstr "" "その後、コールバック関数を呼び出す時が来たら、C 関" "数 :c:func:`PyObject_CallObject` を呼び出します。この関数には二つの引数: " "Python 関数と Python 関数の引数リストがあり、いずれも任意の Python オブジェク" "トを表すポインタ型です。引数リストは常にタプルオブジェクトでなければならず、" "その長さは引数の数になります。Python 関数を引数なしで呼び出すのなら、 " "``NULL`` か空のタプルを渡します; 単一の引数で関数を呼び出すのなら、単要素 " "(singleton) のタプルを渡します。 :c:func:`Py_BuildValue` の書式文字列中に、ゼ" "ロ個または一個以上の書式化コードが入った丸括弧がある場合、この関数はタプルを" "返します。以下に例を示します::" msgid "" "int arg;\n" "PyObject *arglist;\n" "PyObject *result;\n" "...\n" "arg = 123;\n" "...\n" "/* Time to call the callback */\n" "arglist = Py_BuildValue(\"(i)\", arg);\n" "result = PyObject_CallObject(my_callback, arglist);\n" "Py_DECREF(arglist);" msgstr "" "int arg;\n" "PyObject *arglist;\n" "PyObject *result;\n" "...\n" "arg = 123;\n" "...\n" "/* コールバックを呼び出す */\n" "arglist = Py_BuildValue(\"(i)\", arg);\n" "result = PyObject_CallObject(my_callback, arglist);\n" "Py_DECREF(arglist);" #: ../../extending/extending.rst:299 msgid "" ":c:func:`PyObject_CallObject` returns a Python object pointer: this is the " "return value of the Python function. :c:func:`PyObject_CallObject` is " "\"reference-count-neutral\" with respect to its arguments. In the example a " "new tuple was created to serve as the argument list, which " "is :c:func:`Py_DECREF`\\ -ed immediately after " "the :c:func:`PyObject_CallObject` call." msgstr "" ":c:func:`PyObject_CallObject` は Python オブジェクトへのポインタを返します: " "これは Python 関数からの戻り値になります。 :c:func:`PyObject_CallObject` は、" "引数に対して \"参照カウント中立 (reference-count- neutral)\" です。上の例では" "タプルを生成して引数リストとして提供しており、このタプル" "は :c:func:`PyObject_CallObject` の呼び出し直後に :c:func:`Py_DECREF` されて" "います。" msgid "" "The return value of :c:func:`PyObject_CallObject` is \"new\": either it is a " "brand new object, or it is an existing object whose reference count has been " "incremented. So, unless you want to save it in a global variable, you " "should somehow :c:func:`Py_DECREF` the result, even (especially!) if you are " "not interested in its value." msgstr "" ":c:func:`PyObject_CallObject` の戻り値は「新しい参照」です。\n" "新しく作成したオブジェクトか、参照カウントを増やした既存のオブジェクトを指し" "ます。\n" "そのため、グローバル変数に保存するのでなければ、結果に対してどこか" "で :c:func:`Py_DECREF` を呼び出す必要があります。\n" "戻り値を使わない場合も (特にそのような場合こそ) 必要です。" msgid "" "Before you do this, however, it is important to check that the return value " "isn't ``NULL``. If it is, the Python function terminated by raising an " "exception. If the C code that called :c:func:`PyObject_CallObject` is called " "from Python, it should now return an error indication to its Python caller, " "so the interpreter can print a stack trace, or the calling Python code can " "handle the exception. If this is not possible or desirable, the exception " "should be cleared by calling :c:func:`PyErr_Clear`. For example::" msgstr "" "ただし、その前に戻り値が ``NULL`` でないことを確認する必要があります。\n" "``NULL`` なら、Python 関数は例外を送出して終了しています。\n" ":c:func:`PyObject_CallObject` を呼び出した C コード自体が Python から呼び出さ" "れている場合は、Python の呼び出し元にエラーを示す値を返す必要があります。\n" "これにより、インタープリタがスタックトレースを表示したり、呼び出し元の " "Python コードが例外を処理したりできます。\n" "そうすることが不可能、または望ましくない場合は、:c:func:`PyErr_Clear` を呼び" "出して例外を消去する必要があります。\n" "例えば、次のようにします。 ::" msgid "" "if (result == NULL)\n" " return NULL; /* Pass error back */\n" "...use result...\n" "Py_DECREF(result);" msgstr "" "if (result == NULL)\n" " return NULL; /* 呼び出し元にエラーを返す */\n" "...use result...\n" "Py_DECREF(result);" msgid "" "Depending on the desired interface to the Python callback function, you may " "also have to provide an argument list to :c:func:`PyObject_CallObject`. In " "some cases the argument list is also provided by the Python program, through " "the same interface that specified the callback function. It can then be " "saved and used in the same manner as the function object. In other cases, " "you may have to construct a new tuple to pass as the argument list. The " "simplest way to do this is to call :c:func:`Py_BuildValue`. For example, if " "you want to pass an integral event code, you might use the following code::" msgstr "" "Python のコールバック関数にどのようなインターフェースを用意するかによって" "は、:c:func:`PyObject_CallObject` に引数リストを渡す必要もあります。\n" "コールバック関数を指定するインターフェースを通じて、Python プログラムが引数リ" "ストも渡す場合があります。\n" "その場合は、関数オブジェクトと同じ方法で保存して使えます。\n" "一方、引数リストとして渡す新しいタプルを構築しなければならない場合もありま" "す。\n" "最も簡単な方法は :c:func:`Py_BuildValue` を呼び出すことです。\n" "例えば、整数のイベントコードを渡すには、次のように書けます。 ::" msgid "" "PyObject *arglist;\n" "...\n" "arglist = Py_BuildValue(\"(l)\", eventcode);\n" "result = PyObject_CallObject(my_callback, arglist);\n" "Py_DECREF(arglist);\n" "if (result == NULL)\n" " return NULL; /* Pass error back */\n" "/* Here maybe use the result */\n" "Py_DECREF(result);" msgstr "" "PyObject *arglist;\n" "...\n" "arglist = Py_BuildValue(\"(l)\", eventcode);\n" "result = PyObject_CallObject(my_callback, arglist);\n" "Py_DECREF(arglist);\n" "if (result == NULL)\n" " return NULL; /* 呼び出し元にエラーを返す */\n" "/* 必要ならここで結果を使う */\n" "Py_DECREF(result);" #: ../../extending/extending.rst:344 msgid "" "Note the placement of ``Py_DECREF(arglist)`` immediately after the call, " "before the error check! Also note that strictly speaking this code is not " "complete: :c:func:`Py_BuildValue` may run out of memory, and this should be " "checked." msgstr "" "``Py_DECREF(arglist)`` が呼び出しの直後、エラーチェックよりも前に置かれている" "ことに注意してください! また、厳密に言えば、このコードは完全ではありませ" "ん: :c:func:`Py_BuildValue` はメモリ不足におちいるかもしれず、チェックしてお" "くべきです。" msgid "" "You may also call a function with keyword arguments by " "using :c:func:`PyObject_Call`, which supports arguments and keyword " "arguments. As in the above example, we use :c:func:`Py_BuildValue` to " "construct the dictionary. ::" msgstr "" "位置引数とキーワード引数をサポートする :c:func:`PyObject_Call` を使うと、キー" "ワード引数を指定して関数を呼び出すこともできます。\n" "上の例と同じように、:c:func:`Py_BuildValue` を使って辞書を構築します。 ::" #: ../../extending/extending.rst:352 msgid "" "PyObject *dict;\n" "...\n" "dict = Py_BuildValue(\"{s:i}\", \"name\", val);\n" "result = PyObject_Call(my_callback, NULL, dict);\n" "Py_DECREF(dict);\n" "if (result == NULL)\n" " return NULL; /* Pass error back */\n" "/* Here maybe use the result */\n" "Py_DECREF(result);" msgstr "" #: ../../extending/extending.rst:368 msgid "Extracting Parameters in Extension Functions" msgstr "拡張モジュール関数でのパラメタ展開" msgid "" "The :ref:`tutorial <first-extension-module>` uses a \":c:data:`METH_O`\" " "function, which is limited to a single Python argument. If you want more, " "you can use :c:data:`METH_VARARGS` instead. With this flag, the C function " "will receive a :py:class:`tuple` of arguments instead of a single object." msgstr "" ":ref:`チュートリアル <first-extension-module>` では「:c:data:`METH_O`」関数を" "使いましたが、これは Python の引数を 1 個だけ受け取れます。\n" "より多くの引数が必要なら、代わりに :c:data:`METH_VARARGS` を使えます。\n" "このフラグを指定すると、C 関数は単一のオブジェクトではなく、引数" "の :py:class:`tuple` を受け取ります。" msgid "" "For unpacking the tuple, CPython provides the :c:func:`PyArg_ParseTuple` " "function, declared as follows::" msgstr "" "タプルの要素を取り出すために、CPython は次のように宣言され" "た :c:func:`PyArg_ParseTuple` 関数を提供しています。 ::" msgid "int PyArg_ParseTuple(PyObject *arg, const char *format, ...);" msgstr "int PyArg_ParseTuple(PyObject *arg, const char *format, ...);" msgid "" "The *arg* argument must be a tuple object containing an argument list passed " "from Python to a C function. The *format* argument must be a format string, " "whose syntax is explained in :ref:`arg-parsing` in the Python/C API " "Reference Manual. The remaining arguments must be addresses of variables " "whose type is determined by the format string." msgstr "" "引数 *arg* は、Python から C 関数に渡された引数リストを含むタプルオブジェクト" "でなければなりません。\n" "引数 *format* は書式文字列で、その構文は Python/C API リファレンスマニュアル" "の :ref:`arg-parsing` で説明しています。\n" "残りの引数には、書式文字列によって型が決まる変数のアドレスを渡す必要がありま" "す。" msgid "" "For example, to receive a single Python :py:class:`str` object and turn it " "into a C buffer, you would use ``\"s\"`` as the format string::" msgstr "" "例えば、Python の :py:class:`str` オブジェクトを 1 個受け取って C のバッ" "ファーに変換するには、書式文字列に ``\"s\"`` を指定します。 ::" msgid "" "const char *command;\n" "if (!PyArg_ParseTuple(args, \"s\", &command)) {\n" " return NULL;\n" "}" msgstr "" "const char *command;\n" "if (!PyArg_ParseTuple(args, \"s\", &command)) {\n" " return NULL;\n" "}" #: ../../extending/extending.rst:395 msgid "" "If an error is detected in the argument list, :c:func:`!PyArg_ParseTuple` " "returns ``NULL`` (the error indicator for functions returning object " "pointers); your function may return ``NULL``, relying on the exception set " "by :c:func:`PyArg_ParseTuple`." msgstr "" #: ../../extending/extending.rst:400 msgid "" "Note that while :c:func:`PyArg_ParseTuple` checks that the Python arguments " "have the required types, it cannot check the validity of the addresses of C " "variables passed to the call: if you make mistakes there, your code will " "probably crash or at least overwrite random bits in memory. So be careful!" msgstr "" ":c:func:`PyArg_ParseTuple` は Python 側から与えられた引数が必要な型になってい" "るか調べるのに対し、 ``PyArg_ParseTuple()`` は呼び出しの際に渡された C 変数の" "アドレスが有効な値を持つか調べられないことに注意してください: ここで間違いを" "犯すと、コードがクラッシュするかもしれませんし、少なくともでたらめなビットを" "メモリに上書きしてしまいます。慎重に!" #: ../../extending/extending.rst:405 msgid "" "Note that any Python object references which are provided to the caller are " "*borrowed* references; do not decrement their reference count!" msgstr "" "呼び出し側に提供されるオブジェクトへの参照はすべて *借用* 参照 (borrowed " "reference) になります; これらのオブジェクトの参照カウントをデクリメントしては" "なりません!" #: ../../extending/extending.rst:408 msgid "Some example calls::" msgstr "以下にいくつかの呼び出し例を示します::" msgid "#include <Python.h>" msgstr "#include <Python.h>" msgid "" "int ok;\n" "int i, j;\n" "long k, l;\n" "const char *s;\n" "Py_ssize_t size;\n" "\n" "ok = PyArg_ParseTuple(args, \"\"); /* No arguments */\n" " /* Python call: f() */" msgstr "" "int ok;\n" "int i, j;\n" "long k, l;\n" "const char *s;\n" "Py_ssize_t size;\n" "\n" "ok = PyArg_ParseTuple(args, \"\"); /* 引数なし */\n" " /* Python での呼び出し: f() */" msgid "" "ok = PyArg_ParseTuple(args, \"s\", &s); /* A string */\n" " /* Possible Python call: f('whoops!') */" msgstr "" "ok = PyArg_ParseTuple(args, \"s\", &s); /* 文字列 */\n" " /* Python での呼び出し例: f('whoops!') */" msgid "" "ok = PyArg_ParseTuple(args, \"lls\", &k, &l, &s); /* Two longs and a string " "*/\n" " /* Possible Python call: f(1, 2, 'three') */" msgstr "" "ok = PyArg_ParseTuple(args, \"lls\", &k, &l, &s); /* long 型の値 2 個と文字" "列 */\n" " /* Python での呼び出し例: f(1, 2, 'three') */" msgid "" "ok = PyArg_ParseTuple(args, \"(ii)s#\", &i, &j, &s, &size);\n" " /* A pair of ints and a string, whose size is also returned */\n" " /* Possible Python call: f((1, 2), 'three') */" msgstr "" "ok = PyArg_ParseTuple(args, \"(ii)s#\", &i, &j, &s, &size);\n" " /* int 型の値のペアと文字列。\n" " 文字列のサイズも取得する */\n" " /* Python での呼び出し例: f((1, 2), 'three') */" msgid "" "{\n" " const char *file;\n" " const char *mode = \"r\";\n" " int bufsize = 0;\n" " ok = PyArg_ParseTuple(args, \"s|si\", &file, &mode, &bufsize);\n" " /* A string, and optionally another string and an integer */\n" " /* Possible Python calls:\n" " f('spam')\n" " f('spam', 'w')\n" " f('spam', 'wb', 100000) */\n" "}" msgstr "" "{\n" " const char *file;\n" " const char *mode = \"r\";\n" " int bufsize = 0;\n" " ok = PyArg_ParseTuple(args, \"s|si\", &file, &mode, &bufsize);\n" " /* 文字列と、省略可能な別の文字列および整数 */\n" " /* Python での呼び出し例:\n" " f('spam')\n" " f('spam', 'w')\n" " f('spam', 'wb', 100000) */\n" "}" msgid "" "{\n" " int left, top, right, bottom, h, v;\n" " ok = PyArg_ParseTuple(args, \"((ii)(ii))(ii)\",\n" " &left, &top, &right, &bottom, &h, &v);\n" " /* A rectangle and a point */\n" " /* Possible Python call:\n" " f(((0, 0), (400, 300)), (10, 10)) */\n" "}" msgstr "" "{\n" " int left, top, right, bottom, h, v;\n" " ok = PyArg_ParseTuple(args, \"((ii)(ii))(ii)\",\n" " &left, &top, &right, &bottom, &h, &v);\n" " /* 長方形と点 */\n" " /* Python での呼び出し例:\n" " f(((0, 0), (400, 300)), (10, 10)) */\n" "}" msgid "" "{\n" " Py_complex c;\n" " ok = PyArg_ParseTuple(args, \"D:myfunction\", &c);\n" " /* a complex, also providing a function name for errors */\n" " /* Possible Python call: myfunction(1+2j) */\n" "}" msgstr "" "{\n" " Py_complex c;\n" " ok = PyArg_ParseTuple(args, \"D:myfunction\", &c);\n" " /* 複素数。\n" " エラー用の関数名も指定する */\n" " /* Python での呼び出し例: myfunction(1+2j) */\n" "}" #: ../../extending/extending.rst:477 msgid "Keyword Parameters for Extension Functions" msgstr "拡張モジュール関数のキーワードパラメタ" msgid "" "If you also want your function to accept :term:`keyword arguments <keyword " "argument>`, use the :c:data:`METH_KEYWORDS` flag in combination " "with :c:data:`METH_VARARGS`. (:c:data:`!METH_KEYWORDS` can also be used with " "other flags; see its documentation for the allowed combinations.)" msgstr "" "関数が :term:`キーワード引数 <keyword argument>` も受け取れるようにするに" "は、:c:data:`METH_VARARGS` と組み合わせて :c:data:`METH_KEYWORDS` フラグを使" "います。\n" "(:c:data:`!METH_KEYWORDS` はほかのフラグとも組み合わせられます。\n" "使用できる組み合わせについては、そのドキュメントを参照してください。)" msgid "" "In this case, the C function should accept a third ``PyObject *`` parameter " "which will be a dictionary of keywords. " "Use :c:func:`PyArg_ParseTupleAndKeywords` to parse the arguments to such a " "function." msgstr "" "この場合、C 関数はキーワード引数の辞書を受け取るための 3 番目の引数を " "``PyObject *`` 型で受け取る必要があります。\n" "このような関数の引数を解析するには :c:func:`PyArg_ParseTupleAndKeywords` を使" "います。" #: ../../extending/extending.rst:492 msgid "" "The :c:func:`PyArg_ParseTupleAndKeywords` function is declared as follows::" msgstr "" ":c:func:`PyArg_ParseTupleAndKeywords` は、以下のように宣言されています::" msgid "" "int PyArg_ParseTupleAndKeywords(PyObject *arg, PyObject *kwdict,\n" " const char *format, char * const " |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |