Jake Lishman 11/12/2022

Mutating Python tuples by C FFI abuse

Read Original

This technical article details a method to bypass Python's tuple immutability by using the ctypes module and CPython's C-level object structure. It explains the PyObject memory layout, accesses the internal `ob_item` array via pointer manipulation, and provides a Python function that manually adjusts reference counts to swap tuple elements.

Mutating Python tuples by C FFI abuse

Comments

No comments yet

Be the first to share your thoughts!

Browser Extension

Get instant access to AllDevBlogs from your browser

Top of the Week

1
The Beautiful Web
Jens Oliver Meiert 2 votes
3
LLM Use in the Python Source Code
Miguel Grinberg 1 votes
4
Wagon’s algorithm in Python
John D. Cook 1 votes