Skip to content

Commit 03ff58c

Browse files
committed
[GR-46811] Fixes for psycopg2
PullRequest: graalpython/2855
2 parents 57e92ef + a5751a6 commit 03ff58c

File tree

22 files changed

+1775
-1181
lines changed

22 files changed

+1775
-1181
lines changed

graalpython/com.oracle.graal.python.cext/include/datetime.h

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018, 2022, Oracle and/or its affiliates.
1+
/* Copyright (c) 2018, 2023, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2020 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -119,44 +119,37 @@ typedef struct
119119
} PyDateTime_DateTime; /* hastzinfo true */
120120

121121

122+
PyObject* PyTruffle_PyDateTime_GET_TZINFO(PyObject*);
123+
122124
/* Apply for date and datetime instances. */
123125

124126
// o is a pointer to a time or a datetime object.
125-
#define _PyDateTime_HAS_TZINFO(o) (((_PyDateTime_BaseTZInfo *)(o))->hastzinfo)
126-
127-
#define PyDateTime_GET_YEAR(o) ((((PyDateTime_Date*)o)->data[0] << 8) | \
128-
((PyDateTime_Date*)o)->data[1])
129-
#define PyDateTime_GET_MONTH(o) (((PyDateTime_Date*)o)->data[2])
130-
#define PyDateTime_GET_DAY(o) (((PyDateTime_Date*)o)->data[3])
131-
132-
#define PyDateTime_DATE_GET_HOUR(o) (((PyDateTime_DateTime*)o)->data[4])
133-
#define PyDateTime_DATE_GET_MINUTE(o) (((PyDateTime_DateTime*)o)->data[5])
134-
#define PyDateTime_DATE_GET_SECOND(o) (((PyDateTime_DateTime*)o)->data[6])
135-
#define PyDateTime_DATE_GET_MICROSECOND(o) \
136-
((((PyDateTime_DateTime*)o)->data[7] << 16) | \
137-
(((PyDateTime_DateTime*)o)->data[8] << 8) | \
138-
((PyDateTime_DateTime*)o)->data[9])
139-
#define PyDateTime_DATE_GET_FOLD(o) (((PyDateTime_DateTime*)o)->fold)
140-
#define PyDateTime_DATE_GET_TZINFO(o) (_PyDateTime_HAS_TZINFO(o) ? \
141-
((PyDateTime_DateTime *)(o))->tzinfo : Py_None)
127+
#define _PyDateTime_HAS_TZINFO(o) (PyTruffle_PyDateTime_GET_TZINFO((PyObject*)o) != Py_None)
128+
129+
#define PyDateTime_GET_YEAR(o) ((int)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "year")))
130+
#define PyDateTime_GET_MONTH(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "month")))
131+
#define PyDateTime_GET_DAY(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "day")))
132+
133+
#define PyDateTime_DATE_GET_HOUR(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "hour")))
134+
#define PyDateTime_DATE_GET_MINUTE(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "minute")))
135+
#define PyDateTime_DATE_GET_SECOND(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "second")))
136+
#define PyDateTime_DATE_GET_MICROSECOND(o) ((int)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "microsecond")))
137+
#define PyDateTime_DATE_GET_FOLD(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "fold")))
138+
#define PyDateTime_DATE_GET_TZINFO(o) (PyTruffle_PyDateTime_GET_TZINFO((PyObject*)o))
142139

143140
/* Apply for time instances. */
144-
#define PyDateTime_TIME_GET_HOUR(o) (((PyDateTime_Time*)o)->data[0])
145-
#define PyDateTime_TIME_GET_MINUTE(o) (((PyDateTime_Time*)o)->data[1])
146-
#define PyDateTime_TIME_GET_SECOND(o) (((PyDateTime_Time*)o)->data[2])
147-
#define PyDateTime_TIME_GET_MICROSECOND(o) \
148-
((((PyDateTime_Time*)o)->data[3] << 16) | \
149-
(((PyDateTime_Time*)o)->data[4] << 8) | \
150-
((PyDateTime_Time*)o)->data[5])
151-
#define PyDateTime_TIME_GET_FOLD(o) (((PyDateTime_Time*)o)->fold)
152-
#define PyDateTime_TIME_GET_TZINFO(o) (_PyDateTime_HAS_TZINFO(o) ? \
153-
((PyDateTime_Time *)(o))->tzinfo : Py_None)
141+
#define PyDateTime_TIME_GET_HOUR(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "hour")))
142+
#define PyDateTime_TIME_GET_MINUTE(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "minute")))
143+
#define PyDateTime_TIME_GET_SECOND(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "second")))
144+
#define PyDateTime_TIME_GET_MICROSECOND(o) ((int)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "microsecond")))
145+
#define PyDateTime_TIME_GET_FOLD(o) ((unsigned char)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "fold")))
146+
#define PyDateTime_TIME_GET_TZINFO(o) (PyTruffle_PyDateTime_GET_TZINFO((PyObject*)o))
154147

