Can`t access python code: unhashable type: 'ConfigParser'
Specification
-
pywebview version: 5.1
-
operating system: Windows 11 Pro 23H2
-
web renderer: WinForms / Chromium / Bottle v0.12.25
-
pip status:
Package - Version bottle - 0.12.25 cffi - 1.16.0 clr-loader - 0.2.6 pip - 23.2.1 proxy-tools - 0.1.0 pycparser - 2.22 pyserial - 3.5 pythonnet - 3.0.3 pywebview - 5.1 pywin32 - 306 typing_extensions - 4.12.2
Description
I have a problem that I can`t access any python function from JavaScript side.
JavaScript Error:
TypeError: pywebview.api.xyz is not a function
Terminal Output:
[pywebview] unhashable type: 'ConfigParser' Traceback (most recent call last): File "C:\Users\GitLab.venv\Lib\site-packages\webview\util.py", line 188, in inject_pywebview func_list = generate_func() ^^^^^^^^^^^^^^^ File "C:\Users\GitLab.venv\Lib\site-packages\webview\util.py", line 176, in generate_func functions = get_functions(window._js_api) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\GitLab.venv\Lib\site-packages\webview\util.py", line 171, in get_functions get_functions(attr, full_name, functions) File "C:\Users\GitLab.venv\Lib\site-packages\webview\util.py", line 154, in get_functions if obj in exposed_objects: ^^^^^^^^^^^^^^^^^^^^^^ TypeError: unhashable type: 'ConfigParser'
I am using the python configparser.ConfigParser object in one of my functions in Python.
Thank you very much!
Practicalities
-
YES I am willing to work on this issue myself.
-
NO I am not prepared to support this issue financially.
Additional fact: with pywebview version 4.4.1 everything works fine!
Prefix your configparser variable with an underscore to omit it from serialization. Nested JS API was intoduced in 5.0 and as the result it tries to expose everything in the api class recursively. Error check should probably be added there to gracefully handle scenarios like this.
I replaced set with a list in the inject_pywebview function. This should resolve this error. The fix is pushed to master
Ohh wow, thank you very much. I will try it and send you feedback!
I tried it out, used the master branch, but had no luck & got another Exception:
[pywebview] Using WinForms / Chromium
Bottle v0.12.25 server starting up (using ThreadedAdapter())...
Listening on http://127.0.0.1:38041/
Hit Ctrl-C to quit.
127.0.0.1 - - [30/Jul/2024 13:34:32] "GET /index.html HTTP/1.1" 200 523
127.0.0.1 - - [30/Jul/2024 13:34:32] "GET /assets/index-qJEYG0QF.css HTTP/1.1" 200 140053
127.0.0.1 - - [30/Jul/2024 13:34:32] "GET /assets/index-E954PnVu.js HTTP/1.1" 200 238772
127.0.0.1 - - [30/Jul/2024 13:34:32] "GET /assets/dark-5uK65d8z.svg HTTP/1.1" 200 7632
127.0.0.1 - - [30/Jul/2024 13:34:32] "GET /assets/HFqEA9hA.png HTTP/1.1" 200 51254
127.0.0.1 - - [30/Jul/2024 13:34:32] "GET /assets/nttuUCy3.ico HTTP/1.1" 200 76362
[pywebview] Main window failed to start
Traceback (most recent call last):
File "C:\Users\GitLab\tool\webview\util.py", line 189, in inject_pywebview
func_list = generate_func()
^^^^^^^^^^^^^^^
File "C:\Users\GitLab\tool\webview\util.py", line 177, in generate_func
functions = get_functions(window._js_api)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\UsersGitLab\tool\webview\util.py", line 172, in get_functions
get_functions(attr, full_name, functions)
File "C:\Users\GitLab\tool\webview\util.py", line 172, in get_functions
get_functions(attr, full_name, functions)
File "C:\Users\GitLab\tool\webview\util.py", line 167, in get_functions
attr = getattr(obj, name)
^^^^^^^^^^^^^^^^^^
File "C:\Users\GitLab\tool\webview\dom\dom.py", line 18, in body
return self._elements.get('body', Element(self.__window, 'body'))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GitLab\tool\webview\dom\element.py", line 77, in __init__
self.__generate_events()
File "C:\Users\GitLab\tool\webview\dom\element.py", line 36, in wrapper
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GitLab\tool\webview\dom\element.py", line 410, in __generate_events
events = self._window.evaluate_js(f"""
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GitLab\tool\webview\window.py", line 45, in wrapper
raise WebViewException('Main window failed to start')
webview.errors.WebViewException: Main window failed to start
I am not sure if this is related to the initial issue...
It looks like a separate issue. What does your API object looks like? Could you get an original exception out of this?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
The message to post on the issue when closing it. If none provided, will not comment when closing an issue.