Mercurial > pytave
comparison oct-py-types.cc @ 355:8cdc94b83e98
Fix int64 value extraction from long to work on 32-bit, use Octave saturation
* oct-py-types.cc (pytave::extract_py_int64): Extract value to appropriately
sized integer type for platform architecture. Saturate value on overflow.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Mon, 22 Aug 2016 17:27:46 -0700 |
parents | eac35d84ef0d |
children | 07c1b457cb6b |
comparison
equal
deleted
inserted
replaced
354:eec3ed1c0578 | 355:8cdc94b83e98 |
---|---|
22 | 22 |
23 #if defined (HAVE_CONFIG_H) | 23 #if defined (HAVE_CONFIG_H) |
24 # include <config.h> | 24 # include <config.h> |
25 #endif | 25 #endif |
26 | 26 |
27 #include <limits> | |
27 #include <octave/Cell.h> | 28 #include <octave/Cell.h> |
28 #include <octave/oct-map.h> | 29 #include <octave/oct-map.h> |
29 #include <octave/quit.h> | 30 #include <octave/quit.h> |
30 | 31 |
31 #include "exceptions.h" | 32 #include "exceptions.h" |
384 { | 385 { |
385 if (! obj) | 386 if (! obj) |
386 throw object_convert_exception ("failed to extract integer: null object"); | 387 throw object_convert_exception ("failed to extract integer: null object"); |
387 | 388 |
388 if (PyLong_Check (obj)) | 389 if (PyLong_Check (obj)) |
389 return PyLong_AsLong (obj); | 390 { |
391 int overflow = 0; | |
392 #if (defined (HAVE_LONG_LONG) && (SIZEOF_LONG_LONG == 8)) | |
393 PY_LONG_LONG value = PyLong_AsLongLongAndOverflow (obj, &overflow); | |
394 #else | |
395 long value = PyLong_AsLongAndOverflow (obj, &overflow); | |
396 #endif | |
397 if (overflow) | |
398 if (overflow > 0) | |
399 value = std::numeric_limits<int64_t>::max (); | |
400 else | |
401 value = std::numeric_limits<int64_t>::min (); | |
402 return static_cast<int64_t> (value); | |
403 } | |
390 #if PY_VERSION_HEX < 0x03000000 | 404 #if PY_VERSION_HEX < 0x03000000 |
391 else if (PyInt_Check (obj)) | 405 else if (PyInt_Check (obj)) |
392 return PyInt_AsLong (obj); | 406 return PyInt_AsLong (obj); |
393 #endif | 407 #endif |
394 else | 408 else |