xref: /unit/src/python/nxt_python.h (revision 1624:e46b1b422545)
1 
2 /*
3  * Copyright (C) NGINX, Inc.
4  */
5 
6 #ifndef _NXT_PYTHON_H_INCLUDED_
7 #define _NXT_PYTHON_H_INCLUDED_
8 
9 
10 #include <Python.h>
11 #include <nxt_main.h>
12 #include <nxt_unit.h>
13 
14 
15 #if PY_MAJOR_VERSION == 3
16 #define NXT_PYTHON_BYTES_TYPE       "bytestring"
17 
18 #define PyString_FromStringAndSize(str, size)                                 \
19             PyUnicode_DecodeLatin1((str), (size), "strict")
20 #define PyString_AS_STRING          PyUnicode_DATA
21 
22 #else
23 #define NXT_PYTHON_BYTES_TYPE       "string"
24 
25 #define PyBytes_FromStringAndSize   PyString_FromStringAndSize
26 #define PyBytes_Check               PyString_Check
27 #define PyBytes_GET_SIZE            PyString_GET_SIZE
28 #define PyBytes_AS_STRING           PyString_AS_STRING
29 #define PyUnicode_InternInPlace     PyString_InternInPlace
30 #define PyUnicode_AsUTF8            PyString_AS_STRING
31 #endif
32 
33 #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 5
34 #define NXT_HAVE_ASGI  1
35 #endif
36 
37 extern PyObject  *nxt_py_application;
38 
39 typedef struct {
40     nxt_str_t  string;
41     PyObject   **object_p;
42 } nxt_python_string_t;
43 
44 
45 nxt_int_t nxt_python_init_strings(nxt_python_string_t *pstr);
46 void nxt_python_done_strings(nxt_python_string_t *pstr);
47 
48 void nxt_python_print_exception(void);
49 
50 nxt_int_t nxt_python_wsgi_init(nxt_task_t *task, nxt_unit_init_t *init);
51 int nxt_python_wsgi_run(nxt_unit_ctx_t *ctx);
52 void nxt_python_wsgi_done(void);
53 
54 int nxt_python_asgi_check(PyObject *obj);
55 nxt_int_t nxt_python_asgi_init(nxt_task_t *task, nxt_unit_init_t *init);
56 nxt_int_t nxt_python_asgi_run(nxt_unit_ctx_t *ctx);
57 void nxt_python_asgi_done(void);
58 
59 
60 #endif  /* _NXT_PYTHON_H_INCLUDED_ */
61