xref: /unit/src/python/nxt_python.h (revision 2273:a13c26f62477)
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 #define NXT_PYTHON_VER(maj, min)    ((maj << 24) | (min << 16))
15 
16 
17 #if PY_MAJOR_VERSION == 3
18 #define NXT_PYTHON_BYTES_TYPE       "bytestring"
19 
20 #define PyString_FromStringAndSize(str, size)                                 \
21             PyUnicode_DecodeLatin1((str), (size), "strict")
22 #define PyString_AS_STRING          PyUnicode_DATA
23 
24 #else
25 #define NXT_PYTHON_BYTES_TYPE       "string"
26 
27 #define PyBytes_FromStringAndSize   PyString_FromStringAndSize
28 #define PyBytes_Check               PyString_Check
29 #define PyBytes_GET_SIZE            PyString_GET_SIZE
30 #define PyBytes_AS_STRING           PyString_AS_STRING
31 #define PyUnicode_InternInPlace     PyString_InternInPlace
32 #define PyUnicode_AsUTF8            PyString_AS_STRING
33 #define PyUnicode_GET_LENGTH        PyUnicode_GET_SIZE
34 #endif
35 
36 #if PY_VERSION_HEX >= NXT_PYTHON_VER(3, 5)
37 #define NXT_HAVE_ASGI  1
38 #endif
39 
40 
41 typedef struct {
42     PyObject    *application;
43     PyObject    *py_prefix;
44     nxt_str_t   prefix;
45     nxt_bool_t  asgi_legacy;
46 } nxt_python_target_t;
47 
48 
49 typedef struct {
50     nxt_int_t            count;
51     nxt_python_target_t  target[0];
52 } nxt_python_targets_t;
53 
54 
55 extern nxt_python_targets_t  *nxt_py_targets;
56 
57 
58 typedef struct {
59     nxt_str_t  string;
60     PyObject   **object_p;
61 } nxt_python_string_t;
62 
63 
64 typedef struct {
65     int   (*ctx_data_alloc)(void **pdata, int main);
66     void  (*ctx_data_free)(void *data);
67     int   (*startup)(void *data);
68     int   (*run)(nxt_unit_ctx_t *ctx);
69     void  (*done)(void);
70 } nxt_python_proto_t;
71 
72 
73 int nxt_python_init_strings(nxt_python_string_t *pstr);
74 void nxt_python_done_strings(nxt_python_string_t *pstr);
75 
76 void nxt_python_print_exception(void);
77 
78 int nxt_python_wsgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto);
79 
80 int nxt_python_asgi_check(PyObject *obj);
81 int nxt_python_asgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto);
82 
83 
84 #endif  /* _NXT_PYTHON_H_INCLUDED_ */
85