-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e942937
commit fd63c00
Showing
8 changed files
with
231 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,11 @@ | |
__pycache__/ | ||
.venv/ | ||
|
||
# Builds | ||
*.egg-info | ||
test/ | ||
dist/ | ||
pyawaitable-vendor/ | ||
|
||
# LSP | ||
compile_flags.txt | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#ifndef PYAWAITABLE_VENDOR_H | ||
#define PYAWAITABLE_VENDOR_H | ||
|
||
#include <Python.h> | ||
#include <pyawaitable/awaitableobject.h> | ||
#include <pyawaitable/genwrapper.h> | ||
|
||
/* | ||
* vendor.h is only for use by the vendor build tool, don't use it manually! | ||
* (If you're seeing this message from a vendored copy, you're fine) | ||
*/ | ||
|
||
#define PYAWAITABLE_ADD_TYPE(m, tp) \ | ||
do \ | ||
{ \ | ||
Py_INCREF(&tp); \ | ||
if (PyType_Ready(&tp) < 0) { \ | ||
Py_DECREF(&tp); \ | ||
return -1; \ | ||
} \ | ||
if (PyModule_AddObject(m, #tp, (PyObject *)&tp) < 0) { \ | ||
Py_DECREF(&tp); \ | ||
return -1; \ | ||
} \ | ||
} while (0) | ||
|
||
#define PYAWAITABLE_MAJOR_VERSION 1 | ||
#define PYAWAITABLE_MINOR_VERSION 0 | ||
#define PYAWAITABLE_MICRO_VERSION 0 | ||
#define PYAWAITABLE_RELEASE_LEVEL 0xF | ||
|
||
#ifdef PYAWAITABLE_PYAPI | ||
#define PyAwaitable_New pyawaitable_new | ||
#define PyAwaitable_AddAwait pyawaitable_await | ||
#define PyAwaitable_Cancel pyawaitable_cancel | ||
#define PyAwaitable_SetResult pyawaitable_set_result | ||
#define PyAwaitable_SaveValues pyawaitable_save | ||
#define PyAwaitable_SaveArbValues pyawaitable_save_arb | ||
#define PyAwaitable_UnpackValues pyawaitable_unpack | ||
#define PyAwaitable_UnpackArbValues pyawaitable_unpack_arb | ||
#define PyAwaitable_Init pyawaitable_init | ||
#define PyAwaitable_ABI pyawaitable_abi | ||
#define PyAwaitable_Type PyAwaitableType | ||
#define PyAwaitable_AwaitFunction pyawaitable_await_function | ||
#endif | ||
|
||
static int | ||
pyawaitable_init() | ||
{ | ||
PyErr_SetString( | ||
PyExc_SystemError, | ||
"cannot use pyawaitable_init from a vendored copy, use pyawaitable_vendor_init instead!" | ||
); | ||
return -1; | ||
} | ||
|
||
static void | ||
close_pool(PyObject *Py_UNUSED(capsule)) | ||
{ | ||
dealloc_awaitable_pool(); | ||
} | ||
|
||
static int | ||
pyawaitable_vendor_init(PyObject *mod) | ||
{ | ||
PYAWAITABLE_ADD_TYPE(mod, _PyAwaitableType); | ||
PYAWAITABLE_ADD_TYPE(mod, _PyAwaitableGenWrapperType); | ||
|
||
PyObject *capsule = PyCapsule_New( | ||
NULL, | ||
"_pyawaitable.__do_not_touch", | ||
close_pool | ||
); | ||
|
||
if (!capsule) | ||
{ | ||
return -1; | ||
} | ||
|
||
if (PyModule_AddObject(mod, "__do_not_touch", capsule) < 0) | ||
{ | ||
Py_DECREF(capsule); | ||
return -1; | ||
} | ||
|
||
if (alloc_awaitable_pool() < 0) | ||
{ | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import os | ||
from pathlib import Path | ||
from typing import TextIO | ||
from pyawaitable import __version__ | ||
import shutil | ||
|
||
def _write(fp: TextIO, value: str) -> None: | ||
fp.write(value) | ||
print(f"Wrote {len(value.encode('utf-8'))} bytes to {fp.name}") | ||
|
||
|
||
def _header_file(text: str) -> str: | ||
lines = text.replace("_impl", "").split("\n") | ||
|
||
# Remove header guards and trailing newlines | ||
for i in (0, 0, -1, -1): | ||
lines.pop(i) | ||
|
||
return "\n".join([i for i in lines if not i.startswith("#include")]) | ||
|
||
|
||
def _source_file(fp: TextIO, file: Path) -> None: | ||
text: str = file.read_text(encoding="utf-8").replace("_impl", "") | ||
lines = text.split("\n") | ||
lines.insert(0, f"/* Vendor of {file} */") | ||
_write(fp, "\n".join([i for i in lines if not i.startswith("#include")])) | ||
|
||
|
||
def main(): | ||
dist = Path("./pyawaitable-vendor") | ||
if dist.exists(): | ||
print("./pyawaitable-vendor already exists, removing it...") | ||
shutil.rmtree(dist) | ||
print("Creating vendored copy of pyawaitable in ./pyawaitable-vendor...") | ||
os.mkdir(dist) | ||
|
||
with open(dist / "pyawaitable.h", "w", encoding="utf-8") as f: | ||
_write(f, """#ifndef PYAWAITABLE_VENDOR_H | ||
#define PYAWAITABLE_VENDOR_H | ||
#include <Python.h> | ||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
""") | ||
for path in [i for i in Path("./include/pyawaitable").iterdir()] + [Path("./include/vendor.h")]: | ||
_write(f, _header_file(path.read_text(encoding="utf-8"))) | ||
|
||
_write(f, "\n#endif") | ||
|
||
|
||
with open(dist / "pyawaitable.c", "w", encoding="utf-8") as f: | ||
f.write(f"""/* | ||
* PyAwaitable - Vendored copy of version {__version__} | ||
* | ||
* Docs: https://awaitable.zintensity.dev | ||
* Source: https://github.com/ZeroIntensity/pyawaitable | ||
*/ | ||
#include "pyawaitable.h" | ||
""") | ||
for source_file in Path("./src/_pyawaitable/").iterdir(): | ||
if source_file.name == "mod.c": | ||
continue | ||
|
||
_source_file(f, source_file) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |