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

Skip to content

Commit 51d19cf

Browse files
author
Thomas Heller
committed
Merged revisions 83837,83841 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ........ r83837 | thomas.heller | 2010-08-08 19:58:53 +0200 (So, 08 Aug 2010) | 3 lines Fix issue5504: ctypes does now work with systems where mmap can't be PROT_WRITE and PROT_EXEC. ........ r83841 | thomas.heller | 2010-08-08 20:16:20 +0200 (So, 08 Aug 2010) | 2 lines Fix issue6869: refcount problem in the _ctypes extension. ........
1 parent 818afb5 commit 51d19cf

9 files changed

Lines changed: 43 additions & 20 deletions

File tree

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ What's New in Python 3.1.3?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #6869: Fix a refcount problem in the _ctypes extension.
16+
17+
- Issue #5504: ctypes should now work with systems where mmap can't
18+
be PROT_WRITE and PROT_EXEC.
19+
1520
- Issue #8814: function annotations (the ``__annotations__`` attribute)
1621
are now included in the set of attributes copied by default by
1722
functools.wraps and functools.update_wrapper. Patch by Terrence Cole.

Modules/_ctypes/_ctypes.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3367,7 +3367,7 @@ PyCFuncPtr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
33673367
self->callable = callable;
33683368

33693369
self->thunk = thunk;
3370-
*(void **)self->b_ptr = (void *)thunk->pcl;
3370+
*(void **)self->b_ptr = (void *)thunk->pcl_exec;
33713371

