This repository has been archived by the owner on Dec 6, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
python.c
530 lines (455 loc) · 13.3 KB
/
python.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
* Python in PHP - Embedded Python Extension
*
* Copyright (c) 2003,2004,2005,2006,2007,2008 Jon Parise <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* $Id$
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_python.h"
#include "php_python_internal.h"
ZEND_DECLARE_MODULE_GLOBALS(python)
zend_class_entry *python_class_entry;
/* {{{ python_functions[]
*/
zend_function_entry python_functions[] = {
PHP_FE(python_version, NULL)
PHP_FE(python_eval, NULL)
PHP_FE(python_exec, NULL)
PHP_FE(python_call, NULL)
{NULL, NULL, NULL}
};
/* }}} */
/* {{{ python_module_entry
*/
zend_module_entry python_module_entry = {
STANDARD_MODULE_HEADER,
"python",
python_functions,
PHP_MINIT(python),
PHP_MSHUTDOWN(python),
PHP_RINIT(python),
PHP_RSHUTDOWN(python),
PHP_MINFO(python),
PHP_PYTHON_VERSION,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_PYTHON
ZEND_GET_MODULE(python)
#endif
/* }}} */
/* {{{ php_python_constructor_function
*/
zend_internal_function php_python_constructor = {
ZEND_INTERNAL_FUNCTION, /* type */
"__construct", /* function_name */
NULL, /* scope */
0, /* fn_flags */
NULL, /* prototype */
0, /* num_args */
2, /* required_num_args */
NULL, /* arg_info */
0, /* pass_rest_by_reference */
0, /* return_reference */
ZEND_FN(python_construct), /* handler */
NULL /* module */
};
/* }}} */
/* {{{ PHP_INI
*/
PHP_INI_BEGIN()
PHP_INI_ENTRY("python.optimize", "0", PHP_INI_SYSTEM, NULL)
PHP_INI_END()
/* }}} */
/* {{{ python_init_globals(zend_python_globals *globals)
*/
static void
python_init_globals(zend_python_globals *globals)
{
memset(globals, 0, sizeof(zend_python_globals));
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(python)
{
zend_class_entry ce;
ZEND_INIT_MODULE_GLOBALS(python, python_init_globals, NULL);
REGISTER_INI_ENTRIES();
REGISTER_STRING_CONSTANT("PHP_PYTHON_VERSION", PHP_PYTHON_VERSION,
CONST_CS | CONST_PERSISTENT);
INIT_CLASS_ENTRY(ce, "Python", NULL);
python_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
python_class_entry->create_object = python_object_create;
python_class_entry->constructor = (zend_function *)&php_python_constructor;
/*
* We need to set up any flags before we initialize Python. Note that we
* always skip signal handler registration to avoid interfering with PHP's
* handlers.
*/
Py_OptimizeFlag = INI_INT("python.optimize");
Py_IgnoreEnvironmentFlag = 0;
/*
* Initialize the embedded Python interpreter and its threading subsystem.
*/
Py_InitializeEx(0);
PyEval_InitThreads();
python_streams_init();
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
/*
* At this point, the embedded Python interpreter should be initialized
* and ready to go. If it isn't, we're in bad shape and return failure.
*/
return Py_IsInitialized() ? SUCCESS : FAILURE;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(python)
{
PyThreadState *tstate;
UNREGISTER_INI_ENTRIES();
/*
* Create a new thread state so we can run Py_Finalize(). This gives us a
* context in which to run the exit functions.
*/
tstate = Py_NewInterpreter();
/*
* Shut down the embedded Python interpreter. This will destroy all of
* the sub-interpreters and (ideally) free all of the memory allocated
* by Python.
*/
Py_Finalize();
/*
* Swap out our temporary state and release our lock. We're now totally
* done with the Python system.
*/
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(python)
{
PyThreadState *tstate = NULL;
PyEval_AcquireLock();
/*
* Create a new interpreter for this request. If we fail to do so, we
* can't really proceed, so we throw a fatal error.
*/
tstate = Py_NewInterpreter();
if (!tstate)
{
php_error(E_ERROR, "Python: Failed to create new interpreter");
return FAILURE;
}
/*
* Intercept Python's stdout and stderr streams and install appropriate
* PHP handlers.
*/
python_streams_intercept();
/*
* Register all of our Python modules in this interpreter's environment.
*/
python_php_init();
/*
* Save our thread state in a global variable and release our lock. This
* request's Python environment is now set up and ready to use.
*/
PYG(tstate) = tstate;
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(python)
{
PyThreadState *tstate;
tstate = PYG(tstate);
PyEval_AcquireThread(tstate);
Py_EndInterpreter(tstate);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(python)
{
php_info_print_table_start();
php_info_print_table_header(2, "Python Support", "enabled");
php_info_print_table_row(2, "Python Version", Py_GetVersion());
php_info_print_table_row(2, "Extension Version", PHP_PYTHON_VERSION);
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
php_info_print_table_start();
php_info_print_table_header(1, "Python Environment");
php_info_print_table_row(2, "Module Search Path", Py_GetPath());
php_info_print_table_end();
php_info_print_table_start();
php_info_print_table_header(1, "Python Copyright");
php_info_print_box_start(0);
php_printf("%s\n", Py_GetCopyright());
php_info_print_box_end();
php_info_print_table_end();
}
/* }}} */
/* {{{ python_error(int error_type TSRMLS_DC)
*/
static void
python_error(int error_type TSRMLS_DC)
{
PyObject *ptype, *pvalue, *ptraceback;
PyObject *type, *value;
PHP_PYTHON_THREAD_ASSERT();
/* Fetch the last error and store the type and value as strings. */
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
type = PyObject_Str(ptype);
value = PyObject_Str(pvalue);
Py_XDECREF(ptype);
Py_XDECREF(pvalue);
Py_XDECREF(ptraceback);
if (type && value) {
php_error(error_type, "Python: [%s] '%s'", PyString_AsString(type),
PyString_AsString(value));
Py_DECREF(type);
Py_DECREF(value);
}
}
/* }}} */
/* {{{ proto string python_version()
Returns the Python interpreter's version as a string. */
PHP_FUNCTION(python_version)
{
const char *version;
PHP_PYTHON_THREAD_ACQUIRE();
version = Py_GetVersion();
PHP_PYTHON_THREAD_RELEASE();
RETURN_STRING((char *)version, 1);
}
/* }}} */
/* {{{ proto void python_construct(string module, string class[, mixed ...])
*/
PHP_FUNCTION(python_construct)
{
PHP_PYTHON_FETCH(pip, getThis());
PyObject *module;
char *module_name, *class_name;
int module_name_len, class_name_len;
/*
* We expect at least two parameters: the module name and the class name.
* Any additional parameters will be passed to the Python __init__ method
* down below.
*/
if (zend_parse_parameters(2 TSRMLS_CC, "ss",
&module_name, &module_name_len,
&class_name, &class_name_len) == FAILURE) {
return;
}
PHP_PYTHON_THREAD_ACQUIRE();
module = PyImport_ImportModule(module_name);
if (module) {
PyObject *dict, *class;
/*
* The module's dictionary holds references to all of its members. Use
* it to acquire a pointer to the requested class.
*/
dict = PyModule_GetDict(module);
class = PyDict_GetItemString(dict, class_name);
/* If the class exists and is callable ... */
if (class && PyCallable_Check(class)) {
PyObject *args = NULL;
/*
* Convert our PHP arguments into a Python-digestable tuple. We
* skip the first two arguments (module name, class name) and pass
* the rest to the Python constructor.
*/
args = pip_args_to_tuple(ZEND_NUM_ARGS(), 2 TSRMLS_CC);
/*
* Call the class's constructor and store the resulting object. If
* we have a tuple of arguments, remember to free (decref) it.
*/
pip->object = PyObject_CallObject(class, args);
if (args)
Py_DECREF(args);
if (pip->object == NULL)
python_error(E_ERROR TSRMLS_CC);
/* Our new object should be an instance of the requested class. */
assert(PyObject_IsInstance(pip->object, class));
} else
php_error(E_ERROR, "Python: '%s.%s' is not a callable object",
module_name, class_name);
Py_DECREF(module);
} else
php_error(E_ERROR, "Python: '%s' is not a valid module", module_name);
PHP_PYTHON_THREAD_RELEASE();
}
/* }}} */
/* {{{ proto mixed python_eval(string expr)
Evaluate a string of code by passing it to the Python interpreter. */
PHP_FUNCTION(python_eval)
{
PyObject *m, *d, *v;
char *expr;
int len;
if (zend_parse_parameters(1 TSRMLS_CC, "s", &expr, &len) == FAILURE) {
return;
}
PHP_PYTHON_THREAD_ACQUIRE();
/*
* The command will be evaluated in __main__'s context (for both
* globals and locals). If __main__ doesn't already exist, it will be
* created.
*/
m = PyImport_AddModule("__main__");
if (m == NULL) {
PHP_PYTHON_THREAD_RELEASE();
RETURN_NULL();
}
d = PyModule_GetDict(m);
/*
* The string is evaluated as a single, isolated expression. It is not
* treated as a statement or as series of statements. This allows us to
* retrieve the resulting value of the evaluated expression.
*/
v = PyRun_String(expr, Py_eval_input, d, d);
if (v == NULL) {
python_error(E_WARNING TSRMLS_CC);
PHP_PYTHON_THREAD_RELEASE();
RETURN_NULL();
}
/*
* We convert the PyObject* value to a zval* so that we can return the
* result to PHP. If the conversion fails, we'll still be left with a
* valid zval* equal to PHP's NULL.
*
* At this point, we're done with our PyObject* value, as well. We can
* safely release our reference to it now.
*/
if (pip_pyobject_to_zval(v, return_value TSRMLS_CC) == FAILURE)
ZVAL_NULL(return_value);
Py_DECREF(v);
PHP_PYTHON_THREAD_RELEASE();
}
/* }}} */
/* {{{ proto bool python_exec(string command)
Execute a string of code by passing it to the Python interpreter. */
PHP_FUNCTION(python_exec)
{
PyObject *m, *d, *v;
char *command;
int len;
if (zend_parse_parameters(1 TSRMLS_CC, "s", &command, &len) == FAILURE) {
return;
}
PHP_PYTHON_THREAD_ACQUIRE();
/*
* The command will be evaluated in __main__'s context (for both
* globals and locals). If __main__ doesn't already exist, it will be
* created.
*/
m = PyImport_AddModule("__main__");
if (m == NULL) {
PHP_PYTHON_THREAD_RELEASE();
RETURN_FALSE;
}
d = PyModule_GetDict(m);
/*
* The string is executed as a sequence of statements. This means that
* can only detect the overall success or failure of the execution. We
* cannot return any other kind of result value.
*/
v = PyRun_String(command, Py_file_input, d, d);
if (v == NULL) {
python_error(E_WARNING TSRMLS_CC);
PHP_PYTHON_THREAD_RELEASE();
RETURN_FALSE;
}
Py_DECREF(v);
PHP_PYTHON_THREAD_RELEASE();
RETURN_TRUE;
}
/* }}} */
/* {{{ proto void python_call(string module, string function[, mixed ...])
Call the requested function in the requested module. */
PHP_FUNCTION(python_call)
{
char *module_name, *function_name;
int module_name_len, function_name_len;
PyObject *module;
/* Parse only the first two parameters (module name and function name). */
if (zend_parse_parameters(2 TSRMLS_CC, "ss", &module_name, &module_name_len,
&function_name, &function_name_len) == FAILURE) {
return;
}
PHP_PYTHON_THREAD_ACQUIRE();
/* Attempt to import the requested module. */
module = PyImport_ImportModule(module_name);
if (module) {
PyObject *dict, *function;
/* Look up the function name in the module's dictionary. */
dict = PyModule_GetDict(module);
function = PyDict_GetItemString(dict, function_name);
if (function) {
PyObject *args, *result;
/*
* Call the function with a tuple of arguments. We skip the first
* two arguments to python_call (the module name and function name)
* and pack the remaining values into the 'args' tuple.
*/
args = pip_args_to_tuple(ZEND_NUM_ARGS(), 2 TSRMLS_CC);
result = PyObject_CallObject(function, args);
if (args)
Py_DECREF(args);
if (result) {
/* Convert the Python result to its PHP equivalent. */
pip_pyobject_to_zval(result, return_value TSRMLS_CC);
Py_DECREF(result);
} else {
python_error(E_ERROR TSRMLS_CC);
}
} else {
python_error(E_ERROR TSRMLS_CC);
}
Py_DECREF(module);
} else {
python_error(E_ERROR TSRMLS_CC);
}
PHP_PYTHON_THREAD_RELEASE();
}
/* }}} */
/*
* Local variables:
* c-basic-offset: 4
* tab-width: 4
* End:
* vim600: fdm=marker
* vim: sw=4 ts=4 noet
*/