Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a memset with Python buffer object?

How can I do a fast reset for a continue set of values inside a Python buffer object?

Mainly I am looking for a memset :)

PS. The solution should work with Python 2.5 and modify the buffer itself (no copy).

like image 303
sorin Avatar asked Nov 25 '11 15:11

sorin


People also ask

How do you add a buffer in Python?

In Python, you don't have to care about memory management. You don't need to reserve "buffers", just assign the variables you want, and use them. If you assign too many (for your RAM), you might run into MemoryError s, indicating you don't have enough memory. You could use a Queue as some kind of buffer, though, e.g.

What is buffer protocol in Python?

The Python buffer protocol, also known in the community as PEP 3118, is a framework in which Python objects can expose raw byte arrays to other Python objects. This can be extremely useful for scientific computing, where we often use packages such as NumPy to efficiently store and manipulate large arrays of data.

What is memset in Python?

memset could be useful when handling cryptographically sensitive data. Example: destroying the plaintext password after encrypting it. del ling the name or binding it to another object is inadequate; the plaintext object could still persist in memory for a while before being garbage-collected.

What is memset CPP?

Memset() is a C++ function. It copies a single character for a specified number of times to an object. It is useful for filling a number of bytes with a given value starting from a specific memory location. It is defined in <cstring> header file.


2 Answers

The ctypes package has a memset function built right in. Ctypes does work with Python 2.5, but is not included by default. You will need a separate install.

def memsetObject(bufferObject):
    "Note, dangerous"
    import ctypes
    data = ctypes.POINTER(ctypes.c_char)()
    size = ctypes.c_int()  # Note, int only valid for python 2.5
    ctypes.pythonapi.PyObject_AsCharBuffer(ctypes.py_object(bufferObject), ctypes.pointer(data), ctypes.pointer(size))
    ctypes.memset(data, 0, size.value)

testObject = "sneakyctypes"
memsetObject(testObject)
print repr(testObject)
# '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
like image 74
Peter Shinners Avatar answered Oct 28 '22 16:10

Peter Shinners


If you can write into, try with itertools.repeat()

import itertools
my_buffer[:] = itertools.repeat(0, len(my_buffer))
like image 25
Cédric Julien Avatar answered Oct 28 '22 15:10

Cédric Julien