File: guide-cleanup.rst

package info (click to toggle)
py3c 1.4-1
  • links: PTS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 344 kB
  • sloc: ansic: 881; python: 383; makefile: 207
file content (67 lines) | stat: -rw-r--r-- 2,105 bytes parent folder | download | duplicates (3)
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
..
    Copyright (c) 2015, Red Hat, Inc. and/or its affiliates
    Licensed under CC-BY-SA-3.0; see the license file

.. highlight:: c

.. index::
    single: Cleanup

Cleanup – Dropping Support for Python 2
=======================================

When users of your C extension are not using Python 2 any more, or you need to
use one of Python 3's irresistible new features, you can convert the project to
use Python 3 only.
As mentioned earlier, it is usually not a good idea to do this until you
have full support for both Pythons.

With py3c, dropping Python 2 basically amounts to expanding all its compat
macros.
In other words, remove the ``py3c.h`` header, and fix the compile errors.

*   .. index::
        double: Cleanup; Strings
        double: Cleanup; Bytes
        double: Cleanup; Unicode

    Convert ``PyStr_*`` to ``PyUnicode_*``; ``PyInt_*`` to ``PyLong_*``.

*   .. index::
        double: Cleanup; Module Initialization

    Instead of ``MODULE_INIT_FUNC(<name>)``, write::

        PyMODINIT_FUNC PyInit_<name>(void);
        PyMODINIT_FUNC PyInit_<name>(void)

*   .. index::
        double: Cleanup; Types

    Remove ``Py_TPFLAGS_HAVE_WEAKREFS`` and ``Py_TPFLAGS_HAVE_ITER``
    (py3c defines them as 0).


*   .. index::
        double: Cleanup; Comparisons

    Replace ``return PY3C_RICHCMP`` by :c:func:`Py_RETURN_RICHCOMPARE`,
    as mentioned in :c:macro:`PY3C_RICHCMP` documentation.

*   Replace :c:func:`Py_RETURN_RICHCOMPARE` and :c:func:`Py_UNREACHABLE`
    by its expansion, unless you either
    support Python 3.7+ only or keep the ``py3c/comparison.h`` header.

*   Replace :c:func:`Py_RETURN_NOTIMPLEMENTED` by its expansion, unless you either
    support Python 3.3+ only or keep the ``py3c/comparison.h`` header.

*   Drop ``capsulethunk.h``, if you're using it.

*   Remove any code in ``#if !IS_PY3`` blocks, and the ifs around
    ``#if IS_PY3`` ones.

You will want to check the code as you're doing this.
For example, replacing PyLong can easily result in code like
``if (PyInt_Check(o) || PyInt_Check(o))``.

Enjoy your Python 3-compatible extension!