diff --git a/amalgamate.py b/amalgamate.py index 07375d6a..ab0bd3ca 100644 --- a/amalgamate.py +++ b/amalgamate.py @@ -9,7 +9,7 @@ pipeline = [ ["config.h", "common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h", "lexer.h"], ["obj.h", "dict.h", "codeobject.h", "frame.h"], ["gc.h", "vm.h", "ceval.h", "expr.h", "compiler.h", "repl.h"], - ["_generated.h", "cffi.h", "iter.h", "base64.h", "random.h", "re.h", "linalg.h", "easing.h", "requests.h", "io.h"], + ["_generated.h", "cffi.h", "iter.h", "base64.h", "random.h", "re.h", "linalg.h", "easing.h", "io.h"], ["export.h", "pocketpy.h"] ] diff --git a/docs/modules/c.md b/docs/modules/c.md index 1303f296..e04481a3 100644 --- a/docs/modules/c.md +++ b/docs/modules/c.md @@ -73,9 +73,79 @@ class void_p: def set_base_offset(self, offset: str) -> None: ... class struct: + @overload + def __init__(self, size: int): ... + @overload + def __init__(self, p: 'void_p', size: int): ... + @overload + def __init__(self, s: str): ... + @overload + def __init__(self, b: bytes): ... + def addr(self) -> 'void_p': ... def copy(self) -> 'struct': ... def size(self) -> int: ... def __eq__(self, other: 'struct') -> bool: ... def __ne__(self, other: 'struct') -> bool: ... + + def to_string(self) -> str: ... + def to_bytes(self) -> bytes: ... + + def read_char(self, offset=0) -> int: ... + def read_uchar(self, offset=0) -> int: ... + def read_short(self, offset=0) -> int: ... + def read_ushort(self, offset=0) -> int: ... + def read_int(self, offset=0) -> int: ... + def read_uint(self, offset=0) -> int: ... + def read_long(self, offset=0) -> int: ... + def read_ulong(self, offset=0) -> int: ... + def read_longlong(self, offset=0) -> int: ... + def read_ulonglong(self, offset=0) -> int: ... + def read_float(self, offset=0) -> float: ... + def read_double(self, offset=0) -> float: ... + def read_bool(self, offset=0) -> bool: ... + def read_void_p(self, offset=0) -> 'void_p': ... + + def write_char(self, value: int, offset=0) -> None: ... + def write_uchar(self, value: int, offset=0) -> None: ... + def write_short(self, value: int, offset=0) -> None: ... + def write_ushort(self, value: int, offset=0) -> None: ... + def write_int(self, value: int, offset=0) -> None: ... + def write_uint(self, value: int, offset=0) -> None: ... + def write_long(self, value: int, offset=0) -> None: ... + def write_ulong(self, value: int, offset=0) -> None: ... + def write_longlong(self, value: int, offset=0) -> None: ... + def write_ulonglong(self, value: int, offset=0) -> None: ... + def write_float(self, value: float, offset=0) -> None: ... + def write_double(self, value: float, offset=0) -> None: ... + def write_bool(self, value: bool, offset=0) -> None: ... + def write_void_p(self, value: 'void_p', offset=0) -> None: ... + +char_ = refl("char") +uchar_ = refl("uchar") +short_ = refl("short") +ushort_ = refl("ushort") +int_ = refl("int") +uint_ = refl("uint") +long_ = refl("long") +ulong_ = refl("ulong") +longlong_ = refl("longlong") +ulonglong_ = refl("ulonglong") +float_ = refl("float") +double_ = refl("double") +bool_ = refl("bool") + +char_p = void_p +uchar_p = void_p +short_p = void_p +ushort_p = void_p +int_p = void_p +uint_p = void_p +long_p = void_p +ulong_p = void_p +longlong_p = void_p +ulonglong_p = void_p +float_p = void_p +double_p = void_p +bool_p = void_p ``` \ No newline at end of file diff --git a/docs/modules/requests.md b/docs/modules/requests.md deleted file mode 100644 index 3219881d..00000000 --- a/docs/modules/requests.md +++ /dev/null @@ -1,26 +0,0 @@ ---- -icon: package -label: requests ---- - -!!! -This module is experimental. To enable it, download `httplib.h` from [here](https://github.com/yhirose/cpp-httplib) and place it in the same directory as `pocketpy.h`. Also set `PK_MODULE_REQUESTS` to `1` in `config.h`. - -SSL is not supported. -!!! - -### `requests.get(url, headers=None) -> Response` - -Send a GET request to `url` and return a `Response` object. - -### `requests.post(url, data=None, headers=None) -> Response` - -Send a POST request to `url` and return a `Response` object. - -### `requests.put(url, data=None, headers=None) -> Response` - -Send a PUT request to `url` and return a `Response` object. - -### `requests.delete(url, headers=None) -> Response` - -Send a DELETE request to `url` and return a `Response` object. \ No newline at end of file diff --git a/python/requests.py b/python/requests.py deleted file mode 100644 index 2ebf35fb..00000000 --- a/python/requests.py +++ /dev/null @@ -1,40 +0,0 @@ -class Response: - def __init__(self, status_code, reason, content): - self.status_code = status_code - self.reason = reason - self.content = content - - assert type(self.status_code) is int - assert type(self.reason) is str - assert type(self.content) is bytes - - @property - def text(self): - return self.content.decode() - - def __repr__(self): - code = self.status_code - return f'' - -def _parse_h(headers): - if headers is None: - return [] - if type(headers) is dict: - return list(headers.items()) - raise ValueError('headers must be dict or None') - -def get(url, headers=None): - headers = _parse_h(headers) - return _request('GET', url, headers, None) - -def post(url, data: bytes, headers=None): - headers = _parse_h(headers) - return _request('POST', url, headers, data) - -def put(url, data: bytes, headers=None): - headers = _parse_h(headers) - return _request('PUT', url, headers, data) - -def delete(url, headers=None): - headers = _parse_h(headers) - return _request('DELETE', url, headers, None) \ No newline at end of file diff --git a/src/config.h b/src/config.h index e2e41b10..1ee91ba9 100644 --- a/src/config.h +++ b/src/config.h @@ -91,6 +91,5 @@ namespace pkpy{ #define PK_MODULE_BASE64 1 #define PK_MODULE_LINALG 1 #define PK_MODULE_EASING 1 -#define PK_MODULE_REQUESTS 0 #endif \ No newline at end of file diff --git a/src/pocketpy.h b/src/pocketpy.h index 266dba49..79c9073c 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -9,7 +9,6 @@ #include "cffi.h" #include "linalg.h" #include "easing.h" -#include "requests.h" #include "io.h" #include "_generated.h" #include "export.h" @@ -1506,7 +1505,6 @@ inline void VM::post_init(){ if(enable_os){ add_module_io(this); add_module_os(this); - add_module_requests(this); _import_handler = _default_import_handler; } diff --git a/src/requests.h b/src/requests.h deleted file mode 100644 index 4afdc8b6..00000000 --- a/src/requests.h +++ /dev/null @@ -1,102 +0,0 @@ -#pragma once - -#include "common.h" - -#if PK_MODULE_REQUESTS -#include "httplib.h" -#include "cffi.h" -#include "_generated.h" - -namespace pkpy { - -inline void add_module_requests(VM* vm){ - static StrName m_requests("requests"); - static StrName m_Response("Response"); - PyObject* mod = vm->new_module(m_requests); - CodeObject_ code = vm->compile(kPythonLibs["requests"], "requests.py", EXEC_MODE); - vm->_exec(code, mod); - - vm->bind_func<4>(mod, "_request", [](VM* vm, ArgsView args){ - Str method = CAST(Str&, args[0]); - Str url = CAST(Str&, args[1]); - PyObject* headers = args[2]; // a dict object - PyObject* body = args[3]; // a bytes object - - if(url.index("http://") != 0){ - vm->ValueError("url must start with http://"); - } - - for(char c: url){ - switch(c){ - case '.': - case '-': - case '_': - case '~': - case ':': - case '/': - break; - default: - if(!isalnum(c)){ - vm->ValueError(fmt("invalid character in url: '", c, "'")); - } - } - } - - int slash = url.index("/", 7); - Str path = "/"; - if(slash != -1){ - path = url.substr(slash); - url = url.substr(0, slash); - if(path.empty()) path = "/"; - } - - httplib::Client client(url.str()); - - httplib::Headers h; - if(headers != vm->None){ - List list = CAST(List&, headers); - for(auto& item : list){ - Tuple t = CAST(Tuple&, item); - Str key = CAST(Str&, t[0]); - Str value = CAST(Str&, t[1]); - h.emplace(key.str(), value.str()); - } - } - - auto _to_resp = [=](const httplib::Result& res){ - return vm->call( - vm->_modules[m_requests]->attr(m_Response), - VAR(res->status), - VAR(res->reason), - VAR(Bytes(res->body)) - ); - }; - - if(method == "GET"){ - httplib::Result res = client.Get(path.str(), h); - return _to_resp(res); - }else if(method == "POST"){ - Bytes b = CAST(Bytes&, body); - httplib::Result res = client.Post(path.str(), h, b.data(), b.size(), "application/octet-stream"); - return _to_resp(res); - }else if(method == "PUT"){ - Bytes b = CAST(Bytes&, body); - httplib::Result res = client.Put(path.str(), h, b.data(), b.size(), "application/octet-stream"); - return _to_resp(res); - }else if(method == "DELETE"){ - httplib::Result res = client.Delete(path.str(), h); - return _to_resp(res); - }else{ - vm->ValueError("invalid method"); - } - UNREACHABLE(); - }); -} - -} // namespace pkpy - -#else - -ADD_MODULE_PLACEHOLDER(requests) - -#endif \ No newline at end of file