python: Allow wrapping pointers within talloc'ed memory that are not talloc contexts.
[ira/wip.git] / source / pidl / lib / Parse / Pidl / Samba4 / Python.pm
1 ###################################################
2 # Python function wrapper generator
3 # Copyright jelmer@samba.org 2007-2008
4 # released under the GNU GPL
5
6 package Parse::Pidl::Samba4::Python;
7
8 use Exporter;
9 @ISA = qw(Exporter);
10
11 use strict;
12 use Parse::Pidl::Typelist qw(hasType getType mapTypeName expandAlias);
13 use Parse::Pidl::Util qw(has_property ParseExpr);
14 use Parse::Pidl::CUtil qw(get_value_of get_pointer_to);
15
16 use vars qw($VERSION);
17 $VERSION = '0.01';
18
19 sub new($) {
20         my ($class) = @_;
21         my $self = { res => "", res_hdr => "", tabs => "", constants => {},
22                      module_methods => []};
23         bless($self, $class);
24 }
25
26 sub pidl_hdr ($$)
27 {
28         my $self = shift;
29         $self->{res_hdr} .= shift;
30 }
31
32 sub pidl($$)
33 {
34         my ($self, $d) = @_;
35         if ($d) {
36                 $self->{res} .= $self->{tabs};
37                 $self->{res} .= $d;
38         }
39         $self->{res} .= "\n";
40 }
41
42 sub indent($)
43 {
44         my ($self) = @_;
45         $self->{tabs} .= "\t";
46 }
47
48 sub deindent($)
49 {
50         my ($self) = @_;
51         $self->{tabs} = substr($self->{tabs}, 0, -1);
52 }
53
54 sub Import
55 {
56         my $self = shift;
57         my @imports = @_;
58         foreach (@imports) {
59                 s/\.idl\"$//;
60                 s/^\"//;
61                 $self->pidl_hdr("#include \"librpc/gen_ndr/py_$_\.h\"\n");
62         }
63 }
64
65 sub Const($$)
66 {
67     my ($self, $const) = @_;
68         $self->register_constant($const->{NAME}, $const->{DTYPE}, $const->{VALUE});
69 }
70
71 sub register_constant($$$$)
72 {
73         my ($self, $name, $type, $value) = @_;
74
75         $self->{constants}->{$name} = [$type, $value];
76 }
77
78 sub EnumAndBitmapConsts($$$)
79 {
80         my ($self, $name, $d) = @_;
81
82         return unless (defined($d->{ELEMENTS}));
83
84         foreach my $e (@{$d->{ELEMENTS}}) {
85                 $e =~ /^([A-Za-z0-9_]+)=(.*)$/;
86                 my $cname = $1;
87                 
88                 $self->register_constant($cname, $d, $cname);
89         }
90 }
91
92 sub FromUnionToPythonFunction($$$)
93 {
94         my ($self, $type, $switch, $name) = @_;
95
96         $self->pidl("switch ($switch) {");
97         $self->indent;
98
99         foreach my $e (@{$type->{ELEMENTS}}) {
100                 my $conv;
101                 
102                 if ($e->{NAME}) {
103                         $conv = $self->ConvertObjectToPython($e->{TYPE}, "$name->$e->{NAME}");
104                 } else {
105                         $conv = "Py_None";
106                 }
107                 if (defined($e->{CASE})) {
108                         $self->pidl("$e->{CASE}: return $conv;");
109                 } else {
110                         $self->pidl("default: return $conv;");
111                 }
112         }
113
114         $self->deindent;
115         $self->pidl("}");
116
117         $self->pidl("PyErr_SetString(PyExc_TypeError, \"unknown union level\");");
118         $self->pidl("return NULL;");
119 }
120
121 sub FromPythonToUnionFunction($$$$$)
122 {
123         my ($self, $type, $typename, $switch, $mem_ctx, $name) = @_;
124
125         my $has_default = 0;
126
127         $self->pidl("$typename *ret = talloc_zero($mem_ctx, $typename);");
128
129         $self->pidl("switch ($switch) {");
130         $self->indent;
131
132         foreach my $e (@{$type->{ELEMENTS}}) {
133                 if (defined($e->{CASE})) {
134                         $self->pidl("$e->{CASE}:");
135                 } else {
136                         $has_default = 1;
137                         $self->pidl("default:");
138                 }
139                 $self->indent;
140                 if ($e->{NAME}) {
141                         $self->ConvertObjectFromPython($mem_ctx, $e->{TYPE}, $name, "ret->$e->{NAME}", "talloc_free(ret); return NULL;");
142                 }
143                 $self->pidl("break;");
144                 $self->deindent;
145                 $self->pidl("");
146         }
147
148         if (!$has_default) {
149                 $self->pidl("default:");
150                 $self->indent;
151                 $self->pidl("PyErr_SetString(PyExc_TypeError, \"invalid union level value\");");
152                 $self->pidl("talloc_free(ret);");
153                 $self->pidl("ret = NULL;");
154                 $self->deindent;
155         }
156
157         $self->deindent;
158         $self->pidl("}");
159         $self->pidl("");
160         $self->pidl("return ret;");
161 }
162
163 sub PythonStruct($$$$)
164 {
165         my ($self, $name, $cname, $d) = @_;
166
167         $self->pidl("");
168
169         $self->pidl("static PyObject *py_$name\_getattr(PyObject *obj, char *name)");
170         $self->pidl("{");
171         $self->indent;
172         $self->pidl("$cname *object = py_talloc_get_type(obj, $cname);");
173         foreach my $e (@{$d->{ELEMENTS}}) {
174                 $self->pidl("if (!strcmp(name, \"$e->{NAME}\")) {");
175                 my $varname = "object->$e->{NAME}";
176                 $self->indent;
177                 $self->pidl("return ".$self->ConvertObjectToPython($e->{TYPE}, $varname) . ";");
178                 $self->deindent;
179                 $self->pidl("}");
180         }
181         $self->pidl("PyErr_SetString(PyExc_AttributeError, \"no such attribute\");");
182         $self->pidl("return NULL;");
183         $self->deindent;
184         $self->pidl("}");
185         $self->pidl("");
186
187         $self->pidl("static int py_$name\_setattr(PyObject *py_obj, char *name, PyObject *value)");
188         $self->pidl("{");
189         $self->indent;
190         $self->pidl("$cname *object = py_talloc_get_type(py_obj, $cname);");
191         $self->pidl("TALLOC_CTX *mem_ctx = py_talloc_get_mem_ctx(py_obj);");
192         foreach my $e (@{$d->{ELEMENTS}}) {
193                 $self->pidl("if (!strcmp(name, \"$e->{NAME}\")) {");
194                 my $varname = "object->$e->{NAME}";
195                 $self->indent;
196                 if ($e->{ORIGINAL}->{POINTERS} > 0) {
197                         $self->pidl("talloc_free($varname);");
198                 }
199                 $self->ConvertObjectFromPython("mem_ctx", $e->{TYPE}, "value", $varname, "talloc_free(mem_ctx); return -1;");
200                 $self->pidl("return 0;");
201                 $self->deindent;
202                 $self->pidl("}");
203         }
204         $self->pidl("PyErr_SetString(PyExc_AttributeError, \"no such attribute\");");
205         $self->pidl("return -1;");
206         $self->deindent;
207         $self->pidl("}");
208         $self->pidl("");
209
210         $self->pidl_hdr("PyAPI_DATA(PyTypeObject) $name\_Type;\n");
211         $self->pidl_hdr("#define $name\_Check(op) PyObject_TypeCheck(op, &$name\_Type)\n");
212         $self->pidl_hdr("#define $name\_CheckExact(op) ((op)->ob_type == &$name\_Type)\n");
213         $self->pidl_hdr("\n");
214         $self->pidl("PyTypeObject $name\_Type = {");
215         $self->indent;
216         $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
217         $self->pidl(".tp_name = \"$name\",");
218         $self->pidl(".tp_basicsize = sizeof(py_talloc_Object),");
219         $self->pidl(".tp_dealloc = py_talloc_dealloc,");
220         $self->pidl(".tp_getattr = py_$name\_getattr,");
221         $self->pidl(".tp_setattr = py_$name\_setattr,");
222         $self->pidl(".tp_repr = py_talloc_default_repr,");
223         $self->deindent;
224         $self->pidl("};");
225
226         $self->pidl("");
227
228         my $py_fnname = "py_$name";
229         $self->pidl("static PyObject *$py_fnname(PyObject *self, PyObject *args)");
230         $self->pidl("{");
231         $self->indent;
232         $self->pidl("$cname *ret = talloc_zero(NULL, $cname);");
233         $self->pidl("return py_talloc_import(&$name\_Type, ret);");
234         $self->deindent;
235         $self->pidl("}");
236         $self->pidl("");
237
238         return $py_fnname;
239 }
240
241 sub PythonFunction($$$)
242 {
243         my ($self, $fn, $iface) = @_;
244
245         $self->pidl("static PyObject *py_$fn->{NAME}(PyObject *self, PyObject *args)");
246         $self->pidl("{");
247         $self->indent;
248         $self->pidl("$iface\_InterfaceObject *iface = ($iface\_InterfaceObject *)self;");
249         $self->pidl("NTSTATUS status;");
250         $self->pidl("TALLOC_CTX *mem_ctx = talloc_new(NULL);");
251         $self->pidl("struct $fn->{NAME} r;");
252         $self->pidl("PyObject *result;");
253         my $result_size = 0;
254
255         foreach my $e (@{$fn->{ELEMENTS}}) {
256                 if (grep(/in/,@{$e->{DIRECTION}})) {
257                         $self->pidl("PyObject *py_$e->{NAME};");
258                 }
259                 if (grep(/out/,@{$e->{DIRECTION}})) {
260                         $result_size++;
261                 }
262         }
263         if ($result_size > 0) {
264                 $self->pidl("");
265                 $self->pidl("ZERO_STRUCT(r.out);");
266         }
267         if ($fn->{RETURN_TYPE}) {
268                 $result_size++;
269         }
270
271         foreach my $e (@{$fn->{ELEMENTS}}) {
272                 if (grep(/in/,@{$e->{DIRECTION}})) {
273                         $self->ConvertObjectFromPython("mem_ctx", $e->{TYPE}, "py_$e->{NAME}", "r.in.$e->{NAME}", "talloc_free(mem_ctx); return NULL;");
274                 }
275         }
276         $self->pidl("status = dcerpc_$fn->{NAME}(iface->pipe, mem_ctx, &r);");
277         $self->handle_ntstatus("status", "NULL", "mem_ctx");
278
279         $self->pidl("result = PyTuple_New($result_size);");
280
281         my $i = 0;
282
283         foreach my $e (@{$fn->{ELEMENTS}}) {
284                 if (grep(/out/,@{$e->{DIRECTION}})) {
285                         $self->pidl("PyTuple_SetItem(result, $i, " . $self->ConvertObjectToPython($e->{TYPE}, "r.out.$e->{NAME}") . ");");
286
287                         $i++;
288                 }
289         }
290
291         if (defined($fn->{RETURN_TYPE})) {
292                 $self->pidl("PyTuple_SetItem(result, $i, " . $self->ConvertObjectToPython($fn->{RETURN_TYPE}, "r.out.result") . ");");
293         }
294
295         $self->pidl("talloc_free(mem_ctx);");
296         $self->pidl("return result;");
297         $self->deindent;
298         $self->pidl("}");
299         $self->pidl("");
300 }
301
302 sub handle_ntstatus($$$$)
303 {
304         my ($self, $var, $retval, $mem_ctx) = @_;
305
306         $self->pidl("if (NT_STATUS_IS_ERR($var)) {");
307         $self->indent;
308         $self->pidl("PyErr_SetString(PyExc_RuntimeError, nt_errstr($var));");
309         $self->pidl("talloc_free($mem_ctx);") if ($mem_ctx);
310         $self->pidl("return $retval;");
311         $self->deindent;
312         $self->pidl("}");
313         $self->pidl("");
314 }
315
316 sub PythonType($$$)
317 {
318         my ($self, $d, $interface, $basename) = @_;
319
320         my $actual_ctype = $d;
321         if ($actual_ctype->{TYPE} eq "TYPEDEF") {
322                 $actual_ctype = $actual_ctype->{DATA};
323         }
324
325         if ($actual_ctype->{TYPE} eq "STRUCT") {
326                 my $py_fnname;
327                 if ($d->{TYPE} eq "STRUCT") {
328                         $py_fnname = $self->PythonStruct($d->{NAME}, mapTypeName($d), $d);
329                 } else {
330                         $py_fnname = $self->PythonStruct($d->{NAME}, mapTypeName($d), $d->{DATA});
331                 }
332
333                 my $fn_name = $d->{NAME};
334
335                 $fn_name =~ s/^$interface->{NAME}_//;
336                 $fn_name =~ s/^$basename\_//;
337
338                 $self->register_module_method($fn_name, $py_fnname, "METH_NOARGS", "NULL");
339         }
340
341         if ($d->{TYPE} eq "ENUM" or $d->{TYPE} eq "BITMAP") {
342                 $self->EnumAndBitmapConsts($d->{NAME}, $d);
343         }
344
345         if ($d->{TYPE} eq "TYPEDEF" and ($d->{DATA}->{TYPE} eq "ENUM" or $d->{DATA}->{TYPE} eq "BITMAP")) {
346                 $self->EnumAndBitmapConsts($d->{NAME}, $d->{DATA});
347         }
348
349         if ($actual_ctype->{TYPE} eq "UNION") {
350                 $self->pidl("PyObject *py_import_$d->{NAME}(int level, " .mapTypeName($d) . " *in)");
351                 $self->pidl("{");
352                 $self->indent;
353                 $self->FromUnionToPythonFunction($actual_ctype, "level", "in") if ($actual_ctype->{TYPE} eq "UNION");
354                 $self->deindent;
355                 $self->pidl("}");
356                 $self->pidl("");
357
358                 $self->pidl(mapTypeName($d) . " *py_export_$d->{NAME}(TALLOC_CTX *mem_ctx, int level, PyObject *in)");
359                 $self->pidl("{");
360                 $self->indent;
361                 $self->FromPythonToUnionFunction($actual_ctype, mapTypeName($d), "level", "mem_ctx", "in") if ($actual_ctype->{TYPE} eq "UNION");
362                 $self->deindent;
363                 $self->pidl("}");
364                 $self->pidl("");
365         }
366 }
367
368 sub Interface($$$)
369 {
370         my($self,$interface,$basename) = @_;
371
372         $self->pidl_hdr("#ifndef _HEADER_PYTHON_$interface->{NAME}\n");
373         $self->pidl_hdr("#define _HEADER_PYTHON_$interface->{NAME}\n\n");
374
375         $self->pidl_hdr("\n");
376
377         $self->Const($_) foreach (@{$interface->{CONSTS}});
378
379         foreach my $d (@{$interface->{TYPES}}) {
380                 next if has_property($d, "nopython");
381
382                 $self->PythonType($d, $interface, $basename);
383         }
384
385         $self->pidl_hdr("PyAPI_DATA(PyTypeObject) $interface->{NAME}_InterfaceType;\n");
386         $self->pidl("typedef struct {");
387         $self->indent;
388         $self->pidl("PyObject_HEAD");
389         $self->pidl("struct dcerpc_pipe *pipe;");
390         $self->deindent;
391         $self->pidl("} $interface->{NAME}_InterfaceObject;");
392
393         $self->pidl("");
394
395         foreach my $d (@{$interface->{FUNCTIONS}}) {
396                 next if not defined($d->{OPNUM});
397                 next if has_property($d, "nopython");
398
399                 $self->PythonFunction($d, $interface->{NAME});
400         }
401
402         $self->pidl("static PyMethodDef interface_$interface->{NAME}\_methods[] = {");
403         $self->indent;
404         foreach my $d (@{$interface->{FUNCTIONS}}) {
405                 next if not defined($d->{OPNUM});
406                 next if has_property($d, "nopython");
407
408                 my $fn_name = $d->{NAME};
409
410                 $fn_name =~ s/^$interface->{NAME}_//;
411
412                 $self->pidl("{ \"$fn_name\", (PyCFunction)py_$d->{NAME}, METH_VARARGS|METH_KEYWORDS, NULL },");
413         }
414         $self->pidl("{ NULL, NULL, 0, NULL }");
415         $self->deindent;
416         $self->pidl("};");
417         $self->pidl("");
418
419         $self->pidl("static void interface_$interface->{NAME}_dealloc(PyObject* self)");
420         $self->pidl("{");
421         $self->indent;
422         $self->pidl("$interface->{NAME}_InterfaceObject *interface = ($interface->{NAME}_InterfaceObject *)self;");
423         $self->pidl("talloc_free(interface->pipe);");
424         $self->pidl("PyObject_Del(self);");
425         $self->deindent;
426         $self->pidl("}");
427         $self->pidl("");
428
429         $self->pidl("static PyObject *interface_$interface->{NAME}_getattr(PyObject *obj, char *name)");
430         $self->pidl("{");
431         $self->indent;
432         $self->pidl("return Py_FindMethod(interface_$interface->{NAME}\_methods, obj, name);");
433         $self->deindent;
434         $self->pidl("}");
435
436         $self->pidl("");
437
438         $self->pidl("PyTypeObject $interface->{NAME}_InterfaceType = {");
439         $self->indent;
440         $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
441         $self->pidl(".tp_name = \"$interface->{NAME}\",");
442         $self->pidl(".tp_basicsize = sizeof($interface->{NAME}_InterfaceObject),");
443         $self->pidl(".tp_dealloc = interface_$interface->{NAME}_dealloc,");
444         $self->pidl(".tp_getattr = interface_$interface->{NAME}_getattr,");
445         $self->deindent;
446         $self->pidl("};");
447
448         $self->pidl("");
449
450         $self->register_module_method($interface->{NAME}, "interface_$interface->{NAME}", "METH_VARARGS|METH_KEYWORDS", "NULL");
451         $self->pidl("static PyObject *interface_$interface->{NAME}(PyObject *self, PyObject *args)");
452         $self->pidl("{");
453         $self->indent;
454         $self->pidl("$interface->{NAME}_InterfaceObject *ret;");
455         $self->pidl("const char *binding_string;");
456         $self->pidl("struct cli_credentials *credentials;");
457         $self->pidl("struct loadparm_context *lp_ctx;");
458         $self->pidl("TALLOC_CTX *mem_ctx = NULL;");
459         $self->pidl("NTSTATUS status;");
460         $self->pidl("");
461
462         # FIXME: Arguments: binding string, credentials, loadparm context
463         $self->pidl("ret = PyObject_New($interface->{NAME}_InterfaceObject, &$interface->{NAME}_InterfaceType);");
464         $self->pidl("");
465
466         $self->pidl("status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string, ");
467         $self->pidl("             &ndr_table_$interface->{NAME}, credentials, NULL, lp_ctx);");
468         $self->handle_ntstatus("status", "NULL", "mem_ctx");
469
470         $self->pidl("return (PyObject *)ret;");
471         $self->deindent;
472         $self->pidl("}");
473         
474         $self->pidl("");
475
476         $self->pidl_hdr("\n");
477         $self->pidl_hdr("#endif /* _HEADER_NDR_$interface->{NAME} */\n");
478 }
479
480 sub register_module_method($$$$$)
481 {
482         my ($self, $fn_name, $pyfn_name, $flags, $doc) = @_;
483
484         push (@{$self->{module_methods}}, [$fn_name, $pyfn_name, $flags, $doc])
485 }
486
487 sub ConvertObjectFromPython($$$$$$)
488 {
489         my ($self, $mem_ctx, $ctype, $cvar, $target, $fail) = @_;
490
491         die("undef type for $cvar") unless(defined($ctype));
492
493         if (ref($ctype) ne "HASH") {
494                 $ctype = getType($ctype);
495         }
496
497         if (ref($ctype) ne "HASH") {
498                 $self->pidl("$target = FIXME($cvar);");
499                 return;
500         }
501
502         my $actual_ctype = $ctype;
503         if ($ctype->{TYPE} eq "TYPEDEF") {
504                 $actual_ctype = $ctype->{DATA};
505         }
506
507         if ($actual_ctype->{TYPE} eq "ENUM" or $actual_ctype->{TYPE} eq "BITMAP" or 
508                 $actual_ctype->{TYPE} eq "SCALAR" and (
509                 expandAlias($actual_ctype->{NAME}) =~ /^(u?int[0-9]+|hyper|NTTIME|time_t|NTTIME_hyper|NTTIME_1sec|dlong|udlong|udlongr)$/)) {
510                 $self->pidl("PY_CHECK_TYPE(PyInt, $cvar, $fail);");
511                 $self->pidl("$target = PyInt_AsLong($cvar);");
512                 return;
513         }
514
515         if ($actual_ctype->{TYPE} eq "STRUCT") {
516                 $self->pidl("PY_CHECK_TYPE($ctype->{NAME}, $cvar, $fail);");
517                 $self->pidl("$target = py_talloc_get_ptr($cvar);");
518                 return;
519         }
520
521         if ($actual_ctype->{TYPE} eq "UNION") {
522                 $self->pidl("$target = py_export_$ctype->{NAME}($cvar);");
523                 return;
524         }
525
526         if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "DATA_BLOB") {
527                 $self->pidl("$target = data_blob_talloc($mem_ctx, PyString_AsString($cvar), PyString_Size($cvar));");
528                 return;
529         }
530
531         if ($actual_ctype->{TYPE} eq "SCALAR" and 
532                 ($actual_ctype->{NAME} eq "string" or $actual_ctype->{NAME} eq "nbt_string" or $actual_ctype->{NAME} eq "nbt_name" or $actual_ctype->{NAME} eq "wrepl_nbt_name")) {
533                 $self->pidl("$target = talloc_strdup($mem_ctx, PyString_AsString($cvar));");
534                 return;
535         }
536
537         if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "ipv4address") {
538                 $self->pidl("$target = FIXME($cvar);");
539                 return;
540                 }
541
542
543         if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "NTSTATUS") {
544                 $self->pidl("$target = PyInt_AsLong($cvar);");
545                 return;
546         }
547
548         if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "WERROR") {
549                 $self->pidl("$target = PyInt_AsLong($cvar);");
550                 return;
551         }
552
553         if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "string_array") {
554                 $self->pidl("$target = FIXME($cvar);");
555                 return;
556         }
557
558         if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "pointer") {
559                 $self->pidl("$target = PyCObject_AsVoidPtr($cvar);");
560                 return;
561         }
562
563         die("unknown type ".mapTypeName($ctype) . ": $cvar");
564 }
565
566 sub ConvertScalarToPython($$$)
567 {
568         my ($self, $ctypename, $cvar) = @_;
569
570         die("expected string for $cvar, not $ctypename") if (ref($ctypename) eq "HASH");
571
572         $ctypename = expandAlias($ctypename);
573
574         if ($ctypename =~ /^(int|long|char|u?int[0-9]+|hyper|dlong|udlong|udlongr|time_t|NTTIME_hyper|NTTIME|NTTIME_1sec)$/) {
575                 return "PyInt_FromLong($cvar)";
576         }
577
578         if ($ctypename eq "DATA_BLOB") {
579                 return "PyString_FromStringAndSize($cvar->data, $cvar->length)";
580         }
581
582         if ($ctypename eq "NTSTATUS") {
583                 return "PyInt_FromLong(NT_STATUS_V($cvar))";
584         }
585
586         if ($ctypename eq "WERROR") {
587                 return "PyInt_FromLong(W_ERROR_V($cvar))";
588         }
589
590         if (($ctypename eq "string" or $ctypename eq "nbt_string" or $ctypename eq "nbt_name" or $ctypename eq "wrepl_nbt_name")) {
591                 return "PyString_FromString($cvar)";
592         }
593
594         if ($ctypename eq "string_array") { return "FIXME($cvar)"; }
595
596         if ($ctypename eq "ipv4address") { return "FIXME($cvar)"; }
597         if ($ctypename eq "pointer") {
598                 return "PyCObject_FromVoidPtr($cvar, talloc_free)";
599         }
600
601         die("Unknown scalar type $ctypename");
602 }
603
604 sub ConvertObjectToPython($$$)
605 {
606         my ($self, $ctype, $cvar) = @_;
607
608         die("undef type for $cvar") unless(defined($ctype));
609
610         if (ref($ctype) ne "HASH") {
611                 if (not hasType($ctype)) {
612                         if (ref($ctype) eq "HASH") {
613                                 return "py_import_$ctype->{TYPE}_$ctype->{NAME}($cvar)";
614                         } else {
615                                 return "py_import_$ctype($cvar)"; # best bet
616                         }
617                 }
618
619                 $ctype = getType($ctype);
620         }
621
622         my $actual_ctype = $ctype;
623         if ($ctype->{TYPE} eq "TYPEDEF") {
624                 $actual_ctype = $ctype->{DATA};
625         }
626
627         if ($actual_ctype->{TYPE} eq "ENUM") {
628                 return $self->ConvertScalarToPython(Parse::Pidl::Typelist::enum_type_fn($actual_ctype), $cvar);
629         }
630
631         if ($actual_ctype->{TYPE} eq "BITMAP") {
632                 return $self->ConvertScalarToPython(Parse::Pidl::Typelist::bitmap_type_fn($actual_ctype), $cvar);
633         }
634
635         if ($actual_ctype->{TYPE} eq "SCALAR") {
636                 return $self->ConvertScalarToPython($actual_ctype->{NAME}, $cvar);
637         }
638
639         if ($actual_ctype->{TYPE} eq "UNION") {
640                 return "py_import_$ctype->{NAME}($cvar)";
641         }
642
643         if ($actual_ctype->{TYPE} eq "STRUCT") {
644                 # FIXME: if $cvar is not a pointer, do a talloc_dup()
645                 return "py_talloc_import(&$ctype->{NAME}_Type, $cvar)";
646         }
647
648         die("unknown type ".mapTypeName($ctype) . ": $cvar");
649 }
650
651 sub Parse($$$$$)
652 {
653     my($self,$basename,$ndr,$ndr_hdr,$hdr) = @_;
654     
655     my $py_hdr = $hdr;
656     $py_hdr =~ s/ndr_([^\/]+)$/py_$1/g;
657
658     $self->pidl_hdr("/* header auto-generated by pidl */\n\n");
659         
660     $self->pidl("
661 /* Python wrapper functions auto-generated by pidl */
662 #include \"includes.h\"
663 #include <Python.h>
664 #include \"librpc/rpc/dcerpc.h\"
665 #include \"scripting/python/pytalloc.h\"
666 #include \"$hdr\"
667 #include \"$ndr_hdr\"
668 #include \"$py_hdr\"
669
670 #define PY_CHECK_TYPE(type, var, fail) \\
671         if (!type ## _Check(var)) {\\
672                 PyErr_Format(PyExc_TypeError, \"Expected type %s\", type ## _Type.tp_name); \\
673                 fail; \\
674         }
675 ");
676
677         foreach my $x (@$ndr) {
678             ($x->{TYPE} eq "INTERFACE") && $self->Interface($x, $basename);
679                 ($x->{TYPE} eq "IMPORT") && $self->Import(@{$x->{PATHS}});
680         }
681         
682         $self->pidl("static PyMethodDef $basename\_methods[] = {");
683         $self->indent;
684         foreach (@{$self->{module_methods}}) {
685                 my ($fn_name, $pyfn_name, $flags, $doc) = @$_;
686                 $self->pidl("{ \"$fn_name\", (PyCFunction)$pyfn_name, $flags, $doc },");
687         }
688         
689         $self->pidl("{ NULL, NULL, 0, NULL }");
690         $self->deindent;
691         $self->pidl("};");
692
693         $self->pidl("");
694
695         $self->pidl("void init$basename(void)");
696         $self->pidl("{");
697         $self->indent;
698         $self->pidl("PyObject *m;");
699         $self->pidl("m = Py_InitModule(\"$basename\", $basename\_methods);");
700         foreach my $name (keys %{$self->{constants}}) {
701                 my $py_obj;
702                 my ($ctype, $cvar) = @{$self->{constants}->{$name}};
703                 if ($cvar =~ /^[0-9]+$/ or $cvar =~ /^0x[0-9a-fA-F]+$/) {
704                         $py_obj = "PyInt_FromLong($cvar)";
705                 } elsif ($cvar =~ /^".*"$/) {
706                         $py_obj = "PyString_FromString($cvar)";
707                 } else {
708                         $py_obj = $self->ConvertObjectToPython($ctype, $cvar);
709                 }
710
711                 $self->pidl("PyModule_AddObject(m, \"$name\", $py_obj);");
712         }
713         $self->deindent;
714         $self->pidl("}");
715     return ($self->{res_hdr}, $self->{res});
716 }
717
718 1;