Skip to content

Commit 046f761

Browse files
Update app.py
1 parent 1fbb85a commit 046f761

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/view/app.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,15 +1371,28 @@ def finalizer():
13711371

13721372
weakref.finalize(app, finalizer)
13731373

1374-
if store:
1375-
_last_app = app
1374+
if store_address:
1375+
os.environ["_VIEW_APP_ADDRESS"] = str(id(app))
1376+
# id() on cpython returns the address, but it is
1377+
# implementation dependent however, view.py
1378+
# only supports cpython anyway
13761379

13771380
return app
13781381

13791382

1380-
def get_app() -> App:
1383+
# this is forbidden pointers.py technology
1384+
1385+
ctypes.pythonapi.Py_IncRef.argtypes = (ctypes.py_object,)
1386+
1387+
1388+
def get_app(*, address: int | None = None) -> App:
13811389
"""Get the last app created by `new_app`."""
1382-
if not _last_app:
1383-
raise RuntimeError("no app has been set")
1390+
env = os.environ.get("_VIEW_APP_ADDRESS")
1391+
addr = address or env
13841392

1385-
return _last_app
1393+
if (not addr) and (not env):
1394+
raise BadEnvironmentError("no view app registered")
1395+
1396+
app: App = ctypes.cast(int(addr), ctypes.py_object).value # type: ignore
1397+
ctypes.pythonapi.Py_IncRef(app)
1398+
return app

0 commit comments

Comments
 (0)