Skip to content

Commit e0daddd

Browse files
committed
WIP async methods.
1 parent 88b382a commit e0daddd

File tree

3 files changed

+165
-9
lines changed

3 files changed

+165
-9
lines changed

src/lazy_object_proxy/cext.c

Lines changed: 135 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,7 @@ static PyObject *Proxy_oct(ProxyObject *self)
457457

458458
if ((nb = self->wrapped->ob_type->tp_as_number) == NULL ||
459459
nb->nb_oct == NULL) {
460-
PyErr_SetString(PyExc_TypeError,
461-
"oct() argument can't be converted to oct");
460+
PyErr_SetString(PyExc_TypeError, "oct() argument can't be converted to oct");
462461
return NULL;
463462
}
464463

@@ -477,8 +476,7 @@ static PyObject *Proxy_hex(ProxyObject *self)
477476

478477
if ((nb = self->wrapped->ob_type->tp_as_number) == NULL ||
479478
nb->nb_hex == NULL) {
480-
PyErr_SetString(PyExc_TypeError,
481-
"hex() argument can't be converted to hex");
479+
PyErr_SetString(PyExc_TypeError, "hex() argument can't be converted to hex");
482480
return NULL;
483481
}
484482

@@ -854,8 +852,7 @@ static PyObject *Proxy_dir(
854852

855853
/* ------------------------------------------------------------------------- */
856854

857-
static PyObject *Proxy_enter(
858-
ProxyObject *self, PyObject *args, PyObject *kwds)
855+
static PyObject *Proxy_enter(ProxyObject *self)
859856
{
860857
PyObject *method = NULL;
861858
PyObject *result = NULL;
@@ -867,7 +864,7 @@ static PyObject *Proxy_enter(
867864
if (!method)
868865
return NULL;
869866

870-
result = PyObject_Call(method, args, kwds);
867+
result = PyObject_CallObject(method, NULL);
871868

872869
Py_DECREF(method);
873870

@@ -1227,6 +1224,121 @@ static PyObject *Proxy_call(
12271224

12281225
/* ------------------------------------------------------------------------- */;
12291226

1227+
#if PY_MAJOR_VERSION >= 3
1228+
1229+
static PyObject *Proxy_aenter(ProxyObject *self)
1230+
{
1231+
PyObject *method = NULL;
1232+
PyObject *result = NULL;
1233+
1234+
Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
1235+
1236+
method = PyObject_GetAttrString(self->wrapped, "__aenter__");
1237+
1238+
if (!method)
1239+
return NULL;
1240+
1241+
result = PyObject_CallObject(method, NULL);
1242+
1243+
Py_DECREF(method);
1244+
1245+
return result;
1246+
}
1247+
1248+
/* ------------------------------------------------------------------------- */
1249+
1250+
static PyObject *Proxy_aexit(
1251+
ProxyObject *self, PyObject *args, PyObject *kwds)
1252+
{
1253+
PyObject *method = NULL;
1254+
PyObject *result = NULL;
1255+
1256+
Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
1257+
1258+
method = PyObject_GetAttrString(self->wrapped, "__aexit__");
1259+
1260+
if (!method)
1261+
return NULL;
1262+
1263+
result = PyObject_Call(method, args, kwds);
1264+
1265+
Py_DECREF(method);
1266+
1267+
return result;
1268+
}
1269+
1270+
/* ------------------------------------------------------------------------- */
1271+
1272+
static PyObject *Proxy_await(ProxyObject *self)
1273+
{
1274+
Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
1275+
1276+
unaryfunc meth = NULL;
1277+
PyObject *wrapped = self->wrapped;
1278+
PyTypeObject *type = Py_TYPE(wrapped);
1279+
1280+
1281+
if (type->tp_as_async != NULL) {
1282+
meth = type->tp_as_async->am_await;
1283+
}
1284+
1285+
if (meth != NULL) {
1286+
return (*meth)(wrapped);
1287+
}
1288+
1289+
PyErr_Format(PyExc_TypeError, " %.100s is missing the __await__ method", type->tp_name);
1290+
return NULL;
1291+
}
1292+
1293+
/* ------------------------------------------------------------------------- */;
1294+
1295+
static PyObject *Proxy_aiter(ProxyObject *self)
1296+
{
1297+
Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
1298+
1299+
unaryfunc meth = NULL;
1300+
PyObject *wrapped = self->wrapped;
1301+
PyTypeObject *type = Py_TYPE(wrapped);
1302+
1303+
if (type->tp_as_async != NULL) {
1304+
meth = type->tp_as_async->am_aiter;
1305+
}
1306+
1307+
if (meth != NULL) {
1308+
return (*meth)(wrapped);
1309+
}
1310+
1311+
PyErr_Format(PyExc_TypeError, " %.100s is missing the __aiter__ method", type->tp_name);
1312+
return NULL;
1313+
}
1314+
1315+
/* ------------------------------------------------------------------------- */;
1316+
1317+
static PyObject *Proxy_anext(ProxyObject *self)
1318+
{
1319+
Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
1320+
1321+
1322+
unaryfunc meth = NULL;
1323+
PyObject *wrapped = self->wrapped;
1324+
PyTypeObject *type = Py_TYPE(wrapped);
1325+
1326+
if (type->tp_as_async != NULL) {
1327+
meth = type->tp_as_async->am_anext;
1328+
}
1329+
1330+
if (meth != NULL) {
1331+
return (*meth)(wrapped);
1332+
}
1333+
1334+
PyErr_Format(PyExc_TypeError, " %.100s is missing the __anext__ method", type->tp_name);
1335+
return NULL;
1336+
}
1337+
1338+
#endif
1339+
1340+
/* ------------------------------------------------------------------------- */;
1341+
12301342
static PyNumberMethods Proxy_as_number = {
12311343
(binaryfunc)Proxy_add, /*nb_add*/
12321344
(binaryfunc)Proxy_subtract, /*nb_subtract*/
@@ -1299,10 +1411,17 @@ static PyMappingMethods Proxy_as_mapping = {
12991411
(objobjargproc)Proxy_setitem, /*mp_ass_subscript*/
13001412
};
13011413

1414+
#if PY_MAJOR_VERSION >= 3
1415+
static PyAsyncMethods Proxy_as_async = {
1416+
(unaryfunc)Proxy_await, /* am_await */
1417+
(unaryfunc)Proxy_aiter, /* am_aiter */
1418+
(unaryfunc)Proxy_anext, /* am_anext */
1419+
};
1420+
#endif
1421+
13021422
static PyMethodDef Proxy_methods[] = {
13031423
{ "__dir__", (PyCFunction)Proxy_dir, METH_NOARGS, 0 },
1304-
{ "__enter__", (PyCFunction)Proxy_enter,
1305-
METH_VARARGS | METH_KEYWORDS, 0 },
1424+
{ "__enter__", (PyCFunction)Proxy_enter, METH_NOARGS, 0 },
13061425
{ "__exit__", (PyCFunction)Proxy_exit,
13071426
METH_VARARGS | METH_KEYWORDS, 0 },
13081427
{ "__getattr__", (PyCFunction)Proxy_getattr,
@@ -1314,6 +1433,9 @@ static PyMethodDef Proxy_methods[] = {
13141433
{ "__fspath__", (PyCFunction)Proxy_fspath, METH_NOARGS, 0 },
13151434
#if PY_MAJOR_VERSION >= 3
13161435
{ "__round__", (PyCFunction)Proxy_round, METH_NOARGS, 0 },
1436+
{ "__aenter__", (PyCFunction)Proxy_aenter, METH_NOARGS, 0 },
1437+
{ "__aexit__", (PyCFunction)Proxy_aexit,
1438+
METH_VARARGS | METH_KEYWORDS, 0 },
13171439
#endif
13181440
{ NULL, NULL },
13191441
};
@@ -1348,7 +1470,11 @@ PyTypeObject Proxy_Type = {
13481470
0, /*tp_print*/
13491471
0, /*tp_getattr*/
13501472
0, /*tp_setattr*/
1473+
#if PY_MAJOR_VERSION >= 3
1474+
&Proxy_as_async, /* tp_as_async */
1475+
#else
13511476
0, /*tp_compare*/
1477+
#endif
13521478
(unaryfunc)Proxy_repr, /*tp_repr*/
13531479
&Proxy_as_number, /*tp_as_number*/
13541480
&Proxy_as_sequence, /*tp_as_sequence*/

src/lazy_object_proxy/simple.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,18 @@ def __reduce__(self):
256256

257257
def __reduce_ex__(self, protocol):
258258
return identity, (self.__wrapped__,)
259+
260+
def __aiter__(self):
261+
return self.__wrapped__.__aiter__()
262+
263+
async def __anext__(self):
264+
return await self.__wrapped__.__anext__()
265+
266+
def __await__(self):
267+
return await self.__wrapped__
268+
269+
async def __aenter__(self):
270+
return await self.__wrapped__.__aenter__()
271+
272+
async def __aexit__(self, *args, **kwargs):
273+
return await self.__wrapped__.__aexit__(*args, **kwargs)

src/lazy_object_proxy/slots.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,3 +424,18 @@ def __reduce__(self):
424424

425425
def __reduce_ex__(self, protocol):
426426
return identity, (self.__wrapped__,)
427+
428+
def __aiter__(self):
429+
return self.__wrapped__.__aiter__()
430+
431+
def __await__(self):
432+
return self.__wrapped__.__await__()
433+
434+
async def __anext__(self):
435+
return await self.__wrapped__.__anext__()
436+
437+
async def __aenter__(self):
438+
return await self.__wrapped__.__aenter__()
439+
440+
async def __aexit__(self, *args, **kwargs):
441+
return await self.__wrapped__.__aexit__(*args, **kwargs)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy