Skip to content

Commit

Permalink
3.12 + cochonnet basic aio support
Browse files Browse the repository at this point in the history
  • Loading branch information
pmp-p committed Oct 3, 2023
1 parent 6fef0c9 commit 80db3a3
Show file tree
Hide file tree
Showing 14 changed files with 632 additions and 482 deletions.
23 changes: 12 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
build:
runs-on: ubuntu-22.04
env:
SDK_VERSION: 3.1.46.2bi
SDK_VERSION: 3.1.47.0bi
SYS_PYTHON: /usr/bin/python3
PACKAGES: emsdk hpy _ctypes pygame
BUILD_STATIC: emsdk _ctypes hpy
Expand Down Expand Up @@ -47,16 +47,17 @@ jobs:
cd $GITHUB_WORKSPACE
PYBUILD=3.11 bash ./scripts/build-loader.sh
# - name: build 3.12 packages
# run: |
# cd $GITHUB_WORKSPACE
# PYBUILD=3.12 bash ./scripts/build-pkg.sh
#
# - name: build 3.12 loader
# run: |
# cd $GITHUB_WORKSPACE
# PYBUILD=3.12 bash ./scripts/build-loader.sh
#
- name: build 3.12 packages
run: |
cd $GITHUB_WORKSPACE
PYBUILD=3.12 BUILD_STATIC="emsdk hpy" bash ./scripts/build-pkg.sh
- name: build 3.12 loader
run: |
cd $GITHUB_WORKSPACE
PYBUILD=3.12 BUILD_STATIC="emsdk hpy" bash ./scripts/build-loader.sh
- name: publishing
run: |
Expand Down
2 changes: 1 addition & 1 deletion pygbag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from pathlib import Path

__version__ = "0.8.2"
__version__ = "0.8.3"

# hack to test git cdn build without upgrading pygbag
# beware can have side effects when file packager behaviour must change !
Expand Down
20 changes: 11 additions & 9 deletions pygbag/support/cross/__EMSCRIPTEN__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ def shed_yield():
return True


sys.breakpointhook = breakpointhook

this = __import__(__name__)

# those __dunder__ are usually the same used in C conventions.

try:
__UPY__
except:
if hasattr(sys.implementation, "mpy"):
if hasattr(sys.implementation, "_mpy"):
builtins.__UPY__ = this
else:
builtins.__UPY__ = None
Expand All @@ -41,22 +39,25 @@ def shed_yield():
if __UPY__:
sys.modules["sys"] = sys
sys.modules["builtins"] = builtins
try:
from . import uasyncio as uasyncio

print("Warning : using WAPY uasyncio")
except Exception as e:
sys.print_exception(e)
# try:
# from . import uasyncio as uasyncio
#
# print("Warning : using WAPY uasyncio")
# except Exception as e:
# sys.print_exception(e)

else:
sys.breakpointhook = breakpointhook

# fallback to asyncio based implementation
try:
from . import uasyncio_cpy as uasyncio
except:
pdb("INFO: no uasyncio implementation found")
uasyncio = aio

sys.modules["uasyncio"] = uasyncio
sys.modules["uasyncio"] = uasyncio


# detect if cpython is really running on a emscripten browser
Expand Down Expand Up @@ -97,6 +98,7 @@ async def jsprom(prom):

else:
is_browser = False
pdb("101: no emscripten browser interface")
builtins.__EMSCRIPTEN__ = None


Expand Down
39 changes: 27 additions & 12 deletions pygbag/support/cross/aio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
import builtins
import inspect
from time import time as time_time



DEBUG = True
Expand Down Expand Up @@ -33,17 +33,25 @@ def pdb(*argv):
# cascade debug by default
from . import cross

cross.DEBUG = DEBUG
if not __UPY__:
from time import time as time_time

# file+socket support fopen/sopen
from .filelike import *
else:
import utime
time_time = utime.ticks_ms


cross.DEBUG = DEBUG

# file+socket support fopen/sopen
from .filelike import *

# =========================================================================

# TODO: dbg stuff should be in the platform module in aio.cross
# usefull https://pymotw.com/3/sys/tracing.html
if DEBUG:
# upy has no trace module
if DEBUG and not __UPY__:
import trace

_tracer = trace.Trace(count=False, trace=True)
Expand Down Expand Up @@ -139,12 +147,17 @@ def overloaded(i, *attrs):
spent = 0.00001
leave = enter + spent


