Skip to content

Commit

Permalink
Merge pull request #30 from jepler/make-tests-pass
Browse files Browse the repository at this point in the history
Fix uses of 'await sleep(0)'
  • Loading branch information
dhalbert authored Oct 18, 2022
2 parents 3e8c50e + d81e6aa commit fbdb77d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ jobs:
if: contains(steps.need-pypi.outputs.pyproject-toml, 'pyproject.toml')
run: |
pip install --upgrade build twine
for file in $(find -not -path "./.*" -not -path "./docs*" \( -name "*.py" -o -name "*.toml" \) ); do
sed -i -e "s/0.0.0+auto.0/1.2.3/" $file;
done;
find -type f -not -path "./.*" -not -path "./docs*" \( -name "*.py" -o -name "*.toml" \) -exec sed -i -e "s/0.0.0+auto.0/1.2.3/" {} +
python -m build
twine check dist/*
4 changes: 1 addition & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ jobs:
TWINE_USERNAME: ${{ secrets.pypi_username }}
TWINE_PASSWORD: ${{ secrets.pypi_password }}
run: |
for file in $(find -not -path "./.*" -not -path "./docs*" \( -name "*.py" -o -name "*.toml" \) ); do
sed -i -e "s/0.0.0+auto.0/${{github.event.release.tag_name}}/" $file;
done;
find -type f -not -path "./.*" -not -path "./docs*" \( -name "*.py" -o -name "*.toml" \) -exec sed -i -e "s/0.0.0+auto.0/${{github.event.release.tag_name}}/" {} +
python -m build
twine upload dist/*
29 changes: 28 additions & 1 deletion asyncio/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def __next__(self):
self.exc.__traceback__ = None
raise self.exc


# Pause task execution for the given time (integer in milliseconds, uPy extension)
# Use a SingletonGenerator to do it without allocating on the heap
def sleep_ms(t, sgen=SingletonGenerator()):
Expand All @@ -93,6 +92,34 @@ def sleep(t):
return sleep_ms(int(t * 1000))


################################################################################
# "Never schedule" object"
# Don't re-schedule the object that awaits the _never singleton.
# For internal use only. Some constructs, like `await event.wait()`,
# work by NOT re-scheduling the task which calls wait(), but by
# having some other task schedule it later.
class _Never:
def __init__(self):
self.state = None
self.exc = StopIteration()

def __iter__(self):
return self

def __await__(self):
return self

def __next__(self):
if self.state is not None:
self.state = None
return None
else:
self.exc.__traceback__ = None
raise self.exc

_never = _Never()


################################################################################
# Queue and poller for stream IO

Expand Down
3 changes: 2 additions & 1 deletion asyncio/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ async def wait(self):
self.waiting.push_head(core.cur_task)
# Set calling task's data to the event's queue so it can be removed if needed
core.cur_task.data = self.waiting
await core.sleep(0)
core._never.state = False
await core._never
return True


Expand Down
2 changes: 1 addition & 1 deletion asyncio/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async def gather(*aws, return_exceptions=False):
# # cancel all waiting tasks
# raise er
ts[i] = await ts[i]
except Exception as er:
except (core.CancelledError, Exception) as er:
if return_exceptions:
ts[i] = er
else:
Expand Down
3 changes: 2 additions & 1 deletion asyncio/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ async def acquire(self):
# Set calling task's data to the lock's queue so it can be removed if needed
core.cur_task.data = self.waiting
try:
await core.sleep(0)
core._never.state = False
await core._never
except core.CancelledError as er:
if self.state == core.cur_task:
# Cancelled while pending on resume, schedule next waiting Task
Expand Down

0 comments on commit fbdb77d

Please sign in to comment.