Skip to content

Commit c100ea5

Browse files
committed
[GR-57317] Update and clean up patches, part #2
PullRequest: graalpython/3455
2 parents bdda44c + 19a7eef commit c100ea5

File tree

11 files changed

+170
-31
lines changed

11 files changed

+170
-31
lines changed

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

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@
4646

4747
try:
4848
raise TypeError
49-
except:
49+
except TypeError as e:
5050
TB = sys.exc_info()[2]
51+
exception_with_traceback = e
5152

5253

5354
class TestExceptionobject(object):
@@ -148,18 +149,80 @@ def raise_exception_without_cause():
148149
return e
149150

150151

152+
exception_with_cause = AssertionError()
153+
exception_with_cause.__cause__ = NameError()
154+
exception_with_context = AssertionError()
155+
exception_with_context.__context__ = AttributeError()
156+
151157
class TestExceptionobjectFunctions(CPyExtTestCase):
152158

153159
test_PyException_SetTraceback = CPyExtFunction(
154-
lambda args: 0,
160+
lambda args: args[1],
161+
lambda: (
162+
(AssertionError(), TB),
163+
(exception_with_traceback, None),
164+
),
165+
code='''
166+
static PyObject* wrap_PyException_SetTraceback(PyObject* exc, PyObject* traceback) {
167+
PyException_SetTraceback(exc, traceback);
168+
traceback = PyException_GetTraceback(exc);
169+
if (traceback == NULL) {
170+
Py_RETURN_NONE;
171+
} else {
172+
return traceback;
173+
}
174+
}
175+
''',
176+
callfunction='wrap_PyException_SetTraceback',
177+
argspec='OO',
178+
arguments=['PyObject* exc', 'PyObject* traceback'],
179+
resultspec='O',
180+
)
181+
182+
test_PyException_SetCause = CPyExtFunction(
183+
lambda args: args[1],
184+
lambda: (
185+
(AssertionError(), NameError()),
186+
(exception_with_context, None),
187+
),
188+
code='''
189+
static PyObject* wrap_PyException_SetCause(PyObject* exc, PyObject* cause) {
190+
PyException_SetCause(exc, cause != Py_None ? cause : NULL);
191+
cause = PyException_GetCause(exc);
192+
if (cause == NULL) {
193+
Py_RETURN_NONE;
194+
} else {
195+
return cause;
196+
}
197+
}
198+
''',
199+
callfunction='wrap_PyException_SetCause',
200+
argspec='OO',
201+
arguments=['PyObject* exc', 'PyObject* cause'],
202+
resultspec='O',
203+
)
204+
205+
test_PyException_SetContext = CPyExtFunction(
206+
lambda args: args[1],
155207
lambda: (
156-
(
157-
AssertionError(), TB
158-
),
208+
(AssertionError(), NameError()),
209+
(exception_with_cause, None),
159210
),
160-
resultspec="i",
161-
argspec="OO",
162-
arguments=["PyObject* exc", "PyObject* tb"],
211+
code='''
212+
static PyObject* wrap_PyException_SetContext(PyObject* exc, PyObject* context) {
213+
PyException_SetContext(exc, context != Py_None ? context : NULL);
214+
context = PyException_GetContext(exc);
215+
if (context == NULL) {
216+
Py_RETURN_NONE;
217+
} else {
218+
return context;
219+
}
220+
}
221+
''',
222+
callfunction='wrap_PyException_SetContext',
223+
argspec='OO',
224+
arguments=['PyObject* exc', 'PyObject* context'],
225+
resultspec='O',
163226
)
164227

165228

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@
6060
import static com.oracle.graal.python.nodes.BuiltinNames.T_LAST_VALUE;
6161
import static com.oracle.graal.python.nodes.ErrorMessages.EXCEPTION_NOT_BASEEXCEPTION;
6262
import static com.oracle.graal.python.nodes.ErrorMessages.MUST_BE_MODULE_CLASS;
63-
import static com.oracle.graal.python.nodes.SpecialAttributeNames.T___CAUSE__;
64-
import static com.oracle.graal.python.nodes.SpecialAttributeNames.T___CONTEXT__;
6563
import static com.oracle.graal.python.nodes.SpecialAttributeNames.T___DOC__;
6664
import static com.oracle.graal.python.nodes.SpecialAttributeNames.T___MODULE__;
6765
import static com.oracle.graal.python.nodes.SpecialAttributeNames.T___TRACEBACK__;
@@ -560,9 +558,9 @@ abstract static class PyException_SetCause extends CApiBinaryBuiltinNode {
560558
@Specialization
561559
Object setCause(Object exc, Object cause,
562560
@Bind("this") Node inliningTarget,
563-
@Cached PyObjectSetAttr setAttrNode) {
564-
setAttrNode.execute(inliningTarget, exc, T___CAUSE__, cause);
565-
return PNone.NONE;
561+
@Cached ExceptionNodes.SetCauseNode setCauseNode) {
562+
setCauseNode.execute(inliningTarget, exc, cause != PNone.NO_VALUE ? cause : PNone.NONE);
563+
return PNone.NO_VALUE;
566564
}
567565
}
568566

