|  | 
| 2 | 2 | 
 | 
| 3 | 3 | .. _stable: | 
| 4 | 4 | 
 | 
| 5 |  | -*************** | 
| 6 |  | -C API Stability | 
| 7 |  | -*************** | 
|  | 5 | +*********************** | 
|  | 6 | +C API and ABI Stability | 
|  | 7 | +*********************** | 
| 8 | 8 | 
 | 
| 9 | 9 | Unless documented otherwise, Python's C API is covered by the Backwards | 
| 10 | 10 | Compatibility Policy, :pep:`387`. | 
| @@ -199,6 +199,162 @@ This is the case with Windows and macOS releases from ``python.org`` and many | 
| 199 | 199 | third-party distributors. | 
| 200 | 200 | 
 | 
| 201 | 201 | 
 | 
|  | 202 | +ABI Checking | 
|  | 203 | +============ | 
|  | 204 | + | 
|  | 205 | +.. versionadded:: next | 
|  | 206 | + | 
|  | 207 | +Python includes a rudimentary check for ABI compatibility. | 
|  | 208 | + | 
|  | 209 | +This check is not comprehensive. | 
|  | 210 | +It only guards against common cases of incompatible modules being | 
|  | 211 | +installed for the wrong interpreter. | 
|  | 212 | +It also does not take :ref:`platform incompatibilities <stable-abi-platform>` | 
|  | 213 | +into account. | 
|  | 214 | +It can only be done after an extension is successfully loaded. | 
|  | 215 | + | 
|  | 216 | +Despite these limitations, it is recommended that extension modules use this | 
|  | 217 | +mechanism, so that detectable incompatibilities raise exceptions rather than | 
|  | 218 | +crash. | 
|  | 219 | + | 
|  | 220 | +Most modules can use this check via the :c:data:`Py_mod_abi` | 
|  | 221 | +slot and the :c:macro:`PyABIInfo_VAR` macro, for example like this: | 
|  | 222 | + | 
|  | 223 | +.. code-block:: c | 
|  | 224 | +
 | 
|  | 225 | +   PyABIInfo_VAR(abi_info); | 
|  | 226 | +
 | 
|  | 227 | +   static PyModuleDef_Slot mymodule_slots[] = { | 
|  | 228 | +      {Py_mod_abi, &abi_info}, | 
|  | 229 | +      ... | 
|  | 230 | +   }; | 
|  | 231 | +
 | 
|  | 232 | +
 | 
|  | 233 | +The full API is described below for advanced use cases. | 
|  | 234 | + | 
|  | 235 | +.. c:function:: int PyABIInfo_Check(PyABIInfo *info, const char *module_name) | 
|  | 236 | +
 | 
|  | 237 | +   Verify that the given *info* is compatible with the currently running | 
|  | 238 | +   interpreter. | 
|  | 239 | +
 | 
|  | 240 | +   Return 0 on success. On failure, raise an exception and return -1. | 
|  | 241 | +
 | 
|  | 242 | +   If the ABI is incompatible, the raised exception will be :py:exc:`ImportError`. | 
|  | 243 | +
 | 
|  | 244 | +   The *module_name* argument can be ``NULL``, or point to a NUL-terminated | 
|  | 245 | +   UTF-8-encoded string used for error messages. | 
|  | 246 | +
 | 
|  | 247 | +   Note that if *info* describes the ABI that the current code uses (as defined | 
|  | 248 | +   by :c:macro:`PyABIInfo_VAR`, for example), using any other Python C API | 
|  | 249 | +   may lead to crashes. | 
|  | 250 | +   In particular, it is not safe to examine the raised exception. | 
|  | 251 | +
 | 
|  | 252 | +   .. versionadded:: next | 
|  | 253 | +
 | 
|  | 254 | +.. c:macro:: PyABIInfo_VAR(NAME) | 
|  | 255 | +
 | 
|  | 256 | +   Define a static :c:struct:`PyABIInfo` variable with the given *NAME* that | 
|  | 257 | +   describes the ABI that the current code will use. | 
|  | 258 | +   This macro expands to: | 
|  | 259 | +
 | 
|  | 260 | +   .. code-block:: c | 
|  | 261 | +
 | 
|  | 262 | +      static PyABIInfo NAME = { | 
|  | 263 | +          1, 0, | 
|  | 264 | +          PyABIInfo_DEFAULT_FLAGS, | 
|  | 265 | +          PY_VERSION_HEX, | 
|  | 266 | +          PyABIInfo_DEFAULT_ABI_VERSION | 
|  | 267 | +      } | 
|  | 268 | +
 | 
|  | 269 | +   .. versionadded:: next | 
|  | 270 | + | 
|  | 271 | +.. c:type:: PyABIInfo | 
|  | 272 | +
 | 
|  | 273 | +   .. c:member:: uint8_t abiinfo_major_version | 
|  | 274 | +
 | 
