Why different behavior in debug and run? Pycharm+ locals

39 views Asked by At

I have this code:

import ctypes
import inspect


def fun():
    c = 3
    inspect.currentframe().f_locals['c'] = 4
    ctypes.pythonapi.PyFrame_LocalsToFast(ctypes.py_object(inspect.currentframe()), ctypes.c_int(0))
    print(c)


fun()

If you "Run", the output will be: >>> 4

But with "Debug" and a breakpoint on "print(c)". By calling "print(c)" to the console, the output will be like this: >>> 3

Why different behavior? And can this be fixed?

Work with PyDev console pycharm

1

There are 1 answers

1
Kazarin V. On

In my case it works like this:

import ctypes
import inspect


def fun():
    c = 3
    ctypes.pythonapi.PyFrame_FastToLocals(ctypes.py_object(inspect.currentframe()))
    inspect.currentframe().f_locals['c'] = 4
    ctypes.pythonapi.PyFrame_LocalsToFast(ctypes.py_object(inspect.currentframe()), ctypes.c_int(0))
    print(c)


fun()