bpo-26868: Fix example usage of PyModule_AddObject. by brandtbucher · Pull Request #15725 · python/cpython (original) (raw)
There are three sytles currently:
if (PyModule_AddObject())
if (PyModule_AddObject() < 0)
if (PyModule_AddObject() == -1)
After some thinking, I think the second one is not bad.
Usually APIs use a negative return number to indicate failure.
Even the reader is not familiar with Python code, if he/she sees < 0
, may guess that it's likely to detect failure.
So style 2 is a bit better than style 1.
The third style may produce a very slightly longer machine code:
; 19 : if (PyModule_AddObject()) 00004 e8 00 00 00 00 call ?PyModule_AddObject@@YAHXZ ; PyModule_AddObject 00009 85 c0 test eax, eax 0000b 74 07 je SHORT $LN2@main
; 21 : if (PyModule_AddObject() < 0) 00004 e8 00 00 00 00 call ?PyModule_AddObject@@YAHXZ ; PyModule_AddObject 00009 85 c0 test eax, eax 0000b 7d 07 jge SHORT $LN2@main
; 23 : if (PyModule_AddObject() == -1) 00004 e8 00 00 00 00 call ?PyModule_AddObject@@YAHXZ ; PyModule_AddObject 00009 83 f8 ff cmp eax, -1 0000c 75 07 jne SHORT $LN2@main
And < 0
is likely to indicate that all failures are covered, not a specific failure.
So style 2 is a bit better than style 3.
There are reasons, although the reasons are weak. 😂