1 /* -*- C -*- */
2 /* vim: set expandtab shiftwidth=4 softtabstop=4 cinoptions='\:2=2': */
3 #include "EXTERN.h"
4 #include "perl.h"
5 #include "XSUB.h"
6
7 #include "Python.h"
8 #include "py2pl.h"
9 #include "util.h"
10
11 #ifdef EXPOSE_PERL
12 #include "perlmodule.h"
13 #endif
14
15 /* To save a little time, I check the calling context and don't convert
16 * the arguments if I'm in void context, flatten lists in list context,
17 * and return only one element in scalar context.
18 *
19 * If this turns out to be a bad idea, it's easy enough to turn off.
20 */
21 #define CHECK_CONTEXT
22
23 #ifdef CREATE_PYTHON
do_pyinit()24 void do_pyinit() {
25 #ifdef EXPOSE_PERL
26 PyObject *main_dict;
27 PyObject *perl_obj;
28
29 #if PY_MAJOR_VERSION >= 3
30 PyObject *dummy1 = PyBytes_FromString(""),
31 *dummy2 = PyBytes_FromString("main");
32 #else
33 PyObject *dummy1 = PyString_FromString(""),
34 *dummy2 = PyString_FromString("main");
35 #endif
36 #endif
37 /* sometimes Python needs to know about argc and argv to be happy */
38 int _python_argc = 1;
39 #if PY_MAJOR_VERSION >= 3
40 wchar_t *_python_argv[] = {L"python",};
41 Py_SetProgramName(L"python");
42 #else
43 char *_python_argv[] = {"python",};
44 Py_SetProgramName("python");
45 #endif
46
47 Py_Initialize();
48 PySys_SetArgv(_python_argc, _python_argv); /* Tk needs this */
49
50 #ifdef EXPOSE_PERL
51 /* create the perl module and add functions */
52 initperl();
53
54 /* now -- create the main 'perl' object and add it to the dictionary. */
55 perl_obj = newPerlPkg_object(dummy1,dummy2);
56 main_dict = PyModule_GetDict(PyImport_AddModule("__main__"));
57 PyDict_SetItemString(main_dict, "perl", perl_obj);
58
59 Py_DECREF(perl_obj);
60 Py_DECREF(dummy1);
61 Py_DECREF(dummy2);
62 #endif
63 }
64 #endif
65
66 MODULE = Inline::Python PACKAGE = Inline::Python
67
68 BOOT:
69 #ifndef PERL_USE_SAFE_PUTENV
70 PL_use_safe_putenv = 1;
71 #endif
72 py_true = perl_get_sv("Inline::Python::Boolean::true", FALSE);
73 py_false = perl_get_sv("Inline::Python::Boolean::false", FALSE);
74 #ifdef CREATE_PYTHON
75 do_pyinit();
76 #endif
77
78 PROTOTYPES: DISABLE
79
80 void py_initialize()
81 CODE:
82 do_pyinit();
83
84 void
85 py_study_package(PYPKG="__main__")
86 char* PYPKG
87 PREINIT:
88 PyObject *mod;
89 PyObject *dict;
90 PyObject *keys;
91 int len;
92 int i;
93 AV* const functions = newAV();
94 HV* const classes = newHV();
95 PPCODE:
96 mod = PyImport_AddModule(PYPKG);
97 dict = PyModule_GetDict(mod);
98 keys = PyMapping_Keys(dict);
99 len = PyObject_Length(dict);
100
101 Printf(("py_study_package: dict length: %i\n", len));
102 for (i=0; i<len; i++) {
103 PyObject * const key = PySequence_GetItem(keys,i);
104 PyObject * const val = PyObject_GetItem(dict,key);
105 if (PyCallable_Check(val)) {
106 #ifdef I_PY_DEBUG
107 #if PY_MAJOR_VERSION >= 3
108 PyObject* bytes_key = PyUnicode_AsUTF8String(key);
109 char * const key_c_str = PyBytes_AsString(bytes_key);
110 printf("py_study_package: #%i (%s) callable\n", i, key_c_str);
111 Py_DECREF(bytes_key);
112 #else
113 printf("py_study_package: #%i (%s) callable\n", i, PyString_AsString(key));
114 #endif
115 printf("val:\n\t");
116 PyObject_Print(val, stdout, Py_PRINT_RAW);
117 printf("\n");
118 printf("object type check gives: %i\n", PyType_Check(val));
119 #endif
120 if (PyFunction_Check(val)) {
121 #if PY_MAJOR_VERSION >= 3
122 PyObject* bytes_key = PyUnicode_AsUTF8String(key);
123 char * const name = PyBytes_AsString(bytes_key);
124 #else
125 char * const name = PyString_AsString(key);
126 #endif
127 Printf(("Found a function: %s\n", name));
128 av_push(functions, newSVpv(name,0));
129 #if PY_MAJOR_VERSION >= 3
130 Py_DECREF(bytes_key);
131 #endif
132 }
133 /* elw: if we just could get it to go through here! */
134 else if (PyType_Check(val) || PyClass_Check(val)) {
135 #if PY_MAJOR_VERSION >= 3
136 PyObject* bytes_key = PyUnicode_AsUTF8String(key);
137 char * const name = PyBytes_AsString(bytes_key);
138 // In P3.4, __loader__ mapping is not easy to handle... Skip for now
139 if(strcmp(name, "__loader__") == 0) continue;
140 #else
141 char * const name = PyString_AsString(key);
142 #endif
143 PyObject * const cls_dict = PyObject_GetAttrString(val,"__dict__");
144 PyObject * const cls_keys = PyMapping_Keys(cls_dict);
145 int const dict_len = PyObject_Length(cls_dict);
146 int j;
147
148 /* array of method names */
149 AV * const methods = newAV();
150
151 Printf(("Found a class: %s\n", name));
152
153 /* populate the array */
154 for (j=0; j<dict_len; j++) {
155 PyObject * const cls_key = PySequence_GetItem(cls_keys,j);
156 PyObject * const cls_val = PyObject_GetItem(cls_dict,cls_key);
157 #if PY_MAJOR_VERSION >= 3
158 PyObject* bytes_cls_key = PyUnicode_AsUTF8String(cls_key);
159 char * const fname = PyBytes_AsString(bytes_cls_key);
160 #else
161 char * const fname = PyString_AsString(cls_key);
162 #endif
163 if (PyFunction_Check(cls_val)) {
164 Printf(("Found a method of %s: %s\n", name, fname));
165 av_push(methods,newSVpv(fname,0));
166 }
167 else {
168 Printf(("not a method %s: %s\n", name, fname));
169 }
170 #if PY_MAJOR_VERSION >= 3
171 Py_DECREF(bytes_cls_key);
172 #endif
173 }
174 #if PY_MAJOR_VERSION >= 3
175 Py_DECREF(bytes_key);
176 #endif
177 hv_store(classes,name,strlen(name),newRV_noinc((SV*)methods), 0);
178 }
179 }
180 }
181 /* return an expanded hash */
182 XPUSHs(newSVpv("functions",0));
183 XPUSHs(newRV_noinc((SV*)functions));
184 XPUSHs(newSVpv("classes", 0));
185 XPUSHs(newRV_noinc((SV*)classes));
186
187 void
188 py_eval(str, type=1)
189 char *str
190 int type
191 PREINIT:
192 PyObject * main_module;
193 PyObject * globals;
194 PyObject * locals;
195 PyObject * py_result;
196 int context;
197 SV* ret = NULL;
198 PPCODE:
199 Printf(("py_eval: code: %s\n", str));
200 /* doc: if the module wasn't already loaded, you will get an empty
201 * module object. */
202 main_module = PyImport_AddModule("__main__");
203 if(main_module == NULL) {
204 croak("Error -- Import_AddModule of __main__ failed");
205 }
206 Printf(("py_eval: main_module=%p\n", main_module));
207 globals = PyModule_GetDict(main_module);
208 Printf(("py_eval: globals=%p\n", globals));
209 locals = globals;
210 context = (type == 0) ? Py_eval_input :
211 (type == 1) ? Py_file_input :
212 Py_single_input;
213 Printf(("py_eval: type=%i\n", type));
214 Printf(("py_eval: context=%i\n", context));
215 py_result = PyRun_String(str, context, globals, locals);
216 if (!py_result) {
217 PyErr_Print();
218 croak("Error -- py_eval raised an exception");
219 XSRETURN_EMPTY;
220 }
221 ret = Py2Pl(py_result);
222 if (! sv_isobject(ret))
223 sv_2mortal(ret); /* if ret is an object, this already gets done by the following line */
224 Py_DECREF(py_result);
225 if (type == 0)
226 XPUSHs(ret);
227 else
228 XSRETURN_EMPTY;
229
230 #undef NUM_FIXED_ARGS
231 #define NUM_FIXED_ARGS 2
232
233 void
234 py_call_function(PYPKG, FNAME, ...)
235 char* PYPKG;
236 char* FNAME;
237 PREINIT:
238 int i;
239
240 PyObject * const mod = PyImport_AddModule(PYPKG);
241 PyObject * const dict = PyModule_GetDict(mod);
242 PyObject * const func = PyMapping_GetItemString(dict,FNAME);
243 PyObject *o = NULL;
244 PyObject *py_retval = NULL;
245 PyObject *tuple = NULL;
246
247 SV* ret = NULL;
248
249 PPCODE:
250
251 Printf(("py_call_function\n"));
252 Printf(("package: %s\n", PYPKG));
253 Printf(("function: %s\n", FNAME));
254
255 if (!PyCallable_Check(func)) {
256 croak("'%s' is not a callable object", FNAME);
257 XSRETURN_EMPTY;
258 }
259
260 Printf(("function '%s' is callable!\n", FNAME));
261
262 tuple = PyTuple_New(items-NUM_FIXED_ARGS);
263
264 for (i=NUM_FIXED_ARGS; i<items; i++) {
265 o = Pl2Py(ST(i));
266 if (o) {
267 PyTuple_SetItem(tuple, i-NUM_FIXED_ARGS, o);
268 }
269 }
270
271 PUTBACK;
272
273 Printf(("calling func\n"));
274 py_retval = PyObject_CallObject(func, tuple);
275
276 SPAGAIN; /* refresh local stack pointer, could have been modified by Perl code called from Python */
277
278 Py_DECREF(func);
279 Py_DECREF(tuple);
280 Printf(("received a response\n"));
281 if (!py_retval || (PyErr_Occurred() != NULL)) {
282 croak_python_exception();
283 XSRETURN_EMPTY;
284 }
285 Printf(("no error\n"));
286 #ifdef CHECK_CONTEXT
287 Printf(("GIMME_V=%i\n", GIMME_V));
288 Printf(("GIMME=%i\n", GIMME));
289 Printf(("G_VOID=%i\n", G_VOID));
290 Printf(("G_ARRAY=%i\n", G_ARRAY));
291 Printf(("G_SCALAR=%i\n", G_SCALAR));
292
293 /* We can save a little time by checking our context */
294 /* For whatever reason, GIMME_V always returns G_VOID when we get forwarded
295 * from eval_python().
296 */
297 if (GIMME_V == G_VOID) {
298 Py_DECREF(py_retval);
299 XSRETURN_EMPTY;
300 }
301 #endif
302
303 Printf(("calling Py2Pl\n"));
304 ret = Py2Pl(py_retval);
305 if (! sv_isobject(ret))
306 sv_2mortal(ret); /* if ret is an object, this already gets done by the following line */
307 Py_DECREF(py_retval);
308
309 if (
310 #ifdef CHECK_CONTEXT
311 (GIMME_V == G_ARRAY) &&
312 #endif
313 SvROK(ret) && (SvTYPE(SvRV(ret)) == SVt_PVAV)) {
314 AV* const av = (AV*)SvRV(ret);
315 int const len = av_len(av) + 1;
316 int i;
317 EXTEND(SP, len);
318 for (i=0; i<len; i++) {
319 PUSHs(sv_2mortal(av_shift(av)));
320 }
321 }
322 else {
323 XPUSHs(ret);
324 }
325
326 #undef NUM_FIXED_ARGS
327 #define NUM_FIXED_ARGS 1
328
329 void
330 py_call_function_ref(FUNC, ...)
331 SV *FUNC;
332 PREINIT:
333 int i;
334
335 PyObject * const func = (PyObject *) SvIV(FUNC);
336 PyObject *o = NULL;
337 PyObject *py_retval = NULL;
338 PyObject *tuple = NULL;
339
340 SV* ret = NULL;
341
342 PPCODE:
343
344 Printf(("py_call_function_ref\n"));
345
346 if (!PyCallable_Check(func)) {
347 croak("'%p' is not a callable object", func);
348 XSRETURN_EMPTY;
349 }
350
351 Printf(("function '%p' is callable!\n", func));
352
353 tuple = PyTuple_New(items-NUM_FIXED_ARGS);
354
355 for (i=NUM_FIXED_ARGS; i<items; i++) {
356 o = Pl2Py(ST(i));
357 if (o) {
358 PyTuple_SetItem(tuple, i-NUM_FIXED_ARGS, o);
359 }
360 }
361
362 PUTBACK;
363
364 Printf(("calling func\n"));
365 py_retval = PyObject_CallObject(func, tuple);
366
367 SPAGAIN; /* refresh local stack pointer, could have been modified by Perl code called from Python */
368
369 Py_DECREF(tuple);
370 Printf(("received a response\n"));
371 if (!py_retval || (PyErr_Occurred() != NULL)) {
372 croak_python_exception();
373 XSRETURN_EMPTY;
374 }
375 Printf(("no error\n"));
376 #ifdef CHECK_CONTEXT
377 Printf(("GIMME_V=%i\n", GIMME_V));
378 Printf(("GIMME=%i\n", GIMME));
379 Printf(("G_VOID=%i\n", G_VOID));
380 Printf(("G_ARRAY=%i\n", G_ARRAY));
381 Printf(("G_SCALAR=%i\n", G_SCALAR));
382
383 /* We can save a little time by checking our context */
384 /* For whatever reason, GIMME_V always returns G_VOID when we get forwarded
385 * from eval_python().
386 */
387 if (GIMME_V == G_VOID) {
388 Py_DECREF(py_retval);
389 XSRETURN_EMPTY;
390 }
391 #endif
392
393 Printf(("calling Py2Pl\n"));
394 ret = Py2Pl(py_retval);
395 if (! sv_isobject(ret))
396 sv_2mortal(ret); /* if ret is an object, this already gets done by the following line */
397 Py_DECREF(py_retval);
398
399 if (
400 #ifdef CHECK_CONTEXT
401 (GIMME_V == G_ARRAY) &&
402 #endif
403 SvROK(ret) && (SvTYPE(SvRV(ret)) == SVt_PVAV)) {
404 AV* const av = (AV*)SvRV(ret);
405 int const len = av_len(av) + 1;
406 int i;
407 EXTEND(SP, len);
408 for (i=0; i<len; i++) {
409 PUSHs(sv_2mortal(av_shift(av)));
410 }
411 }
412 else {
413 PUSHs(ret);
414 }
415
416
417 #undef NUM_FIXED_ARGS
418 #define NUM_FIXED_ARGS 2
419
420 void
421 py_call_method(_inst, mname, ...)
422 SV* _inst;
423 char* mname;
424 PREINIT:
425
426 PyObject *inst;
427
428 /* Other variables */
429 PyObject *method; /* the method object */
430 PyObject *tuple; /* the parameters */
431 PyObject *py_retval; /* the return value */
432 int i;
433 SV *ret;
434
435 PPCODE:
436
437 Printf(("eval_python_method\n"));
438
439 if (SvROK(_inst) && SvTYPE(SvRV(_inst))==SVt_PVMG) {
440 inst = (PyObject*)SvIV(SvRV(_inst));
441 }
442 else {
443 croak("Object did not have Inline::Python::Object magic");
444 XSRETURN_EMPTY;
445 }
446
447 Printf(("inst {%p} successfully passed the PVMG test\n", inst));
448
449
450 if (!(
451 #if PY_MAJOR_VERSION < 3
452 PyInstance_Check(inst) ||
453 #endif
454 inst->ob_type->tp_flags & Py_TPFLAGS_HEAPTYPE)
455 ) {
456 croak("Attempted to call method '%s' on a non-instance", mname);
457 XSRETURN_EMPTY;
458 }
459
460 Printf(("inst is indeed a Python Instance\n"));
461
462 if (!PyObject_HasAttrString(inst, mname)) {
463 croak("Python object has no method named %s", mname);
464 XSRETURN_EMPTY;
465 }
466
467 Printf(("inst has an attribute named '%s'\n", mname));
468
469 method = PyObject_GetAttrString(inst,mname);
470
471 if (!PyCallable_Check(method)) {
472 croak("Attempted to call non-method '%s'", mname);
473 XSRETURN_EMPTY;
474 }
475
476 tuple = PyTuple_New(items-NUM_FIXED_ARGS);
477 for (i=NUM_FIXED_ARGS; i<items; i++) {
478 PyObject *o = Pl2Py(ST(i));
479 if (o) {
480 PyTuple_SetItem(tuple, i-NUM_FIXED_ARGS, o);
481 }
482 }
483
484 PUTBACK;
485
486 Printf(("calling func\n"));
487 py_retval = PyObject_CallObject(method, tuple);
488
489 SPAGAIN; /* refresh local stack pointer, could have been modified by Perl code called from Python */
490
491 Py_DECREF(method);
492 Py_DECREF(tuple);
493 Printf(("received a response\n"));
494 if (!py_retval || (PyErr_Occurred() != NULL)) {
495 croak_python_exception();
496 XSRETURN_EMPTY;
497 }
498
499 Printf(("no error\n"));
500 #ifdef CHECK_CONTEXT
501 /* We can save a little time by checking our context */
502 if (GIMME_V == G_VOID) {
503 Py_DECREF(py_retval);
504 XSRETURN_EMPTY;
505 }
506 #endif
507
508 Printf(("calling Py2Pl()\n"));
509 ret = Py2Pl(py_retval);
510 if (! sv_isobject(ret))
511 sv_2mortal(ret); /* if ret is an object, this already gets done by the following line */
512 Py_DECREF(py_retval);
513
514 if (
515 #ifdef CHECK_CONTEXT
516 GIMME_V == G_ARRAY &&
517 #endif
518 SvROK(ret) && (SvTYPE(SvRV(ret)) == SVt_PVAV)) {
519 /* if it is an array, return the array elements ourselves. */
520 AV* const av = (AV*)SvRV(ret);
521 int const len = av_len(av) + 1;
522 int i;
523 EXTEND(SP, len);
524 for (i=0; i<len; i++) {
525 PUSHs(sv_2mortal(av_shift(av)));
526 }
527 }
528 else {
529 PUSHs(ret);
530 }
531
532 #undef NUM_FIXED_ARGS
533 #define NUM_FIXED_ARGS 2
534
535 void
536 py_has_attr(_inst, key)
537 SV* _inst;
538 SV* key;
539 PREINIT:
540
541 PyObject *inst;
542 char *key_name;
543 STRLEN len;
544
545 PPCODE:
546
547 Printf(("get_object_data py_has_attr\n"));
548
549 if (SvROK(_inst) && SvTYPE(SvRV(_inst))==SVt_PVMG) {
550 inst = (PyObject*)SvIV(SvRV(_inst));
551 }
552 else {
553 croak("Object did not have Inline::Python::Object magic");
554 XSRETURN_EMPTY;
555 }
556
557 Printf(("inst {%p} successfully passed the PVMG test\n", inst));
558
559 key_name = SvPV(key, len);
560 XPUSHs(newSViv(PyObject_HasAttrString(inst, key_name)));
561
562 #undef NUM_FIXED_ARGS
563 #define NUM_FIXED_ARGS 2
564
565 void
566 py_get_attr(_inst, key)
567 SV* _inst;
568 SV* key;
569 PREINIT:
570
571 PyObject *inst;
572 char *key_name;
573 STRLEN len;
574 PyObject *py_retval; /* the return value */
575 SV *ret;
576
577 PPCODE:
578
579 Printf(("get_object_data py_get_attr\n"));
580
581 if (SvROK(_inst) && SvTYPE(SvRV(_inst))==SVt_PVMG) {
582 inst = (PyObject*)SvIV(SvRV(_inst));
583 }
584 else {
585 croak("Object did not have Inline::Python::Object magic");
586 XSRETURN_EMPTY;
587 }
588
589 Printf(("inst {%p} successfully passed the PVMG test\n", inst));
590
591 key_name = SvPV(key, len);
592 py_retval = PyObject_GetAttrString(inst, key_name);
593 if (!py_retval || (PyErr_Occurred() != NULL)) {
594 croak_python_exception();
595 XSRETURN_EMPTY;
596 }
597
598 Printf(("calling Py2Pl()\n"));
599 ret = Py2Pl(py_retval);
600 if (! sv_isobject(ret))
601 sv_2mortal(ret); /* if ret is an object, this already gets done by the following line */
602 Py_DECREF(py_retval);
603
604 XPUSHs(ret);
605
606 #undef NUM_FIXED_ARGS
607 #define NUM_FIXED_ARGS 2
608
609 void
610 py_set_attr(_inst, key, value)
611 SV* _inst;
612 SV* key;
613 SV* value;
614
615 PREINIT:
616
617 PyObject *inst, *py_value;
618 char *key_name;
619 STRLEN len;
620
621 PPCODE:
622
623 Printf(("set_attr\n"));
624
625 if (SvROK(_inst) && SvTYPE(SvRV(_inst))==SVt_PVMG) {
626 inst = (PyObject*)SvIV(SvRV(_inst));
627 }
628 else {
629 croak("Object did not have Inline::Python::Object magic");
630 XSRETURN_EMPTY;
631 }
632
633 Printf(("inst {%p} successfully passed the PVMG test\n", inst));
634
635 py_value = Pl2Py(value);
636 key_name = SvPV(key, len);
637 PyObject_SetAttrString(inst, key_name, py_value);
638 Py_DECREF(py_value);
639
640 XSRETURN_EMPTY;
641
642 #undef NUM_FIXED_ARGS
643 #define NUM_FIXED_ARGS 0
644
645 void
646 py_finalize()
647 PPCODE:
648
649 Py_Finalize();
650
651 XSRETURN_EMPTY;
652
653 #undef NUM_FIXED_ARGS
654 #define NUM_FIXED_ARGS 1
655
656 int
657 py_is_tuple(_inst)
658 SV* _inst;
659
660 #undef NUM_FIXED_ARGS
661
662