Skip to content

Commit fcf6f11

Browse files
committed
An early attempt to implement global ns.
1 parent 5ef54c1 commit fcf6f11

File tree

2 files changed

+185
-140
lines changed

2 files changed

+185
-140
lines changed

src/ngx_http_python_module.c

Lines changed: 18 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
#include "ngx_http_python_request.h"
1212

1313

14-
typedef struct {
15-
PyObject *ns;
16-
size_t stack_size;
17-
} ngx_http_python_main_conf_t;
18-
19-
2014
typedef struct {
2115
ngx_array_t *access; /* array of PyCodeObject * */
2216
ngx_array_t *log; /* array of PyCodeObject * */
@@ -49,8 +43,6 @@ static char *ngx_http_python_log(ngx_conf_t *cf, ngx_command_t *cmd,
4943
void *conf);
5044
static char *ngx_http_python_content(ngx_conf_t *cf, ngx_command_t *cmd,
5145
void *conf);
52-
static void *ngx_http_python_create_main_conf(ngx_conf_t *cf);
53-
static char *ngx_http_python_init_main_conf(ngx_conf_t *cf, void *conf);
5446
static void *ngx_http_python_create_loc_conf(ngx_conf_t *cf);
5547
static char *ngx_http_python_merge_loc_conf(ngx_conf_t *cf, void *parent,
5648
void *child);
@@ -61,25 +53,19 @@ static ngx_int_t ngx_http_python_init_namespace(ngx_conf_t *cf);
6153
static ngx_command_t ngx_http_python_commands[] = {
6254

6355
{ ngx_string("python"),
64-
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
56+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
6557
ngx_python_set_slot,
66-
NGX_HTTP_MAIN_CONF_OFFSET,
67-
offsetof(ngx_http_python_main_conf_t, ns),
58+
0,
59+
0,
6860
NULL },
6961

7062
{ ngx_string("python_include"),
71-
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
63+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
7264
ngx_python_include_set_slot,
73-
NGX_HTTP_MAIN_CONF_OFFSET,
74-
offsetof(ngx_http_python_main_conf_t, ns),
65+
0,
66+
0,
7567
NULL },
7668

77-
{ ngx_string("python_stack_size"),
78-
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
79-
ngx_conf_set_size_slot,
80-
NGX_HTTP_MAIN_CONF_OFFSET,
81-
offsetof(ngx_http_python_main_conf_t, stack_size),
82-
NULL },
8369

8470
{ ngx_string("python_set"),
8571
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2,
@@ -117,8 +103,8 @@ static ngx_http_module_t ngx_http_python_module_ctx = {
117103
NULL, /* preconfiguration */
118104
ngx_http_python_init, /* postconfiguration */
119105

120-
ngx_http_python_create_main_conf, /* create main configuration */
121-
ngx_http_python_init_main_conf, /* init main configuration */
106+
NULL, /* create main configuration */
107+
NULL, /* init main configuration */
122108

123109
NULL, /* create server configuration */
124110
NULL, /* merge server configuration */
@@ -344,11 +330,9 @@ static PyObject *
344330
ngx_http_python_eval(ngx_http_request_t *r, PyCodeObject *code,
345331
ngx_event_t *wake)
346332
{
347-
PyObject *result, *pr;
348-
ngx_http_python_ctx_t *ctx;
349-
ngx_python_create_ctx_t pc;
350-
ngx_http_core_loc_conf_t *clcf;
351-
ngx_http_python_main_conf_t *pmcf;
333+
PyObject *result, *pr;
334+
ngx_http_python_ctx_t *ctx;
335+
ngx_http_core_loc_conf_t *clcf;
352336

353337
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
354338
"http python eval start code:%p, wake:%p", code, wake);
@@ -372,17 +356,8 @@ ngx_http_python_eval(ngx_http_request_t *r, PyCodeObject *code,
372356
}
373357
}
374358

375-
pmcf = ngx_http_get_module_main_conf(r, ngx_http_python_module);
376-
377359
if (ctx->python == NULL) {
378-
ngx_memzero(&pc, sizeof(ngx_python_create_ctx_t));
379-
380-
pc.pool = r->pool;
381-
pc.log = r->connection->log;
382-
pc.ns = pmcf->ns;
383-
pc.stack_size = pmcf->stack_size;
384-
385-
ctx->python = ngx_python_create_ctx(&pc);
360+
ctx->python = ngx_python_create_ctx(r->pool, r->connection->log);
386361
if (ctx->python == NULL) {
387362
return NULL;
388363
}
@@ -393,10 +368,10 @@ ngx_http_python_eval(ngx_http_request_t *r, PyCodeObject *code,
393368
ngx_python_set_resolver(ctx->python, clcf->resolver,
394369
clcf->resolver_timeout);
395370

396-
pr = PyDict_GetItemString(pmcf->ns, "r");
371+
pr = PyDict_GetItemString(ctx->python->ns, "r");
397372

398373
if (pr == NULL) {
399-
if (PyDict_SetItemString(pmcf->ns, "r", ctx->request) < 0) {
374+
if (PyDict_SetItemString(ctx->python->ns, "r", ctx->request) < 0) {
400375
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
401376
"python error: %s", ngx_python_get_error(r->pool));
402377
}
@@ -405,10 +380,9 @@ ngx_http_python_eval(ngx_http_request_t *r, PyCodeObject *code,
405380
result = ngx_python_eval(ctx->python, code, wake);
406381

407382
if (pr == NULL) {
408-
if (PyDict_DelItemString(pmcf->ns, "r") < 0) {
383+
if (PyDict_DelItemString(ctx->python->ns, "r") < 0) {
409384
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
410-
"python error: %s",
411-
ngx_python_get_error(r->pool));
385+
"python error: %s", ngx_python_get_error(r->pool));
412386
}
413387
}
414388

@@ -420,39 +394,6 @@ ngx_http_python_eval(ngx_http_request_t *r, PyCodeObject *code,
420394
}
421395

422396

423-
static void *
424-
ngx_http_python_create_main_conf(ngx_conf_t *cf)
425-
{
426-
ngx_http_python_main_conf_t *pmcf;
427-
428-
pmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_python_main_conf_t));
429-
if (pmcf == NULL) {
430-
return NULL;
431-
}
432-
433-
/*
434-
* set by ngx_pcalloc():
435-
*
436-
* pmcf->ns = NULL;
437-
*/
438-
439-
pmcf->stack_size = NGX_CONF_UNSET_SIZE;
440-
441-
return pmcf;
442-
}
443-
444-
445-
static char *
446-
ngx_http_python_init_main_conf(ngx_conf_t *cf, void *conf)
447-
{
448-
ngx_http_python_main_conf_t *pmcf = conf;
449-
450-
ngx_conf_init_size_value(pmcf->stack_size, 32768);
451-
452-
return NGX_CONF_OK;
453-
}
454-
455-
456397
static void *
457398
ngx_http_python_create_loc_conf(ngx_conf_t *cf)
458399
{
@@ -658,15 +599,8 @@ ngx_http_python_content(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
658599
static ngx_int_t
659600
ngx_http_python_init_namespace(ngx_conf_t *cf)
660601
{
661-
ngx_http_python_main_conf_t *pmcf;
662-
663-
pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_python_module);
664-
665-
if (pmcf->ns == NULL) {
666-
pmcf->ns = ngx_python_create_namespace(cf);
667-
if (pmcf->ns == NULL) {
668-
return NGX_ERROR;
669-
}
602+
if (ngx_python_get_namespace(cf) == NULL) {
603+
return NGX_ERROR;
670604
}
671605

672606
if (ngx_http_python_request_init(cf) != NGX_OK) {

0 commit comments

Comments
 (0)