Skip to content

Commit 720b1ea

Browse files
committed
[GR-34608] Fixes for urllib3
PullRequest: graalpython/2129
2 parents 31c19ec + 7127d25 commit 720b1ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+667
-330
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, 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
@@ -48,6 +48,7 @@ extern "C" {
4848
#endif
4949

5050
PyAPI_FUNC(Py_hash_t) _Py_HashDouble(double);
51+
PyAPI_FUNC(Py_hash_t) _Py_HashPointer(void*);
5152
PyAPI_FUNC(Py_hash_t) _Py_HashBytes(const void*, Py_ssize_t);
5253

5354
extern long _PyHASH_INF;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ PyObject* PyDictProxy_New(PyObject *mapping) {
6262
return (PyObject*) UPCALL_CEXT_O(_jls_PyDictProxy_New, native_to_java(mapping));
6363
}
6464

65-
typedef PyObject* (*PyDescr_NewClassMethod_fun_t)(void* name,
65+
typedef PyObject* (*PyDescr_NewClassMethod_fun_t)(PyMethodDef* methodDef,
66+
void* name,
6667
const char* doc,
6768
int flags,
6869
int wrapper,
@@ -71,7 +72,8 @@ typedef PyObject* (*PyDescr_NewClassMethod_fun_t)(void* name,
7172
UPCALL_TYPED_ID(PyDescr_NewClassMethod, PyDescr_NewClassMethod_fun_t);
7273
PyObject* PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method) {
7374
int flags = method->ml_flags;
74-
return _jls_PyDescr_NewClassMethod(polyglot_from_string(method->ml_name, SRC_CS),
75+
return _jls_PyDescr_NewClassMethod(method,
76+
polyglot_from_string(method->ml_name, SRC_CS),
7577
method->ml_doc,
7678
flags,
7779
get_method_flags_wrapper(flags),

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, 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
@@ -242,6 +242,11 @@ int PyDict_Merge(PyObject *a, PyObject *b, int override) {
242242
return UPCALL_CEXT_I(_jls_PyDict_Merge, native_to_java(a), native_to_java(b), override);
243243
}
244244

245+
UPCALL_ID(PyDict_Keys);
246+
PyObject * PyDict_Keys(PyObject *dict) {
247+
return UPCALL_CEXT_O(_jls_PyDict_Keys, native_to_java(dict));
248+
}
249+
245250
UPCALL_ID(PyDict_Values);
246251
PyObject * PyDict_Values(PyObject *dict) {
247252
return UPCALL_CEXT_O(_jls_PyDict_Values, native_to_java(dict));

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, 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
@@ -169,9 +169,13 @@ PyObject* PyErr_Format(PyObject* exception, const char* fmt, ...) {
169169
return NULL;
170170
}
171171

172-
UPCALL_ID(PyErr_WriteUnraisable);
173172
void PyErr_WriteUnraisable(PyObject *obj) {
174-
UPCALL_CEXT_VOID(_jls_PyErr_WriteUnraisable, native_to_java(obj));
173+
_PyErr_WriteUnraisableMsg(NULL, obj);
174+
}
175+
176+
UPCALL_ID(_PyErr_WriteUnraisableMsg);
177+
void _PyErr_WriteUnraisableMsg(const char *err_msg_str, PyObject *obj) {
178+
UPCALL_CEXT_VOID(_jls__PyErr_WriteUnraisableMsg, err_msg_str != NULL ? polyglot_from_string(err_msg_str, SRC_CS) : NULL, native_to_java(obj));
175179
}
176180

177181
UPCALL_ID(PyErr_Display);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, 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
@@ -44,7 +44,8 @@ typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
4444

4545
PyTypeObject PyCFunction_Type = PY_TRUFFLE_TYPE_WITH_VECTORCALL("builtin_function_or_method", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | _Py_TPFLAGS_HAVE_VECTORCALL, sizeof(PyCFunctionObject), offsetof(PyCFunctionObject, vectorcall));
4646

47-
typedef PyObject* (*PyCFunction_NewEx_fun_t)(void* name,
47+
typedef PyObject* (*PyCFunction_NewEx_fun_t)(PyMethodDef* methodDef,
48+
void* name,
4849
void* methObj,
4950
int flags,
5051
int wrapper,
@@ -53,7 +54,8 @@ typedef PyObject* (*PyCFunction_NewEx_fun_t)(void* name,
5354
const char* doc);
5455
UPCALL_TYPED_ID(PyCFunction_NewEx, PyCFunction_NewEx_fun_t);
5556
PyObject* PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) {
56-
return _jls_PyCFunction_NewEx(polyglot_from_string(ml->ml_name, SRC_CS),
57+
return _jls_PyCFunction_NewEx(ml,
58+
polyglot_from_string(ml->ml_name, SRC_CS),
5759
function_pointer_to_java(ml->ml_meth),
5860
ml->ml_flags,
5961
get_method_flags_wrapper(ml->ml_flags),

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, 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
@@ -57,24 +57,21 @@ PyModuleDef_Init(struct PyModuleDef* def)
5757
return (PyObject*)def;
5858
}
5959

60-
// cls dict name cfunc flags sig doc
61-
typedef int (*AddFunction_fun_t)(PyObject *, PyObject *, void *, void *, int , int, char *);
62-
UPCALL_TYPED_ID(AddFunction, AddFunction_fun_t);
60+
// method_def module name cfunc flags sig doc
61+
typedef int (*AddFunctionToModule_fun_t)(PyMethodDef *, PyObject *, const char *, void *, int , int, char *);
62+
UPCALL_TYPED_ID(AddFunctionToModule, AddFunctionToModule_fun_t);
6363
int PyModule_AddFunctions(PyObject* mod, PyMethodDef* methods) {
6464
if (!methods) {
6565
return -1;
6666
}
67-
int idx = 0;
68-
PyMethodDef def = methods[idx];
69-
while (def.ml_name != NULL) {
70-
_jls_AddFunction(native_to_java(mod),
71-
NULL,
72-
polyglot_from_string(def.ml_name, SRC_CS),
73-
function_pointer_to_java(def.ml_meth),
74-
def.ml_flags,
75-
get_method_flags_wrapper(def.ml_flags),
76-
(def.ml_doc ? def.ml_doc : ""));
77-
def = methods[++idx];
67+
for (PyMethodDef* def = methods; def->ml_name != NULL; def++) {
68+
_jls_AddFunctionToModule(def,
69+
native_to_java(mod),
70+
polyglot_from_string(def->ml_name, SRC_CS),
71+
function_pointer_to_java(def->ml_meth),
72+
def->ml_flags,
73+
get_method_flags_wrapper(def->ml_flags),
74+
def->ml_doc);
7875
}
7976
return 0;
8077
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, 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
@@ -59,3 +59,16 @@ void initialize_hashes() {
5959
Py_hash_t _Py_HashBytes(const void *src, Py_ssize_t len) {
6060
return UPCALL_L(PY_BUILTIN, polyglot_from_string("hash", SRC_CS), polyglot_from_string(src, "ascii"));
6161
}
62+
63+
/* taken from CPython */
64+
Py_hash_t _Py_HashPointer(void *p) {
65+
Py_hash_t x;
66+
size_t y = (size_t)p;
67+
/* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid
68+
excessive hash collisions for dicts and sets */
69+
y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4));
70+
x = (Py_hash_t)y;
71+
if (x == -1)
72+
x = -2;
73+
return x;
74+
}

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

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, 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
@@ -392,18 +392,18 @@ int add_getset(PyTypeObject* cls, PyObject* type_dict, char* name, getter getter
392392
closure);
393393
}
394394

395-
// cls dict name cfunc flags sig doc
396-
typedef int (*AddFunction_fun_t)(PyTypeObject *, PyObject *, void *, void *, int , int, char *);
397-
UPCALL_TYPED_ID(AddFunction, AddFunction_fun_t);
398-
static void add_method_or_slot(PyTypeObject* cls, PyObject* type_dict, char* name, void* result_conversion, void* meth, int flags, int signature, char* doc) {
399-
void *resolved_meth = function_pointer_to_java(meth);
400-
_jls_AddFunction(cls,
395+
// method_def cls dict name cfunc flags sig doc
396+
typedef int (*AddFunctionToType_fun_t)(PyMethodDef *, PyTypeObject *, PyObject *, const char *, void *, int , int, char *);
397+
UPCALL_TYPED_ID(AddFunctionToType, AddFunctionToType_fun_t);
398+
static void add_method(PyTypeObject* cls, PyObject* type_dict, PyMethodDef* def) {
399+
_jls_AddFunctionToType(def,
400+
cls,
401401
native_to_java(type_dict),
402-
polyglot_from_string(name, SRC_CS),
403-
native_pointer_to_java(result_conversion != NULL ? pytruffle_decorate_function(resolved_meth, result_conversion) : resolved_meth),
404-
flags,
405-
(signature != 0 ? signature : get_method_flags_wrapper(flags)),
406-
doc);
402+
polyglot_from_string(def->ml_name, SRC_CS),
403+
function_pointer_to_java(def->ml_meth),
404+
def->ml_flags,
405+
get_method_flags_wrapper(def->ml_flags),
406+
def->ml_doc);
407407
}
408408

409409
typedef int (*add_slot_fun_t)(PyTypeObject *, PyObject *, void *, void *, int , int, char *);
@@ -440,11 +440,6 @@ int PyType_Ready(PyTypeObject* cls) {
440440
} while(0)
441441

442442
#define ADD_IF_MISSING(attr, def) if (!(attr)) { attr = def; }
443-
#define ADD_METHOD(m) ADD_METHOD_OR_SLOT(m.ml_name, NULL, m.ml_meth, m.ml_flags, NULL, m.ml_doc)
444-
#define ADD_METHOD_OR_SLOT(__name__, __res_conv__, __meth__, __flags__, __signature__, __doc__) \
445-
if (__meth__) { \
446-
add_method_or_slot(cls, dict, (__name__), (__res_conv__), (__meth__), (__flags__), (__signature__), (__doc__)); \
447-
}
448443
#define ADD_SLOT_CONV(__name__, __meth__, __flags__, __signature__) add_slot(cls, dict, (__name__), (__meth__), (__flags__), (__signature__), NULL)
449444

450445
Py_ssize_t n;
@@ -520,13 +515,9 @@ int PyType_Ready(PyTypeObject* cls) {
520515
cls->tp_dict = dict;
521516
}
522517

523-
PyMethodDef* methods = cls->tp_methods;
524-
if (methods) {
525-
int idx = 0;
526-
PyMethodDef def = methods[idx];
527-
while (def.ml_name != NULL) {
528-
ADD_METHOD(def);
529-
def = methods[++idx];
518+
if (cls->tp_methods) {
519+
for (PyMethodDef* def = cls->tp_methods; def->ml_name != NULL; def++) {
520+
add_method(cls, dict, def);
530521
}
531522
}
532523

@@ -760,9 +751,7 @@ int PyType_Ready(PyTypeObject* cls) {
760751
return 0;
761752

762753
#undef ADD_IF_MISSING
763-
#undef ADD_METHOD
764754
#undef ADD_SLOT
765-
#undef ADD_METHOD_OR_SLOT
766755
}
767756

768757
MUST_INLINE static int valid_identifier(PyObject *s) {

graalpython/com.oracle.graal.python.cext/zlib/zlib.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2022, 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
@@ -1108,13 +1108,6 @@ int zlib_Decompress_flush(zlib_stream *zst, ssize_t length) {
11081108
error_occurred(zst, INFLATE_END_ERROR);
11091109
return err;
11101110
}
1111-
} else if (err != Z_OK && err != Z_BUF_ERROR) {
1112-
/* We will only get Z_BUF_ERROR if the output buffer was full
1113-
but there wasn't more output when we tried again, so it is
1114-
not an error condition.
1115-
*/
1116-
error_occurred(zst, INFLATE_FLUSH_ERROR);
1117-
return err;
11181111
}
11191112
if (zst->output->size) {
11201113
zst->output_size = zst->zst.next_out - zst->output->buf;

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -48,11 +48,18 @@ def _reference_get_item(args):
4848
return d.get(args[1])
4949
except Exception:
5050
return None
51-
51+
52+
53+
def _reference_keys(args):
54+
d = args[0]
55+
return list(d.keys())
56+
57+
5258
def _reference_values(args):
5359
d = args[0]
5460
return list(d.values())
55-
61+
62+
5663
def _reference_pop(args):
5764
d = args[0]
5865
if(len(args) == 2):
@@ -505,6 +512,15 @@ def compile_module(self, name):
505512
arguments=["PyObject* a", "PyObject* b", "int override"],
506513
cmpfunc=unhandled_error_compare
507514
)
515+
516+
# PyDict_Keys
517+
test_PyDict_Keys = CPyExtFunction(
518+
_reference_keys,
519+
lambda: (({},), ({'a': "hello"},)),
520+
resultspec="O",
521+
argspec="O",
522+
cmpfunc=unhandled_error_compare
523+
)
508524

509525
# PyDict_Values
510526
test_PyDict_Values = CPyExtFunction(

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ def compile_module(self, name):
367367
resultspec="O",
368368
argspec='Osn',
369369
arguments=["PyObject* category", "char* msg", "Py_ssize_t level"],
370+
stderr_validator=lambda args, stderr: "UserWarning: custom warning" in stderr,
370371
cmpfunc=unhandled_error_compare
371372
)
372373

@@ -389,17 +390,36 @@ def compile_module(self, name):
389390
(None,),
390391
("hello",),
391392
),
392-
resultspec="O",
393393
argspec='O',
394394
arguments=["PyObject* obj"],
395395
code="""void wrap_PyErr_WriteUnraisable(PyObject* object) {
396-
PyErr_SetString(PyExc_RuntimeError, "unraisable exception");
396+
PyErr_SetString(PyExc_RuntimeError, "unraisable_exception");
397397
if (object == Py_None)
398398
object = NULL;
399399
PyErr_WriteUnraisable(object);
400400
}""",
401401
callfunction="wrap_PyErr_WriteUnraisable",
402-
stderr_validator=lambda args, stderr: 'unraisable exception' in stderr,
402+
stderr_validator=lambda args, stderr: "RuntimeError: unraisable_exception" in stderr,
403+
cmpfunc=unhandled_error_compare
404+
)
405+
406+
test_PyErr_WriteUnraisableMsg = CPyExtFunctionVoid(
407+
lambda args: None,
408+
lambda: (
409+
(None,),
410+
("hello",),
411+
),
412+
code="""void wrap_PyErr_WriteUnraisableMsg(PyObject* object) {
413+
PyErr_SetString(PyExc_RuntimeError, "unraisable_exception");
414+
if (object == Py_None)
415+
object = NULL;
416+
_PyErr_WriteUnraisableMsg("in my function", object);
417+
}
418+
""",
419+
argspec='O',
420+
arguments=["PyObject* obj"],
421+
callfunction="wrap_PyErr_WriteUnraisableMsg",
422+
stderr_validator=lambda args, stderr: "RuntimeError: unraisable_exception" in stderr and "Exception ignored in my function:" in stderr,
403423
cmpfunc=unhandled_error_compare
404424
)
405425

0 commit comments

Comments
 (0)