@@ -571,8 +569,8 @@ abstract static class PyException_GetCause extends CApiUnaryBuiltinNode {
571569
@Specialization
572570
Object getCause(Object exc,
573571
@Bind("this") Node inliningTarget,
574-
@Cached PyObjectGetAttr getAttrNode) {
575-
return noneToNativeNull(inliningTarget, getAttrNode.execute(inliningTarget, exc, T___CAUSE__));
572+
@Cached ExceptionNodes.GetCauseNode getCauseNode) {
573+
return noneToNativeNull(inliningTarget, getCauseNode.execute(inliningTarget, exc));
576574
}
577575
}
578576

@@ -581,8 +579,8 @@ abstract static class PyException_GetContext extends CApiUnaryBuiltinNode {
581579
@Specialization
582580
Object setCause(Object exc,
583581
@Bind("this") Node inliningTarget,
584-
@Cached PyObjectGetAttr getAttrNode) {
585-
return noneToNativeNull(inliningTarget, getAttrNode.execute(inliningTarget, exc, T___CONTEXT__));
582+
@Cached ExceptionNodes.GetContextNode getContextNode) {
583+
return noneToNativeNull(inliningTarget, getContextNode.execute(inliningTarget, exc));
586584
}
587585
}
588586

@@ -591,9 +589,9 @@ abstract static class PyException_SetContext extends CApiBinaryBuiltinNode {
591589
@Specialization
592590
Object setContext(Object exc, Object context,
593591
@Bind("this") Node inliningTarget,
594-
@Cached PyObjectSetAttr setAttrNode) {
595-
setAttrNode.execute(inliningTarget, exc, T___CONTEXT__, context);
596-
return PNone.NONE;
592+
@Cached ExceptionNodes.SetContextNode setContextNode) {
593+
setContextNode.execute(inliningTarget, exc, context != PNone.NO_VALUE ? context : PNone.NONE);
594+
return PNone.NO_VALUE;
597595
}
598596
}
599597

@@ -603,8 +601,8 @@ abstract static class PyException_GetTraceback extends CApiUnaryBuiltinNode {
603601
@Specialization
604602
Object getTraceback(Object exc,
605603
@Bind("this") Node inliningTarget,
606-
@Cached PyObjectGetAttr getAttrNode) {
607-
return noneToNativeNull(inliningTarget, getAttrNode.execute(inliningTarget, exc, T___TRACEBACK__));
604+
@Cached ExceptionNodes.GetTracebackNode getTracebackNode) {
605+
return noneToNativeNull(inliningTarget, getTracebackNode.execute(inliningTarget, exc));
608606
}
609607
}
610608

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/TextIOWrapperBuiltins.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules.io;
4242

43+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.AttributeError;
4344
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.IOUnsupportedOperation;
4445
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.PTextIOWrapper;
4546
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.RuntimeError;
@@ -94,6 +95,7 @@
9495
import static com.oracle.graal.python.builtins.modules.io.TextIOWrapperNodes.setNewline;
9596
import static com.oracle.graal.python.builtins.modules.io.TextIOWrapperNodes.validateNewline;
9697
import static com.oracle.graal.python.nodes.ErrorMessages.A_STRICTLY_POSITIVE_INTEGER_IS_REQUIRED;
98+
import static com.oracle.graal.python.nodes.ErrorMessages.CANNOT_DELETE;
9799
import static com.oracle.graal.python.nodes.ErrorMessages.CAN_T_DO_NONZERO_CUR_RELATIVE_SEEKS;
98100
import static com.oracle.graal.python.nodes.ErrorMessages.CAN_T_DO_NONZERO_END_RELATIVE_SEEKS;
99101
import static com.oracle.graal.python.nodes.ErrorMessages.CAN_T_RECONSTRUCT_LOGICAL_FILE_POSITION;
@@ -136,6 +138,7 @@
136138
import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAccessLibrary;
137139
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
138140
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
141+
import com.oracle.graal.python.builtins.objects.getsetdescriptor.DescriptorDeleteMarker;
139142
import com.oracle.graal.python.builtins.objects.ints.PInt;
140143
import com.oracle.graal.python.builtins.objects.str.StringNodes.StringReplaceNode;
141144
import com.oracle.graal.python.builtins.objects.str.StringUtils.SimpleTruffleStringFormatNode;
@@ -1157,7 +1160,7 @@ static TruffleString doit(PTextIO self) {
11571160
}
11581161
}
11591162

