Skip to content

Commit

Permalink
Still broken.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroIntensity committed Aug 8, 2024
1 parent 9c20364 commit b9b43de
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 352 deletions.
16 changes: 4 additions & 12 deletions hatch.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,23 @@ enable-by-default = true
[build.targets.wheel.hooks.scikit-build]
experimental = true

[build.targets.wheel.hooks.scikit-build.cmake]
verbose = true
build-type = "RelWithDebInfo"
source-dir = "."
[envs.foo.scripts]
x = "python -c 'import view'"

[envs.test]
[envs.hatch-test]
features = ["full"]
dev-mode = false
dependencies = [
"coverage",
"pytest",
"pytest-memray",
"pytest-asyncio",
"pytest-rich",
]
platforms = ["linux", "macos"]

[envs.test.overrides]
[envs.hatch-test.overrides]
platform.windows.dependencies = [
"coverage",
"pytest",
"pytest-asyncio",
]

[envs.test.scripts]
no-cov = "pytest"
memray = "pytest --enable-leak-tracking"
cov = "coverage run -m pytest"
22 changes: 14 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["scikit-build-core>=0.9.0", "hatchling", "pyawaitable"]
requires = ["scikit-build-core>=0.9.0", "hatchling>=1", "pyawaitable==1.2.0"]
build-backend = "hatchling.build"

[project]
Expand All @@ -21,14 +21,15 @@ classifiers = [
"Programming Language :: Python :: Implementation :: CPython",
]
dependencies = [
"rich",
"click",
"rich>=13",
"click>=8",
"typing_extensions",
"ujson",
"configzen",
"aiofiles",
"prompts.py",
"pyawaitable"
"ujson>=5",
"pydantic_settings>=2",
"toml~=0.10",
"aiofiles>=24",
"prompts.py~=0.1",
"pyawaitable==1.2.0"
]
dynamic = ["version", "license"]

Expand Down Expand Up @@ -72,3 +73,8 @@ Funding = "https://github.com/sponsors/ZeroIntensity"
[project.scripts]
view = "view.__main__:main"
view-py = "view.__main__:main"

