Skip to content

Fix #2146 - Workaround Pyodide issue with attributes #2148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 27, 2024

Conversation

WebReflection
Copy link
Contributor

@WebReflection WebReflection commented Aug 26, 2024

Description

Fixes #2146

Fixes #2145

It looks like Pyodide has issues with "magic" JS accessors such as onmessage and others exposed by WebSocket instances /cc @hoodmane

Changes

  • use Reflect.set instead to attach properties
  • use create_proxy to ensure no errors once these callbacks are invoked
  • test everything is fine via tests/ws/ and bun index.js after changing mpy to py
  • updated polyscript to its latest so we can just merge once

Checklist

  • All tests pass locally
  • I have updated CHANGELOG.md
  • I have created documentation for this(if applicable)

@WebReflection WebReflection force-pushed the issue-2146 branch 2 times, most recently from 1ecd677 to ecc248c Compare August 26, 2024 10:30
@WebReflection
Copy link
Contributor Author

WebReflection commented Aug 26, 2024

This version of PyScript has been published already on npm as https://cdn.jsdelivr.net/npm/@pyscript/core@0.5.7/dist/core.js and https://cdn.jsdelivr.net/npm/@pyscript/core@0.5.7/dist/core.css

for t in ["onclose", "onerror", "onmessage", "onopen"]:
if t in kw:
socket[t] = kw[t]
# Pyodide fails at setting socket[t] directly
set_attr(socket, t, create_proxy(kw[t]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python has a builtin called setattr that does this:

Suggested change
set_attr(socket, t, create_proxy(kw[t]))
setattr(socket, t, create_proxy(kw[t]))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

silly me ... thanks, fixed that! why wouldn't that accessor be set though? MicroPython had no issue with socket[t] = kw[t] but Pyodide threw an error before due "unable to set property X" issue 🤔

we are good with setattr but it's a bit of a worry/need to remember thing I haven't witnessed before.

@WebReflection WebReflection merged commit 7582cbe into pyscript:main Aug 27, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants