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
|
..
Copyright (c) 2015, Red Hat, Inc. and/or its affiliates
Licensed under CC-BY-SA-3.0; see the license file
~~~~~~~~~~~~~~~~~~~
The py3c Cheatsheet
~~~~~~~~~~~~~~~~~~~
Strings
~~~~~~~
* PyStr_* – for human-readable strings
* PyBytes_* – for binary data
* PyUnicode_* – when you used ``unicode`` in Python 2
* PyString_* – when you don't care about Python 3 yet
Use PyStr_AsUTF8AndSize to get a char* and its length.
Ints
~~~~
Use whatever you used in Python 2. For py3-only code, use ``PyLong``.
Floats
~~~~~~
Don't pass the useless second argument to :c:func:`PyFloat_FromString` as
you needed to do in Python 2.
Comparisons
~~~~~~~~~~~
Use `rich comparisons <https://www.python.org/dev/peps/pep-0207/>`_::
static PyObject* mytype_richcmp(PyObject *obj1, PyObject *obj2, int op)
{
if (mytype_Check(obj2)) {
PY_RETURN_RICHCOMPARE(get_data(obj1), get_data(obj2), op);
}
Py_RETURN_NOTIMPLEMENTED;
}
::
.tp_richcompare = mytype_richcmp
Objects & Types
~~~~~~~~~~~~~~~
============================== ================================
Instead of use
============================== ================================
obj->ob_type Py_TYPE(obj)
obj->ob_refcnt Py_REFCNT(obj)
obj->ob_size Py_SIZE(obj)
PyVarObject_HEAD_INIT(NULL, 0) PyObject_HEAD_INIT(NULL), 0
============================== ================================
Module initialization
~~~~~~~~~~~~~~~~~~~~~
::
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
.m_name = "spam",
.m_doc = PyDoc_STR("Python wrapper for the spam submodule."),
.m_size = -1,
.m_methods = spam_methods,
};
MODULE_INIT_FUNC(name)
{
...
m = PyModule_Create(&moduledef);
...
if (error) {
return NULL;
}
...
return m;
}
CObject
~~~~~~~
Use the `PyCapsule <https://docs.python.org/3/c-api/capsule.html#capsules>`_ API.
If you need to support 2.6, see the chapter :doc:`capsulethunk`.
|