Skip to content

Port to Python 3.11: use Py_SET_SIZE() #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2021
Merged

Port to Python 3.11: use Py_SET_SIZE() #114

merged 1 commit into from
Dec 8, 2021

Conversation

vstinner
Copy link
Contributor

@vstinner vstinner commented Dec 6, 2021

On Python 3.11, "Py_SIZE(str) = actual_size;" fails with a compiler
error. Since Python 3.9, Py_SET_SIZE() must be used instead:

Add a copy of the pythoncapi_compat.h header file to get
Py_SET_SIZE() on Python 3.8 and older:
https://github.com/pythoncapi/pythoncapi_compat

On Python 3.11, "Py_SIZE(str) = actual_size;" fails with a compiler
error. Since Python 3.9, Py_SET_SIZE() must be used instead:

* https://docs.python.org/dev/whatsnew/3.11.html#c-api-changes
* https://docs.python.org/dev/c-api/structures.html#c.Py_SIZE

Add a copy of the pythoncapi_compat.h header file to get
Py_SET_SIZE() on Python 3.8 and older:
https://github.com/pythoncapi/pythoncapi_compat
@martindurant
Copy link
Member

I am no expert, so hopefully someone else can confirm this is good. If there are no objections, I can merge it in a couple of days.

@vstinner
Copy link
Contributor Author

vstinner commented Dec 7, 2021

Without my change, the build fails:

$ git switch master
$ rm -rf build; LANG= ~/python/main/python setup.py build_ext
(...)
building 'snappy._snappy' extension
(...)
gcc -pthread -Wno-unused-result -Wsign-compare -g -Og -Wall -O0 -fPIC -I/home/vstinner/python/main/Include -I/home/vstinner/python/main -c src/snappy/crc32c.c -o build/temp.linux-x86_64-3.11-pydebug/src/snappy/crc32c.o
gcc -pthread -Wno-unused-result -Wsign-compare -g -Og -Wall -O0 -fPIC -I/home/vstinner/python/main/Include -I/home/vstinner/python/main -c src/snappy/snappymodule.cc -o build/temp.linux-x86_64-3.11-pydebug/src/snappy/snappymodule.o
In file included from /home/vstinner/python/main/Include/Python.h:42,
                 from src/snappy/snappymodule.cc:29:
src/snappy/snappymodule.cc: In function ‘PyObject* maybe_resize(PyObject*, size_t, size_t)’:
/home/vstinner/python/main/Include/object.h:146:29: error: lvalue required as left operand of assignment
  146 | #define Py_SIZE(ob) _Py_SIZE(_PyVarObject_CAST_CONST(ob))
      |                     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/snappy/snappymodule.cc:72:13: note: in expansion of macro ‘Py_SIZE’
   72 |             Py_SIZE(str) = actual_size;
      |             ^~~~~~~
error: command '/usr/bin/gcc' failed with exit code 1

With the change, the build completes succesfully:

$ git switch python311 
$ rm -rf build; LANG= ~/python/main/python setup.py build_ext
(...)
building 'snappy._snappy' extension
(...)
gcc -pthread -Wno-unused-result -Wsign-compare -g -Og -Wall -O0 -fPIC -I/home/vstinner/python/main/Include -I/home/vstinner/python/main -c src/snappy/crc32c.c -o build/temp.linux-x86_64-3.11-pydebug/src/snappy/crc32c.o
gcc -pthread -Wno-unused-result -Wsign-compare -g -Og -Wall -O0 -fPIC -I/home/vstinner/python/main/Include -I/home/vstinner/python/main -c src/snappy/snappymodule.cc -o build/temp.linux-x86_64-3.11-pydebug/src/snappy/snappymodule.o
(...)
g++ -pthread -shared build/temp.linux-x86_64-3.11-pydebug/src/snappy/crc32c.o build/temp.linux-x86_64-3.11-pydebug/src/snappy/snappymodule.o -lsnappy -o build/lib.linux-x86_64-3.11-pydebug/snappy/_snappy.cpython-311d-x86_64-linux-gnu.so

@martindurant martindurant merged commit 1a539d7 into intake:master Dec 8, 2021
@vstinner vstinner deleted the python311 branch December 8, 2021 19:18
@vstinner
Copy link
Contributor Author

vstinner commented Dec 8, 2021

Nice, thanks ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants