CPython

CPython は、Pythonのリファレンス実装である。CPython は C言語で記述されている。CPython 以外に、製品としての品質をもつ P…

CPython
CPython
作者 グイド・ヴァンロッサム
開発元 Python Software Foundation
初版 1991年4月 (35年前) (1991-04)
最新版
3.13.3 / 2025年4月8日 (16か月前) (2025-04-08)
最新評価版
3.14.0a7 / 2025年4月8日 (16か月前) (2025-04-08)
リポジトリ ウィキデータを編集
プログラミング
言語
C言語
プラットフォーム 42のプラットフォーム 本文参照
種別 Python Programming Language Interpreter
ライセンス Python Software Foundation License
公式サイト www.python.org ウィキデータを編集
テンプレートを表示

CPython は、Pythonリファレンス実装である。CPython は C言語で記述されている。CPython 以外に、製品としての品質をもつ Python 実装はJava で記述された Jython共通言語ランタイム 用に記述された IronPython の2つがあり、またいくつかの実験的な実装もある。Classic Python というバクロニムが、'C' の別の意味として提案されている。

CPython はバイトコードインタプリタである。C を含むいくつかの言語での外部関数インターフェイスを持っており、Python 以外の言語の言語バインディングを明示的に書く場合に使用される。

機能

拡張モジュール

CPython における拡張モジュールかくちょうモジュール: extension module)は Python インタプリタに組み込みオブジェクト・Cライブラリコール・システムコールを追加する機能である[1]

拡張モジュールは Python 言語の仕様ではなく CPython ランタイムの仕様であるため、拡張モジュールは他のランタイムでは一般に機能しない[2]

Cython は拡張モジュールを活用している代表的なコンパイラである[3]

サポートされているプラットフォーム

UNIX / Unix系

デスクトップオペレーティングシステム

特殊用途、組み込み

メインフレームなど

かつてサポートされていたプラットフォーム

PEP 11にはPython Software FoundationのCPythonではサポートされていないプラットフォームの一覧がある。これらのプラットフォームは現在でも非公式の移植版でサポートされている。下記参照。

  • DOS (2.0以降非サポート)
  • IRIX 4 (2.3以降非サポート)
  • Mac OS 9 (2.4以降非サポート)
  • MINIX (2.3以降非サポート)

非公式の移植版

Python Software Foundationの公式版の CPython に統合されていない移植版が存在する。

移植版には、PSP 用のグラフィックスやサウンド API や S60 用の SMS やカメラ API などのプラットフォーム固有の機能のための拡張モジュールが提供されることが多い。

並列性に関する問題

マルチプロセッサのコンピュータで CPython を用いる際の大きな問題は、各 CPython インタプリタのプロセスが持つグローバルインタプリタロック(GIL)であり、これがプロセス内の Python スレッドの並列性を損なってしまう[4]

マルチプロセッサ環境で完全な並列性を実現するためには別の CPython プロセスを動作させる必要がある。一般的にプロセス間の通信を実現することは困難な作業となるが、CPythonでは標準ライブラリを使い容易に実現させることが可能である[5]。CPython から GIL を取り除くことについては常に議論が行われている[6]

バイトコード

Pythonにおけるバイトコード: bytecode)はCPythonが内部的に利用する中間表現である[7]

CPythonではPythonコードをバイトコードへコンパイルし、このバイトコードをインタープリタによって実行する[8]。コードからバイトコードへのコンパイル結果は .pyc ファイルとしてキャッシュされ、二度目以降の実行時にはリコンパイル無しにバイトコードが実行される[9]

バイトコードはあくまでCPythonの内部表現/実装詳細であり、CPythonのバージョン間や他Python実行環境との互換性は保証されない[10][11]

脚注

出典

  1. ^ 拡張モジュール (extension module) を使うと ... 新しい組み込みオブジェクトの実装、そして全ての C ライブラリ関数とシステムコールに対する呼び出し、ができるようになります。1. C や C++ による Python の拡張”. Python 3.13.4 documentation. 2025年6月11日閲覧。
  2. ^ C 拡張のインターフェイスは CPython に固有のものであり、これによる拡張モジュールはほかの Python 実装では動作しません。1. C や C++ による Python の拡張”. Python 3.13.4 documentation. 2025年6月11日閲覧。
  3. ^ [Cython] is a programming language that makes writing C extensions for the Python languageCython - an overview”. Cython 3.1.2 documentation. 2025年6月11日閲覧。
  4. ^ Python/C API Reference Manual: Thread State and the Global Interpreter Lock
  5. ^ PEP 371 -- Addition of the multiprocessing package to the standard library
  6. ^ Python Library and Extension FAQ: Can't we get rid of the Global Interpreter Lock?
  7. ^ "bytecode ... the internal representation of a Python program in the CPython interpreter." Python. bytecode - Glossary. Python 3.11.5 Documentation. 2023-09-01閲覧.
  8. ^ "Python source code is compiled into bytecode ... This 'intermediate language' is said to run on a virtual machine" Python. bytecode - Glossary. Python 3.11.5 Documentation. 2023-09-01閲覧.
  9. ^ "The bytecode is also cached in .pyc files so that executing the same file is faster the second time (recompilation from source to bytecode can be avoided)." Python. bytecode - Glossary. Python 3.11.5 Documentation. 2023-09-01閲覧.
  10. ^ "Do note that bytecodes are not expected to work between different Python virtual machines, nor to be stable between Python releases." Python. bytecode - Glossary. Python 3.11.5 Documentation. 2023-09-01閲覧.
  11. ^ "CPython implementation detail: Bytecode is an implementation detail of the CPython interpreter." Python. dis — Disassembler for Python bytecode. Python 3.11.5 Documentation. 2023-09-01閲覧.

参考文献

  • Martelli, Alex (2006). Python in a Nutshell (2nd edition ed.). O'Reilly. pp. 5?7. ISBN 0-596-10046-9 

関連項目

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.