Opened 6 years ago

Closed 5 years ago

#237 closed defect (fixed)

Large integer constants wrap around or fail to compile

Reported by: scoder Owned by: scoder
Priority: major Milestone: 0.12
Component: Code Generation Keywords:


Large integer constants and constant expressions can end up incorrectly in the C code. This can even break the compilation.


print 2147483647 + 2147483647
print 2**31 + 2**31

leads to this code:

  __pyx_t_1 = PyInt_FromLong(4294967294); if /* ... */
  __pyx_t_1 = PyInt_FromLong(4294967296); if /* ... */

Change History (6)

comment:1 Changed 6 years ago by dagss

That can be fixed quite easily by expressing the number as a string in the source instead if it overflows, can it not? Assuming 32-bit for the MAX_INT cut-off point for that should be OK if it is done at module startup time...

comment:2 Changed 6 years ago by scoder

  • Owner changed from somebody to scoder
  • Status changed from new to assigned

This is fixed as part of the constant folding refactoring.

See ticket #253.

comment:3 Changed 6 years ago by scoder

This should probably go into cython-devel soon.

comment:4 Changed 5 years ago by dagss

  • Milestone changed from 0.11.2 to 0.11.3

comment:5 Changed 5 years ago by scoder

  • Milestone changed from 0.11.3 to 0.12

I'm moving this to 0.12. This means that Cython 0.11.x stays broken for large constants (with or without constant folding).

The reason is that it would require a rather critical backport of the constant folding refactoring to enable the constant caching (__pyx_int_12345) for calculated values. And still, this only solves the problem for Python values. C values are still affected (although I consider that less critical as users have to care about large constants in C code anyway: they are not necessarily portable).

comment:6 Changed 5 years ago by scoder

  • Resolution set to fixed
  • Status changed from assigned to closed

Marking as fixed for Python constants. C constants are left to the user.

Note: See TracTickets for help on using tickets.