[issue12476] ctypes: need example how to pass raw data from Python

New submission from anatoly techtonik <techtonik@gmail.com>: ctypes documentation lacks an example, how to pass a raw data block from Python to a C function. For example, how to pass this chunk: data = open('somefile', 'rb').read() to this function: int checkBuffer(LPSTR lpData, DWORD dwBufferLength); ? ---------- assignee: docs@python components: Documentation, ctypes messages: 139675 nosy: docs@python, techtonik priority: normal severity: normal status: open title: ctypes: need example how to pass raw data from Python versions: Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue12476> _______________________________________

Amaury Forgeot d'Arc <amauryfa@gmail.com> added the comment: Can you suggest a patch? ---------- nosy: +amaury.forgeotdarc _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue12476> _______________________________________

anatoly techtonik <techtonik@gmail.com> added the comment: ISTM that this code works ok. data = open('data.raw', 'rb').read() ret = checkBuffer(data, len(data)) You need to make sure that checkBuffer doesn't try to modify memory though. For mutable memory blocks use create_string_buffer(). ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue12476> _______________________________________

Amaury Forgeot d'Arc <amauryfa@gmail.com> added the comment: Can you suggest a patch? ---------- nosy: +amaury.forgeotdarc _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue12476> _______________________________________

anatoly techtonik <techtonik@gmail.com> added the comment: ISTM that this code works ok. data = open('data.raw', 'rb').read() ret = checkBuffer(data, len(data)) You need to make sure that checkBuffer doesn't try to modify memory though. For mutable memory blocks use create_string_buffer(). ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue12476> _______________________________________
participants (2)
-
Amaury Forgeot d'Arc
-
anatoly techtonik