from asyncio import *

__run__ = run

import asyncio.events as events
if __UPY__:
def _set_running_loop(l):pass
sys.modules['asyncio.events'] = aio
aio.get_running_loop = aio.get_event_loop
events = aio
else:
from asyncio.events import _set_running_loop


# Within a coroutine, simply use `asyncio.get_running_loop()`,
Expand All @@ -157,9 +170,9 @@ def overloaded(i, *attrs):
loop = get_event_loop()


import asyncio.events

asyncio.events._set_running_loop(loop)
#import asyncio.events
#asyncio.events._set_running_loop(loop)
_set_running_loop(loop)


sys.modules["asyncio"] = __import__(__name__)
Expand Down Expand Up @@ -390,7 +403,7 @@ async def __main__():

# fallback to blocking asyncio
else:
asyncio.events._set_running_loop(None)
_set_running_loop(None)
# TODO: implement RaF from here
try:
loop.run_forever()
Expand Down Expand Up @@ -461,7 +474,9 @@ async def atexit(coro):
exit_now(maybecoro)


sys.exit = aio_exit
if not __UPY__:
sys.exit = aio_exit


# check if we have a Time handler.
try:
Expand Down
54 changes: 30 additions & 24 deletions pygbag/support/cross/aio/cross.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,39 @@
#
platform_impl = False

# that sym cannot be overloaded in a simulator
# ================== leverage known python implementations ====================

if not defined("__WASM__"):
if __import__("os").uname().machine.startswith("wasm"):
import __WASM__
else:
__WASM__ = False
# always come down to upy choices because cpython syntax can more easily be adapted

define("__WASM__", __WASM__)

if not defined("__UPY__"):
define("__UPY__", hasattr(sys, "print_exception") )

if not __UPY__:
# setup exception display with same syntax as upy
import traceback

def print_exception(e, out=sys.stderr, **kw):
kw["file"] = out
traceback.print_exc(**kw)

# those can
sys.print_exception = print_exception
del print_exception



if not defined("__WASM__"):
try:
# that sym cannot be overloaded in the simulator
if __import__("os").uname().machine.startswith("wasm"):
import __WASM__
else:
__WASM__ = False
except AttributeError:
# upy does not have os.uname
__WASM__ = True

define("__WASM__", __WASM__)


if not defined("__wasi__"):
Expand All @@ -29,19 +50,10 @@
else:
__wasi__ = False

# setup exception display with same syntax as upy
import traceback

def print_exception(e, out=sys.stderr, **kw):
kw["file"] = out
traceback.print_exc(**kw)

sys.print_exception = print_exception
del print_exception

define("__wasi__", __wasi__)



# this *is* the cpython way
if hasattr(sys, "getandroidapilevel"):
platform_impl = defined("__ANDROID__")
Expand Down Expand Up @@ -121,13 +133,7 @@ def print_exception(e, out=sys.stderr, **kw):
sys.modules["platform"] = platform_impl


# ================== leverage known python implementations ====================

# always come down to upy choices because cpython syntax can more easily be adapted


if not defined("__UPY__"):
define("__UPY__", hasattr(sys.implementation, "mpy"))


if not __UPY__:
Expand Down
16 changes: 12 additions & 4 deletions pygbag/support/cross/aio/filelike.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import sys
import io
import socket

if not __UPY__:
import socket
socket.setdefaulttimeout(0.0)
else:
print("7: usocket implementation required")


import os # unlink

socket.setdefaulttimeout(0.0)


import aio
import platform
Expand Down Expand Up @@ -51,21 +57,23 @@ def mktemp(suffix=""):


class fopen:
if __WASM__:
if __WASM__ and hasattr(platform, "ffi"):

flags = platform.ffi(
{
"redirect": "follow",
"credentials": "omit",
}
)
else:
print("69: platform has no object serializer")
flags = {}

def __init__(self, maybe_url, mode="r", flags=None):
self.url = fix_url(maybe_url)
self.mode = mode
flags = flags or self.__class__.flags
print(f'68: fopen: fetching "{self.url}"')
print(f'76: fopen: fetching "{self.url}"')

self.tmpfile = None

Expand Down
Loading

0 comments on commit 80db3a3

Please sign in to comment.