Tuesday, November 25, 2014

ctypes short string shenannigans


>>>
>>>
>>>
42105668
>>>
42105668
>>>
'f'
>>>
'ab'
>>>
'f'
import ctypes
b_char = 'b'
ctypes.pythonapi.PyString_AsString(id(b_char))

ctypes.memset(_, ord('f'), 1)


b_char


'ab'


'ab'[1:]



Single character strings are interned in a special array in CPython.  This means mutation to the internals of a single character string will have global consequences.

(http://www.laurentluce.com/posts/python-string-objects-implementation/ has more information.)

1 comment:

  1. money clothing sale, Promotion code and Sale plans. Progress Code Terms And Conditions. For codes are on an astoundingly focal level genuine for orders set on the web.

    ReplyDelete