Skip to content

Commit d022f72

Browse files
hoodmanewjakob
authored andcommitted
Fix the signature of tp_new (#687)
According to the docs, the signature of `tp_new` should be: `PyObject* tp_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)` https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_new We don't care about the arguments, but according to the C standard 6.3.2.3.8 this is undefined behavior: > A pointer to a function of one type may be converted to a pointer to a > function of another type and back again; the result shall compare equal to > the original pointer. If a converted pointer is used to call a function whose > type is not compatible with the pointed-to type, the behavior is undefined. https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf#page=60 In normal platforms the behavior is to work as expected, but in WebAssembly, this crashes. Fixes pyodide/pyodide#5015.
1 parent 8e5fd14 commit d022f72

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/nb_internals.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ extern char *type_name(const std::type_info *t);
285285

286286
// Forward declarations
287287
extern PyObject *inst_new_ext(PyTypeObject *tp, void *value);
288-
extern PyObject *inst_new_int(PyTypeObject *tp);
288+
extern PyObject *inst_new_int(PyTypeObject *tp, PyObject *args, PyObject *kwds);
289289
extern PyTypeObject *nb_static_property_tp() noexcept;
290290
extern type_data *nb_type_c2p(nb_internals *internals,
291291
const std::type_info *type);

src/nb_type.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ static int inst_init(PyObject *self, PyObject *, PyObject *) {
6565
}
6666

6767
/// Allocate memory for a nb_type instance with internal storage
68-
PyObject *inst_new_int(PyTypeObject *tp) {
68+
PyObject *inst_new_int(PyTypeObject *tp, PyObject * /* args */,
69+
PyObject * /*kwd */) {
6970
bool gc = PyType_HasFeature(tp, Py_TPFLAGS_HAVE_GC);
7071

7172
nb_inst *self;
@@ -100,7 +101,6 @@ PyObject *inst_new_int(PyTypeObject *tp) {
100101
}
101102

102103
return (PyObject *) self;
103-
104104
}
105105

106106
/// Allocate memory for a nb_type instance with external storage
@@ -1389,7 +1389,7 @@ static PyObject *nb_type_put_common(void *value, type_data *t, rv_policy rvp,
13891389

13901390
nb_inst *inst;
13911391
if (create_new)
1392-
inst = (nb_inst *) inst_new_int(t->type_py);
1392+
inst = (nb_inst *) inst_new_int(t->type_py, NULL, NULL);
13931393
else
13941394
inst = (nb_inst *) inst_new_ext(t->type_py, value);
13951395

@@ -1783,7 +1783,7 @@ void *nb_type_supplement(PyObject *t) noexcept {
17831783
}
17841784

17851785
PyObject *nb_inst_alloc(PyTypeObject *t) {
1786-
PyObject *result = inst_new_int(t);
1786+
PyObject *result = inst_new_int(t, NULL, NULL);
17871787
if (!result)
17881788
raise_python_error();
17891789
return result;
@@ -1824,7 +1824,7 @@ void nb_inst_zero(PyObject *o) noexcept {
18241824
}
18251825

18261826
PyObject *nb_inst_alloc_zero(PyTypeObject *t) {
1827-
PyObject *result = inst_new_int(t);
1827+
PyObject *result = inst_new_int(t, NULL, NULL);
18281828
if (!result)
18291829
raise_python_error();
18301830
nb_inst *nbi = (nb_inst *) result;

0 commit comments

Comments
 (0)