-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathperlmodule.c
1250 lines (1079 loc) · 35.3 KB
/
perlmodule.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vim: set shiftwidth=4 softtabstop=4 expandtab: */
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "Python.h"
#include "perlmodule.h"
#include "py2pl.h"
#include "util.h"
#ifdef __cplusplus
}
#endif
#ifdef CREATE_PERL
static PerlInterpreter *my_perl;
#endif
staticforward PyObject * special_perl_eval(PyObject *, PyObject *);
staticforward PyObject * special_perl_use(PyObject *, PyObject *);
staticforward PyObject * special_perl_require(PyObject *, PyObject *);
/***************************************
* METHOD DECLARATIONS *
***************************************/
PyObject * newPerlPkg_object(PyObject *base, PyObject *pkg);
staticforward void PerlPkg_dealloc(PerlPkg_object *self);
staticforward PyObject * PerlPkg_repr(PerlPkg_object *self, PyObject *args);
staticforward PyObject * PerlPkg_getattr(PerlPkg_object *self, char *name);
PyObject * newPerlObj_object(SV *obj, PyObject *pkg);
staticforward void PerlObj_dealloc(PerlObj_object *self);
staticforward PyObject * PerlObj_repr(PerlObj_object *self);
staticforward PyObject * PerlObj_str(PerlObj_object *self);
staticforward PyObject * PerlObj_call(PerlObj_object *self, PyObject *args, PyObject *kw);
staticforward PyObject * PerlObj_getattr(PerlObj_object *self, char *name);
staticforward PyObject * PerlObj_mp_subscript(PerlObj_object *self, PyObject *key);
PyObject * newPerlSub_object(PyObject *base,
PyObject *pkg,
SV *cv);
PyObject * newPerlMethod_object(PyObject *base,
PyObject *pkg,
SV *obj);
PyObject * newPerlCfun_object(PyObject* (*cfun)(PyObject *self, PyObject *args));
staticforward void PerlSub_dealloc(PerlSub_object *self);
staticforward PyObject * PerlSub_call(PerlSub_object *self, PyObject *args, PyObject *kw);
staticforward PyObject * PerlSub_repr(PerlSub_object *self, PyObject *args);
staticforward PyObject * PerlSub_getattr(PerlSub_object *self, char *name);
staticforward int PerlSub_setattr(PerlSub_object *self,
char *name,
PyObject *value);
/**************************************
* METHOD DEFINITIONS *
**************************************/
/* methods of _perl_pkg */
PyObject *
newPerlPkg_object(PyObject *base, PyObject *package) {
PerlPkg_object * const self = PyObject_NEW(PerlPkg_object, &PerlPkg_type);
#if PY_MAJOR_VERSION >= 3
char * const bs = PyBytes_AsString(base);
char * const pkg = PyBytes_AsString(package);
#else
char * const bs = PyString_AsString(base);
char * const pkg = PyString_AsString(package);
#endif
char * const str = (char*)malloc((strlen(bs) + strlen(pkg) + strlen("::") + 1)
* sizeof(char));
if(!self) {
free(str);
PyErr_Format(PyExc_MemoryError, "Couldn't create Perl Package object.\n");
return NULL;
}
sprintf(str, "%s%s::", bs, pkg);
Py_INCREF(base);
Py_INCREF(package);
self->base = base;
self->pkg = package;
#if PY_MAJOR_VERSION >= 3
self->full = PyBytes_FromString(str);
#else
self->full = PyString_FromString(str);
#endif
free(str);
return (PyObject*)self;
}
static void
PerlPkg_dealloc(PerlPkg_object *self) {
Py_XDECREF(self->pkg);
Py_XDECREF(self->base);
Py_XDECREF(self->full);
PyObject_Del(self);
}
static PyObject *
PerlPkg_repr(PerlPkg_object *self, PyObject *args) {
PyObject *s;
char * const str = (char*)malloc((strlen("<perl package: ''>")
+ PyObject_Length(self->full)
+ 1) * sizeof(char));
#if PY_MAJOR_VERSION >= 3
sprintf(str, "<perl package: '%s'>", PyBytes_AsString(self->full));
s = PyUnicode_FromString(str);
#else
sprintf(str, "<perl package: '%s'>", PyString_AsString(self->full));
s = PyString_FromString(str);
#endif
free(str);
return s;
}
static PyObject *
PerlPkg_getattr(PerlPkg_object *self, char *name) {
/*** Python Methods ***/
if (strcmp(name,"__methods__") == 0) {
return get_perl_pkg_subs(self->full);
}
else if (strcmp(name,"__members__") == 0) {
PyObject *retval = PyList_New(0);
return retval ? retval : NULL;
}
else if (strcmp(name,"__dict__") == 0) {
PyObject *retval = PyDict_New();
return retval ? retval : NULL;
}
/*** Special Names (but only for 'main' package) ***/
else if (PKG_EQ(self, "main::") && strcmp(name,"eval")==0) {
/* return a PerlSub_object which just does: eval(@_) */
return newPerlCfun_object(&special_perl_eval);
}
else if (PKG_EQ(self, "main::") && strcmp(name,"use")==0) {
/* return a PerlSub_object which just does:
* eval("use $_[0]; $_[0]->import") */
return newPerlCfun_object(&special_perl_use);
}
else if (PKG_EQ(self, "main::") && strcmp(name,"require")==0) {
/* return a PerlSub_object which just does:
* eval("require $_[0];") */
return newPerlCfun_object(&special_perl_require);
}
/*** A Perl Package, Sub, or Method ***/
else {
#if PY_MAJOR_VERSION >= 3
PyObject * const tmp = PyBytes_FromString(name);
char * const full_c = PyBytes_AsString(self->full);
#else
PyObject * const tmp = PyString_FromString(name);
char * const full_c = PyString_AsString(self->full);
#endif
PyObject * const res = perl_pkg_exists(full_c, name)
? newPerlPkg_object(self->full, tmp)
: newPerlSub_object(self->full, tmp, NULL);
Py_DECREF(tmp);
return res;
}
}
static PyObject * module_dir(PerlPkg_object *self, PyObject *args) {
return get_perl_pkg_subs(self->full);
}
static struct PyMethodDef PerlPkg_methods[] = {
{"__dir__", (PyCFunction)module_dir, METH_NOARGS, NULL},
{NULL} /* sentinel */
};
/* doc string */
static char PerlPkg_type__doc__[] =
"_perl_pkg -- Wrap a Perl package in a Python class"
;
/* type definition */
PyTypeObject PerlPkg_type = {
PyVarObject_HEAD_INIT(NULL, 0)
"_perl_pkg", /*tp_name*/
sizeof(PerlPkg_object), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)PerlPkg_dealloc, /*tp_dealloc*/
(printfunc)0, /*tp_print*/
(getattrfunc)PerlPkg_getattr, /*tp_getattr*/
(setattrfunc)0, /*tp_setattr*/
#if PY_MAJOR_VERSION < 3
(cmpfunc)0, /*tp_compare*/
#else
0, /*reserved*/
#endif
(reprfunc)PerlPkg_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
(hashfunc)0, /*tp_hash*/
(ternaryfunc)0, /*tp_call*/
(reprfunc)PerlPkg_repr, /*tp_str*/
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
PerlPkg_type__doc__, /* Documentation string */
(traverseproc)0, /* tp_traverse */
(inquiry)0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
PerlPkg_methods, /* tp_methods */
};
/* methods of _perl_obj */
PyObject *
newPerlObj_object(SV *obj, PyObject *package) {
PerlObj_object * const self = PyObject_NEW(PerlObj_object, &PerlObj_type);
if(!self) {
PyErr_Format(PyExc_MemoryError, "Couldn't create Perl Obj object.\n");
return NULL;
}
Py_INCREF(package);
SvREFCNT_inc(obj);
self->pkg = package;
self->obj = obj;
return (PyObject*)self;
}
static void
PerlObj_dealloc(PerlObj_object *self) {
Py_XDECREF(self->pkg);
if (self->obj) sv_2mortal(self->obj); /* mortal instead of DECREF. Object might be return value */
PyObject_Del(self);
}
static PyObject *
PerlObj_repr(PerlObj_object *self) {
PyObject *s;
char * const str = (char*)malloc((strlen("<perl object: ''>")
+ PyObject_Length(self->pkg)
+ 1) * sizeof(char));
#if PY_MAJOR_VERSION >= 3
sprintf(str, "<perl object: '%s'>", PyBytes_AsString(self->pkg));
s = PyUnicode_FromString(str);
#else
sprintf(str, "<perl object: '%s'>", PyString_AsString(self->pkg));
s = PyString_FromString(str);
#endif
free(str);
return s;
}
static PyObject *
PerlObj_str(PerlObj_object *self) {
STRLEN len;
SV* const sv = ((SvTHINKFIRST(self->obj) && !SvIsCOW(self->obj)) || isGV_with_GP(self->obj))
? sv_mortalcopy(self->obj)
: self->obj;
char * const str = SvPVutf8(sv, len);
return PyUnicode_DecodeUTF8(str, len, "replace");
}
static PyObject *
PerlObj_getattr(PerlObj_object *self, char *name) {
PyObject *retval = NULL;
if (strcmp(name,"__methods__") == 0) {
return get_perl_pkg_subs(self->pkg);
}
else if (strcmp(name,"__members__") == 0) {
retval = PyList_New(0);
return retval ? retval : NULL;
}
else if (strcmp(name,"__dict__") == 0) {
retval = PyDict_New();
return retval ? retval : NULL;
}
else {
SV * const obj = (SV*)SvRV(self->obj);
HV * const pkg = SvSTASH(obj);
/* probably a request for a method */
GV * const gv = Perl_gv_fetchmethod_autoload(aTHX_ pkg, name, TRUE);
if (gv && isGV(gv)) {
#if PY_MAJOR_VERSION >= 3
PyObject * const py_name = PyBytes_FromString(name);
#else
PyObject * const py_name = PyString_FromString(name);
#endif
retval = newPerlMethod_object(self->pkg, py_name, self->obj);
Py_DECREF(py_name);
}
else {
/* search for an attribute */
/* check if the object supports the __getattr__ protocol */
GV* const gv = Perl_gv_fetchmethod_autoload(aTHX_ pkg, "__getattr__", FALSE);
if (gv && isGV(gv)) { /* __getattr__ supported! Let's see if an attribute is found. */
dSP;
ENTER;
SAVETMPS;
SV * const rv = sv_2mortal(newRV((SV*)GvCV(gv)));
PUSHMARK(SP);
XPUSHs(self->obj);
XPUSHs(sv_2mortal(newSVpv(name, 0)));
PUTBACK;
/* array context needed, so it's possible to return nothing (not even undef)
if the attribute does not exist */
int const count = call_sv(rv, G_ARRAY);
SPAGAIN;
if (count > 1)
croak("__getattr__ may only return a single scalar or an empty list!\n");
if (count == 1) { /* attribute exists! Now give the value back to Python */
retval = Pl2Py(POPs);
}
PUTBACK;
FREETMPS;
LEAVE;
}
if (! retval) { /* give up and raise a AttributeError */
char attribute_error[strlen(name) + 21];
sprintf(attribute_error, "attribute %s not found", name);
PyErr_SetString(PyExc_AttributeError, attribute_error);
}
}
return retval;
}
}
static PyObject*
PerlObj_mp_subscript(PerlObj_object *self, PyObject *key) {
/* check if the object supports the __getitem__ protocol */
PyObject *item = NULL;
PyObject *key_str = PyObject_Str(key); /* new reference */
#if PY_MAJOR_VERSION >= 3
PyObject* string_as_bytes = PyUnicode_AsUTF8String(key_str);/* new reference */
char * const name = PyBytes_AsString(string_as_bytes);
#else
char * const name = PyString_AsString(key_str);
#endif
SV * const obj = (SV*)SvRV(self->obj);
HV * const pkg = SvSTASH(obj);
GV* const gv = Perl_gv_fetchmethod_autoload(aTHX_ pkg, "__getitem__", FALSE);
if (gv && isGV(gv)) { /* __getitem__ supported! Let's see if the key is found. */
dSP;
ENTER;
SAVETMPS;
SV * const rv = sv_2mortal(newRV((SV*)GvCV(gv)));
PUSHMARK(SP);
XPUSHs(self->obj);
XPUSHs(sv_2mortal(newSVpv(name, 0)));
PUTBACK;
/* array context needed, so it's possible to return nothing (not even undef)
if the attribute does not exist */
int const count = call_sv(rv, G_ARRAY);
SPAGAIN;
if (count > 1)
croak("__getitem__ may only return a single scalar or an empty list!\n");
if (count == 1) { /* item exists! Now give the value back to Python */
item = Pl2Py(POPs);
}
PUTBACK;
FREETMPS;
LEAVE;
if (count == 0) {
char attribute_error[strlen(name) + 21];
sprintf(attribute_error, "attribute %s not found", name);
PyErr_SetString(PyExc_KeyError, attribute_error);
}
}
else {
PyErr_Format(PyExc_TypeError, "'%.200s' object is unsubscriptable", Py_TYPE(self)->tp_name);
}
#if PY_MAJOR_VERSION >= 3
Py_DECREF(string_as_bytes);
#endif
Py_DECREF(key_str);
return item;
}
static PyObject *
PerlObj_call(PerlObj_object *self, PyObject *args, PyObject *kw) {
dSP;
int i;
int const len = PyObject_Length(args);
int count;
PyObject *retval;
ENTER;
SAVETMPS;
PUSHMARK(SP);
if (self->obj) XPUSHs(self->obj);
if (kw) { /* if keyword arguments are present, positional arguments get pushed as into an arrayref */
AV * const positional = newAV();
for (i=0; i<len; i++) {
SV * const arg = Py2Pl(PyTuple_GetItem(args, i));
av_push(positional, sv_isobject(arg) ? SvREFCNT_inc(arg) : arg);
}
XPUSHs((SV *) sv_2mortal((SV *) newRV_inc((SV *) positional)));
SV * const kw_hash = Py2Pl(kw);
XPUSHs(kw_hash);
sv_2mortal(kw_hash);
sv_2mortal((SV *)positional);
}
else {
for (i=0; i<len; i++) {
SV * const arg = Py2Pl(PyTuple_GetItem(args, i));
XPUSHs(arg);
if (! sv_isobject(arg))
sv_2mortal(arg);
}
}
PUTBACK;
/* call the function */
/* because the Perl sub *could* be arbitrary Python code,
* I probably should temporarily hold a reference here */
Py_INCREF(self);
count = perl_call_sv(self->obj, G_EVAL);
SPAGAIN;
Py_DECREF(self); /* release*/
if (SvTRUE(ERRSV)) {
PyObject *exc = Pl2Py(ERRSV);
PyErr_SetObject(PyExc_Perl, exc);
ERRSV = NULL;
return NULL;
}
/* what to return? */
if (count == 0) {
Py_INCREF(Py_None);
retval = Py_None;
}
else if (count == 1) {
retval = Pl2Py(POPs);
}
else {
AV * const lst = newAV();
av_extend(lst, count);
for (i = count - 1; i >= 0; i--) {
av_store(lst, i, SvREFCNT_inc(POPs));
}
SV * const rv_lst = newRV_inc((SV*)lst);
retval = Pl2Py(rv_lst);
SvREFCNT_dec(rv_lst);
sv_2mortal((SV*)lst); /* this will get killed shortly */
}
PUTBACK;
FREETMPS;
LEAVE;
return retval;
}
#if PY_MAJOR_VERSION >= 3 // Python 3 rich compare
static PyObject*
PerlObj_richcompare(PerlObj_object *o1, PerlObj_object *o2, int op) {
/* Unable to compare different a Perl object with something else */
if (!PerlObjObject_Check(o1) || !PerlObjObject_Check(o2)) {
Py_RETURN_FALSE;
}
/* check if the object supports the __cmp__ protocol */
SV * const obj = (SV*)SvRV(o1->obj);
HV * const pkg = SvSTASH(obj);
const char* method_name = NULL;
switch (op) {
case Py_LT: method_name = "__lt__"; break;
case Py_LE: method_name = "__le__"; break;
case Py_EQ: method_name = "__eq__"; break;
case Py_NE: method_name = "__ne__"; break;
case Py_GT: method_name = "__gt__"; break;
case Py_GE: method_name = "__ge__"; break;
}
GV* const gv = Perl_gv_fetchmethod_autoload(aTHX_ pkg, method_name, FALSE);
if (gv && isGV(gv)) {
int retval = 1;
dSP;
ENTER;
SAVETMPS;
SV * const rv = sv_2mortal(newRV((SV*)GvCV(gv)));
PUSHMARK(SP);
XPUSHs(o1->obj);
XPUSHs(o2->obj);
PUTBACK;
int const count = call_sv(rv, G_SCALAR);
SPAGAIN;
if (count > 1)
croak("%s may only return a single scalar!\n", method_name);
if (count == 1) { /* attribute exists! Now give the value back to Python */
SV * const result = POPs;
if(!SvIOK(result))
croak("%s must return an integer!\n", method_name);
retval = SvIV(result);
}
PUTBACK;
FREETMPS;
LEAVE;
if(retval == 0) {Py_RETURN_TRUE;}
Py_RETURN_FALSE;
}
if (SvRV(o1->obj) == SvRV(o2->obj)) {/* just compare the dereferenced object pointers */
if(op == Py_EQ) {Py_RETURN_TRUE;}
Py_RETURN_FALSE;
}
if (SvRV(o1->obj) != SvRV(o2->obj)) {
if(op == Py_NE) {Py_RETURN_TRUE;}
Py_RETURN_FALSE;
}
Py_RETURN_NOTIMPLEMENTED;
}
#else // Python 2 __cmp__ method
static int
PerlObj_compare(PerlObj_object *o1, PerlObj_object *o2) {
/* check if the object supports the __cmp__ protocol */
SV * const obj = (SV*)SvRV(o1->obj);
HV * const pkg = SvSTASH(obj);
GV* const gv = Perl_gv_fetchmethod_autoload(aTHX_ pkg, "__cmp__", FALSE);
if (gv && isGV(gv)) {
int retval = 1;
dSP;
ENTER;
SAVETMPS;
SV * const rv = sv_2mortal(newRV((SV*)GvCV(gv)));
PUSHMARK(SP);
XPUSHs(o1->obj);
XPUSHs(o2->obj);
PUTBACK;
int const count = call_sv(rv, G_SCALAR);
SPAGAIN;
if (count > 1)
croak("__cmp__ may only return a single scalar!\n");
if (count == 1) { /* attribute exists! Now give the value back to Python */
SV * const result = POPs;
if(!SvIOK(result))
croak("__cmp__ must return an integer!\n");
retval = SvIV(result);
}
PUTBACK;
FREETMPS;
LEAVE;
return retval;
}
if (SvRV(o1->obj) == SvRV(o2->obj)) /* just compare the dereferenced object pointers */
return 0;
return 1;
}
#endif
static PyObject * object_dir(PerlObj_object *self, PyObject *args) {
return get_perl_pkg_subs(self->pkg);
}
static struct PyMethodDef PerlObj_methods[] = {
{"__dir__", (PyCFunction)object_dir, METH_NOARGS, NULL},
{NULL} /* sentinel */
};
/* doc string */
static char PerlObj_type__doc__[] =
"_perl_obj -- Wrap a Perl object in a Python class"
;
PyMappingMethods mp_methods = {
(lenfunc) 0, /*mp_length*/
(binaryfunc) PerlObj_mp_subscript, /*mp_subscript*/
(objobjargproc) 0, /*mp_ass_subscript*/
};
/* type definition */
PyTypeObject PerlObj_type = {
PyVarObject_HEAD_INIT(NULL, 0)
"_perl_obj", /*tp_name*/
sizeof(PerlObj_object), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)PerlObj_dealloc, /*tp_dealloc*/
(printfunc)0, /*tp_print*/
(getattrfunc)PerlObj_getattr, /*tp_getattr*/
(setattrfunc)0, /*tp_setattr*/
#if PY_MAJOR_VERSION < 3
(cmpfunc)PerlObj_compare, /*tp_compare*/
#else
0, /*reserved*/
#endif
(reprfunc)PerlObj_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
&mp_methods, /*tp_as_mapping*/
(hashfunc)0, /*tp_hash*/
(ternaryfunc)PerlObj_call, /*tp_call*/
(reprfunc)PerlObj_str, /*tp_str*/
/* Space for future expansion */
0L,0L,0L,0L,
PerlObj_type__doc__, /* Documentation string */
(traverseproc)0, /* tp_traverse */
(inquiry)0, /* tp_clear */
#if PY_MAJOR_VERSION < 3
0, /* unused */
#else
(richcmpfunc)PerlObj_richcompare, /* tp_richcompare */
#endif
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
PerlObj_methods, /* tp_methods */
};
/* methods of _perl_sub */
PyObject *
newPerlSub_object(PyObject *package, PyObject *sub, SV *cv) {
PerlSub_object * const self = PyObject_NEW(PerlSub_object, &PerlSub_type);
char *str = NULL;
if(!self) {
PyErr_Format(PyExc_MemoryError, "Couldn't create Perl Sub object.\n");
return NULL;
}
/* initialize the name of the sub or method */
if (package && sub) {
str = malloc((PyObject_Length(package) + PyObject_Length(sub) + 1)
*sizeof(char));
#if PY_MAJOR_VERSION >= 3
sprintf(str, "%s%s", PyBytes_AsString(package),
PyBytes_AsString(sub));
#else
sprintf(str, "%s%s", PyString_AsString(package),
PyString_AsString(sub));
#endif
Py_INCREF(sub);
Py_INCREF(package);
self->sub = sub;
self->pkg = package;
#if PY_MAJOR_VERSION >= 3
self->full = PyBytes_FromString(str);
#else
self->full = PyString_FromString(str);
#endif
}
else {
self->sub = NULL;
self->pkg = NULL;
self->full = NULL;
}
/* we don't have to check for errors because we shouldn't have been
* created unless perl_get_cv worked once.
*/
if (cv) {
self->ref = cv;
self->conf = 1;
}
else if (str) {
self->ref = (SV*)perl_get_cv(str,0); /* can return NULL if not found */
self->conf = self->ref ? 1 : 0;
}
else {
croak("Can't call newPerlSub_object() with all NULL arguments!\n");
}
SvREFCNT_inc(self->ref); /* quite important -- otherwise we lose it */
self->obj = NULL;
self->flgs = G_ARRAY;
self->cfun = 0;
if (str) free(str);
return (PyObject*)self;
}
PyObject *
newPerlMethod_object(PyObject *package, PyObject *sub, SV *obj) {
PerlSub_object * const self = (PerlSub_object*)newPerlSub_object(package,
sub, NULL);
self->obj = obj;
SvREFCNT_inc(obj);
return (PyObject*)self;
}
PyObject * newPerlCfun_object(PyObject* (*cfun)(PyObject *self,
PyObject *args))
{
PerlSub_object * const self = PyObject_NEW(PerlSub_object, &PerlSub_type);
self->pkg = NULL;
self->sub = NULL;
self->full = NULL;
self->ref = NULL;
self->obj = NULL;
self->flgs = 0;
self->cfun = cfun;
return (PyObject *)self;
}
static void
PerlSub_dealloc(PerlSub_object *self) {
Py_XDECREF(self->sub);
Py_XDECREF(self->pkg);
Py_XDECREF(self->full);
if (self->obj) SvREFCNT_dec(self->obj);
if (self->ref) SvREFCNT_dec(self->ref);
PyObject_Del(self);
}
static PyObject *
PerlSub_call(PerlSub_object *self, PyObject *args, PyObject *kw) {
dSP;
int i;
int const len = PyObject_Length(args);
int count;
PyObject *retval;
/* if this wraps a C function, execute that */
if (self->cfun) return self->cfun((PyObject*)self, args);
ENTER;
SAVETMPS;
PUSHMARK(SP);
if (self->obj) XPUSHs(self->obj);
if (kw) { /* if keyword arguments are present, positional arguments get pushed as into an arrayref */
AV * const positional = newAV();
for (i=0; i<len; i++) {
SV * const arg = Py2Pl(PyTuple_GetItem(args, i));
av_push(positional, sv_isobject(arg) ? SvREFCNT_inc(arg) : arg);
}
XPUSHs((SV *) sv_2mortal((SV *) newRV_inc((SV *) positional)));
SV * const kw_hash = Py2Pl(kw);
XPUSHs(kw_hash);
sv_2mortal(kw_hash);
sv_2mortal((SV *)positional);
}
else {
for (i=0; i<len; i++) {
SV * const arg = Py2Pl(PyTuple_GetItem(args, i));
XPUSHs(arg);
if (! sv_isobject(arg))
sv_2mortal(arg);
}
}
PUTBACK;
/* call the function */
/* because the Perl sub *could* be arbitrary Python code,
* I probably should temporarily hold a reference here */
Py_INCREF(self);
if (self->ref)
count = perl_call_sv(self->ref, self->flgs | G_EVAL);
else if (self->sub && self->obj)
#if PY_MAJOR_VERSION >= 3
count = perl_call_method(PyBytes_AsString(self->sub), self->flgs | G_EVAL);
#else
count = perl_call_method(PyString_AsString(self->sub), self->flgs | G_EVAL);
#endif
else {
croak("Error: PerlSub called, but no C function, sub, or name found!\n");
}
SPAGAIN;
Py_DECREF(self); /* release*/
if (SvTRUE(ERRSV)) {
PyObject *exc = Pl2Py(ERRSV);
PyErr_SetObject(PyExc_Perl, exc);
ERRSV = NULL;
return NULL;
}
/* what to return? */
if (count == 0) {
Py_INCREF(Py_None);
retval = Py_None;
}
else if (count == 1) {
retval = Pl2Py(POPs);
}
else {
AV * const lst = newAV();
av_extend(lst, count);
for (i = count - 1; i >= 0; i--) {
av_store(lst, i, SvREFCNT_inc(POPs));
}
SV * const rv_lst = newRV_inc((SV*)lst);
retval = Pl2Py(rv_lst);
SvREFCNT_dec(rv_lst);
sv_2mortal((SV*)lst); /* this will get killed shortly */
}
PUTBACK;
FREETMPS;
LEAVE;
return retval;
}
static PyObject *
PerlSub_repr(PerlSub_object *self, PyObject *args) {
PyObject *s;
char * const str = (char*)malloc((strlen("<perl sub: ''>")
+ (self->full
? PyObject_Length(self->full)
: strlen("anonymous"))
+ 1) * sizeof(char));
#if PY_MAJOR_VERSION >= 3
sprintf(str, "<perl sub: '%s'>", (self->full
? PyBytes_AsString(self->full)
: "anonymous"));
s = PyUnicode_FromString(str);
#else
sprintf(str, "<perl sub: '%s'>", (self->full
? PyString_AsString(self->full)
: "anonymous"));
s = PyString_FromString(str);
#endif
free(str);
return s;
}
static PyObject *
PerlSub_getattr(PerlSub_object *self, char *name) {
PyObject *retval = NULL;
if (strcmp(name,"flags")==0) {
retval = PyInt_FromLong((long)self->flgs);
}
else if (strcmp(name,"G_VOID")==0) {
retval = PyInt_FromLong((long)G_VOID);
}
else if (strcmp(name,"G_SCALAR")==0) {
retval = PyInt_FromLong((long)G_SCALAR);
}
else if (strcmp(name,"G_ARRAY")==0) {
retval = PyInt_FromLong((long)G_ARRAY);
}
else if (strcmp(name,"G_DISCARD")==0) {
retval = PyInt_FromLong((long)G_DISCARD);
}
else if (strcmp(name,"G_NOARGS")==0) {
retval = PyInt_FromLong((long)G_NOARGS);
}
else if (strcmp(name,"G_EVAL")==0) {
retval = PyInt_FromLong((long)G_EVAL);
}
else if (strcmp(name,"G_KEEPERR")==0) {
retval = PyInt_FromLong((long)G_KEEPERR);
}
else {
PyErr_Format(PyExc_AttributeError,
"Attribute '%s' not found for Perl sub '%s'", name,
#if PY_MAJOR_VERSION < 3
(self->full
? PyString_AsString(self->full)
: (self->pkg ? PyString_AsString(self->pkg) : ""))
#else
(self->full
? PyBytes_AsString(self->full)
: (self->pkg ? PyBytes_AsString(self->pkg) : ""))
#endif
);
retval = NULL;
}
return retval;
}
static int
PerlSub_setattr(PerlSub_object *self, char *name, PyObject *v) {
if (strcmp(name, "flags")==0 && PyInt_Check(v)) {
self->flgs = (int)PyInt_AsLong(v);
return 0; /* success */
}
else if (strcmp(name,"flags")==0) {
PyErr_Format(PyExc_TypeError,
"'flags' can only be set from an integer. '%s'",
#if PY_MAJOR_VERSION < 3
(self->pkg ? PyString_AsString(self->pkg) : ""));
#else
(self->pkg ? PyBytes_AsString(self->pkg) : ""));
#endif
return -1; /* failure */
}
else {
PyErr_Format(PyExc_AttributeError,
"Attribute '%s' not found for Perl sub '%s'", name,
#if PY_MAJOR_VERSION < 3
(self->full
? PyString_AsString(self->full)
: (self->pkg ? PyString_AsString(self->pkg) : ""))
#else
(self->full
? PyBytes_AsString(self->full)
: (self->pkg ? PyBytes_AsString(self->pkg) : ""))
#endif
);
return -1; /* failure */
}
}
/* doc string */
static char PerlSub_type__doc__[] =
"_perl_sub -- Wrap a Perl sub in a Python class"
;
/* type definition */
PyTypeObject PerlSub_type = {
PyVarObject_HEAD_INIT(NULL, 0)
"_perl_sub", /*tp_name*/
sizeof(PerlSub_object), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)PerlSub_dealloc, /*tp_dealloc*/
(printfunc)0, /*tp_print*/
(getattrfunc)PerlSub_getattr, /*tp_getattr*/
(setattrfunc)PerlSub_setattr, /*tp_setattr*/
#if PY_MAJOR_VERSION < 3
(cmpfunc)0, /*tp_compare*/
#else
0, /*reserved*/
#endif
(reprfunc)PerlSub_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
(hashfunc)0, /*tp_hash*/
(ternaryfunc)PerlSub_call, /*tp_call*/
(reprfunc)PerlSub_repr, /*tp_str*/
/* Space for future expansion */
0L,0L,0L,0L,
PerlSub_type__doc__, /* Documentation string */
};