@@ -2165,12 +2165,33 @@ static apr_status_t wsgi_python_parent_cleanup(void *data)
2165
2165
}
2166
2166
2167
2167
2168
+ static void wsgi_python_init_failed (PyStatus status )
2169
+ {
2170
+ /*
2171
+ * On a PyConfig API failure, usually a memory allocation failure,
2172
+ * log a critical error.
2173
+ */
2174
+ ap_log_error (APLOG_MARK , APLOG_CRIT , 0 , wsgi_server ,
2175
+ "mod_wsgi (pid=%d): Initializing Python failed: %s" ,
2176
+ getpid (), status .err_msg );
2177
+ }
2178
+
2179
+
2168
2180
void wsgi_python_init (apr_pool_t * p )
2169
2181
{
2170
2182
const char * python_home = 0 ;
2171
2183
2172
2184
int is_pyvenv = 0 ;
2173
2185
2186
+ #if PY_VERSION_HEX >= 0x03080000
2187
+ # define USE_PYCONFIG
2188
+ #endif
2189
+ #ifdef USE_PYCONFIG
2190
+ PyConfig config ;
2191
+ PyStatus status ;
2192
+ PyConfig_InitPythonConfig (& config );
2193
+ #endif
2194
+
2174
2195
/* Perform initialisation if required. */
2175
2196
2176
2197
if (!Py_IsInitialized ()) {
@@ -2186,16 +2207,30 @@ void wsgi_python_init(apr_pool_t *p)
2186
2207
2187
2208
#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 3 ) || \
2188
2209
(PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6 )
2189
- if (wsgi_server_config -> dont_write_bytecode == 1 )
2210
+ if (wsgi_server_config -> dont_write_bytecode == 1 ) {
2211
+ #ifdef USE_PYCONFIG
2212
+ config .write_bytecode = 0 ;
2213
+ #else
2190
2214
Py_DontWriteBytecodeFlag ++ ;
2191
2215
#endif
2216
+ }
2217
+ #endif
2192
2218
2193
2219
/* Check for Python paths and optimisation flag. */
2194
2220
2195
- if (wsgi_server_config -> python_optimize > 0 )
2221
+ if (wsgi_server_config -> python_optimize > 0 ) {
2222
+ #ifdef USE_PYCONFIG
2223
+ config .optimization_level = wsgi_server_config -> python_optimize ;
2224
+ #else
2196
2225
Py_OptimizeFlag = wsgi_server_config -> python_optimize ;
2197
- else
2226
+ #endif
2227
+ } else {
2228
+ #ifdef USE_PYCONFIG
2229
+ config .optimization_level = 0 ;
2230
+ #else
2198
2231
Py_OptimizeFlag = 0 ;
2232
+ #endif
2233
+ }
2199
2234
2200
2235
/* Check for control options for Python warnings. */
2201
2236
@@ -2215,12 +2250,18 @@ void wsgi_python_init(apr_pool_t *p)
2215
2250
2216
2251
s = (wchar_t * )apr_palloc (p , len * sizeof (wchar_t ));
2217
2252
2218
- #if defined(WIN32 ) && defined(APR_HAS_UNICODE_FS )
2253
+ # if defined(WIN32 ) && defined(APR_HAS_UNICODE_FS )
2219
2254
wsgi_utf8_to_unicode_path (s , len , entries [i ]);
2220
- #else
2255
+ # else
2221
2256
mbstowcs (s , entries [i ], len );
2222
- #endif
2257
+ # endif
2258
+ # ifdef USE_PYCONFIG
2259
+ status = PyWideStringList_Append (& config .warnoptions , s );
2260
+ if (PyStatus_Exception (status ))
2261
+ wsgi_python_init_failed (status );
2262
+ # else
2223
2263
PySys_AddWarnOption (s );
2264
+ # endif
2224
2265
#else
2225
2266
PySys_AddWarnOption (entries [i ]);
2226
2267
#endif
@@ -2265,12 +2306,17 @@ void wsgi_python_init(apr_pool_t *p)
2265
2306
2266
2307
s = (wchar_t * )apr_palloc (p , len * sizeof (wchar_t ));
2267
2308
2268
- #if defined(WIN32 ) && defined(APR_HAS_UNICODE_FS )
2309
+ # if defined(WIN32 ) && defined(APR_HAS_UNICODE_FS )
2269
2310
wsgi_utf8_to_unicode_path (s , len , python_home );
2270
- #else
2311
+ # else
2271
2312
mbstowcs (s , python_home , len );
2272
- #endif
2313
+ # endif
2314
+ # ifdef USE_PYCONFIG
2315
+ status = PyConfig_SetString (& config , & config .home , s );
2316
+ if (PyStatus_Exception (status )) wsgi_python_init_failed (status );
2317
+ # else
2273
2318
Py_SetPythonHome (s );
2319
+ # endif
2274
2320
#else
2275
2321
Py_SetPythonHome ((char * )python_home );
2276
2322
#endif
@@ -2375,13 +2421,19 @@ void wsgi_python_init(apr_pool_t *p)
2375
2421
#if PY_MAJOR_VERSION >= 3
2376
2422
len = strlen (python_exe )+ 1 ;
2377
2423
s = (wchar_t * )apr_palloc (p , len * sizeof (wchar_t ));
2378
- #if defined(WIN32 ) && defined(APR_HAS_UNICODE_FS )
2424
+ # if defined(WIN32 ) && defined(APR_HAS_UNICODE_FS )
2379
2425
wsgi_utf8_to_unicode_path (s , len , python_exe );
2380
- #else
2426
+ # else
2381
2427
mbstowcs (s , python_exe , len );
2382
- #endif
2428
+ # endif
2383
2429
2430
+ # ifdef USE_PYCONFIG
2431
+ status = PyConfig_SetString (& config , & config .program_name , s );
2432
+ if (PyStatus_Exception (status ))
2433
+ wsgi_python_init_failed (status );
2434
+ # else
2384
2435
Py_SetProgramName (s );
2436
+ # endif
2385
2437
#else
2386
2438
Py_SetProgramName ((char * )python_exe );
2387
2439
#endif
@@ -2390,13 +2442,19 @@ void wsgi_python_init(apr_pool_t *p)
2390
2442
#if PY_MAJOR_VERSION >= 3
2391
2443
len = strlen (python_home )+ 1 ;
2392
2444
s = (wchar_t * )apr_palloc (p , len * sizeof (wchar_t ));
2393
- #if defined(WIN32 ) && defined(APR_HAS_UNICODE_FS )
2445
+ # if defined(WIN32 ) && defined(APR_HAS_UNICODE_FS )
2394
2446
wsgi_utf8_to_unicode_path (s , len , python_home );
2395
- #else
2447
+ # else
2396
2448
mbstowcs (s , python_home , len );
2397
- #endif
2449
+ # endif
2398
2450
2451
+ # ifdef USE_PYCONFIG
2452
+ status = PyConfig_SetString (& config , & config .home , s );
2453
+ if (PyStatus_Exception (status ))
2454
+ wsgi_python_init_failed (status );
2455
+ # else
2399
2456
Py_SetPythonHome (s );
2457
+ # endif
2400
2458
#else
2401
2459
Py_SetPythonHome ((char * )python_home );
2402
2460
#endif
@@ -2412,12 +2470,18 @@ void wsgi_python_init(apr_pool_t *p)
2412
2470
*/
2413
2471
2414
2472
if (wsgi_server_config -> python_hash_seed != NULL ) {
2415
- char * envvar = apr_pstrcat (p , "PYTHONHASHSEED=" ,
2416
- wsgi_server_config -> python_hash_seed , NULL );
2417
2473
ap_log_error (APLOG_MARK , APLOG_DEBUG , 0 , wsgi_server ,
2418
2474
"mod_wsgi (pid=%d): Setting hash seed to %s." ,
2419
2475
getpid (), wsgi_server_config -> python_hash_seed );
2476
+ #ifdef USE_PYCONFIG
2477
+ long seed = atol (wsgi_server_config -> python_hash_seed );
2478
+ config .use_hash_seed = 1 ;
2479
+ config .hash_seed = (unsigned long )seed ;
2480
+ #else
2481
+ char * envvar = apr_pstrcat (p , "PYTHONHASHSEED=" ,
2482
+ wsgi_server_config -> python_hash_seed , NULL );
2420
2483
putenv (envvar );
2484
+ #endif
2421
2485
}
2422
2486
2423
2487
/*
@@ -2436,12 +2500,19 @@ void wsgi_python_init(apr_pool_t *p)
2436
2500
ap_log_error (APLOG_MARK , APLOG_INFO , 0 , wsgi_server ,
2437
2501
"mod_wsgi (pid=%d): Initializing Python." , getpid ());
2438
2502
2503
+ #ifdef USE_PYCONFIG
2504
+ status = Py_InitializeFromConfig (& config );
2505
+ if (PyStatus_Exception (status )) wsgi_python_init_failed (status );
2506
+ #else
2439
2507
Py_Initialize ();
2508
+ #endif
2440
2509
2441
2510
#if PY_VERSION_HEX < 0x03090000
2442
2511
/* Initialise threading. */
2443
2512
PyEval_InitThreads ();
2444
2513
#endif
2514
+
2515
+ #ifndef USE_PYCONFIG
2445
2516
/*
2446
2517
* Remove the environment variable we set for the hash
2447
2518
* seed. This has to be done in os.environ, which will
@@ -2478,7 +2549,8 @@ void wsgi_python_init(apr_pool_t *p)
2478
2549
Py_DECREF (module );
2479
2550
}
2480
2551
}
2481
-
2552
+ #endif
2553
+
2482
2554
/*
2483
2555
* We now want to release the GIL. Before we do that
2484
2556
* though we remember what the current thread state is.
0 commit comments