33723372
Py_INCREF((PyObject *)thunk); /* for KeepRef */
33733373
if (-1 == KeepRef((CDataObject *)self, 0, (PyObject *)thunk)) {
@@ -5326,36 +5326,42 @@ PyInit__ctypes(void)
53265326
Struct_Type.tp_base = &PyCData_Type;
53275327
if (PyType_Ready(&Struct_Type) < 0)
53285328
return NULL;
5329+
Py_INCREF(&Struct_Type);
53295330
PyModule_AddObject(m, "Structure", (PyObject *)&Struct_Type);
53305331

53315332
Py_TYPE(&Union_Type) = &UnionType_Type;
53325333
Union_Type.tp_base = &PyCData_Type;
53335334
if (PyType_Ready(&Union_Type) < 0)
53345335
return NULL;
5336+
Py_INCREF(&Union_Type);
53355337
PyModule_AddObject(m, "Union", (PyObject *)&Union_Type);
53365338

53375339
Py_TYPE(&PyCPointer_Type) = &PyCPointerType_Type;
53385340
PyCPointer_Type.tp_base = &PyCData_Type;
53395341
if (PyType_Ready(&PyCPointer_Type) < 0)
53405342
return NULL;
5343+
Py_INCREF(&PyCPointer_Type);
53415344
PyModule_AddObject(m, "_Pointer", (PyObject *)&PyCPointer_Type);
53425345

53435346
Py_TYPE(&PyCArray_Type) = &PyCArrayType_Type;
53445347
PyCArray_Type.tp_base = &PyCData_Type;
53455348
if (PyType_Ready(&PyCArray_Type) < 0)
53465349
return NULL;
5350+
Py_INCREF(&PyCArray_Type);
53475351
PyModule_AddObject(m, "Array", (PyObject *)&PyCArray_Type);
53485352

53495353
Py_TYPE(&Simple_Type) = &PyCSimpleType_Type;
53505354
Simple_Type.tp_base = &PyCData_Type;
53515355
if (PyType_Ready(&Simple_Type) < 0)
53525356
return NULL;
5357+
Py_INCREF(&Simple_Type);
53535358
PyModule_AddObject(m, "_SimpleCData", (PyObject *)&Simple_Type);
53545359

53555360
Py_TYPE(&PyCFuncPtr_Type) = &PyCFuncPtrType_Type;
53565361
PyCFuncPtr_Type.tp_base = &PyCData_Type;
53575362
if (PyType_Ready(&PyCFuncPtr_Type) < 0)
53585363
return NULL;
5364+
Py_INCREF(&PyCFuncPtr_Type);
53595365
PyModule_AddObject(m, "CFuncPtr", (PyObject *)&PyCFuncPtr_Type);
53605366

53615367
/*************************************************

Modules/_ctypes/callbacks.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ CThunkObject_dealloc(PyObject *_self)
1616
Py_XDECREF(self->converters);
1717
Py_XDECREF(self->callable);
1818
Py_XDECREF(self->restype);
19-
if (self->pcl)
20-
_ctypes_free_closure(self->pcl);
19+
if (self->pcl_write)
20+
ffi_closure_free(self->pcl_write);
2121
PyObject_GC_Del(self);
2222
}
2323

@@ -370,7 +370,8 @@ static CThunkObject* CThunkObject_new(Py_ssize_t nArgs)
370370
return NULL;
371371
}
372372

373-
p->pcl = NULL;
373+
p->pcl_exec = NULL;
374+
p->pcl_write = NULL;
374375
memset(&p->cif, 0, sizeof(p->cif));
375376
p->converters = NULL;
376377
p->callable = NULL;
@@ -400,8 +401,9 @@ CThunkObject *_ctypes_alloc_callback(PyObject *callable,
400401

401402
assert(CThunk_CheckExact((PyObject *)p));
402403

403-
p->pcl = _ctypes_alloc_closure();
404-
if (p->pcl == NULL) {
404+
p->pcl_write = ffi_closure_alloc(sizeof(ffi_closure),
405+
&p->pcl_exec);
406+
if (p->pcl_write == NULL) {
405407
PyErr_NoMemory();
406408
goto error;
407409
}
@@ -446,7 +448,9 @@ CThunkObject *_ctypes_alloc_callback(PyObject *callable,
446448
"ffi_prep_cif failed with %d", result);
447449
goto error;
448450
}
449-
result = ffi_prep_closure(p->pcl, &p->cif, closure_fcn, p);
451+
result = ffi_prep_closure_loc(p->pcl_write, &p->cif, closure_fcn,
452+
p,
453+
p->pcl_exec);
450454
if (result != FFI_OK) {
451455
PyErr_Format(PyExc_RuntimeError,
452456
"ffi_prep_closure failed with %d", result);

Modules/_ctypes/ctypes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ struct tagCDataObject {
5858

5959
typedef struct {
6060
PyObject_VAR_HEAD
61-
ffi_closure *pcl; /* the C callable */
61+
ffi_closure *pcl_write; /* the C callable, writeable */
62+
void *pcl_exec; /* the C callable, executable */
6263
ffi_cif cif;
6364
int flags;
6465
PyObject *converters;

Modules/_ctypes/libffi/fficonfig.py.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
ffi_sources = """
22
src/prep_cif.c
3+
src/closures.c
4+
src/dlmalloc.c
35
""".split()
46

57
ffi_platforms = {

Modules/_ctypes/libffi_msvc/ffi.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,11 @@ ffi_prep_incoming_args_SYSV(char *stack, void **rvalue,
371371
extern void ffi_closure_OUTER();
372372

373373
ffi_status
374-
ffi_prep_closure (ffi_closure* closure,
375-
ffi_cif* cif,
376-
void (*fun)(ffi_cif*,void*,void**,void*),
377-
void *user_data)
374+
ffi_prep_closure_loc (ffi_closure* closure,
375+
ffi_cif* cif,
376+
void (*fun)(ffi_cif*,void*,void**,void*),
377+
void *user_data,
378+
void *codeloc)
378379
{
379380
short bytes;
380381
char *tramp;
@@ -452,6 +453,5 @@ ffi_prep_closure (ffi_closure* closure,
452453
closure->cif = cif;
453454
closure->user_data = user_data;
454455
closure->fun = fun;
455-
456456
return FFI_OK;
457457
}

Modules/_ctypes/libffi_msvc/ffi.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,15 @@ typedef struct {
221221
void *user_data;
222222
} ffi_closure;
223223

224+
void ffi_closure_free(void *);
225+
void *ffi_closure_alloc (size_t size, void **code);
226+
224227
ffi_status
225-
ffi_prep_closure (ffi_closure*,
228+
ffi_prep_closure_loc (ffi_closure*,
226229
ffi_cif *,
227230
void (*fun)(ffi_cif*,void*,void**,void*),
228-
void *user_data);
231+
void *user_data,
232+
void *codeloc);
229233

230234
typedef struct {
231235
char tramp[FFI_TRAMPOLINE_SIZE];

Modules/_ctypes/malloc_closure.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ static void more_core(void)
8989
/******************************************************************/
9090

9191
/* put the item back into the free list */
92-
void _ctypes_free_closure(void *p)
92+
void ffi_closure_free(void *p)
9393
{
9494
ITEM *item = (ITEM *)p;
9595
item->next = free_list;
9696
free_list = item;
9797
}
9898

9999
/* return one item from the free list, allocating more if needed */
100-
void *_ctypes_alloc_closure(void)
100+
void *ffi_closure_alloc(size_t ignored, void** codeloc)
101101
{
102102
ITEM *item;
103103
if (!free_list)
@@ -106,5 +106,7 @@ void *_ctypes_alloc_closure(void)
106106
return NULL;
107107
item = free_list;
108108
free_list = item->next;
109-
return item;
109+
*codeloc = (void *)item;
110+
return (void *)item;
110111
}
112+

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,8 +1636,7 @@ def detect_ctypes(self, inc_dirs, lib_dirs):
16361636
'_ctypes/callbacks.c',
16371637
'_ctypes/callproc.c',
16381638
'_ctypes/stgdict.c',
1639-
'_ctypes/cfield.c',
1640-
'_ctypes/malloc_closure.c']
1639+
'_ctypes/cfield.c']
16411640
depends = ['_ctypes/ctypes.h']
16421641

16431642
if sys.platform == 'darwin':

0 commit comments

Comments
 (0)