Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ def __ceil__(self):
return 'hello'

self.assertEqual(math.ceil(I(22)), 22)
self.assertEqual(type(math.ceil(I(22))), int)
self.assertEqual(math.ceil(I2(256)), 11)
self.assertEqual(math.ceil(I(156)), 156)
self.assertEqual(math.ceil(I2(777)), 11)
Expand Down
14 changes: 12 additions & 2 deletions graalpython/com.oracle.graal.python.test/src/tests/test_string.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018, 2025, Oracle and/or its affiliates.
# Copyright (c) 2018, 2026, Oracle and/or its affiliates.
# Copyright (C) 1996-2017 Python Software Foundation
#
# Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
Expand Down Expand Up @@ -168,6 +168,10 @@ def test_strip():
assert u' test '.strip() == u'test'


def test_splitlines_keepends_type_error():
assert "foo\n".splitlines("bla") == ["foo\n"]


def assertEqual(value, expected):
assert value == expected, ("'%s' was expected to be equal to '%s'" % (value, expected))

Expand Down Expand Up @@ -1095,6 +1099,12 @@ def test_splitlines():
assert len(str.splitlines("a\nb")) == 2


def test_split_negative_maxsplit_matches_unlimited():
s = "0x1.e800000000000p+5"
assert s.split(s, maxsplit=-84) == ["", ""]
assert s.split(s, maxsplit=-1) == ["", ""]