1160-
@Builtin(name = J__CHUNK_SIZE, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, isGetter = true, isSetter = true)
1163+
@Builtin(name = J__CHUNK_SIZE, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, isGetter = true, isSetter = true, allowsDelete = true)
11611164
@GenerateNodeFactory
11621165
abstract static class ChunkSizeNode extends PythonBuiltinNode {
11631166

@@ -1166,7 +1169,7 @@ static Object none(PTextIO self, @SuppressWarnings("unused") PNone none) {
11661169
return self.getChunkSize();
11671170
}
11681171

1169-
@Specialization(guards = {"self.isOK()", "!self.isDetached()", "!isNoValue(arg)"})
1172+
@Specialization(guards = {"self.isOK()", "!self.isDetached()", "!isNoValue(arg)", "!isDeleteMarker(arg)"})
11701173
static Object chunkSize(VirtualFrame frame, PTextIO self, Object arg,
11711174
@Bind("this") Node inliningTarget,
11721175
@Cached PyNumberAsSizeNode asSizeNode,
@@ -1179,6 +1182,12 @@ static Object chunkSize(VirtualFrame frame, PTextIO self, Object arg,
11791182
return 0;
11801183
}
11811184

1185+
@Specialization(guards = {"self.isOK()", "!self.isDetached()"})
1186+
static Object noDelete(@SuppressWarnings("unused") PTextIO self, @SuppressWarnings("unused") DescriptorDeleteMarker marker,
1187+
@Shared @Cached PRaiseNode raiseNode) {
1188+
throw raiseNode.raise(AttributeError, CANNOT_DELETE);
1189+
}
1190+
11821191
@Specialization(guards = "!self.isOK()")
11831192
static Object initError(@SuppressWarnings("unused") PTextIO self, @SuppressWarnings("unused") Object arg,
11841193
@Shared @Cached PRaiseNode raiseNode) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/getsetdescriptor/DescriptorBuiltins.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.getsetdescriptor;
4242

43-
import static com.oracle.graal.python.builtins.modules.io.IONodes.T__CHUNK_SIZE;
4443
import static com.oracle.graal.python.nodes.SpecialAttributeNames.J___NAME__;
4544
import static com.oracle.graal.python.nodes.SpecialAttributeNames.J___QUALNAME__;
4645
import static com.oracle.graal.python.runtime.exception.PythonErrorType.AttributeError;
4746
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
48-
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;
4947

5048
import java.util.List;
5149

@@ -226,11 +224,6 @@ Object doGetSetDescriptor(VirtualFrame frame, GetSetDescriptor descr, Object obj
226224
} else {
227225
branchProfile.enter(inliningTarget);
228226
if (descr.getSet() != null) {
229-
if (descr.getName().equalsUncached(T__CHUNK_SIZE, TS_ENCODING)) {
230-
// This is a special error message case. see
231-
// Modules/_io/textio.c:textiowrapper_chunk_size_set
232-
throw raiseNode.get(inliningTarget).raise(AttributeError, ErrorMessages.CANNOT_DELETE);
233-
}
234227
throw raiseNode.get(inliningTarget).raise(TypeError, ErrorMessages.CANNOT_DELETE_ATTRIBUTE, getNameNode.execute(inliningTarget, descr.getType()), descr.getName());
235228
} else {
236229
throw raiseNode.get(inliningTarget).raise(AttributeError, ErrorMessages.ATTRIBUTE_S_OF_P_OBJECTS_IS_NOT_WRITABLE, descr.getName(), obj);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
diff --git a/crates/jiter/src/py_string_cache.rs b/crates/jiter/src/py_string_cache.rs
2+
index 96dcf66..21e6979 100644
3+
--- a/crates/jiter/src/py_string_cache.rs
4+
+++ b/crates/jiter/src/py_string_cache.rs
5+
@@ -200,7 +200,7 @@ pub fn pystring_fast_new<'py>(py: Python<'py>, s: &str, ascii_only: bool) -> Bou
6+
7+
/// Faster creation of PyString from an ASCII string, inspired by
8+
/// https://github.com/ijl/orjson/blob/3.10.0/src/str/create.rs#L41
9+
-#[cfg(not(PyPy))]
10+
+#[cfg(all(not(PyPy), not(GraalPy)))]
11+
unsafe fn pystring_ascii_new<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString> {
12+
let ptr = pyo3::ffi::PyUnicode_New(s.len() as isize, 127);
13+
// see https://github.com/pydantic/jiter/pull/72#discussion_r1545485907
14+
@@ -212,7 +212,7 @@ unsafe fn pystring_ascii_new<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyStri
15+
}
16+
17+
// ffi::PyUnicode_DATA seems to be broken for pypy, hence this, marked as unsafe to avoid warnings
18+
-#[cfg(PyPy)]
19+
+#[cfg(any(PyPy, GraalPy))]
20+
unsafe fn pystring_ascii_new<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString> {
21+
PyString::new_bound(py, s)
22+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[[rules]]
2+
patch = 'jiter.patch'
3+
license = 'MIT'

graalpython/lib-graalpython/patches/mlx/metadata.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
[[rules]]
2+
patch = 'mlx.patch'
3+
license = 'MIT'
4+
15
[[add-sources]]
26
version = '0.16.3'
37
url = 'https://github.com/ml-explore/mlx/archive/refs/tags/v0.16.3.tar.gz'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff --git a/setup.py b/setup.py
2+
index 7bd1be4..390c84c 100644
3+
--- a/setup.py
4+
+++ b/setup.py
5+
@@ -13,6 +13,7 @@ from setuptools.command.build_ext import build_ext
6+
7+
8+
def get_version(version):
9+
+ return version
10+
if "PYPI_RELEASE" not in os.environ:
11+
today = datetime.date.today()
12+
version = f"{version}.dev{today.year}{today.month:02d}{today.day:02d}"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
[[rules]]
2+
# Recent versions should use pyo3 with upstream support for graalpy
3+
install-priority = 0
24
version = '== 0.3.3'
35
patch = 'safetensors-0.3.3.patch'
46
license = 'Apache-2.0'

graalpython/lib-graalpython/patches/tokenizers/metadata.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
[[rules]]
2+
version = '>= 0.19'
3+
patch = 'tokenizers-0.19-plus.patch'
4+
license = 'Apache-2.0'
5+
16
[[rules]]
27
version = '== 0.13.3'
38
patch = 'tokenizers-0.13.3.patch'
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
diff --git a/bindings/python/src/lib.rs b/bindings/python/src/lib.rs
2+
index 3f1e713..6dd3c72 100644
3+
--- a/bindings/python/src/lib.rs
4+
+++ b/bindings/python/src/lib.rs
5+
@@ -50,14 +50,16 @@ extern "C" fn child_after_fork() {
6+
pub fn tokenizers(m: &Bound<'_, PyModule>) -> PyResult<()> {
7+
let _ = env_logger::try_init_from_env("TOKENIZERS_LOG");
8+
9+
+ // GraalPy change: Disable the atfork warning. This triggers a ton of false positives when
10+
+ // jline calls stty and we don't support fork anyway
11+
// Register the fork callback
12+
- #[cfg(target_family = "unix")]
13+
- unsafe {
14+
- if !REGISTERED_FORK_CALLBACK {
15+
- libc::pthread_atfork(None, None, Some(child_after_fork));
16+
- REGISTERED_FORK_CALLBACK = true;
17+
- }
18+
- }
19+
+ // #[cfg(target_family = "unix")]
20+
+ // unsafe {
21+
+ // if !REGISTERED_FORK_CALLBACK {
22+
+ // libc::pthread_atfork(None, None, Some(child_after_fork));
23+
+ // REGISTERED_FORK_CALLBACK = true;
24+
+ // }
25+
+ // }
26+
27+
m.add_class::<tokenizer::PyTokenizer>()?;
28+
m.add_class::<tokenizer::PyAddedToken>()?;

0 commit comments

Comments
 (0)