Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
046e6a43
Commit
046e6a43
authored
Apr 14, 2008
by
Thomas Heller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #2616: Implement ctypes.pointer() and ctypes.POINTER() in C for
better performance.
parent
da950eb0
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
83 additions
and
25 deletions
+83
-25
Lib/ctypes/__init__.py
Lib/ctypes/__init__.py
+1
-25
Misc/NEWS
Misc/NEWS
+3
-0
Modules/_ctypes/_ctypes.c
Modules/_ctypes/_ctypes.c
+10
-0
Modules/_ctypes/callproc.c
Modules/_ctypes/callproc.c
+68
-0
Modules/_ctypes/ctypes.h
Modules/_ctypes/ctypes.h
+1
-0
No files found.
Lib/ctypes/__init__.py
View file @
046e6a43
...
@@ -242,27 +242,7 @@ _check_size(c_void_p)
...
@@ -242,27 +242,7 @@ _check_size(c_void_p)
class
c_bool
(
_SimpleCData
):
class
c_bool
(
_SimpleCData
):
_type_
=
"?"
_type_
=
"?"
# This cache maps types to pointers to them.
from
_ctypes
import
POINTER
,
pointer
,
_pointer_type_cache
_pointer_type_cache
=
{}
def
POINTER
(
cls
):
try
:
return
_pointer_type_cache
[
cls
]
except
KeyError
:
pass
if
type
(
cls
)
is
str
:
klass
=
type
(
_Pointer
)(
"LP_%s"
%
cls
,
(
_Pointer
,),
{})
_pointer_type_cache
[
id
(
klass
)]
=
klass
return
klass
else
:
name
=
"LP_%s"
%
cls
.
__name__
klass
=
type
(
_Pointer
)(
name
,
(
_Pointer
,),
{
'_type_'
:
cls
})
_pointer_type_cache
[
cls
]
=
klass
return
klass
try
:
try
:
from
_ctypes
import
set_conversion_mode
from
_ctypes
import
set_conversion_mode
...
@@ -312,10 +292,6 @@ def SetPointerType(pointer, cls):
...
@@ -312,10 +292,6 @@ def SetPointerType(pointer, cls):
_pointer_type_cache
[
cls
]
=
pointer
_pointer_type_cache
[
cls
]
=
pointer
del
_pointer_type_cache
[
id
(
pointer
)]
del
_pointer_type_cache
[
id
(
pointer
)]
def
pointer
(
inst
):
return
POINTER
(
type
(
inst
))(
inst
)
# XXX Deprecated
# XXX Deprecated
def
ARRAY
(
typ
,
len
):
def
ARRAY
(
typ
,
len
):
return
typ
*
len
return
typ
*
len
...
...
Misc/NEWS
View file @
046e6a43
...
@@ -29,6 +29,9 @@ Extensions Modules
...
@@ -29,6 +29,9 @@ Extensions Modules
Library
Library
-------
-------
- Issue #2616: The ctypes.pointer() and ctypes.POINTER() functions are
now implemented in C for better performance.
- Issue #2408: The ``_types`` module, which was used as in implementation
- Issue #2408: The ``_types`` module, which was used as in implementation
detail of the public ``types`` module, has been removed and replaced by pure
detail of the public ``types`` module, has been removed and replaced by pure
python code.
python code.
...
...
Modules/_ctypes/_ctypes.c
View file @
046e6a43
...
@@ -128,6 +128,10 @@ bytes(cdata)
...
@@ -128,6 +128,10 @@ bytes(cdata)
#include "ctypes.h"
#include "ctypes.h"
PyObject
*
PyExc_ArgError
;
PyObject
*
PyExc_ArgError
;
/* This dict maps ctypes types to POINTER types */
PyObject
*
_pointer_type_cache
;
static
PyTypeObject
Simple_Type
;
static
PyTypeObject
Simple_Type
;
/* a callable object used for unpickling */
/* a callable object used for unpickling */
...
@@ -5169,6 +5173,12 @@ init_ctypes(void)
...
@@ -5169,6 +5173,12 @@ init_ctypes(void)
if
(
!
m
)
if
(
!
m
)
return
;
return
;
_pointer_type_cache
=
PyDict_New
();
if
(
_pointer_type_cache
==
NULL
)
return
;
PyModule_AddObject
(
m
,
"_pointer_type_cache"
,
(
PyObject
*
)
_pointer_type_cache
);
_unpickle
=
PyObject_GetAttrString
(
m
,
"_unpickle"
);
_unpickle
=
PyObject_GetAttrString
(
m
,
"_unpickle"
);
if
(
_unpickle
==
NULL
)
if
(
_unpickle
==
NULL
)
return
;
return
;
...
...
Modules/_ctypes/callproc.c
View file @
046e6a43
...
@@ -1600,7 +1600,75 @@ unpickle(PyObject *self, PyObject *args)
...
@@ -1600,7 +1600,75 @@ unpickle(PyObject *self, PyObject *args)
return
result
;
return
result
;
}
}
static
PyObject
*
POINTER
(
PyObject
*
self
,
PyObject
*
cls
)
{
PyObject
*
result
;
PyTypeObject
*
typ
;
PyObject
*
key
;
char
*
buf
;
result
=
PyDict_GetItem
(
_pointer_type_cache
,
cls
);
if
(
result
)
{
Py_INCREF
(
result
);
return
result
;
}
if
(
PyString_CheckExact
(
cls
))
{
buf
=
alloca
(
strlen
(
PyString_AS_STRING
(
cls
))
+
3
+
1
);
sprintf
(
buf
,
"LP_%s"
,
PyString_AS_STRING
(
cls
));
result
=
PyObject_CallFunction
((
PyObject
*
)
Py_TYPE
(
&
Pointer_Type
),
"s(O){}"
,
buf
,
&
Pointer_Type
);
if
(
result
==
NULL
)
return
result
;
key
=
PyLong_FromVoidPtr
(
result
);
}
else
if
(
PyType_Check
(
cls
))
{
typ
=
(
PyTypeObject
*
)
cls
;
buf
=
alloca
(
strlen
(
typ
->
tp_name
)
+
3
+
1
);
sprintf
(
buf
,
"LP_%s"
,
typ
->
tp_name
);
result
=
PyObject_CallFunction
((
PyObject
*
)
Py_TYPE
(
&
Pointer_Type
),
"s(O){sO}"
,
buf
,
&
Pointer_Type
,
"_type_"
,
cls
);
if
(
result
==
NULL
)
return
result
;
Py_INCREF
(
cls
);
key
=
cls
;
}
else
{
PyErr_SetString
(
PyExc_TypeError
,
"must be a ctypes type"
);
return
NULL
;
}
if
(
-
1
==
PyDict_SetItem
(
_pointer_type_cache
,
key
,
result
))
{
Py_DECREF
(
result
);
Py_DECREF
(
key
);
return
NULL
;
}
Py_DECREF
(
key
);
return
result
;
}
static
PyObject
*
pointer
(
PyObject
*
self
,
PyObject
*
arg
)
{
PyObject
*
result
;
PyObject
*
typ
;
typ
=
PyDict_GetItem
(
_pointer_type_cache
,
(
PyObject
*
)
Py_TYPE
(
arg
));
if
(
typ
)
return
PyObject_CallFunctionObjArgs
(
typ
,
arg
,
NULL
);
typ
=
POINTER
(
NULL
,
(
PyObject
*
)
Py_TYPE
(
arg
));
if
(
typ
==
NULL
)
return
NULL
;
result
=
PyObject_CallFunctionObjArgs
(
typ
,
arg
,
NULL
);
Py_DECREF
(
typ
);
return
result
;
}
PyMethodDef
module_methods
[]
=
{
PyMethodDef
module_methods
[]
=
{
{
"POINTER"
,
POINTER
,
METH_O
},
{
"pointer"
,
pointer
,
METH_O
},
{
"_unpickle"
,
unpickle
,
METH_VARARGS
},
{
"_unpickle"
,
unpickle
,
METH_VARARGS
},
{
"resize"
,
resize
,
METH_VARARGS
,
"Resize the memory buffer of a ctypes instance"
},
{
"resize"
,
resize
,
METH_VARARGS
,
"Resize the memory buffer of a ctypes instance"
},
#ifdef CTYPES_UNICODE
#ifdef CTYPES_UNICODE
...
...
Modules/_ctypes/ctypes.h
View file @
046e6a43
...
@@ -415,6 +415,7 @@ extern PyObject *CData_FromBaseObj(PyObject *type, PyObject *base, Py_ssize_t in
...
@@ -415,6 +415,7 @@ extern PyObject *CData_FromBaseObj(PyObject *type, PyObject *base, Py_ssize_t in
/* XXX better name needed! */
/* XXX better name needed! */
extern
int
IsSimpleSubType
(
PyObject
*
obj
);
extern
int
IsSimpleSubType
(
PyObject
*
obj
);
extern
PyObject
*
_pointer_type_cache
;
#ifdef MS_WIN32
#ifdef MS_WIN32
extern
PyObject
*
ComError
;
extern
PyObject
*
ComError
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment