[Python] Python Debugger Error - for loop ending with if block

Discussão em 'Python' iniciado por Stack, Novembro 22, 2024.

  1. Stack

    Stack Membro Participativo

    I have a Python script with this basic structure:

    import os
    import glob
    import re

    class ClassInfo():
    pass

    def read_busclass_files():
    pattern = os.path.join(os.path.dirname(__file__), "*.txt")

    matching_files = glob.glob(pattern)

    re_block_with_rules = re.compile(r"( {0,3}\t| {4})(Audit Entry Rules|Field Rules|Commit Rules|Create Rules|Create Exit Rules|Delete Rules|Attach Rules|Translation Field Rules|Apply Pending Effective Rules|Translation Rules|Action Exit Rules|Parent Attach Rules|Dynamic Creation Rules)\s*(\/\/.*)?$")
    re_relations = re.compile(r"( {0,3}\t| {4})Relations\s*(\/\/.*)?$")
    re_actions = re.compile(r"( {0,3}\t| {4})Actions\s*(\/\/.*)?$")
    re_class_name = re.compile(r"(\w+)\s+is a BusinessClass\s*(\/\/.*)?$")

    class_map: dict[str, ClassInfo] = {}

    # Read each file
    for file_path in matching_files:
    try:
    line_num = 0
    action_info = None
    class_name = None
    action_name = None
    with open(file_path, 'r') as f:
    while True:
    line = f.readline()
    if not line:
    break
    line_num += 1
    if len(line.strip()) == 0:
    continue
    line = line.rstrip()
    if class_name is None:
    class_name = re_class_name.match(line).group(1) if re_class_name.match(line) else "bad"
    if class_name not in class_map:
    class_map[class_name] = ClassInfo()
    continue
    if re_block_with_rules.match(line):
    read_state = 1
    for_each_nested = []
    continue
    if re_relations.match(line):
    read_state = 2
    continue
    if re_actions.match(line):
    read_state = 4
    continue
    if (action_info is not None):
    class_map[class_name].actions[action_name] = action_info
    except IOError as e:
    print(f"Error reading file {file_path}: {e}")

    return class_map

    def main():
    class_map = read_busclass_files()
    return 0

    if __name__ == "__main__":
    exit(main())


    I put a txt file in the directory with the script with this content:

    AbsenceCalculation is a BusinessClass
    Relations
    AbsenceTableDetailRel is an AbsenceTableDetail set //Used in Computation field for edits


    Name the txt file such that it is the first one in the array of files matched, like "atxtfile.txt". Then add another txt file so that there's more than 1 file matching the pattern *.txt.

    When I run the script without any breakpoints, everything is fine. (If I add a line to report sys.version, it reports Python version: 3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)]). When I run the script in the VS Code Python Debugger with a breakpoint in the if (action_info is not None): line, execution breaks on the for line with this error:

    Exception has occurred: TypeError
    an integer is required
    File "C:\landmark\ox1\src\hcm\scripts\tmp.py", line 29, in read_busclass_files
    for file_path in matching_files:
    File "C:\landmark\ox1\src\hcm\scripts\tmp.py", line 70, in main
    class_map = read_busclass_files()
    ^^^^^^^^^^^^^^^^^^^^^
    File "C:\landmark\ox1\src\hcm\scripts\tmp.py", line 74, in <module>
    exit(main())
    ^^^^^^
    TypeError: an integer is required


    The error occurs if I put a breakpoint on if (action_info is not None):, run the script, and then try to step forward when execution stops on that line (executing that line triggers the error on the for line somehow.) Is this indeed a bug in the Python debugger?

    Continue reading...

Compartilhe esta Página