Skip to content

Commit c2e1ae4

Browse files
committed
Fix: PyLong_AsUnsignedLong accepts subtypes of int
1 parent 7507f10 commit c2e1ae4

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ def compile_module(self, name):
175175
(0,),
176176
(-1,),
177177
(-2,),
178+
(True,),
179+
(False,),
178180
(0x7fffffff,),
179181
(0xffffffff,),
180182
# we could use larger values on 64-bit systems but how should we know?

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PythonCextBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ long doPrimitiveNativeWrapperToLong(VirtualFrame frame, Object object, int mode,
11771177
try {
11781178
if (resolvedPointer instanceof PrimitiveNativeWrapper) {
11791179
PrimitiveNativeWrapper wrapper = (PrimitiveNativeWrapper) resolvedPointer;
1180-
if (requiredPInt(mode) && !wrapper.isIntLike()) {
1180+
if (requiredPInt(mode) && !wrapper.isSubtypeOfInt()) {
11811181
throw raise(TypeError, ErrorMessages.INTEGER_REQUIRED);
11821182
}
11831183
return ensureConvertPIntToPrimitiveNode().executeLong(wrapper, signed(mode), PInt.intValueExact(targetTypeSize), exact(mode));
@@ -1198,7 +1198,7 @@ Object doGeneric(VirtualFrame frame, Object objectPtr, int mode, long targetType
11981198
try {
11991199
if (resolvedPointer instanceof PrimitiveNativeWrapper) {
12001200
PrimitiveNativeWrapper wrapper = (PrimitiveNativeWrapper) resolvedPointer;
1201-
if (requiredPInt(mode) && !wrapper.isIntLike()) {
1201+
if (requiredPInt(mode) && !wrapper.isSubtypeOfInt()) {
12021202
throw raise(TypeError, ErrorMessages.INTEGER_REQUIRED);
12031203
}
12041204
return ensureConvertPIntToPrimitiveNode().execute(wrapper, signed(mode), PInt.intValueExact(targetTypeSize), exact(mode));

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/DynamicObjectNativeWrapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,10 @@ public boolean isIntLike() {
17151715
return (state & (PRIMITIVE_STATE_BYTE | PRIMITIVE_STATE_INT | PRIMITIVE_STATE_LONG)) != 0;
17161716
}
17171717

1718+
public boolean isSubtypeOfInt() {
1719+
return !isDouble();
1720+
}
1721+
17181722
// this method exists just for readability
17191723
public Object getMaterializedObject(PythonNativeWrapperLibrary lib) {
17201724
return lib.getDelegate(this);

0 commit comments

Comments
 (0)