Using callable_iterator (re.finditer) causes Python to freeze

I have a function that is called for every line of a text.

def tokenize_line(line: str, cmd = ''):
    matches = re.finditer(Patterns.SUPPORTED_TOKENS, line)
    tokens_found, not_found, start_idx = [], [], 0
    print(matches)
    for match in matches:
        pass
        # Rest of code

The result of print(matches) is something like: <callable_iterator object at 0x0000021201445000>

However, when I convert the iterator into a list:

matches = list(re.finditer(Patterns.SUPPORTED_TOKENS, line))

or when I iterate with for:

for match in matches:
   print(match)

…Python freezes.

This issue occurs inconsistently. For example:

tokenize_line('$color AS $length') # Works fine
tokenize_line('FALSE + $length IS GT 7 + $length IS 4') # Freezes

So, the problem arises when converting the callable_iterator into a list or iterating over it.

Here is the pattern (Patterns.SUPPORTED_TOKENS) I’m using:

(°pd+°|°ad+°|°md+°)|((?<!S)(?:!'(?:\.|[^'n\])*'|!"(?:\.|[^n"\])*")(?!S))|((?:'(?:\.|[^'n\])*'|"(?:\.|[^n"\])*"))|(({(.*)}))|((?<!S)([@$][w]*(?:.[w]*)*)(?!S))|((?<!d)-?d*.?d+)|(**|[+-*()/%^]|==|&&||||!=|>=|<=|>|<|~~|!~~|::|!::)|([:/])|(b(?:AS|AND|AT|:|BETWEEN|BY|FROM|IN|INTO|ON|OF|OR|THAN|TO|USING|WITH)b)|(b[a-zA-Z_][a-zA-Z0-9_]* *((?:[^;()'""]*|"(?:[^"\]|\.)*"|'(?:[^'\]|\.)*'|([^)]*))*?;))|((b(?:EMPTY|STRING|NUMBER|BOOL|ARRAY|MAP|TRUE|FALSE|NULL|UNKNOWN|DOTALL|IGNORECASE|MULTILINE|ARRAY_ARRAY|ARRAY_STRING|ARRAY_MAP|ARRAY_NUMBER|ARRAY_NULL|DOT|SPACE|NEWLINE|SEMICOLON|COLON|HASH|COMMA|TAB)b)|(b(?:IS NOT LT|IS NOT GT|IS NOT GEQ|IS NOT LEQ|IS NOT|IS LT|IS GT|IS GEQ|IS LEQ|IS|NOT IN|NOT|IN|HAS NOT|HAS|AND|OR)b))

Explanation of the Regular Expression Pattern:

Custom Tokens: Matches specific custom tokens that start with particular characters and are followed by digits.

Quoted Strings: Matches both single and double-quoted strings, including those with escape characters.
Curly Braces Content: Matches anything enclosed in curly braces.

Variables: Matches variables that start with specific characters (like @ or $) and can include dots for nested properties.

Numbers: Matches both integers and floating-point numbers, including negative numbers.

Operators: Matches various mathematical and logical operators.
Colons and Slashes: Matches specific punctuation characters like colons and slashes.

Keywords: Matches certain keywords that are reserved in the language.
Function Definitions: Matches function definitions or similar structures, ensuring they follow specific syntax rules.

Data Types and Modifiers: Matches keywords that represent data types or modifiers.

Logical Operators: Matches complex logical operators used in conditional expressions.

Any help to understand why this happens and how to fix it would be greatly appreciated!

New contributor

Martin A. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật