microsoft / microsoft/pxt-microbit

forever function does not have write-access on top-level variables in Python

Open
#3,381 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Error Message P2 python
Dominant language
TypeScript
Stars
804
Forks
721
Avg merge
2d 9h
Merged PRs (30d)
2

Description

Describe the bug
In python:
The function passed to basic.forever can read top-level variables, but not modify. Trying to do so makes the function read the variable as NaN.

To Reproduce

my_var = 2
def f():
    basic.show_number(my_var)
    my_var = my_var+1      # Offending line
    basic.show_number(my_var)
    basic.pause(1000000)
basic.forever(f)

Running as is will print NaN twice. Removing the offending line prints the correct value twice. This error occurs even when the offending line is changed to my_var = 1, i.e. simple assignment.

Expected behavior
The above code to print 2, then 3.

Desktop (please complete the following information):

  • OS: Linux
  • Browser: Mozilla Firefox 80.0.1

Additional context
Happens on master branch, and in live website.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how Python code passed to basic.forever is translated and how assignments to top-level variables are handled. Use the reproduction in this issue, including both my_var = my_var + 1 and simple assignment, and consider the work done when the function can read and update my_var so it displays 2 and then 3.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.