why can’t I set my python global variable when not running step-by-step debugger

  Kiến thức lập trình

When executing this code using the step-by-step debugger, I get the expected results where error_str = "Error (11): Resource temporarily unavailable". When using the interpreters (3.6, 3.8, and 3.11), I get error_str = ""

Here’s the code:

import ctypes, os

LIBC = ctypes.CDLL(None)

global error_str
error_str = ""

def sscanf(buf: str, fmt: str, *args) -> int:  #   
    """
    Read formatted data from string
    Reads data from s and stores them according to parameter format into the locations given by the additional arguments, 
    as if scanf was used, but reading from s instead of the standard input (stdin).
    """

# set up prototype
    LIBC.sscanf.restype = ctypes.c_int # correct return type
    LIBC.sscanf.argtypes = tuple((ctypes.c_char_p, ctypes.c_char_p)) + tuple(ctypes.c_void_p for i in range(0, len(args)))
    
    rc: int = LIBC.sscanf(*(tuple((buf.encode(), fmt.encode(),)) + tuple(ctypes.byref(args[i]) for i in range(0, len(args)))))

    errno = ctypes.c_int.in_dll(ctypes.pythonapi,"errno")
    if errno.value: 
        global error_str
        error_str = f"Error ({errno.value}): {os.strerror(errno.value)}"
    return rc


if __name__ == "__main__":
    PID = ctypes.c_int(0)
    SPID = ctypes.c_int(0)
    POOP = ctypes.c_int(0)
    input = '<13>Jul 10 15:37:56 [../dgh/DeployTools/start_emu.sh: Wed Jul 10 15:37:56 MDT 2024]: ============== Launched: "../dgh/DeployTools/start_emu.sh FUNCTION launch_fsw" on "centos8" =============="'
    rc: int = sscanf(input, "%*[^(] (%d", PID)

    print(PID.value); 
    print(error_str)

BTW: I’m using Linux

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT