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 nxt_bool_t asgi_legacy; 44 } nxt_python_target_t; 45 46 47 typedef struct { 48 nxt_int_t count; 49 nxt_python_target_t target[0]; 50 } nxt_python_targets_t; 51 52 53 extern nxt_python_targets_t *nxt_py_targets; 54 55 56 typedef struct { 57 nxt_str_t string; 58 PyObject **object_p; 59 } nxt_python_string_t; 60 61 62 typedef struct { 63 int (*ctx_data_alloc)(void **pdata, int main); 64 void (*ctx_data_free)(void *data); 65 int (*startup)(void *data); 66 int (*run)(nxt_unit_ctx_t *ctx); 67 void (*done)(void); 68 } nxt_python_proto_t; 69 70 71 int nxt_python_init_strings(nxt_python_string_t *pstr); 72 void nxt_python_done_strings(nxt_python_string_t *pstr); 73 74 void nxt_python_print_exception(void); 75 76 int nxt_python_wsgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto); 77 78 int nxt_python_asgi_check(PyObject *obj); 79 int nxt_python_asgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto); 80 81 82 #endif /* _NXT_PYTHON_H_INCLUDED_ */ 83