Skip to main content

Real pointers and some memory utilities for python

Project description

pyptrs

This library provides real pointers (as in languages like C) and some memory access utilites.

Installation

pip install pyptrs

Creating A Pointer

You should never create pointers directly. Instead, use the functions below:

pyptrs.pointer_to_object(obj, c_object = False)

Return a pointer to obj. c_object specifies whether to treat the obj as a python object or a C object. If c_object is True, obj must be an instance of a ctypes C data type. You can learn more information about these data types from here, here and here

pyptrs.pointer_to_address(address, ctype = None)

Return a pointer that points to the given address. If ctype is None, a python object is assumed to be living in that address. Otherwise, ctype must be the ctypes C data type corresponding to the type of the object living in address. You can learn more information about these data types from here, here and here

Pointer Objects

Pointer.get_size()

Return the number of bytes occupied by the pointed object in memory.

Pointer.get_mem()

Return a bytes object representing the pointed object.

Pointer.temp_value(value, force_write = False)

Dereference and assign value to the pointed object, then return a context manager that reverts this assignment at exit. If size of value is bigger than the pointed object, a MemoryError will be raised. In order to prevent this, pass True to force_write.

Simulated Operators

pyptrs.dereference(pointer, *value, force_write = False)

If value is not given, it just dereferences the pointer. If value is given, it must be a single argument and it dereferences the pointed object and assigns value to it then returns a backup_id that can be passed to pyptrs.mem_restore(). If size of value is bigger than the pointed object, a MemoryError will be raised. In order to prevent this, pass True to force_write. Type of value can be anything if the pointed object is a python object. If the type of the pointed object is a C type, type of value must be the corresponding ctypes C data type.

pyptrs.address_of(obj, c_object = False)

Return address of the obj. If c_object is True type of obj must be a ctypes C data type. You can learn more information about these data types from here, here and here If c_object is True, address of the actual C object is returned, not the address of its ctypes wrapper.

Utils

pyptrs.mem_read(address, amount = 1)

Read amount bytes starting from address and return them as a bytes object.

pyptrs.mem_write(address, data = b"")

Write the bytes represented by data to address and return a backup_id that can be passed to pyptrs.mem_restore().

pyptrs.mem_restore(backup_id)

Revert the changes done to memory by the function that returned backup_id.

pyptrs.mem_restore_last()

Revert the last change done to memory either by pyptrs.dereference() or pyptrs.mem_write().

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyptrs-1.0.0.post3.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyptrs-1.0.0.post3-py2.py3-none-any.whl (4.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pyptrs-1.0.0.post3.tar.gz.

File metadata

  • Download URL: pyptrs-1.0.0.post3.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for pyptrs-1.0.0.post3.tar.gz
Algorithm Hash digest
SHA256 eba7660110f63425b09502829f2442db7e2b3c0cbe5b200c0f2a049ba90e86d3
MD5 f6ab5d610c55aaef6a6acb14d6b00dbc
BLAKE2b-256 c17e99bf09325b9f12014d9e9f84b8cc1c7d05a89d3c1e0ef47ae7c54cca467b

See more details on using hashes here.

File details

Details for the file pyptrs-1.0.0.post3-py2.py3-none-any.whl.

File metadata

  • Download URL: pyptrs-1.0.0.post3-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for pyptrs-1.0.0.post3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4c4587165181987b77f3d4408a129b7eae20b8bc22b609131510d324608f38fd
MD5 f4b544004f713498fb5fd6ea273aa58d
BLAKE2b-256 21b51ea6748cf6d405a70a97e2721d2f06172cb8bab82c53ac6d2d2fcd6f0920

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page