def test_literals():
s = "hello\[world\]"
assert len(s) == 14
Expand Down Expand Up @@ -1233,4 +1243,4 @@ def test_fstring():
assert type(f"hello {FunkyFormat('world')}!") == str
assert f"hello {FunkyFormat('world')}!" == "hello world!"
assert f"hello {FunkyStr('world')}!" == "hello world!"
assertRaises(TypeError, lambda: f"hello {FunkyFormat(33)}!")
assertRaises(TypeError, lambda: f"hello {FunkyFormat(33)}!")
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2025, Oracle and/or its affiliates.
* Copyright (c) 2017, 2026, Oracle and/or its affiliates.
* Copyright (c) 2014, Regents of the University of California
*
* All rights reserved.
Expand Down Expand Up @@ -96,7 +96,6 @@
import com.oracle.graal.python.lib.PyNumberIndexNode;
import com.oracle.graal.python.lib.PyUnicodeCheckNode;
import com.oracle.graal.python.nodes.ErrorMessages;
import com.oracle.graal.python.nodes.PGuards;
import com.oracle.graal.python.nodes.PRaiseNode;
import com.oracle.graal.python.nodes.SpecialAttributeNames;
import com.oracle.graal.python.nodes.SpecialMethodNames;
Expand All @@ -112,7 +111,6 @@
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryClinicBuiltinNode;
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode;
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
import com.oracle.graal.python.runtime.ExecutionContext.BoundaryCallContext;
import com.oracle.graal.python.runtime.IndirectCallData.BoundaryCallData;
Expand Down Expand Up @@ -1804,30 +1802,22 @@ protected List<byte[]> splitDelimiter(byte[] bytes, int len, byte[] sep, int max
// bytes.splitlines([keepends])
// bytearray.splitlines([keepends])
@Builtin(name = "splitlines", minNumOfPositionalArgs = 1, parameterNames = {"self", "keepends"})
@ArgumentClinic(name = "keepends", conversion = ArgumentClinic.ClinicConversion.Boolean, defaultValue = "false")
@GenerateNodeFactory
public abstract static class SplitLinesNode extends PythonBinaryBuiltinNode {
public abstract static class SplitLinesNode extends PythonBinaryClinicBuiltinNode {

@Override
protected ArgumentClinicProvider getArgumentClinic() {
return BytesCommonBuiltinsClinicProviders.SplitLinesNodeClinicProviderGen.INSTANCE;
}

@Specialization
static PList doSplitlines(Object self, Object keependsObj,
static PList doSplitlines(Object self, boolean keepends,
@Bind Node inliningTarget,
@Bind PythonLanguage language,
@Cached InlinedBranchProfile isPNoneProfile,
@Cached InlinedBranchProfile isBooleanProfile,
@Cached InlinedConditionProfile keependsProfile,
@Cached CastToJavaIntExactNode cast,
@Cached BytesNodes.ToBytesNode toBytesNode,
@Cached ListNodes.AppendNode appendNode,
@Cached BytesNodes.CreateBytesNode create) {
boolean keepends;
if (keependsObj instanceof Boolean b) {
isBooleanProfile.enter(inliningTarget);
keepends = b;
} else if (PGuards.isPNone(keependsObj)) {
isPNoneProfile.enter(inliningTarget);
keepends = false;
} else {
keepends = cast.execute(inliningTarget, keependsObj) != 0;
}
keepends = keependsProfile.profile(inliningTarget, keepends);
byte[] bytes = toBytesNode.execute(null, self);
PList list = PFactory.createList(language);
int sliceStart = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1541,8 +1541,9 @@ static long ceil(long arg) {
}

@Specialization
static PInt ceil(PInt arg) {
return arg;
static PInt ceil(PInt arg,
@Bind PythonLanguage language) {
return PFactory.createInt(language, arg.getValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
import com.oracle.graal.python.builtins.objects.slice.SliceNodes.ComputeIndices;
import com.oracle.graal.python.builtins.objects.str.StringBuiltinsClinicProviders.FormatNodeClinicProviderGen;
import com.oracle.graal.python.builtins.objects.str.StringBuiltinsClinicProviders.SplitNodeClinicProviderGen;
import com.oracle.graal.python.builtins.objects.str.StringBuiltinsClinicProviders.SplitLinesNodeClinicProviderGen;
import com.oracle.graal.python.builtins.objects.str.StringNodes.CastToJavaStringCheckedNode;
import com.oracle.graal.python.builtins.objects.str.StringNodes.CastToTruffleStringChecked0Node;
import com.oracle.graal.python.builtins.objects.str.StringNodes.CastToTruffleStringChecked1Node;
Expand Down Expand Up @@ -157,7 +158,6 @@
import com.oracle.graal.python.nodes.object.BuiltinClassProfiles.IsBuiltinClassExactProfile;
import com.oracle.graal.python.nodes.object.GetClassNode;
import com.oracle.graal.python.nodes.util.CannotCastException;
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
import com.oracle.graal.python.runtime.ExecutionContext.BoundaryCallContext;
import com.oracle.graal.python.runtime.IndirectCallData.BoundaryCallData;
Expand Down Expand Up @@ -1277,7 +1277,7 @@ static PList doStringSep(TruffleString self, TruffleString sep, int maxsplit,
if (sep.isEmpty()) {
throw raiseNode.raise(inliningTarget, ValueError, ErrorMessages.EMPTY_SEPARATOR);
}
int splits = maxsplit == -1 ? Integer.MAX_VALUE : maxsplit;
int splits = maxsplit < 0 ? Integer.MAX_VALUE : maxsplit;

PList list = PFactory.createList(PythonLanguage.get(inliningTarget));
int lastEnd = 0;
Expand Down Expand Up @@ -1460,14 +1460,13 @@ static PList doStringMaxsplit(VirtualFrame frame, TruffleString s, @SuppressWarn

// str.splitlines([keepends])
@Builtin(name = "splitlines", minNumOfPositionalArgs = 1, parameterNames = {"self", "keepends"})
@ArgumentClinic(name = "keepends", conversion = ClinicConversion.Boolean, defaultValue = "false")
@GenerateNodeFactory
public abstract static class SplitLinesNode extends PythonBinaryBuiltinNode {
public abstract static class SplitLinesNode extends PythonBinaryClinicBuiltinNode {

@Specialization
static PList doString(TruffleString self, @SuppressWarnings("unused") PNone keepends,
@Bind Node inliningTarget,
@Cached @Shared SplitLinesInnerNode innerNode) {
return innerNode.execute(inliningTarget, self, false);
@Override
protected ArgumentClinicProvider getArgumentClinic() {
return SplitLinesNodeClinicProviderGen.INSTANCE;
}

@Specialization
Expand All @@ -1477,15 +1476,13 @@ static PList doStringKeepends(TruffleString selfTs, boolean keepends,
return innerNode.execute(inliningTarget, selfTs, keepends);
}

@Specialization(replaces = {"doString", "doStringKeepends"})
static PList doGeneric(Object self, Object keepends,
@Specialization(replaces = "doStringKeepends")
static PList doGeneric(Object self, boolean keepends,
@Bind Node inliningTarget,
@Cached CastToTruffleStringChecked2Node castSelfNode,
@Cached CastToJavaIntExactNode castToJavaIntNode,
@Cached @Exclusive SplitLinesInnerNode innerNode) {
TruffleString selfStr = castSelfNode.cast(inliningTarget, self, ErrorMessages.REQUIRES_STR_OBJECT_BUT_RECEIVED_P, "splitlines", self);
boolean bKeepends = !PGuards.isPNone(keepends) && castToJavaIntNode.execute(inliningTarget, keepends) != 0;
return innerNode.execute(inliningTarget, selfStr, bKeepends);
return innerNode.execute(inliningTarget, selfStr, keepends);
}

@GenerateCached(false)
Expand Down
Loading