[tool.scikit-build]
cmake.verbose = true
cmake.build-type = "RelWithDebInfo"
cmake.source-dir = "."
11 changes: 11 additions & 0 deletions src/_view/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ show_error(bool dev)
Py_INCREF(err); // Save a reference to it
PyErr_SetRaisedException(err);
PyErr_Print();
// PyErr_Print() clears the error indicator, so
// we need to reset it.
PyErr_SetRaisedException(err);
} else PyErr_Clear();
}
Expand Down Expand Up @@ -699,6 +701,7 @@ route_error(
PyObject *err
)
{
puts("1");
if (((PyObject *) Py_TYPE(err)) == ws_disconnect_err)
{
// the socket prematurely disconnected, let's complain about it
Expand Down Expand Up @@ -740,6 +743,7 @@ route_error(
PyObject *send;
bool handler_was_called;

puts("2");
if (
PyAwaitable_UnpackValues(
awaitable,
Expand Down Expand Up @@ -770,8 +774,10 @@ route_error(
if (PyAwaitable_UnpackIntValues(awaitable, &is_http) < 0)
return -1;

puts("3");
if (((PyObject *) Py_TYPE(err)) == self->error_type)
{
puts("4");
PyObject *status_obj = PyObject_GetAttrString(
err,
"status"
Expand All @@ -798,6 +804,7 @@ route_error(
return -2;
}

puts("5");
const char *message = NULL;

if (msg_obj != Py_None)
Expand Down Expand Up @@ -831,6 +838,7 @@ route_error(

Py_DECREF(status_obj);
Py_DECREF(msg_obj);
puts("6");
return 0;
}

Expand Down Expand Up @@ -894,6 +902,7 @@ route_error(
return 0;
}

puts("7");
if (
server_err_exc(
self,
Expand All @@ -909,12 +918,14 @@ route_error(
return -1;
}

puts("8");
if (!handler_was_called)
{
PyErr_SetRaisedException(err);
PyErr_Print();
}

puts("9");
return 0;
}

Expand Down
6 changes: 4 additions & 2 deletions src/_view/handling.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@

#include <pyawaitable.h>

#define INITIAL_BUF_SIZE 1024
// NOTE: This should be below 512 for PyMalloc to be effective
// on the first call.
#define INITIAL_BUF_SIZE 256

/*
* Call a route object with both query and body parameters.
Expand Down Expand Up @@ -274,7 +276,7 @@ handle_route(PyObject *awaitable, char *query)
PyAwaitable_SaveArbValues(
aw,
4,
&buf,
buf,
size,
used,
query
Expand Down
37 changes: 16 additions & 21 deletions src/_view/inputs.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* view.py route inputs implementation
*
* This file is responsible for parsing route inputs through query
// * This file is responsible for parsing route inputs through query
* strings and body parameters.
*
* If a route has no inputs, then the parsing
Expand Down Expand Up @@ -70,7 +70,6 @@ body_inc_buf(PyObject *awaitable, PyObject *result)
);
if (!more_body)
{
Py_DECREF(body);
return PyErr_BadASGI();
}

Expand All @@ -85,12 +84,10 @@ body_inc_buf(PyObject *awaitable, PyObject *result)
) < 0
)
{
Py_DECREF(body);
Py_DECREF(more_body);
return -1;
}

char **buf;
char *buf;
Py_ssize_t *size;
Py_ssize_t *used;
char *query;
Expand All @@ -105,26 +102,28 @@ body_inc_buf(PyObject *awaitable, PyObject *result)
) < 0
)
{
Py_DECREF(body);
Py_DECREF(more_body);
return -1;
}

char *nbuf = *buf;
char *nbuf = buf;
bool needs_realloc = false;

while (((*used) + buf_inc_size) > (*size))
{
// The buffer would overflow, we need to reallocate it
*size *= 2;
needs_realloc = true;
}

if (needs_realloc)
{
nbuf = PyMem_Realloc(
buf,
(*size)
);

if (!nbuf)
{
Py_DECREF(body);
Py_DECREF(more_body);
PyErr_NoMemory();
return -1;
}
Expand All @@ -136,8 +135,11 @@ body_inc_buf(PyObject *awaitable, PyObject *result)
buf_inc_size
);
*used += buf_inc_size;
// TODO: Add SetArbValue here
*buf = nbuf;
PyAwaitable_SetArbValue(
awaitable,
0,
nbuf
);

PyObject *aw;
PyObject *receive;
Expand All @@ -150,8 +152,6 @@ body_inc_buf(PyObject *awaitable, PyObject *result)
) < 0
)
{
Py_DECREF(more_body);
Py_DECREF(body);
return -1;
}

Expand All @@ -168,8 +168,6 @@ body_inc_buf(PyObject *awaitable, PyObject *result)
) < 0
)
{
Py_DECREF(more_body);
Py_DECREF(body);
Py_DECREF(receive_coro);
PyMem_Free(query);
PyMem_Free(nbuf);
Expand All @@ -187,15 +185,12 @@ body_inc_buf(PyObject *awaitable, PyObject *result)
) < 0
)
{
Py_DECREF(more_body);
Py_DECREF(body);
PyMem_Free(nbuf);
return -1;
}
}

Py_DECREF(more_body);
Py_DECREF(body);
PyMem_Free(nbuf);
}

return 0;
}
Expand Down
1 change: 0 additions & 1 deletion src/view/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def index():
from .default_page import *
from .exceptions import *
from .integrations import *
from .logging import *
from .patterns import *
from .response import *
from .routing import *
Expand Down
40 changes: 1 addition & 39 deletions src/view/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import ujson
from rich import print
from rich.traceback import install
from typing_extensions import ParamSpec, TypeAlias, Unpack
from typing_extensions import ParamSpec, TypeAlias

from _view import InvalidStatusError, ViewApp, register_ws_cls

Expand All @@ -70,7 +70,6 @@
ViewInternalError,
WebSocketDisconnectError,
)
from .logging import _LogArgs, log
from .response import HTML
from .routing import Path as _RouteDeco
from .routing import (
Expand Down Expand Up @@ -918,43 +917,6 @@ async def index():
path, doc, cache_rate, options, steps, parallel_build
)

def _set_log_arg(self, kwargs: _LogArgs, key: str) -> None:
if key not in kwargs:
kwargs[key] = getattr(self.config.log.user, key) # type: ignore

def _splat_log_args(self, kwargs: _LogArgs) -> _LogArgs:
self._set_log_arg(kwargs, "log_file")
self._set_log_arg(kwargs, "show_time")
self._set_log_arg(kwargs, "show_caller")
self._set_log_arg(kwargs, "show_color")
self._set_log_arg(kwargs, "show_urgency")
self._set_log_arg(kwargs, "file_write")
self._set_log_arg(kwargs, "strftime")

if "caller_frame" not in kwargs:
frame = inspect.currentframe()
assert frame, "failed to get frame"
back = frame.f_back
assert back, "frame has no f_back"
kwargs["caller_frame"] = back

return kwargs

def debug(self, *messages: object, **kwargs: Unpack[_LogArgs]) -> None:
log(*messages, urgency="debug", **self._splat_log_args(kwargs))

def info(self, *messages: object, **kwargs: Unpack[_LogArgs]) -> None:
log(*messages, urgency="info", **self._splat_log_args(kwargs))

def warning(self, *messages: object, **kwargs: Unpack[_LogArgs]) -> None:
log(*messages, urgency="warning", **self._splat_log_args(kwargs))

def error(self, *messages: object, **kwargs: Unpack[_LogArgs]) -> None:
log(*messages, urgency="error", **self._splat_log_args(kwargs))

def critical(self, *messages: object, **kwargs: Unpack[_LogArgs]) -> None:
log(*messages, urgency="critical", **self._splat_log_args(kwargs))

def query(
self,
name: str,
Expand Down
Loading

0 comments on commit b9b43de

Please sign in to comment.