155148
/* Apply for time delta instances */
156-
#define PyDateTime_DELTA_GET_DAYS(o) (((PyDateTime_Delta*)o)->days)
157-
#define PyDateTime_DELTA_GET_SECONDS(o) (((PyDateTime_Delta*)o)->seconds)
158-
#define PyDateTime_DELTA_GET_MICROSECONDS(o) \
159-
(((PyDateTime_Delta*)o)->microseconds)
149+
#define PyDateTime_DELTA_GET_DAYS(o) ((int)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "days")))
150+
#define PyDateTime_DELTA_GET_SECONDS(o) ((int)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "seconds")))
151+
#define PyDateTime_DELTA_GET_MICROSECONDS(o) ((int)PyLong_AsLong(PyObject_GetAttrString((PyObject*)o, "microseconds")))
152+
160153

161154

162155
/* Define structure for C API. */

graalpython/com.oracle.graal.python.cext/src/abstract.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,31 @@ int PyTruffle_PySequence_Check(PyObject *s) {
228228
return seq && seq->sq_item != NULL;
229229
}
230230

231+
// downcall for native python objects
232+
// partially taken from CPython "Objects/abstract.c/PySequence_GetItem"
233+
PyObject* PyTruffle_PySequence_GetItem(PyObject *s, Py_ssize_t i)
234+
{
235+
PySequenceMethods *m = Py_TYPE(s)->tp_as_sequence;
236+
if (m && m->sq_item) {
237+
if (i < 0) {
238+
if (m->sq_length) {
239+
Py_ssize_t l = (*m->sq_length)(s);
240+
if (l < 0) {
241+
return NULL;
242+
}
243+
i += l;
244+
}
245+
}
246+
PyObject *res = m->sq_item(s, i);
247+
return res;
248+
}
249+
250+
if (Py_TYPE(s)->tp_as_mapping && Py_TYPE(s)->tp_as_mapping->mp_subscript) {
251+
return PyErr_Format(PyExc_TypeError, "%.200s is not a sequence", Py_TYPE(s)->tp_name);
252+
}
253+
return PyErr_Format(PyExc_TypeError, "'%.200s' object does not support indexing", Py_TYPE(s)->tp_name);
254+
}
255+
231256
// downcall for native python objects
232257
// taken from CPython "Objects/abstract.c/Py_Sequence_Size"
233258
Py_ssize_t PyTruffle_PySequence_Size(PyObject *s) {

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,10 @@ PyAPI_FUNC(int) PyFile_WriteObject(PyObject* a, PyObject* b, int c) {
17201720
PyAPI_FUNC(PyObject*) PyFloat_FromDouble(double a) {
17211721
return GraalPyFloat_FromDouble(a);
17221722
}
1723+
#undef PyFloat_FromString
1724+
PyAPI_FUNC(PyObject*) PyFloat_FromString(PyObject* a) {
1725+
return GraalPyFloat_FromString(a);
1726+
}
17231727
#undef PyFrame_GetBack
17241728
PyAPI_FUNC(PyFrameObject*) PyFrame_GetBack(PyFrameObject* a) {
17251729
return GraalPyFrame_GetBack(a);
@@ -2252,6 +2256,10 @@ PyAPI_FUNC(int) PyTruffle_Debug(void* a) {
22522256
PyAPI_FUNC(void) PyTruffle_DebugTrace() {
22532257
GraalPyTruffle_DebugTrace();
22542258
}
2259+
#undef PyTruffle_PyDateTime_GET_TZINFO
2260+
PyAPI_FUNC(PyObject*) PyTruffle_PyDateTime_GET_TZINFO(PyObject* a) {
2261+
return GraalPyTruffle_PyDateTime_GET_TZINFO(a);
2262+
}
22552263
#undef PyTruffle_ToNative
22562264
PyAPI_FUNC(int) PyTruffle_ToNative(void* a) {
22572265
return GraalPyTruffle_ToNative(a);

graalpython/com.oracle.graal.python.cext/src/capi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ typedef struct {
162162
BUILTIN(PyException_SetTraceback, int, PyObject*, PyObject*) \
163163
BUILTIN(PyFile_WriteObject, int, PyObject*, PyObject*, int) \
164164
BUILTIN(PyFloat_FromDouble, PyObject*, double) \
165+
BUILTIN(PyFloat_FromString, PyObject*, PyObject*) \
165166
BUILTIN(PyFrame_GetBack, PyFrameObject*, PyFrameObject*) \
166167
BUILTIN(PyFrame_GetBuiltins, PyObject*, PyFrameObject*) \
167168
BUILTIN(PyFrame_GetCode, PyCodeObject*, PyFrameObject*) \
@@ -367,6 +368,7 @@ typedef struct {
367368
BUILTIN(PyTruffle_None, PyObject*) \
368369
BUILTIN(PyTruffle_NotImplemented, PyObject*) \
369370
BUILTIN(PyTruffle_Object_Free, void, void*) \
371+
BUILTIN(PyTruffle_PyDateTime_GET_TZINFO, PyObject*, PyObject*) \
370372
BUILTIN(PyTruffle_Register_NULL, void, void*) \
371373
BUILTIN(PyTruffle_Set_Native_Slots, int, PyTypeObject*, void*, void*) \
372374
BUILTIN(PyTruffle_Set_SulongType, void*, PyTypeObject*, void*) \

graalpython/com.oracle.graal.python.cext/src/datetime.c

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -121,6 +121,64 @@ static int PyLong_AsInt(PyObject *arg) {
121121
return (int) ival;
122122
}
123123

124+
#undef _PyDateTime_HAS_TZINFO
125+
#undef PyDateTime_GET_YEAR
126+
#undef PyDateTime_GET_MONTH
127+
#undef PyDateTime_GET_DAY
128+
#undef PyDateTime_DATE_GET_HOUR
129+
#undef PyDateTime_DATE_GET_MINUTE
130+
#undef PyDateTime_DATE_GET_SECOND
131+
#undef PyDateTime_DATE_GET_MICROSECOND
132+
#undef PyDateTime_DATE_GET_FOLD
133+
#undef PyDateTime_DATE_GET_TZINFO
134+
#undef PyDateTime_TIME_GET_HOUR
135+
#undef PyDateTime_TIME_GET_MINUTE
136+
#undef PyDateTime_TIME_GET_SECOND
137+
#undef PyDateTime_TIME_GET_MICROSECOND
138+
#undef PyDateTime_TIME_GET_FOLD
139+
#undef PyDateTime_TIME_GET_TZINFO
140+
#undef PyDateTime_DELTA_GET_DAYS
141+
#undef PyDateTime_DELTA_GET_SECONDS
142+
#undef PyDateTime_DELTA_GET_MICROSECONDS
143+
144+
// Our normal datetime.h macros handle managed objects, redefine to handle only native
145+
146+
#define _PyDateTime_HAS_TZINFO(o) (((_PyDateTime_BaseTZInfo *)(o))->hastzinfo)
147+
148+
#define PyDateTime_GET_YEAR(o) ((((PyDateTime_Date*)o)->data[0] << 8) | \
149+
((PyDateTime_Date*)o)->data[1])
150+
#define PyDateTime_GET_MONTH(o) (((PyDateTime_Date*)o)->data[2])
151+
#define PyDateTime_GET_DAY(o) (((PyDateTime_Date*)o)->data[3])
152+
153+
#define PyDateTime_DATE_GET_HOUR(o) (((PyDateTime_DateTime*)o)->data[4])
154+
#define PyDateTime_DATE_GET_MINUTE(o) (((PyDateTime_DateTime*)o)->data[5])
155+
#define PyDateTime_DATE_GET_SECOND(o) (((PyDateTime_DateTime*)o)->data[6])
156+
#define PyDateTime_DATE_GET_MICROSECOND(o) \
157+
((((PyDateTime_DateTime*)o)->data[7] << 16) | \
158+
(((PyDateTime_DateTime*)o)->data[8] << 8) | \
159+
((PyDateTime_DateTime*)o)->data[9])
160+
#define PyDateTime_DATE_GET_FOLD(o) (((PyDateTime_DateTime*)o)->fold)
161+
#define PyDateTime_DATE_GET_TZINFO(o) (_PyDateTime_HAS_TZINFO(o) ? \
162+
((PyDateTime_DateTime *)(o))->tzinfo : Py_None)
163+
164+
/* Apply for time instances. */
165+
#define PyDateTime_TIME_GET_HOUR(o) (((PyDateTime_Time*)o)->data[0])
166+
#define PyDateTime_TIME_GET_MINUTE(o) (((PyDateTime_Time*)o)->data[1])
167+
#define PyDateTime_TIME_GET_SECOND(o) (((PyDateTime_Time*)o)->data[2])
168+
#define PyDateTime_TIME_GET_MICROSECOND(o) \
169+
((((PyDateTime_Time*)o)->data[3] << 16) | \
170+
(((PyDateTime_Time*)o)->data[4] << 8) | \
171+
((PyDateTime_Time*)o)->data[5])
172+
#define PyDateTime_TIME_GET_FOLD(o) (((PyDateTime_Time*)o)->fold)
173+
#define PyDateTime_TIME_GET_TZINFO(o) (_PyDateTime_HAS_TZINFO(o) ? \
174+
((PyDateTime_Time *)(o))->tzinfo : Py_None)
175+
176+
/* Apply for time delta instances */
177+
#define PyDateTime_DELTA_GET_DAYS(o) (((PyDateTime_Delta*)o)->days)
178+
#define PyDateTime_DELTA_GET_SECONDS(o) (((PyDateTime_Delta*)o)->seconds)
179+
#define PyDateTime_DELTA_GET_MICROSECONDS(o) \
180+
(((PyDateTime_Delta*)o)->microseconds)
181+
124182

125183
/* The following code is taken from CPython '_datetimemodule.c' */
126184

graalpython/com.oracle.graal.python.jni/src/capi_forwards.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ void unimplemented(const char* name) {
790790
#undef PyTruffle_None
791791
#undef PyTruffle_NotImplemented
792792
#undef PyTruffle_Object_Free
793+
#undef PyTruffle_PyDateTime_GET_TZINFO
793794
#undef PyTruffle_Register_NULL
794795
#undef PyTruffle_SeqIter_New
795796
#undef PyTruffle_Set_Native_Slots
@@ -2454,7 +2455,8 @@ PyAPI_FUNC(PyObject*) PyFloat_FromDouble(double a) {
24542455
return result;
24552456
}
24562457
PyAPI_FUNC(PyObject*) PyFloat_FromString(PyObject* a) {
2457-
unimplemented("PyFloat_FromString"); exit(-1);
2458+
PyObject* result = (PyObject*) GraalPyFloat_FromString(a);
2459+
return result;
24582460
}
24592461
PyAPI_FUNC(PyObject*) PyFloat_GetInfo() {
24602462
unimplemented("PyFloat_GetInfo"); exit(-1);
@@ -3969,6 +3971,10 @@ PyAPI_FUNC(int) PyTruffle_Debug(void* a) {
39693971
PyAPI_FUNC(void) PyTruffle_DebugTrace() {
39703972
GraalPyTruffle_DebugTrace();
39713973
}
3974+
PyAPI_FUNC(PyObject*) PyTruffle_PyDateTime_GET_TZINFO(PyObject* a) {
3975+
PyObject* result = (PyObject*) GraalPyTruffle_PyDateTime_GET_TZINFO(a);
3976+
return result;
3977+
}
39723978
PyAPI_FUNC(PyObject*) PyTruffle_SeqIter_New(PyObject* a) {
39733979
unimplemented("PyTruffle_SeqIter_New"); exit(-1);
39743980
}

0 commit comments

Comments
 (0)