[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-04-04 Thread Gregory P. Smith
Change by Gregory P. Smith : -- nosy: +gregory.p.smith ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: https:

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-31 Thread Dong-hee Na
Change by Dong-hee Na : -- nosy: +corona10 ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.pytho

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-31 Thread STINNER Victor
Change by STINNER Victor : -- pull_requests: +30286 pull_request: https://github.com/python/cpython/pull/32210 ___ Python tracker ___ __

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-31 Thread STINNER Victor
STINNER Victor added the comment: New changeset 7fc39a21cb85163a456eab91b52e5fe85e7f7e3e by Victor Stinner in branch 'main': bpo-47164: Add _PyCFunctionObject_CAST() macr (GH-32190) https://github.com/python/cpython/commit/7fc39a21cb85163a456eab91b52e5fe85e7f7e3e -- ___

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-31 Thread STINNER Victor
STINNER Victor added the comment: New changeset f0bc69485677ae8973685866ada0982976d3878f by Victor Stinner in branch 'main': bpo-47164: Add _PyCFunction_CAST() macro (GH-32192) https://github.com/python/cpython/commit/f0bc69485677ae8973685866ada0982976d3878f --

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-31 Thread STINNER Victor
STINNER Victor added the comment: New changeset c14d7e4b816134b8e93ece4066a86d229631ce96 by Victor Stinner in branch 'main': bpo-47164: Add _PyASCIIObject_CAST() macro (GH-32191) https://github.com/python/cpython/commit/c14d7e4b816134b8e93ece4066a86d229631ce96 -- __

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-31 Thread STINNER Victor
STINNER Victor added the comment: After reading Mark's comments, I reworked my GH-32190 PR to only use the CAST macros in other macros, not in the C code. The CAST macros are not used in such code pattern: else if (PyCFunction_Check(func)) -return ((PyCFunctionObject*)func)->m_m

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-30 Thread STINNER Victor
STINNER Victor added the comment: > I've had to debug a segfault before only because the inline function > implicitly cast its arguments, and it was accessing a non-existent member. If > it were a macro it would access the struct member directly, and the compiler > would be able to catch tha

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-30 Thread Ken Jin
Ken Jin added the comment: @Victor, I'm not against (A) or any of the PRs you proposed. They look fine to me. My concern was with certain static inline functions (there are none here :). The _CAST macros have genuinely helped me debug code before. -- ___

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-30 Thread STINNER Victor
STINNER Victor added the comment: GH-32192 "Add _PyCFunction_CAST() macro" is special. It's used to define functions in PyTypeObject.tp_methods or PyModuleDef.m_methods. Casting a function pointer can cause issues with Control Flow Integrity (CFI) implemented in LLVM. The _thread module has

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-30 Thread STINNER Victor
STINNER Victor added the comment: Hum, there are two things and maybe we are not talking about the same thing. * (A) Modifying macros defined in the Python C API (Include/ header files) doing cast on their arguments to use CAST macros * (B) Modify C code doing casts to use CAST macros I crea

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-30 Thread STINNER Victor
STINNER Victor added the comment: > By the way, the current Python C API is not fully compatible with C++. (...) I created bpo-47165 "[C API] Test that the Python C API is compatible with C++". -- ___ Python tracker

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-30 Thread STINNER Victor
STINNER Victor added the comment: > I think that adding macros makes readability worse. See the attached PRs, I can remove multiple layers of parenthesis in macros by using CAST macros. > ``` > PyObject *func = ...; > int flags = PyCFunction_GET_FLAGS(func); > ``` > > is dangerous. Right.

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-30 Thread Ken Jin
Ken Jin added the comment: > tiny inline function obscures the meaning of code Sadly I have to agree with Mark for that specific case. I've had to debug a segfault before only because the inline function implicitly cast its arguments, and it was accessing a non-existent member. If it were a

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-30 Thread STINNER Victor
Change by STINNER Victor : -- pull_requests: +30270 pull_request: https://github.com/python/cpython/pull/32192 ___ Python tracker ___ __

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-30 Thread STINNER Victor
Change by STINNER Victor : -- pull_requests: +30269 pull_request: https://github.com/python/cpython/pull/32191 ___ Python tracker ___ __

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-30 Thread Mark Shannon
Mark Shannon added the comment: The problem in the example you give is the need for the cast in the first place. If `func` were a `PyCFunctionObject *` instead of a `PyObject *`, then there would be no cast. Making the casts explicit serves as a reminder that a type check is needed. ``` Py

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-30 Thread Mark Shannon
Mark Shannon added the comment: I think that adding macros makes readability worse. The macro is only more readable if you already know what it does. If you don't, then you need to look up the macro, and understand the cast in the macro (which is harder than understanding the original cast).

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-30 Thread STINNER Victor
Change by STINNER Victor : -- keywords: +patch pull_requests: +30268 stage: -> patch review pull_request: https://github.com/python/cpython/pull/32190 ___ Python tracker ___ _

[issue47164] [C API] Add private "CAST" macros to clean up casts in C code

2022-03-30 Thread STINNER Victor
New submission from STINNER Victor : Last years, I started to add "CAST" macros like: #define _PyObject_CAST(op) ((PyObject*)(op)) #define _PyType_CAST(op) (assert(PyType_Check(op)), (PyTypeObject*)(op)) Example of usage: #define PyObject_TypeCheck(ob, type) PyObject_TypeCheck(_PyObject_CAST(