File: buffer_mutate.py

package info (click to toggle)
pypy3 7.3.20%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 212,340 kB
  • sloc: python: 2,100,989; ansic: 540,684; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (30 lines) | stat: -rw-r--r-- 873 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#
# The various methods of bufferobject.c (here buffer_subscript()) call
# get_buf() before calling potentially more Python code (here via
# PySlice_GetIndicesEx()).  But get_buf() already returned a void*
# pointer.  This void* pointer can become invalid if the object
# underlying the buffer is mutated (here a bytearray object).
#
# As usual, please keep in mind that the three "here" in the sentence
# above are only examples.  Each can be changed easily and lead to
# another crasher.
#
# This crashes for me on Linux 32-bits with CPython 2.6 and 2.7
# with a segmentation fault.
#


class PseudoIndex(object):
    def __index__(self):
        for c in "foobar"*n:
            a.append(c)
        return n * 4


for n in range(1, 100000, 100):
    a = bytearray("test"*n)
    buf = buffer(a)

    s = buf[:PseudoIndex():1]
    #print repr(s)
    #assert s == "test"*n