This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Use forward compatible macro in example code for creating new type
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, methane, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-01-05 06:47 by methane, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 211 merged methane, 2017-02-21 07:54
Messages (6)
msg284709 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2017-01-05 06:47
https://docs.python.org/2.7/extending/newtypes.html#the-basics uses PyObject_HEAD_INIT for type object header.

static PyTypeObject noddy_NoddyType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/

This code isn't compatible with Python 3.  In Python 3, PyVarObject_HEAD_INIT is used instead.
https://docs.python.org/3.6/extending/newtypes.html#the-basics

static PyTypeObject noddy_NoddyType = {
    PyVarObject_HEAD_INIT(NULL, 0)

This code is compatible with Python 2.


This example code can be copy and pasted when creating new extension.
If people start writing Python 2 extension, and forward port it to Python 3,
this small incompatibility cause compile error.

Let's use more forward compatible and short code for example.
msg288281 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-02-21 09:50
PyVarObject_HEAD_INIT() is more used in Python 3 code, but the code with PyObject_HEAD_INIT() doesn't look incompatible with Python 3. I don't see a need of this change.

PyObject_HEAD_INIT() also is used in .c files in Doc/includes/.
msg288283 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2017-02-21 09:59
> but the code with PyObject_HEAD_INIT() doesn't look incompatible with Python 3.

It's incompatible actually.

https://github.com/python/cpython/blob/2.7/Include/object.h

/* PyObject_HEAD defines the initial segment of every PyObject. */

#define PyObject_HEAD_INIT(type)        \
    _PyObject_EXTRA_INIT                \
    1, type,


https://github.com/python/cpython/blob/3.5/Include/object.h

#define PyObject_HEAD_INIT(type)        \
    { _PyObject_EXTRA_INIT              \
    1, type },


I noticed PyVarObject_HEAD_INIT is compatible, and simplified an extension I maintain.
https://github.com/PyMySQL/mysqlclient-python/commit/2feb5ed6850a3905edf0333e0cd11ea6218f0f4f


This small doc change helps people who writing Python 2's extension module now, and port it to Python 3 later.
msg288286 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-02-21 10:32
Ah, now I see.

I think sample C files in Doc/includes/ should be updated too. And it seems to me that Doc/includes/shoddy.c in Python 3 is not correct.
msg288289 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2017-02-21 12:12
New changeset 9436bbd87b7eed18dec4c32f25b88452fe282e1c by GitHub in branch '2.7':
bpo-29165: doc: make extending/newtypes more Python 3 friendly (GH-211)
https://github.com/python/cpython/commit/9436bbd87b7eed18dec4c32f25b88452fe282e1c
msg288290 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2017-02-21 12:14
> And it seems to me that Doc/includes/shoddy.c in Python 3 is not correct.

I created another pull request PR 215.
History
Date User Action Args
2022-04-11 14:58:41adminsetgithub: 73351
2017-02-21 12:14:16methanesetmessages: + msg288290
2017-02-21 12:12:30methanesetstatus: open -> closed
resolution: fixed
stage: resolved
2017-02-21 12:12:05methanesetmessages: + msg288289
2017-02-21 10:32:32serhiy.storchakasetmessages: + msg288286
2017-02-21 09:59:24methanesetmessages: + msg288283
2017-02-21 09:50:10serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg288281
2017-02-21 07:54:34methanesetpull_requests: + pull_request179
2017-01-05 06:47:59methanecreate