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

Skip to content

Commit e2c2609

Browse files
committed
TMP
1 parent 4fe9b44 commit e2c2609

3 files changed

Lines changed: 113 additions & 12 deletions

File tree

Lib/ctypes/__init__.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from _ctypes import RTLD_LOCAL, RTLD_GLOBAL
1313
from _ctypes import ArgumentError
1414
from _ctypes import SIZEOF_TIME_T
15+
from _ctypes import memoryview_at
1516

1617
from struct import calcsize as _calcsize
1718

@@ -559,15 +560,6 @@ def wstring_at(ptr, size=-1):
559560
Return the wide-character string at void *ptr."""
560561
return _wstring_at(ptr, size)
561562

562-
from _ctypes import _memoryview_at_addr
563-
564-
_memoryview_at = PYFUNCTYPE(py_object, c_void_p, c_ssize_t, c_int)(_memoryview_at_addr)
565-
def memoryview_at(ptr, size, readonly=False):
566-
"""memoryview_at(addr, size[, readonly]) -> memoryview
567-
568-
Return a memoryview representing the memory at addr."""
569-
return _memoryview_at(ptr, size, bool(readonly))
570-
571563
if _os.name == "nt": # COM stuff
572564
def DllGetClassObject(rclsid, riid, ppv):
573565
try:

Modules/_ctypes/_ctypes.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5704,9 +5704,29 @@ wstring_at(const wchar_t *ptr, int size)
57045704
return PyUnicode_FromWideChar(ptr, ssize);
57055705
}
57065706

5707+
/*[clinic input]
5708+
_ctypes.memoryview_at
5709+
5710+
obj: object
5711+
/
5712+
size: Py_ssize_t
5713+
readonly: bool = False
5714+
5715+
Return a memoryview representing the memory at addr.
5716+
[clinic start generated code]*/
5717+
57075718
static PyObject *
5708-
memoryview_at(char *ptr, Py_ssize_t size, int readonly)
5719+
_ctypes_memoryview_at_impl(PyObject *module, PyObject *obj, Py_ssize_t size,
5720+
int readonly)
5721+
/*[clinic end generated code: output=c89fdda64bd9901d input=c960c5a2b3ccb9fb]*/
57095722
{
5723+
ctypes_state *st = get_module_state(module);
5724+
if (!CDataObject_Check(st, obj)) {
5725+
PyErr_SetString(PyExc_TypeError, "invalid type");
5726+
return NULL;
5727+
}
5728+
void *ptr = ((CDataObject *)obj)->b_ptr;
5729+
57105730
if (PySys_Audit("ctypes.memoryview_at", "nni", (Py_ssize_t)ptr, size,
57115731
readonly) < 0) {
57125732
return NULL;
@@ -5843,7 +5863,6 @@ _ctypes_add_objects(PyObject *mod)
58435863
MOD_ADD("_string_at_addr", PyLong_FromVoidPtr(string_at));
58445864
MOD_ADD("_cast_addr", PyLong_FromVoidPtr(cast));
58455865
MOD_ADD("_wstring_at_addr", PyLong_FromVoidPtr(wstring_at));
5846-
MOD_ADD("_memoryview_at_addr", PyLong_FromVoidPtr(memoryview_at));
58475866

58485867
/* If RTLD_LOCAL is not defined (Windows!), set it to zero. */
58495868
#if !HAVE_DECL_RTLD_LOCAL
@@ -5864,6 +5883,12 @@ _ctypes_add_objects(PyObject *mod)
58645883
#undef MOD_ADD
58655884
}
58665885

5886+
// Most ctypes methods are defined in callproc.c.
5887+
// Here is the rest.
5888+
static PyMethodDef module_methods[] = {
5889+
_CTYPES_MEMORYVIEW_AT_METHODDEF
5890+
{NULL, NULL} /* Sentinel */
5891+
};
58675892

58685893
static int
58695894
_ctypes_mod_exec(PyObject *mod)
@@ -5891,6 +5916,10 @@ _ctypes_mod_exec(PyObject *mod)
58915916
if (_ctypes_add_objects(mod) < 0) {
58925917
return -1;
58935918
}
5919+
if (PyModule_AddFunctions(mod, module_methods) < 0) {
5920+
return -1;
5921+
}
5922+
58945923
return 0;
58955924
}
58965925

Modules/_ctypes/clinic/_ctypes.c.h

Lines changed: 81 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)