Skip to content

Commit 054ff3c

Browse files
Update int range detection for Python 3 (cztomczak#603)
* Update int range detection for Python 3 Python 3 unified ints and longs into a single int type. The long type is still available in Cython as an alias to int. * Update javascript_bindings.pyx * Update process_message_utils.pyx Co-authored-by: Czarek Tomczak <[email protected]>
1 parent 96f3b5e commit 054ff3c

4 files changed

Lines changed: 15 additions & 24 deletions

File tree

Authors

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ Contributors:
1919
Dónal McMullan <https://github.com/donalm>
2020
nobodyguy <https://github.com/nobodyguy>
2121
Elliot Woods <https://github.com/elliotwoods>
22+
Bryan Koroleski <https://github.com/bryan-koroleski-fivestars>

src/javascript_bindings.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ cdef class JavascriptBindings:
140140
return True
141141
elif valueType == int:
142142
return True
143+
elif valueType == long:
144+
return True
143145
elif valueType == type(None):
144146
return True
145147
elif IsFunctionOrMethod(valueType):

src/process_message_utils.pyx

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,9 @@ cdef CefRefPtr[CefListValue] PyListToCefListValue(
238238
ret.get().SetNull(index)
239239
elif valueType == bool:
240240
ret.get().SetBool(index, bool(value))
241-
elif valueType == int:
242-
ret.get().SetInt(index, int(value))
243-
elif valueType == long:
244-
# Int32 range is -2147483648..2147483647, we've increased the
245-
# minimum size by one as Cython was throwing a warning:
246-
# "unary minus operator applied to unsigned type, result still
247-
# unsigned".
248-
if -2147483647 <= value <= 2147483647:
241+
elif valueType == int or valueType == long: # In Py3 int and long types are the same type.
242+
# Int32 range is -2147483648..2147483647
243+
if INT_MIN <= value <= INT_MAX:
249244
ret.get().SetInt(index, int(value))
250245
else:
251246
# Long values become strings.
@@ -297,14 +292,9 @@ cdef void PyListToExistingCefListValue(
297292
cefListValue.get().SetNull(index)
298293
elif valueType == bool:
299294
cefListValue.get().SetBool(index, bool(value))
300-
elif valueType == int:
301-
cefListValue.get().SetInt(index, int(value))
302-
elif valueType == long:
303-
# Int32 range is -2147483648..2147483647, we've increased the
304-
# minimum size by one as Cython was throwing a warning:
305-
# "unary minus operator applied to unsigned type, result still
306-
# unsigned".
307-
if -2147483647 <= value <= 2147483647:
295+
elif valueType == int or valueType == long: # In Py3 int and long types are the same type.
296+
# Int32 range is -2147483648..2147483647
297+
if INT_MIN <= value <= INT_MAX:
308298
cefListValue.get().SetInt(index, int(value))
309299
else:
310300
# Long values become strings.
@@ -357,14 +347,9 @@ cdef CefRefPtr[CefDictionaryValue] PyDictToCefDictionaryValue(
357347
ret.get().SetNull(cefKey)
358348
elif valueType == bool:
359349
ret.get().SetBool(cefKey, bool(value))
360-
elif valueType == int:
361-
ret.get().SetInt(cefKey, int(value))
362-
elif valueType == long:
363-
# Int32 range is -2147483648..2147483647, we've increased the
364-
# minimum size by one as Cython was throwing a warning:
365-
# "unary minus operator applied to unsigned type, result still
366-
# unsigned".
367-
if -2147483647 <= value <= 2147483647:
350+
elif valueType == int or valueType == long: # In Py3 int and long types are the same type.
351+
# Int32 range is -2147483648..2147483647
352+
if INT_MIN <= value <= INT_MAX:
368353
ret.get().SetInt(cefKey, int(value))
369354
else:
370355
# Long values become strings.

tools/cython_setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,9 @@ def compile_time_constants():
470470
# A way around Python 3.2 bug: UNAME_SYSNAME is not set
471471
contents += 'DEF UNAME_SYSNAME = "%s"\n' % platform.uname()[0]
472472
contents += 'DEF PY_MAJOR_VERSION = %s\n' % sys.version_info.major
473+
contents += 'cdef extern from "limits.h":\n'
474+
contents += ' cdef int INT_MIN\n'
475+
contents += ' cdef int INT_MAX\n'
473476
fo.write(contents.encode("utf-8"))
474477

475478

0 commit comments

Comments
 (0)