imgui_bundle icon indicating copy to clipboard operation
imgui_bundle copied to clipboard

`imgui_demo.py` - RuntimeError: regex_error(error_stack): There was insufficient memory

Open eabase opened this issue 9 months ago • 3 comments

Version & Platform

Version: Using a git clone Platform: Windows-11 Pro Language: Python 3.12.9 or C++17 ? Compiler: MSVS 2022, MSVC 2019 platform backend: ❓ rendering backend Vulkan or DirectX

# cd C:\mydev\gitclones\imgui_bundle\bindings\imgui_bundle\demos_python\
# python.exe -qu -X utf8=1 .\imgui_demo.py                                                                                                                                                                                                          

Traceback (most recent call last):
  File "C:\mydev\gitclones\imgui_bundle\bindings\imgui_bundle\demos_python\demos_immapp\imgui_demo.py", line 3295, in <module>
    main()
  File "C:\mydev\gitclones\imgui_bundle\bindings\imgui_bundle\demos_python\demos_immapp\imgui_demo.py", line 3291, in main
    immapp.run(runner_params, addons)
  File "C:\venvs\imgui\Lib\site-packages\imgui_bundle\_patch_runners_add_save_screenshot_param.py", line 38, in patched_run
    run_backup(*args, **kwargs)
  File "C:\mydev\gitclones\imgui_bundle\bindings\imgui_bundle\demos_python\demos_immapp\imgui_demo.py", line 3247, in gui_code
    code_editor.render("Code")

RuntimeError: regex_error(error_stack): There was insufficient memory to determine whether the regular expression could match the specified character sequence.

@pthom Do you think an update of the Python API with the latest ImGui could help resolve this?

UPDATE:
There seem to be some issue with the way you read in the whole file, here:

  • https://github.com/pthom/imgui/blob/imgui_bundle/imgui_demo.py#L3234-L3242
  • As the error suggest, the RegEx is applied to a too large block.
  • Memory is not enough, and maybe it's a compiler issue, using outdated setting with the *.cpp?
  • Large files/blobs of code should be read line-by-line and sanitizes for using different character set.
    import codecs
...
    with open(__file__, mode='rb') as f:
        #print('\nTrying to read file: {}'.format(__file__))
        code = f.read()        
        # Decoding to Unicode
        decoded_text = codecs.decode(code, 'utf_8', errors='ignore')
        code_lines = decoded_text.splitlines()
        print('\nSize of Lines: {}'.format(len(code_lines)))
        #print('\nLines:\n{}'.format(code_lines))
        code_editor.set_text(decoded_text)

With python.exe -qu -X utf8=1 .\imgui_demo.py, this runs and flashed the windows and then crashes.

Large files read, something like:

def read_large_file(file_name):
    with open(file_name) as f:
        for line in f:
            yield line.strip()

for line in read_large_file('large_file.txt'):
    # Process each line without loading the entire file into memory
    print(line)

But may need to add decoding.

eabase avatar Mar 17 '25 23:03 eabase

There seem to be some issue with the way you read in the whole file

Yes, there was an issue in how the code is read: one should write

    with open(__file__, encoding="utf-8") as f:    # added encoding=...

See 85e99f57f59dee3adb4a3a5aadd9beed0c2047f5 where this is fixed.

RuntimeError: regex_error(error_stack): There was insufficient memory to determine whether the regular expression could match the specified character sequence.

I can reproduce this issue when using the version from pypi. The reason for this error is probably in the text editor which uses a lot of memory to handle this big source file.

However, this is solved in the current version, if you install from source (an update to imGuiColorTextEdit did fix this)

pthom avatar Mar 18 '25 14:03 pthom

This fixed the error in OP, but the fix only moved the error to the next function gui_code():

    def gui_code():
        with imgui_ctx.push_font(imgui_md.get_code_font()):
            code_editor.render("Code")
File "C:\mydev\gitclones\imgui_bundle\bindings\imgui_bundle\demos_python\demos_immapp\imgui_demo.py", line 3261, in gui_code
    code_editor.render("Code")
RuntimeError: regex_error(error_stack): There was insufficient memory to determine whether the regular expression could match the specified character sequence.

eabase avatar Mar 20 '25 01:03 eabase

I suspect this happened with the pypi version. It was fixed in the source, but the fix is not available yet on pypi.

you may either compile from source, or download a wheel at https://github.com/pthom/imgui_bundle/actions/workflows/wheels.yml

pthom avatar Mar 20 '25 10:03 pthom