123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431 |
- /* ------------------------------------------------------------------------- */
- #include "Python.h"
- #include "structmember.h"
- #define Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(object) \
- if (PyObject_TypeCheck(object, &Proxy_Type)) { \
- object = Proxy__ensure_wrapped((ProxyObject *)object); \
- if (!object) return NULL; \
- }
- #define Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self) if (!Proxy__ensure_wrapped(self)) return NULL;
- #define Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self) if (!Proxy__ensure_wrapped(self)) return -1;
- /* ------------------------------------------------------------------------- */
- typedef struct {
- PyObject_HEAD
- PyObject *dict;
- PyObject *wrapped;
- PyObject *factory;
- } ProxyObject;
- PyTypeObject Proxy_Type;
- /* ------------------------------------------------------------------------- */
- static PyObject *identity_ref = NULL;
- static PyObject *await_ref = NULL;
- static PyObject *
- identity(PyObject *self, PyObject *value)
- {
- Py_INCREF(value);
- return value;
- }
- /* ------------------------------------------------------------------------- */
- PyDoc_STRVAR(identity_doc, "Indentity function: returns the single argument.");
- static struct PyMethodDef module_functions[] = {
- {"identity", identity, METH_O, identity_doc},
- {NULL, NULL}
- };
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy__ensure_wrapped(ProxyObject *self)
- {
- PyObject *wrapped;
- if (self->wrapped) {
- return self->wrapped;
- } else {
- if (self->factory) {
- wrapped = PyObject_CallFunctionObjArgs(self->factory, NULL);
- if (wrapped) {
- self->wrapped = wrapped;
- return wrapped;
- } else {
- return NULL;
- }
- } else {
- PyErr_SetString(PyExc_ValueError, "Proxy hasn't been initiated: __factory__ is missing.");
- return NULL;
- }
- }
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_new(PyTypeObject *type,
- PyObject *args, PyObject *kwds)
- {
- ProxyObject *self;
- self = (ProxyObject *)type->tp_alloc(type, 0);
- if (!self)
- return NULL;
- self->dict = PyDict_New();
- self->wrapped = NULL;
- self->factory = NULL;
- return (PyObject *)self;
- }
- /* ------------------------------------------------------------------------- */
- static int Proxy_raw_init(ProxyObject *self,
- PyObject *factory)
- {
- Py_INCREF(factory);
- Py_XDECREF(self->wrapped);
- Py_XDECREF(self->factory);
- self->factory = factory;
- return 0;
- }
- /* ------------------------------------------------------------------------- */
- static int Proxy_init(ProxyObject *self,
- PyObject *args, PyObject *kwds)
- {
- PyObject *wrapped = NULL;
- static char *kwlist[] = { "wrapped", NULL };
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:ObjectProxy",
- kwlist, &wrapped)) {
- return -1;
- }
- return Proxy_raw_init(self, wrapped);
- }
- /* ------------------------------------------------------------------------- */
- static int Proxy_traverse(ProxyObject *self,
- visitproc visit, void *arg)
- {
- Py_VISIT(self->dict);
- Py_VISIT(self->wrapped);
- Py_VISIT(self->factory);
- return 0;
- }
- /* ------------------------------------------------------------------------- */
- static int Proxy_clear(ProxyObject *self)
- {
- Py_CLEAR(self->dict);
- Py_CLEAR(self->wrapped);
- Py_CLEAR(self->factory);
- return 0;
- }
- /* ------------------------------------------------------------------------- */
- static void Proxy_dealloc(ProxyObject *self)
- {
- PyObject_GC_UnTrack(self);
- Proxy_clear(self);
- Py_TYPE(self)->tp_free(self);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_repr(ProxyObject *self)
- {
- if (self->wrapped) {
- return PyUnicode_FromFormat("<%s at %p wrapping %R at %p with factory %R>",
- Py_TYPE(self)->tp_name, self,
- self->wrapped, self->wrapped,
- self->factory);
- } else {
- return PyUnicode_FromFormat("<%s at %p with factory %R>",
- Py_TYPE(self)->tp_name, self,
- self->factory);
- }
- }
- /* ------------------------------------------------------------------------- */
- static Py_hash_t Proxy_hash(ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
- return PyObject_Hash(self->wrapped);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_str(ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyObject_Str(self->wrapped);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_fspath(ProxyObject *self)
- {
- PyObject *method = NULL;
- PyObject *fspath = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- if (PyUnicode_Check(self->wrapped) || PyBytes_Check(self->wrapped)) {
- Py_INCREF(self->wrapped);
- return self->wrapped;
- }
- method = PyObject_GetAttrString(self->wrapped, "__fspath__");
- if (!method) {
- PyErr_Clear();
- Py_INCREF(self->wrapped);
- return self->wrapped;
- }
- fspath = PyObject_CallFunctionObjArgs(method, NULL);
- Py_DECREF(method);
- return fspath;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_add(PyObject *o1, PyObject *o2)
- {
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
- return PyNumber_Add(o1, o2);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_subtract(PyObject *o1, PyObject *o2)
- {
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
- return PyNumber_Subtract(o1, o2);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_multiply(PyObject *o1, PyObject *o2)
- {
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
- return PyNumber_Multiply(o1, o2);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_matrix_multiply(PyObject *o1, PyObject *o2)
- {
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
- return PyNumber_MatrixMultiply(o1, o2);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_remainder(PyObject *o1, PyObject *o2)
- {
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
- return PyNumber_Remainder(o1, o2);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_divmod(PyObject *o1, PyObject *o2)
- {
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
- return PyNumber_Divmod(o1, o2);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_power(PyObject *o1, PyObject *o2,
- PyObject *modulo)
- {
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
- return PyNumber_Power(o1, o2, modulo);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_negative(ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyNumber_Negative(self->wrapped);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_positive(ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyNumber_Positive(self->wrapped);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_absolute(ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyNumber_Absolute(self->wrapped);
- }
- /* ------------------------------------------------------------------------- */
- static int Proxy_bool(ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
- return PyObject_IsTrue(self->wrapped);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_invert(ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyNumber_Invert(self->wrapped);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_lshift(PyObject *o1, PyObject *o2)
- {
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
- return PyNumber_Lshift(o1, o2);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_rshift(PyObject *o1, PyObject *o2)
- {
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
- return PyNumber_Rshift(o1, o2);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_and(PyObject *o1, PyObject *o2)
- {
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
- return PyNumber_And(o1, o2);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_xor(PyObject *o1, PyObject *o2)
- {
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
- return PyNumber_Xor(o1, o2);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_or(PyObject *o1, PyObject *o2)
- {
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
- return PyNumber_Or(o1, o2);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_long(ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyNumber_Long(self->wrapped);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_float(ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyNumber_Float(self->wrapped);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_inplace_add(ProxyObject *self,
- PyObject *other)
- {
- PyObject *object = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
- object = PyNumber_InPlaceAdd(self->wrapped, other);
- if (!object)
- return NULL;
- Py_DECREF(self->wrapped);
- self->wrapped = object;
- Py_INCREF(self);
- return (PyObject *)self;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_inplace_subtract(
- ProxyObject *self, PyObject *other)
- {
- PyObject *object = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
- object = PyNumber_InPlaceSubtract(self->wrapped, other);
- if (!object)
- return NULL;
- Py_DECREF(self->wrapped);
- self->wrapped = object;
- Py_INCREF(self);
- return (PyObject *)self;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_inplace_multiply(
- ProxyObject *self, PyObject *other)
- {
- PyObject *object = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
- object = PyNumber_InPlaceMultiply(self->wrapped, other);
- if (!object)
- return NULL;
- Py_DECREF(self->wrapped);
- self->wrapped = object;
- Py_INCREF(self);
- return (PyObject *)self;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_inplace_matrix_multiply(
- ProxyObject *self, PyObject *other)
- {
- PyObject *object = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
- object = PyNumber_InPlaceMatrixMultiply(self->wrapped, other);
- if (!object)
- return NULL;
- Py_DECREF(self->wrapped);
- self->wrapped = object;
- Py_INCREF(self);
- return (PyObject *)self;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_inplace_remainder(
- ProxyObject *self, PyObject *other)
- {
- PyObject *object = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
- object = PyNumber_InPlaceRemainder(self->wrapped, other);
- if (!object)
- return NULL;
- Py_DECREF(self->wrapped);
- self->wrapped = object;
- Py_INCREF(self);
- return (PyObject *)self;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_inplace_power(ProxyObject *self,
- PyObject *other, PyObject *modulo)
- {
- PyObject *object = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
- object = PyNumber_InPlacePower(self->wrapped, other, modulo);
- if (!object)
- return NULL;
- Py_DECREF(self->wrapped);
- self->wrapped = object;
- Py_INCREF(self);
- return (PyObject *)self;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_inplace_lshift(ProxyObject *self,
- PyObject *other)
- {
- PyObject *object = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
- object = PyNumber_InPlaceLshift(self->wrapped, other);
- if (!object)
- return NULL;
- Py_DECREF(self->wrapped);
- self->wrapped = object;
- Py_INCREF(self);
- return (PyObject *)self;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_inplace_rshift(ProxyObject *self,
- PyObject *other)
- {
- PyObject *object = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
- object = PyNumber_InPlaceRshift(self->wrapped, other);
- if (!object)
- return NULL;
- Py_DECREF(self->wrapped);
- self->wrapped = object;
- Py_INCREF(self);
- return (PyObject *)self;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_inplace_and(ProxyObject *self,
- PyObject *other)
- {
- PyObject *object = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
- object = PyNumber_InPlaceAnd(self->wrapped, other);
- if (!object)
- return NULL;
- Py_DECREF(self->wrapped);
- self->wrapped = object;
- Py_INCREF(self);
- return (PyObject *)self;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_inplace_xor(ProxyObject *self,
- PyObject *other)
- {
- PyObject *object = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
- object = PyNumber_InPlaceXor(self->wrapped, other);
- if (!object)
- return NULL;
- Py_DECREF(self->wrapped);
- self->wrapped = object;
- Py_INCREF(self);
- return (PyObject *)self;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_inplace_or(ProxyObject *self,
- PyObject *other)
- {
- PyObject *object = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
- object = PyNumber_InPlaceOr(self->wrapped, other);
- Py_DECREF(self->wrapped);
- self->wrapped = object;
- Py_INCREF(self);
- return (PyObject *)self;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_floor_divide(PyObject *o1, PyObject *o2)
- {
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
- return PyNumber_FloorDivide(o1, o2);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_true_divide(PyObject *o1, PyObject *o2)
- {
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
- return PyNumber_TrueDivide(o1, o2);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_inplace_floor_divide(
- ProxyObject *self, PyObject *other)
- {
- PyObject *object = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
- object = PyNumber_InPlaceFloorDivide(self->wrapped, other);
- if (!object)
- return NULL;
- Py_DECREF(self->wrapped);
- self->wrapped = object;
- Py_INCREF(self);
- return (PyObject *)self;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_inplace_true_divide(
- ProxyObject *self, PyObject *other)
- {
- PyObject *object = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
- object = PyNumber_InPlaceTrueDivide(self->wrapped, other);
- if (!object)
- return NULL;
- Py_DECREF(self->wrapped);
- self->wrapped = object;
- Py_INCREF(self);
- return (PyObject *)self;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_index(ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyNumber_Index(self->wrapped);
- }
- /* ------------------------------------------------------------------------- */
- static Py_ssize_t Proxy_length(ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
- return PyObject_Length(self->wrapped);
- }
- /* ------------------------------------------------------------------------- */
- static int Proxy_contains(ProxyObject *self,
- PyObject *value)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
- return PySequence_Contains(self->wrapped, value);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_getitem(ProxyObject *self,
- PyObject *key)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyObject_GetItem(self->wrapped, key);
- }
- /* ------------------------------------------------------------------------- */
- static int Proxy_setitem(ProxyObject *self,
- PyObject *key, PyObject* value)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
- if (value == NULL)
- return PyObject_DelItem(self->wrapped, key);
- else
- return PyObject_SetItem(self->wrapped, key, value);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_dir(
- ProxyObject *self, PyObject *args)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyObject_Dir(self->wrapped);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_enter(ProxyObject *self)
- {
- PyObject *method = NULL;
- PyObject *result = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- method = PyObject_GetAttrString(self->wrapped, "__enter__");
- if (!method)
- return NULL;
- result = PyObject_CallObject(method, NULL);
- Py_DECREF(method);
- return result;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_exit(
- ProxyObject *self, PyObject *args, PyObject *kwds)
- {
- PyObject *method = NULL;
- PyObject *result = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- method = PyObject_GetAttrString(self->wrapped, "__exit__");
- if (!method)
- return NULL;
- result = PyObject_Call(method, args, kwds);
- Py_DECREF(method);
- return result;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_bytes(
- ProxyObject *self, PyObject *args)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyObject_Bytes(self->wrapped);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_reversed(
- ProxyObject *self, PyObject *args)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyObject_CallFunctionObjArgs((PyObject *)&PyReversed_Type,
- self->wrapped, NULL);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_reduce(
- ProxyObject *self, PyObject *args)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return Py_BuildValue("(O(O))", identity_ref, self->wrapped);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_round(
- ProxyObject *self, PyObject *args)
- {
- PyObject *module = NULL;
- PyObject *dict = NULL;
- PyObject *round = NULL;
- PyObject *result = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- module = PyImport_ImportModule("builtins");
- if (!module)
- return NULL;
- dict = PyModule_GetDict(module);
- round = PyDict_GetItemString(dict, "round");
- if (!round) {
- Py_DECREF(module);
- return NULL;
- }
- Py_INCREF(round);
- Py_DECREF(module);
- result = PyObject_CallFunctionObjArgs(round, self->wrapped, NULL);
- Py_DECREF(round);
- return result;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_get_name(
- ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyObject_GetAttrString(self->wrapped, "__name__");
- }
- /* ------------------------------------------------------------------------- */
- static int Proxy_set_name(ProxyObject *self,
- PyObject *value)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
- return PyObject_SetAttrString(self->wrapped, "__name__", value);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_get_qualname(
- ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyObject_GetAttrString(self->wrapped, "__qualname__");
- }
- /* ------------------------------------------------------------------------- */
- static int Proxy_set_qualname(ProxyObject *self,
- PyObject *value)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
- return PyObject_SetAttrString(self->wrapped, "__qualname__", value);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_get_module(
- ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyObject_GetAttrString(self->wrapped, "__module__");
- }
- /* ------------------------------------------------------------------------- */
- static int Proxy_set_module(ProxyObject *self,
- PyObject *value)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
- if (PyObject_SetAttrString(self->wrapped, "__module__", value) == -1)
- return -1;
- return PyDict_SetItemString(self->dict, "__module__", value);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_get_doc(
- ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyObject_GetAttrString(self->wrapped, "__doc__");
- }
- /* ------------------------------------------------------------------------- */
- static int Proxy_set_doc(ProxyObject *self,
- PyObject *value)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
- if (PyObject_SetAttrString(self->wrapped, "__doc__", value) == -1)
- return -1;
- return PyDict_SetItemString(self->dict, "__doc__", value);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_get_class(
- ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyObject_GetAttrString(self->wrapped, "__class__");
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_get_annotations(
- ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyObject_GetAttrString(self->wrapped, "__annotations__");
- }
- /* ------------------------------------------------------------------------- */
- static int Proxy_set_annotations(ProxyObject *self,
- PyObject *value)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
- return PyObject_SetAttrString(self->wrapped, "__annotations__", value);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_get_resolved(
- ProxyObject *self)
- {
- PyObject *result;
- result = self->wrapped ? Py_True : Py_False;
- Py_INCREF(result);
- return result;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_get_wrapped(
- ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- Py_INCREF(self->wrapped);
- return self->wrapped;
- }
- /* ------------------------------------------------------------------------- */
- static int Proxy_set_wrapped(ProxyObject *self,
- PyObject *value)
- {
- if (value) Py_INCREF(value);
- Py_XDECREF(self->wrapped);
- self->wrapped = value;
- return 0;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_get_factory(
- ProxyObject *self)
- {
- Py_INCREF(self->factory);
- return self->factory;
- }
- /* ------------------------------------------------------------------------- */
- static int Proxy_set_factory(ProxyObject *self,
- PyObject *value)
- {
- if (value) Py_INCREF(value);
- Py_XDECREF(self->factory);
- self->factory = value;
- return 0;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_getattro(
- ProxyObject *self, PyObject *name)
- {
- PyObject *object = NULL;
- PyObject *result = NULL;
- static PyObject *getattr_str = NULL;
- object = PyObject_GenericGetAttr((PyObject *)self, name);
- if (object)
- return object;
- PyErr_Clear();
- if (!getattr_str) {
- getattr_str = PyUnicode_InternFromString("__getattr__");
- }
- object = PyObject_GenericGetAttr((PyObject *)self, getattr_str);
- if (!object)
- return NULL;
- result = PyObject_CallFunctionObjArgs(object, name, NULL);
- Py_DECREF(object);
- return result;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_getattr(
- ProxyObject *self, PyObject *args)
- {
- PyObject *name = NULL;
- if (!PyArg_ParseTuple(args, "U:__getattr__", &name))
- return NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyObject_GetAttr(self->wrapped, name);
- }
- /* ------------------------------------------------------------------------- */
- static int Proxy_setattro(
- ProxyObject *self, PyObject *name, PyObject *value)
- {
- if (PyObject_HasAttr((PyObject *)Py_TYPE(self), name))
- return PyObject_GenericSetAttr((PyObject *)self, name, value);
- Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
- return PyObject_SetAttr(self->wrapped, name, value);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_richcompare(ProxyObject *self,
- PyObject *other, int opcode)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyObject_RichCompare(self->wrapped, other, opcode);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_iter(ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyObject_GetIter(self->wrapped);
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_call(
- ProxyObject *self, PyObject *args, PyObject *kwds)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyObject_Call(self->wrapped, args, kwds);
- }
- /* ------------------------------------------------------------------------- */;
- static PyObject *Proxy_aenter(ProxyObject *self)
- {
- PyObject *method = NULL;
- PyObject *result = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- method = PyObject_GetAttrString(self->wrapped, "__aenter__");
- if (!method)
- return NULL;
- result = PyObject_CallObject(method, NULL);
- Py_DECREF(method);
- return result;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_aexit(
- ProxyObject *self, PyObject *args, PyObject *kwds)
- {
- PyObject *method = NULL;
- PyObject *result = NULL;
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- method = PyObject_GetAttrString(self->wrapped, "__aexit__");
- if (!method)
- return NULL;
- result = PyObject_Call(method, args, kwds);
- Py_DECREF(method);
- return result;
- }
- /* ------------------------------------------------------------------------- */
- static PyObject *Proxy_await(ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- return PyObject_CallFunctionObjArgs(await_ref, self->wrapped, NULL);
- }
- /* ------------------------------------------------------------------------- */;
- static PyObject *Proxy_aiter(ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- unaryfunc meth = NULL;
- PyObject *wrapped = self->wrapped;
- PyTypeObject *type = Py_TYPE(wrapped);
- if (type->tp_as_async != NULL) {
- meth = type->tp_as_async->am_aiter;
- }
- if (meth != NULL) {
- return (*meth)(wrapped);
- }
- PyErr_Format(PyExc_AttributeError, "'%.100s' object has no attribute '__aiter__'", type->tp_name);
- return NULL;
- }
- /* ------------------------------------------------------------------------- */;
- static PyObject *Proxy_anext(ProxyObject *self)
- {
- Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
- unaryfunc meth = NULL;
- PyObject *wrapped = self->wrapped;
- PyTypeObject *type = Py_TYPE(wrapped);
- if (type->tp_as_async != NULL) {
- meth = type->tp_as_async->am_anext;
- }
- if (meth != NULL) {
- return (*meth)(wrapped);
- }
- PyErr_Format(PyExc_TypeError, "'%.100s' is missing the __anext__ method", type->tp_name);
- return NULL;
- }
- /* ------------------------------------------------------------------------- */;
- static PyNumberMethods Proxy_as_number = {
- (binaryfunc)Proxy_add, /*nb_add*/
- (binaryfunc)Proxy_subtract, /*nb_subtract*/
- (binaryfunc)Proxy_multiply, /*nb_multiply*/
- (binaryfunc)Proxy_remainder, /*nb_remainder*/
- (binaryfunc)Proxy_divmod, /*nb_divmod*/
- (ternaryfunc)Proxy_power, /*nb_power*/
- (unaryfunc)Proxy_negative, /*nb_negative*/
- (unaryfunc)Proxy_positive, /*nb_positive*/
- (unaryfunc)Proxy_absolute, /*nb_absolute*/
- (inquiry)Proxy_bool, /*nb_nonzero/nb_bool*/
- (unaryfunc)Proxy_invert, /*nb_invert*/
- (binaryfunc)Proxy_lshift, /*nb_lshift*/
- (binaryfunc)Proxy_rshift, /*nb_rshift*/
- (binaryfunc)Proxy_and, /*nb_and*/
- (binaryfunc)Proxy_xor, /*nb_xor*/
- (binaryfunc)Proxy_or, /*nb_or*/
- (unaryfunc)Proxy_long, /*nb_int*/
- 0, /*nb_long/nb_reserved*/
- (unaryfunc)Proxy_float, /*nb_float*/
- (binaryfunc)Proxy_inplace_add, /*nb_inplace_add*/
- (binaryfunc)Proxy_inplace_subtract, /*nb_inplace_subtract*/
- (binaryfunc)Proxy_inplace_multiply, /*nb_inplace_multiply*/
- (binaryfunc)Proxy_inplace_remainder, /*nb_inplace_remainder*/
- (ternaryfunc)Proxy_inplace_power, /*nb_inplace_power*/
- (binaryfunc)Proxy_inplace_lshift, /*nb_inplace_lshift*/
- (binaryfunc)Proxy_inplace_rshift, /*nb_inplace_rshift*/
- (binaryfunc)Proxy_inplace_and, /*nb_inplace_and*/
- (binaryfunc)Proxy_inplace_xor, /*nb_inplace_xor*/
- (binaryfunc)Proxy_inplace_or, /*nb_inplace_or*/
- (binaryfunc)Proxy_floor_divide, /*nb_floor_divide*/
- (binaryfunc)Proxy_true_divide, /*nb_true_divide*/
- (binaryfunc)Proxy_inplace_floor_divide, /*nb_inplace_floor_divide*/
- (binaryfunc)Proxy_inplace_true_divide, /*nb_inplace_true_divide*/
- (unaryfunc)Proxy_index, /*nb_index*/
- (binaryfunc)Proxy_matrix_multiply, /*nb_matrix_multiply*/
- (binaryfunc)Proxy_inplace_matrix_multiply, /*nb_inplace_matrix_multiply*/
- };
- static PySequenceMethods Proxy_as_sequence = {
- (lenfunc)Proxy_length, /*sq_length*/
- 0, /*sq_concat*/
- 0, /*sq_repeat*/
- 0, /*sq_item*/
- 0, /*sq_slice*/
- 0, /*sq_ass_item*/
- 0, /*sq_ass_slice*/
- (objobjproc)Proxy_contains, /* sq_contains */
- };
- static PyMappingMethods Proxy_as_mapping = {
- (lenfunc)Proxy_length, /*mp_length*/
- (binaryfunc)Proxy_getitem, /*mp_subscript*/
- (objobjargproc)Proxy_setitem, /*mp_ass_subscript*/
- };
- static PyAsyncMethods Proxy_as_async = {
- (unaryfunc)Proxy_await, /* am_await */
- (unaryfunc)Proxy_aiter, /* am_aiter */
- (unaryfunc)Proxy_anext, /* am_anext */
- };
- static PyMethodDef Proxy_methods[] = {
- { "__dir__", (PyCFunction)Proxy_dir, METH_NOARGS, 0 },
- { "__enter__", (PyCFunction)Proxy_enter, METH_NOARGS, 0 },
- { "__exit__", (PyCFunction)Proxy_exit, METH_VARARGS | METH_KEYWORDS, 0 },
- { "__getattr__", (PyCFunction)Proxy_getattr, METH_VARARGS , 0 },
- { "__bytes__", (PyCFunction)Proxy_bytes, METH_NOARGS, 0 },
- { "__reversed__", (PyCFunction)Proxy_reversed, METH_NOARGS, 0 },
- { "__reduce__", (PyCFunction)Proxy_reduce, METH_NOARGS, 0 },
- { "__reduce_ex__", (PyCFunction)Proxy_reduce, METH_O, 0 },
- { "__fspath__", (PyCFunction)Proxy_fspath, METH_NOARGS, 0 },
- { "__round__", (PyCFunction)Proxy_round, METH_NOARGS, 0 },
- { "__aenter__", (PyCFunction)Proxy_aenter, METH_NOARGS, 0 },
- { "__aexit__", (PyCFunction)Proxy_aexit, METH_VARARGS | METH_KEYWORDS, 0 },
- { NULL, NULL },
- };
- static PyGetSetDef Proxy_getset[] = {
- { "__name__", (getter)Proxy_get_name, (setter)Proxy_set_name, 0 },
- { "__qualname__", (getter)Proxy_get_qualname, (setter)Proxy_set_qualname, 0 },
- { "__module__", (getter)Proxy_get_module, (setter)Proxy_set_module, 0 },
- { "__doc__", (getter)Proxy_get_doc, (setter)Proxy_set_doc, 0 },
- { "__class__", (getter)Proxy_get_class, NULL, 0 },
- { "__annotations__", (getter)Proxy_get_annotations, (setter)Proxy_set_annotations, 0 },
- { "__wrapped__", (getter)Proxy_get_wrapped, (setter)Proxy_set_wrapped, 0 },
- { "__factory__", (getter)Proxy_get_factory, (setter)Proxy_set_factory, 0 },
- { "__resolved__", (getter)Proxy_get_resolved, NULL, 0 },
- { NULL },
- };
- PyTypeObject Proxy_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "Proxy", /*tp_name*/
- sizeof(ProxyObject), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- (destructor)Proxy_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- &Proxy_as_async, /* tp_as_async */
- (unaryfunc)Proxy_repr, /*tp_repr*/
- &Proxy_as_number, /*tp_as_number*/
- &Proxy_as_sequence, /*tp_as_sequence*/
- &Proxy_as_mapping, /*tp_as_mapping*/
- (hashfunc)Proxy_hash, /*tp_hash*/
- (ternaryfunc)Proxy_call, /*tp_call*/
- (unaryfunc)Proxy_str, /*tp_str*/
- (getattrofunc)Proxy_getattro, /*tp_getattro*/
- (setattrofunc)Proxy_setattro, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
- /*tp_flags*/
- 0, /*tp_doc*/
- (traverseproc)Proxy_traverse, /*tp_traverse*/
- (inquiry)Proxy_clear, /*tp_clear*/
- (richcmpfunc)Proxy_richcompare, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- (getiterfunc)Proxy_iter, /*tp_iter*/
- 0, /*tp_iternext*/
- Proxy_methods, /*tp_methods*/
- 0, /*tp_members*/
- Proxy_getset, /*tp_getset*/
- 0, /*tp_base*/
- 0, /*tp_dict*/
- 0, /*tp_descr_get*/
- 0, /*tp_descr_set*/
- offsetof(ProxyObject, dict), /*tp_dictoffset*/
- (initproc)Proxy_init, /*tp_init*/
- PyType_GenericAlloc, /*tp_alloc*/
- Proxy_new, /*tp_new*/
- PyObject_GC_Del, /*tp_free*/
- 0, /*tp_is_gc*/
- };
- /* ------------------------------------------------------------------------- */
- static struct PyModuleDef moduledef = {
- PyModuleDef_HEAD_INIT,
- "lazy_object_proxy.cext", /* m_name */
- NULL, /* m_doc */
- -1, /* m_size */
- module_functions, /* m_methods */
- NULL, /* m_reload */
- NULL, /* m_traverse */
- NULL, /* m_clear */
- NULL, /* m_free */
- };
- static PyObject *
- moduleinit(void)
- {
- PyObject *module;
- PyObject *dict;
- module = PyModule_Create(&moduledef);
- if (module == NULL)
- return NULL;
- if (PyType_Ready(&Proxy_Type) < 0)
- return NULL;
- dict = PyModule_GetDict(module);
- if (dict == NULL)
- return NULL;
- identity_ref = PyDict_GetItemString(dict, "identity");
- if (identity_ref == NULL)
- return NULL;
- Py_INCREF(identity_ref);
- PyObject *utils_module = PyImport_ImportModule("lazy_object_proxy.utils");
- if (utils_module == NULL)
- return NULL;
- await_ref = PyObject_GetAttrString(utils_module, "await_");
- Py_DECREF(utils_module);
- if (await_ref == NULL)
- return NULL;
- Py_INCREF(&Proxy_Type);
- PyModule_AddObject(module, "Proxy",
- (PyObject *)&Proxy_Type);
- return module;
- }
- PyMODINIT_FUNC PyInit_cext(void)
- {
- return moduleinit();
- }
- /* ------------------------------------------------------------------------- */
|