Skip to content

Commit ddbc8d2

Browse files
lukasstadlerfangerer
authored andcommitted
add more test_long
1 parent ff1c245 commit ddbc8d2

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_long.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,21 +360,27 @@ def compile_module(self, name):
360360
(-9223372036854775808, 8, False, True, b'\x80\x00\x00\x00\x00\x00\x00\x00'),
361361
(9223372036854775807, 9, False, True, b'\x00\x7f\xff\xff\xff\xff\xff\xff\xff'),
362362
(-9223372036854775808, 9, False, True, b'\xff\x80\x00\x00\x00\x00\x00\x00\x00'),
363+
(12, 8, False, True, b'\x00\x00\x00\x00\x00\x00\x00\x0c'),
363364
(1234, 8, False, True, b'\x00\x00\x00\x00\x00\x00\x04\xd2'),
364365
(0xdeadbeefdead, 8, False, True, b'\x00\x00\xde\xad\xbe\xef\xde\xad'),
365366
(0xdeadbeefdead, 8, True, True, b'\xad\xde\xef\xbe\xad\xde\x00\x00'),
367+
(0xdeadbeefdead, 7, True, True, b'\xad\xde\xef\xbe\xad\xde\x00'),
366368
(0xdeadbeefdeadbeefbeefdeadcafebabe, 17, False, True, b'\x00\xde\xad\xbe\xef\xde\xad\xbe\xef\xbe\xef\xde\xad\xca\xfe\xba\xbe'),
367369
),
368370
code='''PyObject* wrap_PyLong_AsByteArray(PyObject* object, Py_ssize_t n, int little_endian, int is_signed, PyObject* unused) {
369-
unsigned char* buf = (unsigned char *) malloc(n * sizeof(unsigned char));
371+
unsigned char* buf = (unsigned char *) malloc(n * sizeof(unsigned char) + 1);
372+
memset(buf, 0x33, n + 1);
370373
PyObject* result;
371-
372374
Py_INCREF(object);
373375
if (_PyLong_AsByteArray((PyLongObject*) object, buf, n, little_endian, is_signed)) {
374376
Py_DECREF(object);
375377
return NULL;
376378
}
377379
Py_DECREF(object);
380+
if (buf[n] != 0x33) {
381+
PyErr_SetString(PyExc_SystemError, "Sentinel value corrupted.");
382+
return NULL;
383+
}
378384
result = PyBytes_FromStringAndSize((const char *) buf, n);
379385
free(buf);
380386
return result;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextLongBuiltins.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -414,16 +414,21 @@ static int run() {
414414

415415
@CApiBuiltin(ret = Int, args = {PyLongObject, UNSIGNED_CHAR_PTR, SIZE_T, Int, Int}, call = Direct)
416416
abstract static class _PyLong_AsByteArray extends CApi5BuiltinNode {
417+
private static void checkSign(boolean negative, int isSigned, PRaiseNode raise) {
418+
if (negative) {
419+
if (isSigned == 0) {
420+
throw raise.raise(OverflowError, ErrorMessages.MESSAGE_CONVERT_NEGATIVE);
421+
}
422+
}
423+
}
424+
417425
@Specialization
418426
static Object get(int value, Object bytes, long n, int littleEndian, int isSigned,
419427
@Bind("this") Node inliningTarget,
420428
@Shared @Cached InlinedConditionProfile profile,
421429
@Shared @Cached PRaiseNode raise,
422430
@Shared @Cached CStructAccess.WriteByteNode write) {
423-
if (isSigned == 0 && value < 0) {
424-
throw raise.raise(OverflowError, ErrorMessages.MESSAGE_CONVERT_NEGATIVE);
425-
}
426-
431+
checkSign(value < 0, isSigned, raise);
427432
byte[] array = IntBuiltins.ToBytesNode.fromLong(value, PythonUtils.toIntError(n), littleEndian == 0, isSigned != 0, inliningTarget, profile, raise);
428433
write.writeByteArray(bytes, array);
429434
return 0;
@@ -435,10 +440,7 @@ static Object get(long value, Object bytes, long n, int littleEndian, int isSign
435440
@Shared @Cached InlinedConditionProfile profile,
436441
@Shared @Cached PRaiseNode raise,
437442
@Shared @Cached CStructAccess.WriteByteNode write) {
438-
if (isSigned == 0 && value < 0) {
439-
throw raise.raise(OverflowError, ErrorMessages.MESSAGE_CONVERT_NEGATIVE);
440-
}
441-
443+
checkSign(value < 0, isSigned, raise);
442444
byte[] array = IntBuiltins.ToBytesNode.fromLong(value, PythonUtils.toIntError(n), littleEndian == 0, isSigned != 0, inliningTarget, profile, raise);
443445
write.writeByteArray(bytes, array);
444446
return 0;
@@ -450,10 +452,7 @@ static Object get(PInt value, Object bytes, long n, int littleEndian, int isSign
450452
@Shared @Cached InlinedConditionProfile profile,
451453
@Shared @Cached PRaiseNode raise,
452454
@Shared @Cached CStructAccess.WriteByteNode write) {
453-
if (isSigned == 0 && value.isNegative()) {
454-
throw raise.raise(OverflowError, ErrorMessages.MESSAGE_CONVERT_NEGATIVE);
455-
}
456-
455+
checkSign(value.isNegative(), isSigned, raise);
457456
byte[] array = IntBuiltins.ToBytesNode.fromBigInteger(value, PythonUtils.toIntError(n), littleEndian == 0, isSigned != 0, inliningTarget, profile, raise);
458457
write.writeByteArray(bytes, array);
459458
return 0;

0 commit comments

Comments
 (0)