UnboundLocalError: local variable 'magic_words' referenced before assignment #18

Open
opened 2021-03-17 16:54:06 +01:00 by Ghost · 4 comments

So I can't tell if this isn't some kind of user error, but I don't have a Python IDE set up or anything, nor am I really a developer, so I can't even guess at what's happening.

I mean, I know what's happening - the variable magic_words was referenced before it was assigned - but I'm more curious as to why it happened. The documentation on magic words is minimal, and this happens regardless of whether I'm using one or not.

(I did have other errors, but most of those seem to have subsided once I installed depedencies and used Poetry - that being said, I think pycairo has external dependencies on cairo that aren't Python-related and can't be installed with pip, so documenting any dependencies would help. I tried both the release version off pip and cloning from this repo.) From a simple poetry run etherpump pull:

===============================================================================
Etherpump is warming up the engines ...
===============================================================================
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/fiona/etherpump/etherpump/__init__.py", line 93, in main
    cmdmod.main(args)
  File "/home/fiona/etherpump/etherpump/commands/pull.py", line 579, in main
    trio.run(handle_pads, args)
  File "/home/fiona/.cache/pypoetry/virtualenvs/etherpump-SQlX67wf-py3.8/lib/python3.8/site-packages/trio/_core/_run.py", line 1928, in run
    raise runner.main_task_outcome.error
  File "/home/fiona/etherpump/etherpump/commands/pull.py", line 561, in handle_pads
    nursery.start_soon(
  File "/home/fiona/.cache/pypoetry/virtualenvs/etherpump-SQlX67wf-py3.8/lib/python3.8/site-packages/trio/_core/_run.py", line 815, in __aexit__
    raise combined_error_from_nursery
  File "/home/fiona/etherpump/etherpump/commands/pull.py", line 534, in handle_pad
    if magic_words
UnboundLocalError: local variable 'magic_words' referenced before assignment

It could possibly be odd issues with my Python installation (it's 3.8 and has always been oddly temperamental), so I'll see if I can't test it out elsewhere temporarily and see if the problem persists. Otherwise, my theory is that magic words are hardcoded to varia somehow or stuck in the JSON settings or something, so when I run it without that information it chokes. (I tried adding magic_words to the JSON settings file, but it didn't take.)

So I can't tell if this isn't some kind of user error, but I don't have a Python IDE set up or anything, nor am I really a developer, so I can't even guess at what's happening. I mean, I *know* what's happening - the variable `magic_words` was referenced before it was assigned - but I'm more curious as to *why* it happened. The documentation on magic words is minimal, and this happens regardless of whether I'm using one or not. (I did have other errors, but most of those seem to have subsided once I installed depedencies and used Poetry - that being said, I think pycairo has external dependencies on cairo that aren't Python-related and can't be installed with pip, so documenting any dependencies would help. I tried both the release version off pip and cloning from this repo.) From a simple `poetry run etherpump pull`: ``` =============================================================================== Etherpump is warming up the engines ... =============================================================================== Traceback (most recent call last): File "<string>", line 1, in <module> File "/home/fiona/etherpump/etherpump/__init__.py", line 93, in main cmdmod.main(args) File "/home/fiona/etherpump/etherpump/commands/pull.py", line 579, in main trio.run(handle_pads, args) File "/home/fiona/.cache/pypoetry/virtualenvs/etherpump-SQlX67wf-py3.8/lib/python3.8/site-packages/trio/_core/_run.py", line 1928, in run raise runner.main_task_outcome.error File "/home/fiona/etherpump/etherpump/commands/pull.py", line 561, in handle_pads nursery.start_soon( File "/home/fiona/.cache/pypoetry/virtualenvs/etherpump-SQlX67wf-py3.8/lib/python3.8/site-packages/trio/_core/_run.py", line 815, in __aexit__ raise combined_error_from_nursery File "/home/fiona/etherpump/etherpump/commands/pull.py", line 534, in handle_pad if magic_words UnboundLocalError: local variable 'magic_words' referenced before assignment ``` It could possibly be odd issues with my Python installation (it's 3.8 and has always been oddly temperamental), so I'll see if I can't test it out elsewhere temporarily and see if the problem persists. Otherwise, my theory is that magic words are hardcoded to varia somehow or stuck in the JSON settings or something, so when I run it without that information it chokes. (I tried adding `magic_words` to the JSON settings file, but it didn't take.)
Owner

Thanks for raising this @queenfiona! I have taken a look and this does indeed seem to be a bug on our part in the source. I was able to reproduce it and will get a release out with a fix soon.

(I tested with this diff just to make sure, you could use this as a work-around for now or alternatively, pass --magicwords, I think that should also get you past this).

diff --git a/etherpump/commands/pull.py b/etherpump/commands/pull.py
index 73221c2..2888cdb 100644
--- a/etherpump/commands/pull.py
+++ b/etherpump/commands/pull.py
@@ -529,11 +529,10 @@ async def handle_pad(args, padid, data, info, session):
         async with await trio.open_file(metapath, "w") as f:
             await f.write(json.dumps(meta))
 
-    mwords_msg = (
-        ", magic words: {}".format(", ".join(magic_words))
-        if magic_words
-        else ""
-    )
+    try:
+        mwords_msg = ", magic words: {}".format(", ".join(magic_words))
+    except UnboundLocalError:
+        mwords_msg = ""
 
     print("[x] {} (saved{})".format(padid, mwords_msg))
     saved += 1
Thanks for raising this @queenfiona! I have taken a look and this does indeed seem to be a bug on our part in the source. I was able to reproduce it and will get a release out with a fix soon. (I tested with this diff just to make sure, you could use this as a work-around for now or alternatively, pass `--magicwords`, I think that should also get you past this). ```diff diff --git a/etherpump/commands/pull.py b/etherpump/commands/pull.py index 73221c2..2888cdb 100644 --- a/etherpump/commands/pull.py +++ b/etherpump/commands/pull.py @@ -529,11 +529,10 @@ async def handle_pad(args, padid, data, info, session): async with await trio.open_file(metapath, "w") as f: await f.write(json.dumps(meta)) - mwords_msg = ( - ", magic words: {}".format(", ".join(magic_words)) - if magic_words - else "" - ) + try: + mwords_msg = ", magic words: {}".format(", ".join(magic_words)) + except UnboundLocalError: + mwords_msg = "" print("[x] {} (saved{})".format(padid, mwords_msg)) saved += 1 ```
Owner

hey @queenfiona, I've published a new version of etherpump with this hopefully fixed! You can pip install -U etherpump or do a git pull from your source check-out and that should work out. Let me know!

Also, please feel free to raise further tickets on bugs/docs/whatever, it is really valuable to get feedback on using this tool outside of varia. Also, would be cool to see what you publish! 🖤

hey @queenfiona, I've published a [new version](https://pypi.org/project/etherpump/0.0.20/) of `etherpump` with this hopefully fixed! You can `pip install -U etherpump` or do a `git pull` from your source check-out and that should work out. Let me know! Also, please feel free to raise further tickets on bugs/docs/whatever, it is really valuable to get feedback on using this tool outside of varia. Also, would be cool to see what you publish! 🖤
Author

So I wanted to confirm that this is working as intended, as far as I can tell. It's pulling things, indexing them, reading the magic words, etcetera. However, right now...

it feels a lot like this is made very fit-to-purpose for Constant and Varia. While the tools to make something more suited to another use case appear to be in place, currently it's very fuzzy to me how to get there, and the existing files reference CSS files that aren't copied into place.

Ideally, I'd want a page very similar in strucutre to https://etherpump.vvvvvvaria.org/, which is what got me into wanting to use the tool in the first place, but I'm not sure how that page is created using etherpump. I assume it's a particular custom template and set of commands that produces that page, so if I had some idea of the steps you (or presumably your cron script) take to do so, I could probably muddle through it despite not really being much of a programmer.

I'll also have to see how the tool plays with plugins, since I'm using it for more than just raw text. But it should be fine if Etherpump provides proper HTML; I'll mention if there's any issues on that front.

So I wanted to confirm that this is working as intended, as far as I can tell. It's pulling things, indexing them, reading the magic words, etcetera. However, right now... it feels a lot like this is made very fit-to-purpose for Constant and Varia. While the tools to make something more suited to another use case appear to be in place, currently it's very fuzzy to me how to get there, and the existing files reference CSS files that aren't copied into place. Ideally, I'd want a page very similar in strucutre to https://etherpump.vvvvvvaria.org/, which is what got me into wanting to use the tool in the first place, but I'm not sure how that page is created using etherpump. I assume it's a particular custom template and set of commands that produces that page, so if I had some idea of the steps you (or presumably your cron script) take to do so, I could probably muddle through it despite not really being much of a programmer. I'll also have to see how the tool plays with plugins, since I'm using it for more than just raw text. But it should be fine if Etherpump provides proper HTML; I'll mention if there's any issues on that front.
Owner

Hey @queenfiona thanks for reporting in, happy to hear things are working.

Totally get you on the lack of documentation/context. Etherpump is actually a very generic tool and that also makes it a bit overwhelming - there are so many options! We're definitely lacking some tutorial stuff. I'm taking notes in #18.

In the meantime, here is some code (script, stylesheet, etc.) which was used to make the etherpump Varia page https://git.vvvvvvaria.org/varia/etherpump-live (I am not sure if this is super up-to-date but it should get you started!).

re: plugins, please raise an issue on what you run into! I was experimenting with storing also comments via the comments plugin but didn't get far (see #7). It is all totally possible though and would be interesting to support.

Hey @queenfiona thanks for reporting in, happy to hear things are working. Totally get you on the lack of documentation/context. Etherpump is actually a very generic tool and that also makes it a bit overwhelming - there are so many options! We're definitely lacking some tutorial stuff. I'm taking notes in #18. In the meantime, here is some code (script, stylesheet, etc.) which was used to make the etherpump Varia page https://git.vvvvvvaria.org/varia/etherpump-live (I am not sure if this is super up-to-date but it should get you started!). re: plugins, please raise an issue on what you run into! I was experimenting with storing also comments via the comments plugin but didn't get far (see https://git.vvvvvvaria.org/varia/etherpump/issues/7). It is all totally possible though and would be interesting to support.
Sign in to join this conversation.
No Label
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: varia/etherpump#18
No description provided.