Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c0ac106

Browse files
committed
Fixed bug #1505 Changes to PyMethod_New breaks ctypes on Windows
I converted the ComError exception into a full type.
1 parent 218a5ed commit c0ac106

1 file changed

Lines changed: 63 additions & 50 deletions

File tree

Modules/_ctypes/_ctypes.c

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4559,82 +4559,95 @@ static char *module_docs =
45594559

45604560
static char comerror_doc[] = "Raised when a COM method call failed.";
45614561

4562-
static PyObject *
4563-
comerror_init(PyObject *self, PyObject *args)
4562+
int
4563+
comerror_init(PyObject *self, PyObject *args, PyObject *kwds)
45644564
{
45654565
PyObject *hresult, *text, *details;
4566+
PyBaseExceptionObject *bself;
45664567
PyObject *a;
45674568
int status;
45684569

4569-
if (!PyArg_ParseTuple(args, "OOOO:COMError", &self, &hresult, &text, &details))
4570-
return NULL;
4570+
if (!_PyArg_NoKeywords(Py_Type(self)->tp_name, kwds))
4571+
return -1;
4572+
4573+
if (!PyArg_ParseTuple(args, "OOO:COMError", &hresult, &text, &details))
4574+
return -1;
45714575

45724576
a = PySequence_GetSlice(args, 1, PySequence_Size(args));
45734577
if (!a)
4574-
return NULL;
4578+
return -1;
45754579
status = PyObject_SetAttrString(self, "args", a);
45764580
Py_DECREF(a);
45774581
if (status < 0)
4578-
return NULL;
4582+
return -1;
45794583

45804584
if (PyObject_SetAttrString(self, "hresult", hresult) < 0)
4581-
return NULL;
4585+
return -1;
45824586

45834587
if (PyObject_SetAttrString(self, "text", text) < 0)
4584-
return NULL;
4588+
return -1;
45854589

45864590
if (PyObject_SetAttrString(self, "details", details) < 0)
4587-
return NULL;
4591+
return -1;
45884592

4589-
Py_INCREF(Py_None);
4590-
return Py_None;
4593+
bself = (PyBaseExceptionObject *)self;
4594+
Py_DECREF(bself->args);
4595+
bself->args = args;
4596+
Py_INCREF(bself->args);
4597+
4598+
return 0;
45914599
}
45924600

4593-
static PyMethodDef comerror_methods[] = {
4594-
{ "__init__", comerror_init, METH_VARARGS },
4595-
{ NULL, NULL },
4601+
static PyTypeObject PyComError_Type = {
4602+
PyVarObject_HEAD_INIT(NULL, 0)
4603+
"_ctypes.COMError", /* tp_name */
4604+
sizeof(PyBaseExceptionObject), /* tp_basicsize */
4605+
0, /* tp_itemsize */
4606+
0, /* tp_dealloc */
4607+
0, /* tp_print */
4608+
0, /* tp_getattr */
4609+
0, /* tp_setattr */
4610+
0, /* tp_compare */
4611+
0, /* tp_repr */
4612+
0, /* tp_as_number */
4613+
0, /* tp_as_sequence */
4614+
0, /* tp_as_mapping */
4615+
0, /* tp_hash */
4616+
0, /* tp_call */
4617+
0, /* tp_str */
4618+
0, /* tp_getattro */
4619+
0, /* tp_setattro */
4620+
0, /* tp_as_buffer */
4621+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
4622+
| Py_TPFLAGS_HAVE_GC, /* tp_flags */
4623+
PyDoc_STR(comerror_doc), /* tp_doc */
4624+
0, /* tp_traverse */
4625+
0, /* tp_clear */
4626+
0, /* tp_richcompare */
4627+
0, /* tp_weaklistoffset */
4628+
0, /* tp_iter */
4629+
0, /* tp_iternext */
4630+
0, /* tp_methods */
4631+
0, /* tp_members */
4632+
0, /* tp_getset */
4633+
0, /* tp_base */
4634+
0, /* tp_dict */
4635+
0, /* tp_descr_get */
4636+
0, /* tp_descr_set */
4637+
0, /* tp_dictoffset */
4638+
(initproc)comerror_init, /* tp_init */
4639+
0, /* tp_alloc */
4640+
0, /* tp_new */
45964641
};
45974642

4643+
45984644
static int
45994645
create_comerror(void)
46004646
{
4601-
PyObject *dict = PyDict_New();
4602-
PyMethodDef *methods = comerror_methods;
4603-
PyObject *s;
4604-
int status;
4605-
4606-
while (methods->ml_name) {
4607-
/* get a wrapper for the built-in function */
4608-
PyObject *func = PyCFunction_New(methods, NULL);
4609-
PyObject *meth;
4610-
if (func == NULL)
4611-
return -1;
4612-
/*meth = PyMethod_New(func, NULL, ComError);
4613-
Py_DECREF(func);
4614-
if (meth == NULL)
4615-
return -1;*/
4616-
PyDict_SetItemString(dict, methods->ml_name, func);
4617-
/*Py_DECREF(meth);*/
4618-
Py_DECREF(func);
4619-
++methods;
4620-
}
4621-
4622-
s = PyUnicode_FromString(comerror_doc);
4623-
if (s == NULL)
4647+
PyComError_Type.tp_base = (PyTypeObject*)PyExc_Exception;
4648+
if (PyType_Ready(&PyComError_Type) < 0)
46244649
return -1;
4625-
status = PyDict_SetItemString(dict, "__doc__", s);
4626-
Py_DECREF(s);
4627-
if (status == -1) {
4628-
Py_DECREF(dict);
4629-
return -1;
4630-
}
4631-
4632-
ComError = PyErr_NewException("_ctypes.COMError",
4633-
NULL,
4634-
dict);
4635-
if (ComError == NULL)
4636-
return -1;
4637-
4650+
ComError = (PyObject*)&PyComError_Type;
46384651
return 0;
46394652
}
46404653

0 commit comments

Comments
 (0)