[issue28215] PyModule_AddIntConstant() wraps >=2^31 values when long is 4 bytes

2016-09-20 Thread Kyle Altendorf

Kyle Altendorf added the comment:

A little macro funny business gets a function the ability to know if the type 
passed to its wrapping macro is signed or not.

http://ideone.com/NZYs7u


  // http://stackoverflow.com/questions/7469915
  #define IS_UNSIGNED(v) (v >= 0 && ~v >= 0)
  #define F(v) f(IS_UNSIGNED(v), v, v)
  void f (bool is_unsigned, intmax_t s, uintmax_t u)

Looking in `Objects/longobject.c` suggests that perhaps the two functions that 
could be chosen from would be `PyLong_From[Unsigned]LongLong()` to avoid 
truncation.  Is there some reason not to use these?  I don't know the habits of 
CPython developers to know if there's a significant optimization going on here.

Just to throw it out there, in the case of macros, `PyLong_FromString()` might 
even be usable...


Included for quick reference:

int PyModule_AddIntConstant(PyObject *m, const char *name, long value)
  https://hg.python.org/cpython/file/tip/Python/modsupport.c#l566

PyModule_AddIntMacro(m, CAN_EFF_FLAG);
  https://hg.python.org/cpython/file/tip/Modules/socketmodule.c#l7098

PyObject * PyLong_FromLong(long ival)
  https://hg.python.org/cpython/file/tip/Objects/longobject.c#l231

#define PyModule_AddIntMacro(m, c) PyModule_AddIntConstant(m, #c, c)
  https://hg.python.org/cpython/file/tip/Include/modsupport.h#l80

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28215] PyModule_AddIntConstant() wraps >=2^31 values when long is 4 bytes

2016-09-20 Thread Kyle Altendorf

Kyle Altendorf added the comment:

I do not seem to be getting a compiler warning.

arm-fsl-linux-gnueabi-gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv 
-O3 -Wall -Wstrict-prototypes-Werror=declaration-after-statement   -I. 
-IInclude -I./Include 
-I"/epc/t/262/misc-build-arm-fsl-linux-gnueabi/sysroot/root/all//opt/epc/include"
   -DPy_BUILD_CORE  -c ./Modules/socketmodule.c -o Modules/socketmodule.o

To really encompass all cases I think you are correct that both a signed and an 
unsigned handler are needed.  Though, I have an idea for something nifty, I'll 
share if it works.

Regardless, shouldn't they use `intmax_t` and `uintmax_t` from stdtypes.h to 
make sure they handle anything that could be defined in the referenced C code?  
In my case simply switching from `long` to `intmax_t` would be sufficient.

Note that I am not worried about getting this fixed for my one case.  My 
workaround is fine for my application.

I also will hopefully be correcting the subject to >=2**31 if this change does 
what I think.  Good ol' off-by-one.

--
title: PyModule_AddIntConstant() wraps >=2^32 values when long is 4 bytes -> 
PyModule_AddIntConstant() wraps >=2^31 values when long is 4 bytes

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com