This repository has been archived by the owner on Apr 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
optimizations.c
101 lines (78 loc) · 2.15 KB
/
optimizations.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <Python.h>
#include <frameobject.h>
static PyObject *RequestNotFound, *RequestHandler;
/* current_request frame obtainer */
static PyObject*
current_request()
{
PyFrameObject *f = PyThreadState_GET()->frame;
int found = 0;
PyObject *request = NULL;
PyObject *self;
while (found == 0 && f != NULL) {
if (PyFrame_FastToLocalsWithError(f) < 0)
return NULL;
Py_INCREF(f->f_locals);
if (PyDict_CheckExact(f->f_locals)) {
self = PyDict_GetItem(f->f_locals, PyUnicode_FromString("self"));
if (self != NULL) {
if (PyObject_HasAttr(self, PyUnicode_FromString("request"))) {
found = 1;
request = PyObject_GetAttr(self, PyUnicode_FromString("request"));
}
if (PyObject_IsInstance(self, RequestHandler)) {
found = 1;
request = PyDict_GetItem(f->f_locals, PyUnicode_FromString("request"));
Py_INCREF(request);
}
}
}
Py_DECCREF(f->f_locals);
f = f->f_back;
}
if (f == NULL) {
PyErr_SetString(RequestNotFound,
"Could not found the request");
return NULL;
}
return (PyObject*)request;
}
static PyMethodDef OptimizationsMethods[] =
{
{"get_current_request", current_request, METH_VARARGS, "Get the request"},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
PyInit_optimizations(void)
{
PyObject* m;
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"optimizations",
"Optimizations plone server", -1, OptimizationsMethods, };
PyObject* ob = PyModule_Create(&moduledef);
if (ob == NULL)
{
return NULL;
}
if ((m = PyImport_ImportModule("plone.server.exceptions")) == NULL)
{
return NULL;
}
RequestNotFound = PyObject_GetAttrString(m, "RequestNotFound");
if (RequestNotFound == NULL)
{
return NULL;
}
Py_DECREF(m);
if ((m = PyImport_ImportModule("aiohttp.web_server")) == NULL)
{
return NULL;
}
if ((RequestHandler = PyObject_GetAttrString(m, "RequestHandler")) == NULL)
{
return NULL;
}
Py_DECREF(m);
return ob;
}