-
-
Notifications
You must be signed in to change notification settings - Fork 122
CAPI Draft #1183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
asvetlov
wants to merge
22
commits into
master
Choose a base branch
from
capi
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
CAPI Draft #1183
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
342fdca
CAPI
asvetlov a1bfcdb
Fix wheels builder
asvetlov 59dc0cc
Merge branch 'master' into capi
asvetlov b4a2925
Add CHANGELOG
asvetlov cc0c989
Merge branch 'master' into capi
asvetlov ea64d9c
Implement MultiDict_Clear()
asvetlov 2b653ea
Implement MultiDict_SetDefault()
asvetlov a73585e
Merge branch 'master' into capi
asvetlov ddbbf68
Merge branch 'master' into capi
asvetlov 5081a60
Merge branch 'master' into capi
asvetlov b71e8a8
Merge branch 'master' into capi
asvetlov 2046974
Merge branch 'master' into capi
asvetlov b185968
Merge branch 'master' into capi
asvetlov 0836793
Merge branch 'master' into capi
asvetlov aa9fcdb
Merge branch 'master' into capi
asvetlov 283afdf
Merge branch 'master' into capi
asvetlov bce46b5
Merge branch 'master' into capi
asvetlov 83f3654
Merge branch 'master' into capi
asvetlov c48cffd
Cherry-pick from #1190
asvetlov 2adc4b7
Merge branch 'master' into capi
asvetlov bfee715
Merge branch 'master' into capi
asvetlov 636f265
Merge branch 'master' into capi
asvetlov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,95 @@ | ||
| #ifndef _MULTIDICT_CAPSULE_H | ||
| #define _MULTIDICT_CAPSULE_H | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| #define MULTIDICT_IMPL | ||
|
|
||
| #include "../multidict_api.h" | ||
| #include "dict.h" | ||
| #include "hashtable.h" | ||
| #include "state.h" | ||
|
|
||
| inline static void | ||
| _invalid_type() | ||
| { | ||
| PyErr_SetString(PyExc_TypeError, "self should be a MultiDict instance"); | ||
| } | ||
|
|
||
| static PyTypeObject * | ||
| MultiDict_GetType(void *state_) | ||
| { | ||
| mod_state *state = (mod_state *)state_; | ||
| return (PyTypeObject *)Py_NewRef(state->MultiDictType); | ||
| } | ||
|
|
||
| static PyObject * | ||
| MultiDict_New(void *state_, int prealloc_size) | ||
| { | ||
| mod_state *state = (mod_state *)state_; | ||
| MultiDictObject *md = | ||
| PyObject_GC_New(MultiDictObject, state->MultiDictType); | ||
| if (md == NULL) { | ||
| return NULL; | ||
| } | ||
| if (md_init(md, state, false, prealloc_size) < 0) { | ||
| Py_CLEAR(md); | ||
| return NULL; | ||
| } | ||
| PyObject_GC_Track(md); | ||
| return (PyObject *)md; | ||
| } | ||
|
|
||
| static int | ||
| MultiDict_Add(void *state_, PyObject *self, PyObject *key, PyObject *value) | ||
| { | ||
| mod_state *state = (mod_state *)state_; | ||
| if (MultiDict_Check(state, self) <= 0) { | ||
| _invalid_type(); | ||
| return -1; | ||
| } | ||
| return md_add((MultiDictObject *)self, key, value); | ||
| } | ||
|
|
||
| static void | ||
| capsule_free(MultiDict_CAPI *capi) | ||
| { | ||
| PyMem_Free(capi); | ||
| } | ||
|
|
||
| static void | ||
| capsule_destructor(PyObject *o) | ||
| { | ||
| MultiDict_CAPI *capi = PyCapsule_GetPointer(o, MultiDict_CAPSULE_NAME); | ||
| capsule_free(capi); | ||
| } | ||
|
|
||
| static PyObject * | ||
| new_capsule(mod_state *state) | ||
| { | ||
| MultiDict_CAPI *capi = | ||
| (MultiDict_CAPI *)PyMem_Malloc(sizeof(MultiDict_CAPI)); | ||
| if (capi == NULL) { | ||
| PyErr_NoMemory(); | ||
| return NULL; | ||
| } | ||
| capi->state = state; | ||
| capi->MultiDict_GetType = MultiDict_GetType; | ||
| capi->MultiDict_New = MultiDict_New; | ||
| capi->MultiDict_Add = MultiDict_Add; | ||
|
|
||
| PyObject *ret = | ||
| PyCapsule_New(capi, MultiDict_CAPSULE_NAME, capsule_destructor); | ||
| if (ret == NULL) { | ||
| capsule_free(capi); | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif |
This file contains hidden or 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 hidden or 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,85 @@ | ||
| #ifndef _MULTIDICT_API_H | ||
| #define _MULTIDICT_API_H | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| #include <Python.h> | ||
|
|
||
| #define MultiDict_MODULE_NAME "multidict._multidict" | ||
| #define MultiDict_CAPI_NAME "CAPI" | ||
| #define MultiDict_CAPSULE_NAME MultiDict_MODULE_NAME "." MultiDict_CAPI_NAME | ||
|
|
||
| typedef struct { | ||
| /* N.B. | ||
|
|
||
| For the sake of backward and future compatibility, | ||
| new fields should be added at the end of the structure, | ||
| unused fields should be never removed. | ||
|
|
||
| Otherwise, it could lead to crashes with memory corruptions | ||
| if the client is compiled with older multidict_api.h header | ||
| */ | ||
|
|
||
| void *state; | ||
|
|
||
| PyTypeObject *(*MultiDict_GetType)(void *state); | ||
|
|
||
| PyObject *(*MultiDict_New)(void *state, int prealloc_size); | ||
| int (*MultiDict_Add)(void *state, PyObject *self, PyObject *key, | ||
| PyObject *value); | ||
| } MultiDict_CAPI; | ||
|
|
||
| #ifndef MULTIDICT_IMPL | ||
|
|
||
| static inline MultiDict_CAPI * | ||
| MultiDict_Import() | ||
| { | ||
| return (MultiDict_CAPI *)PyCapsule_Import(MultiDict_CAPSULE_NAME, 0); | ||
| } | ||
|
|
||
| static inline PyTypeObject * | ||
| MultiDict_GetType(MultiDict_CAPI *api) | ||
| { | ||
| return api->MultiDict_GetType(api->state); | ||
| } | ||
|
|
||
| static inline int | ||
| MultiDict_CheckExact(MultiDict_CAPI *api, PyObject *op) | ||
| { | ||
| PyTypeObject *type = api->MultiDict_GetType(api->state); | ||
| int ret = Py_IS_TYPE(op, type); | ||
| Py_DECREF(type); | ||
| return ret; | ||
| } | ||
|
|
||
| static inline int | ||
| MultiDict_Check(MultiDict_CAPI *api, PyObject *op) | ||
| { | ||
| PyTypeObject *type = api->MultiDict_GetType(api->state); | ||
| int ret = Py_IS_TYPE(op, type) || PyObject_TypeCheck(op, type); | ||
| Py_DECREF(type); | ||
| return ret; | ||
| } | ||
|
|
||
| static inline PyObject * | ||
| MultiDict_New(MultiDict_CAPI *api, int prealloc_size) | ||
| { | ||
| return api->MultiDict_New(api->state, prealloc_size); | ||
| } | ||
|
|
||
| static inline int | ||
| MultiDict_Add(MultiDict_CAPI *api, PyObject *self, PyObject *key, | ||
| PyObject *value) | ||
| { | ||
| return api->MultiDict_Add(api->state, self, key, value); | ||
| } | ||
|
|
||
| #endif | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif |
This file contains hidden or 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 hidden or 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 hidden or 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,3 @@ | ||
| [build-system] | ||
| requires = ["setuptools >= 40"] | ||
| build-backend = "setuptools.build_meta" |
This file contains hidden or 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,17 @@ | ||
| [bdist_wheel] | ||
| # wheels should be OS-specific: | ||
| # their names must contain macOS/manulinux1/2010/2014/Windows identifiers | ||
| universal = 0 | ||
|
|
||
| [metadata] | ||
| name = testcapi | ||
| # the version doesn't matter, the helper library is never uploaded to pypi | ||
| version = 0.0.0.dev0 | ||
| description = multidict capi test utils | ||
| author = Andrew Svetlov | ||
| author_email = [email protected] | ||
| license = Apache 2 | ||
| [options] | ||
| python_requires = >= 3.9 | ||
| packages = | ||
| testcapi | ||
This file contains hidden or 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,24 @@ | ||
| from setuptools import Extension, setup | ||
| import multidict | ||
| import os | ||
|
|
||
| NO_EXTENSIONS = bool(os.environ.get("MULTIDICT_NO_EXTENSIONS")) | ||
|
|
||
| extensions = [ | ||
| Extension( | ||
| "testcapi._api", | ||
| ["testcapi/_api.c"], | ||
| include_dirs=multidict.__path__, | ||
| ), | ||
| ] | ||
|
|
||
| if not NO_EXTENSIONS: | ||
| print("*********************") | ||
| print("* Accelerated build *") | ||
| print("*********************") | ||
| setup(ext_modules=extensions) | ||
| else: | ||
| print("*********************") | ||
| print("* Pure Python build *") | ||
| print("*********************") | ||
| setup() |
This file contains hidden or 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,4 @@ | ||
| try: | ||
| from testcapi._api import * # noqa | ||
| except ImportError: | ||
|
|
||
| pass | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.