|  | 275 | +      The major version of :c:struct:`PyABIInfo`. Can be set to: | 
|  | 276 | + | 
|  | 277 | +      * ``0`` to skip all checking, or | 
|  | 278 | +      * ``1`` to specify this version of :c:struct:`!PyABIInfo`. | 
|  | 279 | + | 
|  | 280 | +   .. c:member:: uint8_t abiinfo_minor_version | 
|  | 281 | +
 | 
|  | 282 | +      The major version of :c:struct:`PyABIInfo`. | 
|  | 283 | +      Must be set to ``0``; larger values are reserved for backwards-compatible | 
|  | 284 | +      future versions of :c:struct:`!PyABIInfo`. | 
|  | 285 | + | 
|  | 286 | +   .. c:member:: uint16_t flags | 
|  | 287 | +
 | 
|  | 288 | +      .. c:namespace:: NULL | 
|  | 289 | +
 | 
|  | 290 | +      This field is usually set to the following macro: | 
|  | 291 | + | 
|  | 292 | +      .. c:macro:: PyABIInfo_DEFAULT_FLAGS | 
|  | 293 | +
 | 
|  | 294 | +         Default flags, based on current values of macros such as | 
|  | 295 | +         :c:macro:`Py_LIMITED_API` and :c:macro:`Py_GIL_DISABLED`. | 
|  | 296 | + | 
|  | 297 | +      Alternately, the field can be set to to the following flags, combined | 
|  | 298 | +      by bitwise OR. | 
|  | 299 | +      Unused bits must be set to zero. | 
|  | 300 | + | 
|  | 301 | +      ABI variant -- one of: | 
|  | 302 | + | 
|  | 303 | +         .. c:macro:: PyABIInfo_STABLE | 
|  | 304 | +
 | 
|  | 305 | +            Specifies that the stable ABI is used. | 
|  | 306 | + | 
|  | 307 | +         .. c:macro:: PyABIInfo_INTERNAL | 
|  | 308 | +
 | 
|  | 309 | +            Specifies ABI specific to a particular build of CPython. | 
|  | 310 | +            Internal use only. | 
|  | 311 | + | 
|  | 312 | +      Free-threading compatibility -- one of: | 
|  | 313 | + | 
|  | 314 | +         .. c:macro:: PyABIInfo_FREETHREADED | 
|  | 315 | +
 | 
|  | 316 | +            Specifies ABI compatible with free-threading builds of CPython. | 
|  | 317 | +            (That is, ones compiled with :option:`--disable-gil`; with ``t`` | 
|  | 318 | +            in :py:data:`sys.abiflags`) | 
|  | 319 | + | 
|  | 320 | +         .. c:macro:: PyABIInfo_GIL | 
|  | 321 | +
 | 
|  | 322 | +            Specifies ABI compatible with non-free-threading builds of CPython | 
|  | 323 | +            (ones compiled *without* :option:`--disable-gil`). | 
|  | 324 | + | 
|  | 325 | +   .. c:member:: uint32_t build_version | 
|  | 326 | +
 | 
|  | 327 | +      The version of the Python headers used to build the code, in the format | 
|  | 328 | +      used by :c:macro:`PY_VERSION_HEX`. | 
|  | 329 | + | 
|  | 330 | +      This can be set to ``0`` to skip any checks related to this field. | 
|  | 331 | +      This option is meant mainly for projects that do not use the CPython | 
|  | 332 | +      headers directly, and do not emulate a specific version of them. | 
|  | 333 | + | 
|  | 334 | +   .. c:member:: uint32_t abi_version | 
|  | 335 | +
 | 
|  | 336 | +      The ABI version. | 
|  | 337 | + | 
|  | 338 | +      For the Stable ABI, this field should be the value of | 
|  | 339 | +      :c:macro:`Py_LIMITED_API` | 
|  | 340 | +      (except if :c:macro:`Py_LIMITED_API` is ``3``; use | 
|  | 341 | +      :c:expr:`Py_PACK_VERSION(3, 2)` in that case). | 
|  | 342 | + | 
|  | 343 | +      Otherwise, it should be set to :c:macro:`PY_VERSION_HEX`. | 
|  | 344 | + | 
|  | 345 | +      It can also be set to ``0`` to skip any checks related to this field. | 
|  | 346 | + | 
|  | 347 | +      .. c:namespace:: NULL | 
|  | 348 | +
 | 
|  | 349 | +      .. c:macro:: PyABIInfo_DEFAULT_ABI_VERSION | 
|  | 350 | +
 | 
|  | 351 | +         The value that should be used for this field, based on current | 
|  | 352 | +         values of macros such as :c:macro:`Py_LIMITED_API`, | 
|  | 353 | +         :c:macro:`PY_VERSION_HEX` and :c:macro:`Py_GIL_DISABLED`. | 
|  | 354 | + | 
|  | 355 | +   .. versionadded:: next | 
|  | 356 | + | 
|  | 357 | + | 
| 202 | 358 | .. _limited-api-list: | 
| 203 | 359 | 
 | 
| 204 | 360 | Contents of Limited API | 
|  | 
0 commit comments