Commits

Ronald Oussoren committed 9afd33b

Ensure saner conversion from negative double to unsigned integers

(the conversion is undefined by C according to information found on google,
the current PyObjC implementation ensures that the conversion behaves simular
to the conversion from signed integers to unsigned integers)

Comments (0)

Files changed (1)

pyobjc-core/Modules/objc/OC_PythonNumber.m

 #endif
 		} else if (PyFloat_Check(value)) {
 			double temp = PyFloat_AsDouble(value);
-			result =  (unsigned long long)temp;
+			if (temp < 0) {
+				/* Conversion of negative numbers to
+				 * unsigned long long is undefined behaviour,
+				 * the code below seems to get the behaviour
+				 * we'd like: casting to unsigned long long
+				 * behaves simular to casting a signed integer
+				 * to undefined.
+				 */
+				long long t = (long long)temp;
+				result = (unsigned long long)t;
+			} else {
+				result =  (unsigned long long)temp;
+			}
 			PyObjC_GIL_RETURN(result);
 		}
 	PyObjC_END_WITH_GIL