API 和 ABI 版本管理¶
Build-time version constants¶
CPython 透過以下巨集 (macro) 公開其版本號。請注意,對應到的是建置 (built) 所用到的版本。關於run time 所使用的版本,請見 Py_Version。
關於跨版本 API 和 ABI 穩定性的討論,請見 C API 穩定性。
-
PY_MAJOR_VERSION¶
在
3.4.1a2中的3。
-
PY_MINOR_VERSION¶
在
3.4.1a2中的4。
-
PY_MICRO_VERSION¶
在
3.4.1a2中的1。
-
PY_RELEASE_LEVEL¶
在
3.4.1a2中的a。0xA代表 alpha 版本、0xB代表 beta 版本、0xC為發布候選版本、0xF則為最終版。
-
PY_RELEASE_SERIAL¶
在
3.4.1a2中的2。零則為最終發布版本。
-
PY_VERSION_HEX¶
被編碼為單一整數的 Python 版本號。詳見
Py_PACK_FULL_VERSION()以了解編碼詳細資訊。使用它進行數值比較,例如
#if PY_VERSION_HEX >= ...。
Run-time version¶
-
const unsigned long Py_Version¶
- 為 穩定 ABI 的一部分 自 3.11 版本開始.
編碼為單個常數整數的 Python runtime 版本號。詳見
Py_PACK_FULL_VERSION()以了解編碼詳細資訊。這包含在 runtime 使用的 Python 版本。使用它進行數值比較,例如
if (Py_Version >= ...)。在 3.11 版被加入.
Bit-packing macros¶
-
uint32_t Py_PACK_FULL_VERSION(int major, int minor, int micro, int release_level, int release_serial)¶
- 為 穩定 ABI 的一部分 自 3.14 版本開始.
Return the given version, encoded as a single 32-bit integer with the following structure:
引數
No. of bits
Bit mask
Bit shift
Example values
3.4.1a23.10.0major
8
0xFF00000024
0x030x03minor
8
0x00FF000016
0x040x0Amicro
8
0x0000FF008
0x010x00release_level
4
0x000000F04
0xA0xFrelease_serial
4
0x0000000F0
0x20x0例如:
版本
Py_PACK_FULL_VERSION引數Encoded version
3.4.1a2(3, 4, 1, 0xA, 2)0x030401a23.10.0(3, 10, 0, 0xF, 0)0x030a00f0Out-of range bits in the arguments are ignored. That is, the macro can be defined as:
#ifndef Py_PACK_FULL_VERSION #define Py_PACK_FULL_VERSION(X, Y, Z, LEVEL, SERIAL) ( \ (((X) & 0xff) << 24) | \ (((Y) & 0xff) << 16) | \ (((Z) & 0xff) << 8) | \ (((LEVEL) & 0xf) << 4) | \ (((SERIAL) & 0xf) << 0)) #endif
Py_PACK_FULL_VERSIONis primarily a macro, intended for use in#ifdirectives, but it is also available as an exported function.在 3.14 版被加入.
-
uint32_t Py_PACK_VERSION(int major, int minor)¶
- 為 穩定 ABI 的一部分 自 3.14 版本開始.
Equivalent to
Py_PACK_FULL_VERSION(major, minor, 0, 0, 0). The result does not correspond to any Python release, but is useful in numeric comparisons.在 3.14 版被加入.