pidl: Print actual type names in errors about unknown origins rather than 'HASH(0x...
[ira/wip.git] / 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 qw(warning fatal error);
13 use Parse::Pidl::Typelist qw(hasType resolveType getType mapTypeName expandAlias);
14 use Parse::Pidl::Util qw(has_property ParseExpr unmake_str);
15 use Parse::Pidl::NDR qw(GetPrevLevel GetNextLevel ContainsDeferred is_charset_array);
16 use Parse::Pidl::CUtil qw(get_value_of get_pointer_to);
17 use Parse::Pidl::Samba4 qw(ArrayDynamicallyAllocated);
18 use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv GenerateFunctionOutEnv EnvSubstituteValue GenerateStructEnv);
19
20 use vars qw($VERSION);
21 $VERSION = '0.01';
22
23 sub new($) {
24         my ($class) = @_;
25         my $self = { res => "", res_hdr => "", tabs => "", constants => {},
26                      module_methods => [], module_objects => [], ready_types => [],
27                                  module_imports => [], type_imports => {},
28                                  patch_type_calls => [], readycode => [] };
29         bless($self, $class);
30 }
31
32 sub pidl_hdr ($$)
33 {
34         my $self = shift;
35         $self->{res_hdr} .= shift;
36 }
37
38 sub pidl($$)
39 {
40         my ($self, $d) = @_;
41         if ($d) {
42                 if ((!($d =~ /^#/))) {
43                         $self->{res} .= $self->{tabs};
44                 }
45                 $self->{res} .= $d;
46         }
47         $self->{res} .= "\n";
48 }
49
50 sub indent($)
51 {
52         my ($self) = @_;
53         $self->{tabs} .= "\t";
54 }
55
56 sub deindent($)
57 {
58         my ($self) = @_;
59         $self->{tabs} = substr($self->{tabs}, 0, -1);
60 }
61
62 sub PrettifyTypeName($$)
63 {
64         my ($name, $basename) = @_;
65
66         $name =~ s/^$basename\_//;
67
68         return $name;
69 }
70
71 sub Import
72 {
73         my $self = shift;
74         my @imports = @_;
75         foreach (@imports) {
76                 $_ = unmake_str($_);
77                 s/\.idl$//;
78                 $self->pidl_hdr("#include \"librpc/gen_ndr/$_\.h\"\n");
79                 $self->register_module_import($_);
80         }
81 }
82
83 sub Const($$)
84 {
85     my ($self, $const) = @_;
86         $self->register_constant($const->{NAME}, $const->{DTYPE}, $const->{VALUE});
87 }
88
89 sub register_constant($$$$)
90 {
91         my ($self, $name, $type, $value) = @_;
92
93         $self->{constants}->{$name} = [$type, $value];
94 }
95
96 sub EnumAndBitmapConsts($$$)
97 {
98         my ($self, $name, $d) = @_;
99
100         return unless (defined($d->{ELEMENTS}));
101
102         foreach my $e (@{$d->{ELEMENTS}}) {
103                 $e =~ /^([A-Za-z0-9_]+)/;
104                 my $cname = $1;
105                 
106                 $self->register_constant($cname, $d, $cname);
107         }
108 }
109
110 sub FromUnionToPythonFunction($$$$)
111 {
112         my ($self, $mem_ctx, $type, $switch, $name) = @_;
113
114         $self->pidl("PyObject *ret;");
115         $self->pidl("");
116
117         $self->pidl("switch ($switch) {");
118         $self->indent;
119
120         foreach my $e (@{$type->{ELEMENTS}}) {
121                 $self->pidl("$e->{CASE}:");
122
123                 $self->indent;
124
125                 if ($e->{NAME}) {
126                         $self->ConvertObjectToPython($mem_ctx, {}, $e, "$name->$e->{NAME}", "ret", "return NULL;");
127                 } else {
128                         $self->pidl("ret = Py_None;");
129                 }
130
131                 $self->pidl("return ret;");
132                 $self->pidl("");
133
134                 $self->deindent;
135         }
136
137         $self->deindent;
138         $self->pidl("}");
139
140         $self->pidl("PyErr_SetString(PyExc_TypeError, \"unknown union level\");");
141         $self->pidl("return NULL;");
142 }
143
144 sub FromPythonToUnionFunction($$$$$)
145 {
146         my ($self, $type, $typename, $switch, $mem_ctx, $name) = @_;
147
148         my $has_default = 0;
149
150         $self->pidl("$typename *ret = talloc_zero($mem_ctx, $typename);");
151
152         $self->pidl("switch ($switch) {");
153         $self->indent;
154
155         foreach my $e (@{$type->{ELEMENTS}}) {
156                 $self->pidl("$e->{CASE}:");
157                 if ($e->{CASE} eq "default") { $has_default = 1; }
158                 $self->indent;
159                 if ($e->{NAME}) {
160                         $self->ConvertObjectFromPython({}, $mem_ctx, $e, $name, "ret->$e->{NAME}", "talloc_free(ret); return NULL;");
161                 }
162                 $self->pidl("break;");
163                 $self->deindent;
164                 $self->pidl("");
165         }
166
167         if (!$has_default) {
168                 $self->pidl("default:");
169                 $self->indent;
170                 $self->pidl("PyErr_SetString(PyExc_TypeError, \"invalid union level value\");");
171                 $self->pidl("talloc_free(ret);");
172                 $self->pidl("ret = NULL;");
173                 $self->deindent;
174         }
175
176         $self->deindent;
177         $self->pidl("}");
178         $self->pidl("");
179         $self->pidl("return ret;");
180 }
181
182 sub PythonStruct($$$$$$)
183 {
184         my ($self, $modulename, $prettyname, $name, $cname, $d) = @_;
185
186         my $env = GenerateStructEnv($d, "object");
187
188         $self->pidl("");
189
190         my $getsetters = "NULL";
191
192         if ($#{$d->{ELEMENTS}} > -1) {
193                 foreach my $e (@{$d->{ELEMENTS}}) {
194                         my $varname = "object->$e->{NAME}";
195                         $self->pidl("static PyObject *py_$name\_get_$e->{NAME}(PyObject *obj, void *closure)");
196                         $self->pidl("{");
197                         $self->indent;
198                         $self->pidl("$cname *object = ($cname *)py_talloc_get_ptr(obj);");
199                         $self->pidl("PyObject *py_$e->{NAME};");
200                         $self->ConvertObjectToPython("py_talloc_get_mem_ctx(obj)", $env, $e, $varname, "py_$e->{NAME}", "return NULL;");
201                         $self->pidl("return py_$e->{NAME};");
202                         $self->deindent;
203                         $self->pidl("}");
204                         $self->pidl("");
205
206                         $self->pidl("static int py_$name\_set_$e->{NAME}(PyObject *py_obj, PyObject *value, void *closure)");
207                         $self->pidl("{");
208                         $self->indent;
209                         $self->pidl("$cname *object = ($cname *)py_talloc_get_ptr(py_obj);");
210                         my $mem_ctx = "py_talloc_get_mem_ctx(py_obj)";
211                         my $l = $e->{LEVELS}[0];
212                         my $nl = GetNextLevel($e, $l);
213                         if ($l->{TYPE} eq "POINTER" and 
214                                 not ($nl->{TYPE} eq "ARRAY" and ($nl->{IS_FIXED} or is_charset_array($e, $nl))) and
215                                 not ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE}))) {
216                                 $self->pidl("talloc_free($varname);");
217                         }
218                         $self->ConvertObjectFromPython($env, $mem_ctx, $e, "value", $varname, "return -1;");
219                         $self->pidl("return 0;");
220                         $self->deindent;
221                         $self->pidl("}");
222                         $self->pidl("");
223                 }
224
225                 $getsetters = "py_$name\_getsetters";
226                 $self->pidl("static PyGetSetDef ".$getsetters."[] = {");
227                 $self->indent;
228                 foreach my $e (@{$d->{ELEMENTS}}) {
229                         $self->pidl("{ discard_const_p(char, \"$e->{NAME}\"), py_$name\_get_$e->{NAME}, py_$name\_set_$e->{NAME} },");
230                 }
231                 $self->pidl("{ NULL }");
232                 $self->deindent;
233                 $self->pidl("};");
234                 $self->pidl("");
235         }
236
237         $self->pidl("static PyObject *py_$name\_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)");
238         $self->pidl("{");
239         $self->indent;
240         $self->pidl("return py_talloc_new($cname, type);");
241         $self->deindent;
242         $self->pidl("}");
243         $self->pidl("");
244
245         my $py_methods = "NULL";
246
247         # If the struct is not public there ndr_pull/ndr_push functions will 
248         # be static so not callable from here
249         if (has_property($d, "public")) {
250                 $self->pidl("static PyObject *py_$name\_ndr_pack(PyObject *py_obj)");
251                 $self->pidl("{");
252                 $self->indent;
253                 $self->pidl("$cname *object = ($cname *)py_talloc_get_ptr(py_obj);");
254                 $self->pidl("DATA_BLOB blob;");
255                 $self->pidl("enum ndr_err_code err;");
256                 $self->pidl("err = ndr_push_struct_blob(&blob, py_talloc_get_mem_ctx(py_obj), NULL, object, (ndr_push_flags_fn_t)ndr_push_$name);");
257                 $self->pidl("if (err != NDR_ERR_SUCCESS) {");
258                 $self->indent;
259                 $self->pidl("PyErr_SetNdrError(err);");
260                 $self->pidl("return NULL;");
261                 $self->deindent;
262                 $self->pidl("}");
263                 $self->pidl("");
264                 $self->pidl("return PyString_FromStringAndSize((char *)blob.data, blob.length);");
265                 $self->deindent;
266                 $self->pidl("}");
267                 $self->pidl("");
268
269                 $self->pidl("static PyObject *py_$name\_ndr_unpack(PyObject *py_obj, PyObject *args)");
270                 $self->pidl("{");
271                 $self->indent;
272                 $self->pidl("$cname *object = ($cname *)py_talloc_get_ptr(py_obj);");
273                 $self->pidl("DATA_BLOB blob;");
274                 $self->pidl("enum ndr_err_code err;");
275                 $self->pidl("if (!PyArg_ParseTuple(args, \"s#:__ndr_unpack__\", &blob.data, &blob.length))");
276                 $self->pidl("\treturn NULL;");
277                 $self->pidl("");
278
279                 # This disgusting hack works around the fact that ndr_pull_struct_blob_all will always fail on structures with relative pointers.  
280                 # So, map ndr_unpack to ndr_pull_struct_blob_all only if we don't have any relative pointers in this
281                 my $got_relative = 0;
282                 if ($#{$d->{ELEMENTS}} > -1) {
283                         foreach my $e (@{$d->{ELEMENTS}}) {
284                                 my $l = $e->{LEVELS}[0];
285                                 if ($l->{TYPE} eq "POINTER" and ($l->{POINTER_TYPE} eq "relative")) {
286                                         $got_relative = 1;
287                                         last;
288                                 }
289                         }
290                 }
291                 if ($got_relative == 0) {
292                         $self->pidl("err = ndr_pull_struct_blob_all(&blob, py_talloc_get_mem_ctx(py_obj), NULL, object, (ndr_pull_flags_fn_t)ndr_pull_$name);");
293                 } else {
294                         $self->pidl("err = ndr_pull_struct_blob(&blob, py_talloc_get_mem_ctx(py_obj), NULL, object, (ndr_pull_flags_fn_t)ndr_pull_$name);");
295                 }
296                 $self->pidl("if (err != NDR_ERR_SUCCESS) {");
297                 $self->indent;
298                 $self->pidl("PyErr_SetNdrError(err);");
299                 $self->pidl("return NULL;");
300                 $self->deindent;
301                 $self->pidl("}");
302                 $self->pidl("");
303                 $self->pidl("Py_RETURN_NONE;");
304                 $self->deindent;
305                 $self->pidl("}");
306                 $self->pidl("");
307                 $py_methods = "py_$name\_methods";
308                 $self->pidl("static PyMethodDef $py_methods\[] = {");
309                 $self->indent;
310                 $self->pidl("{ \"__ndr_pack__\", (PyCFunction)py_$name\_ndr_pack, METH_NOARGS, \"S.pack() -> blob\\nNDR pack\" },");
311                 $self->pidl("{ \"__ndr_unpack__\", (PyCFunction)py_$name\_ndr_unpack, METH_VARARGS, \"S.unpack(blob) -> None\\nNDR unpack\" },");
312                 $self->pidl("{ NULL, NULL, 0, NULL }");
313                 $self->deindent;
314                 $self->pidl("};");
315                 $self->pidl("");
316         }
317
318         $self->pidl_hdr("staticforward PyTypeObject $name\_Type;\n");
319         $self->pidl("");
320         my $docstring = $self->DocString($d, $name);
321         my $typeobject = "$name\_Type";
322         $self->pidl("static PyTypeObject $typeobject = {");
323         $self->indent;
324         $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
325         $self->pidl(".tp_name = \"$modulename.$prettyname\",");
326         $self->pidl(".tp_basicsize = sizeof(py_talloc_Object),");
327         $self->pidl(".tp_dealloc = py_talloc_dealloc,");
328         $self->pidl(".tp_getset = $getsetters,");
329         $self->pidl(".tp_repr = py_talloc_default_repr,");
330         if ($docstring) {
331                 $self->pidl(".tp_doc = $docstring,");
332         }
333         $self->pidl(".tp_methods = $py_methods,");
334         $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,");
335         $self->pidl(".tp_new = py_$name\_new,");
336         $self->deindent;
337         $self->pidl("};");
338
339         $self->pidl("");
340
341         return "&$typeobject";
342 }
343
344 sub get_metadata_var($)
345 {
346         my ($e) = @_;
347         sub get_var($) { my $x = shift; $x =~ s/\*//g; return $x; }
348
349          if (has_property($e, "length_is")) {
350                 return get_var($e->{PROPERTIES}->{length_is});
351          } elsif (has_property($e, "size_is")) {
352                 return get_var($e->{PROPERTIES}->{size_is});
353          }
354
355          return undef;
356 }
357
358 sub find_metadata_args($)
359 {
360         my ($fn) = @_;
361         my $metadata_args = { in => {}, out => {} };
362
363         # Determine arguments that are metadata for other arguments (size_is/length_is)
364         foreach my $e (@{$fn->{ELEMENTS}}) {
365                 foreach my $dir (@{$e->{DIRECTION}}) {
366                          my $main = get_metadata_var($e);
367                          if ($main) { 
368                                  $metadata_args->{$dir}->{$main} = $e->{NAME}; 
369                          }
370                  }
371         }
372
373         return $metadata_args;
374 }
375
376 sub PythonFunctionUnpackOut($$$)
377 {
378         my ($self, $fn, $fnname) = @_;
379
380         my $outfnname = "unpack_$fnname\_args_out";
381         my $signature = "";
382
383         my $metadata_args = find_metadata_args($fn);
384
385         my $env = GenerateFunctionOutEnv($fn, "r->");
386         my $result_size = 0;
387
388         $self->pidl("static PyObject *$outfnname(struct $fn->{NAME} *r)");
389         $self->pidl("{");
390         $self->indent;
391         $self->pidl("PyObject *result = Py_None;");
392         foreach my $e (@{$fn->{ELEMENTS}}) {
393                 next unless (grep(/out/,@{$e->{DIRECTION}}));
394                 next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or 
395                          ($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}}));
396                 $self->pidl("PyObject *py_$e->{NAME};");
397                 $result_size++;
398         }
399
400         if ($fn->{RETURN_TYPE}) {
401                 $result_size++ unless ($fn->{RETURN_TYPE} eq "WERROR" or $fn->{RETURN_TYPE} eq "NTSTATUS");
402         }
403
404         my $i = 0;
405
406         if ($result_size > 1) {
407                 $self->pidl("result = PyTuple_New($result_size);");
408                 $signature .= "(";
409         } elsif ($result_size == 0) {
410                 $signature .= "None";
411         }
412
413         foreach my $e (@{$fn->{ELEMENTS}}) {
414                 next if ($metadata_args->{out}->{$e->{NAME}});
415                 my $py_name = "py_$e->{NAME}";
416                 if (grep(/out/,@{$e->{DIRECTION}})) {
417                         $self->ConvertObjectToPython("r", $env, $e, "r->out.$e->{NAME}", $py_name, "return NULL;");
418                         if ($result_size > 1) {
419                                 $self->pidl("PyTuple_SetItem(result, $i, $py_name);");
420                                 $i++;
421                                 $signature .= "$e->{NAME}, ";
422                         } else {
423                                 $self->pidl("result = $py_name;");
424                                 $signature .= $e->{NAME};
425                         }
426                 }
427         }
428
429         if (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "NTSTATUS") {
430                 $self->handle_ntstatus("r->out.result", "NULL", undef);
431         } elsif (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "WERROR") {
432                 $self->handle_werror("r->out.result", "NULL", undef);
433         } elsif (defined($fn->{RETURN_TYPE})) {
434                 my $conv = $self->ConvertObjectToPythonData("r", $fn->{RETURN_TYPE}, "r->out.result", $fn);
435                 if ($result_size > 1) {
436                         $self->pidl("PyTuple_SetItem(result, $i, $conv);");
437                 } else {
438                         $self->pidl("result = $conv;");
439                 }
440                 $signature .= "result";
441         }
442
443         if (substr($signature, -2) eq ", ") {
444                 $signature = substr($signature, 0, -2);
445         }
446         if ($result_size > 1) {
447                 $signature .= ")";
448         }
449
450         $self->pidl("return result;");
451         $self->deindent;
452         $self->pidl("}");
453         $self->pidl("");
454
455         return ($outfnname, $signature);
456 }
457
458 sub PythonFunctionPackIn($$$)
459 {
460         my ($self, $fn, $fnname) = @_;
461         my $metadata_args = find_metadata_args($fn);
462
463         my $infnname = "pack_$fnname\_args_in";
464
465         $self->pidl("static bool $infnname(PyObject *args, PyObject *kwargs, struct $fn->{NAME} *r)");
466         $self->pidl("{");
467         $self->indent;
468         my $args_format = "";
469         my $args_string = "";
470         my $args_names = "";
471         my $signature = "";
472
473         foreach my $e (@{$fn->{ELEMENTS}}) {
474                 next unless (grep(/in/,@{$e->{DIRECTION}}));
475                 next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or 
476                                  ($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}}));
477                 $self->pidl("PyObject *py_$e->{NAME};");
478                 $args_format .= "O";
479                 $args_string .= ", &py_$e->{NAME}";
480                 $args_names .= "\"$e->{NAME}\", ";
481                 $signature .= "$e->{NAME}, ";
482         }
483         if (substr($signature, -2) eq ", ") {
484                 $signature = substr($signature, 0, -2);
485         }
486         $self->pidl("const char *kwnames[] = {");
487         $self->indent;
488         $self->pidl($args_names . "NULL");
489         $self->deindent;
490         $self->pidl("};");
491
492         $self->pidl("");
493         $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"$args_format:$fn->{NAME}\", discard_const_p(char *, kwnames)$args_string)) {");
494         $self->indent;
495         $self->pidl("return false;");
496         $self->deindent;
497         $self->pidl("}");
498         $self->pidl("");
499
500         my $env = GenerateFunctionInEnv($fn, "r->");
501
502         my $fail = "return false;";
503         foreach my $e (@{$fn->{ELEMENTS}}) {
504                 next unless (grep(/in/,@{$e->{DIRECTION}}));
505                 if ($metadata_args->{in}->{$e->{NAME}}) {
506                         my $py_var = "py_".$metadata_args->{in}->{$e->{NAME}};
507                         $self->pidl("PY_CHECK_TYPE(&PyList_Type, $py_var, $fail);");
508                         my $val = "PyList_Size($py_var)";
509                         if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
510                                 $self->pidl("r->in.$e->{NAME} = talloc_ptrtype(r, r->in.$e->{NAME});");
511                                 $self->pidl("*r->in.$e->{NAME} = $val;");
512                         } else {
513                                 $self->pidl("r->in.$e->{NAME} = $val;");
514                         }
515                 } else {
516                         $self->ConvertObjectFromPython($env, "r", $e, "py_$e->{NAME}", "r->in.$e->{NAME}", $fail);
517                 }
518         }
519         $self->pidl("return true;");
520         $self->deindent;
521         $self->pidl("}");
522         $self->pidl("");
523         return ($infnname, $signature);
524 }
525
526 sub PythonFunction($$$)
527 {
528         my ($self, $fn, $iface, $prettyname) = @_;
529
530         my $fnname = "py_$fn->{NAME}";
531         my $docstring = $self->DocString($fn, $fn->{NAME});
532
533         my ($insignature, $outsignature);
534         my ($infn, $outfn);
535
536         if (has_property($fn, "todo")) {
537                 unless ($docstring) { $docstring = "NULL"; }
538                 $infn = "NULL";
539                 $outfn = "NULL";
540         } else {
541                 ($infn, $insignature) = $self->PythonFunctionPackIn($fn, $fnname);
542                 ($outfn, $outsignature) = $self->PythonFunctionUnpackOut($fn, $fnname);
543                 my $signature = "S.$prettyname($insignature) -> $outsignature";
544                 if ($docstring) {
545                         $docstring = "\"$signature\\n\\n\"$docstring";
546                 } else {
547                         $docstring = "\"$signature\"";
548                 }
549         }
550
551         return ($infn, $outfn, $docstring);
552 }
553
554 sub handle_werror($$$$)
555 {
556         my ($self, $var, $retval, $mem_ctx) = @_;
557
558         $self->pidl("if (!W_ERROR_IS_OK($var)) {");
559         $self->indent;
560         $self->pidl("PyErr_SetWERROR($var);");
561         $self->pidl("talloc_free($mem_ctx);") if ($mem_ctx);
562         $self->pidl("return $retval;");
563         $self->deindent;
564         $self->pidl("}");
565         $self->pidl("");
566 }
567
568 sub handle_ntstatus($$$$)
569 {
570         my ($self, $var, $retval, $mem_ctx) = @_;
571
572         $self->pidl("if (NT_STATUS_IS_ERR($var)) {");
573         $self->indent;
574         $self->pidl("PyErr_SetNTSTATUS($var);");
575         $self->pidl("talloc_free($mem_ctx);") if ($mem_ctx);
576         $self->pidl("return $retval;");
577         $self->deindent;
578         $self->pidl("}");
579         $self->pidl("");
580 }
581
582 sub PythonType($$$$)
583 {
584         my ($self, $modulename, $d, $interface, $basename) = @_;
585
586         my $actual_ctype = $d;
587         if ($actual_ctype->{TYPE} eq "TYPEDEF") {
588                 $actual_ctype = $actual_ctype->{DATA};
589         }
590
591         if ($actual_ctype->{TYPE} eq "STRUCT") {
592                 my $typeobject;
593                 my $fn_name = PrettifyTypeName($d->{NAME}, $basename);
594
595                 if ($d->{TYPE} eq "STRUCT") {
596                         $typeobject = $self->PythonStruct($modulename, $fn_name, $d->{NAME}, mapTypeName($d), $d);
597                 } else {
598                         $typeobject = $self->PythonStruct($modulename, $fn_name, $d->{NAME}, mapTypeName($d), $d->{DATA});
599                 }
600
601                 $self->register_module_typeobject($fn_name, $typeobject);
602         }
603
604         if ($d->{TYPE} eq "ENUM" or $d->{TYPE} eq "BITMAP") {
605                 $self->EnumAndBitmapConsts($d->{NAME}, $d);
606         }
607
608         if ($d->{TYPE} eq "TYPEDEF" and ($d->{DATA}->{TYPE} eq "ENUM" or $d->{DATA}->{TYPE} eq "BITMAP")) {
609                 $self->EnumAndBitmapConsts($d->{NAME}, $d->{DATA});
610         }
611
612         if ($actual_ctype->{TYPE} eq "UNION" and defined($actual_ctype->{ELEMENTS})) {
613                 $self->pidl("PyObject *py_import_$d->{NAME}(TALLOC_CTX *mem_ctx, int level, " .mapTypeName($d) . " *in)");
614                 $self->pidl("{");
615                 $self->indent;
616                 $self->FromUnionToPythonFunction("mem_ctx", $actual_ctype, "level", "in") if ($actual_ctype->{TYPE} eq "UNION");
617                 $self->deindent;
618                 $self->pidl("}");
619                 $self->pidl("");
620
621                 $self->pidl(mapTypeName($d) . " *py_export_$d->{NAME}(TALLOC_CTX *mem_ctx, int level, PyObject *in)");
622                 $self->pidl("{");
623                 $self->indent;
624                 $self->FromPythonToUnionFunction($actual_ctype, mapTypeName($d), "level", "mem_ctx", "in") if ($actual_ctype->{TYPE} eq "UNION");
625                 $self->deindent;
626                 $self->pidl("}");
627                 $self->pidl("");
628         }
629 }
630
631 sub DocString($$$)
632 {
633         my ($self, $d, $name) = @_;
634         if (has_property($d, "helpstring")) {
635                 my $docstring = uc("py_doc_$name");
636                 $self->pidl("#define $docstring ".has_property($d, "helpstring"));
637                 return $docstring;
638         }
639
640         return undef;
641 }
642
643 sub Interface($$$)
644 {
645         my($self,$interface,$basename) = @_;
646
647         if (has_property($interface, "pyhelper")) {
648                 $self->pidl("#include \"".unmake_str($interface->{PROPERTIES}->{pyhelper})."\"\n");
649         }
650
651         $self->Const($_) foreach (@{$interface->{CONSTS}});
652
653         foreach my $d (@{$interface->{TYPES}}) {
654                 next if has_property($d, "nopython");
655
656                 $self->PythonType($basename, $d, $interface, $basename);
657         }
658
659         if (defined $interface->{PROPERTIES}->{uuid}) {
660                 $self->pidl_hdr("staticforward PyTypeObject $interface->{NAME}_InterfaceType;\n");
661                 $self->pidl("");
662
663                 my @fns = ();
664
665                 foreach my $d (@{$interface->{FUNCTIONS}}) {
666                         next if not defined($d->{OPNUM});
667                         next if has_property($d, "nopython");
668
669                         my $prettyname = $d->{NAME};
670
671                         $prettyname =~ s/^$interface->{NAME}_//;
672                         $prettyname =~ s/^$basename\_//;
673
674                         my ($infn, $outfn, $fndocstring) = $self->PythonFunction($d, $interface->{NAME}, $prettyname);
675
676                         push (@fns, [$infn, $outfn, "dcerpc_$d->{NAME}", $prettyname, $fndocstring, $d->{OPNUM}]);
677                 }
678
679                 $self->pidl("const struct PyNdrRpcMethodDef py_ndr_$interface->{NAME}\_methods[] = {");
680                 $self->indent;
681                 foreach my $d (@fns) {
682                         my ($infn, $outfn, $callfn, $prettyname, $docstring, $opnum) = @$d;
683                         $self->pidl("{ \"$prettyname\", $docstring, (dcerpc_call_fn)$callfn, (py_data_pack_fn)$infn, (py_data_unpack_fn)$outfn, $opnum, &ndr_table_$interface->{NAME} },");
684                 }
685                 $self->pidl("{ NULL }");
686                 $self->deindent;
687                 $self->pidl("};");
688                 $self->pidl("");
689
690                 $self->pidl("static PyObject *interface_$interface->{NAME}_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)");
691                 $self->pidl("{");
692                 $self->indent;
693                 $self->pidl("return py_dcerpc_interface_init_helper(type, args, kwargs, &ndr_table_$interface->{NAME});");
694                 $self->deindent;
695                 $self->pidl("}");
696         
697                 $self->pidl("");
698
699                 my $signature = 
700 "\"$interface->{NAME}(binding, lp_ctx=None, credentials=None) -> connection\\n\"
701 \"\\n\"
702 \"binding should be a DCE/RPC binding string (for example: ncacn_ip_tcp:127.0.0.1)\\n\"
703 \"lp_ctx should be a path to a smb.conf file or a param.LoadParm object\\n\"
704 \"credentials should be a credentials.Credentials object.\\n\\n\"";
705
706                 my $docstring = $self->DocString($interface, $interface->{NAME});
707
708                 if ($docstring) {
709                         $docstring = "$signature$docstring";
710                 } else {
711                         $docstring = $signature;
712                 }
713
714                 $self->pidl("static PyTypeObject $interface->{NAME}_InterfaceType = {");
715                 $self->indent;
716                 $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
717                 $self->pidl(".tp_name = \"$basename.$interface->{NAME}\",");
718                 $self->pidl(".tp_basicsize = sizeof(dcerpc_InterfaceObject),");
719                 $self->pidl(".tp_base = &dcerpc_InterfaceType,");
720                 $self->pidl(".tp_doc = $docstring,");
721                 $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,");
722                 $self->pidl(".tp_new = interface_$interface->{NAME}_new,");
723                 $self->deindent;
724                 $self->pidl("};");
725
726                 $self->pidl("");
727
728                 $self->register_module_typeobject($interface->{NAME}, "&$interface->{NAME}_InterfaceType");
729                 $self->register_module_readycode(["if (!PyInterface_AddNdrRpcMethods(&$interface->{NAME}_InterfaceType, py_ndr_$interface->{NAME}\_methods))", "\treturn;", ""]);
730         }
731
732         $self->pidl_hdr("\n");
733 }
734
735 sub register_module_method($$$$$)
736 {
737         my ($self, $fn_name, $pyfn_name, $flags, $doc) = @_;
738
739         push (@{$self->{module_methods}}, [$fn_name, $pyfn_name, $flags, $doc])
740 }
741
742 sub register_module_typeobject($$$)
743 {
744         my ($self, $name, $py_name) = @_;
745
746         $self->register_module_object($name, "(PyObject *)$py_name");
747
748         $self->check_ready_type($py_name);
749
750         $self->register_patch_type_call($name, $py_name);
751 }
752
753 sub check_ready_type($$)
754 {
755         my ($self, $py_name) = @_;
756         push (@{$self->{ready_types}}, $py_name) unless (grep(/^$py_name$/,@{$self->{ready_types}}));
757 }
758
759 sub register_module_import($$)
760 {
761         my ($self, $basename) = @_;
762
763         push (@{$self->{module_imports}}, $basename) unless (grep(/^$basename$/,@{$self->{module_imports}}));
764 }
765
766 sub use_type_variable($$)
767 {
768         my ($self, $orig_ctype) = @_;
769         my $ctype = resolveType($orig_ctype);
770         unless (defined($ctype->{BASEFILE})) {
771                 return undef;
772         }
773         # If this is an external type, make sure we do the right imports.
774         if (($ctype->{BASEFILE} ne $self->{BASENAME})) {
775                 $self->register_module_import($ctype->{BASEFILE});
776                 unless (defined($self->{type_imports}->{$ctype->{NAME}})) {
777                         $self->{type_imports}->{$ctype->{NAME}} = $ctype->{BASEFILE};
778                 }
779                 return "$ctype->{NAME}_Type";
780         }
781         return "&$ctype->{NAME}_Type";
782 }
783
784 sub register_patch_type_call($$$)
785 {
786         my ($self, $typename, $cvar) = @_;
787
788         push(@{$self->{patch_type_calls}}, [$typename, $cvar]);
789
790 }
791
792 sub register_module_readycode($$)
793 {
794         my ($self, $code) = @_;
795
796         push (@{$self->{readycode}}, @$code);
797 }
798
799 sub register_module_object($$$)
800 {
801         my ($self, $name, $py_name) = @_;
802
803         push (@{$self->{module_objects}}, [$name, $py_name])
804 }
805
806 sub assign($$$)
807 {
808         my ($self, $dest, $src) = @_;
809         if ($dest =~ /^\&/ and $src eq "NULL") {
810                 $self->pidl("memset($dest, 0, sizeof(" . get_value_of($dest) . "));");
811         } elsif ($dest =~ /^\&/) {
812                 $self->pidl("memcpy($dest, $src, sizeof(" . get_value_of($dest) . "));");
813         } else {
814                 $self->pidl("$dest = $src;");
815         }
816 }
817
818 sub ConvertObjectFromPythonData($$$$$$;$)
819 {
820         my ($self, $mem_ctx, $cvar, $ctype, $target, $fail, $location) = @_;
821
822         fatal($location, "undef type for $cvar") unless(defined($ctype));
823
824         $ctype = resolveType($ctype);
825
826         my $actual_ctype = $ctype;
827         if ($actual_ctype->{TYPE} eq "TYPEDEF") {
828                 $actual_ctype = $actual_ctype->{DATA};
829         }
830
831         if ($actual_ctype->{TYPE} eq "ENUM" or $actual_ctype->{TYPE} eq "BITMAP" or 
832                 $actual_ctype->{TYPE} eq "SCALAR" and (
833                 expandAlias($actual_ctype->{NAME}) =~ /^(u?int[0-9]*|hyper|NTTIME|time_t|NTTIME_hyper|NTTIME_1sec|dlong|udlong|udlongr)$/)) {
834                 $self->pidl("PY_CHECK_TYPE(&PyInt_Type, $cvar, $fail);");
835                 $self->pidl("$target = PyInt_AsLong($cvar);");
836                 return;
837         }
838
839         if ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") {
840                 my $ctype_name = $self->use_type_variable($ctype);
841                 unless (defined ($ctype_name)) {
842                         error($location, "Unable to determine origin of type `" . mapTypeName($ctype) . "'");
843                         $self->assign($target, "NULL");
844                         # FIXME:
845                         return;
846                 }
847                 $self->pidl("PY_CHECK_TYPE($ctype_name, $cvar, $fail);");
848                 $self->assign($target, "py_talloc_get_ptr($cvar)");
849                 return;
850         }
851
852         if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "DATA_BLOB") {
853                 $self->pidl("$target = data_blob_talloc($mem_ctx, PyString_AsString($cvar), PyString_Size($cvar));");
854                 return;
855         }
856
857         if ($actual_ctype->{TYPE} eq "SCALAR" and 
858                 ($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")) {
859                 $self->pidl("$target = talloc_strdup($mem_ctx, PyString_AsString($cvar));");
860                 return;
861         }
862
863         if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "ipv4address") {
864                 $self->pidl("$target = PyString_AsString($cvar);");
865                 return;
866                 }
867
868
869         if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "NTSTATUS") {
870                 $self->pidl("$target = NT_STATUS(PyInt_AsLong($cvar));");
871                 return;
872         }
873
874         if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "WERROR") {
875                 $self->pidl("$target = W_ERROR(PyInt_AsLong($cvar));");
876                 return;
877         }
878
879         if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "string_array") {
880                 $self->pidl("$target = PyCObject_AsVoidPtr($cvar);");
881                 return;
882         }
883
884         if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "pointer") {
885                 $self->assign($target, "PyCObject_AsVoidPtr($cvar)");
886                 return;
887         }
888
889         fatal($location, "unknown type `$actual_ctype->{TYPE}' for ".mapTypeName($ctype) . ": $cvar");
890
891 }
892
893 sub ConvertObjectFromPythonLevel($$$$$$$$)
894 {
895         my ($self, $env, $mem_ctx, $py_var, $e, $l, $var_name, $fail) = @_;
896         my $nl = GetNextLevel($e, $l);
897
898         if ($l->{TYPE} eq "POINTER") {
899                 if ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) {
900                         $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, $nl, $var_name, $fail);
901                         return;
902                 }
903                 if ($l->{POINTER_TYPE} ne "ref") {
904                         $self->pidl("if ($py_var == Py_None) {");
905                         $self->indent;
906                         $self->pidl("$var_name = NULL;");
907                         $self->deindent;
908                         $self->pidl("} else {");
909                         $self->indent;
910                 }
911                 $self->pidl("$var_name = talloc_ptrtype($mem_ctx, $var_name);");
912                 $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, $nl, get_value_of($var_name), $fail);
913                 if ($l->{POINTER_TYPE} ne "ref") {
914                         $self->deindent;
915                         $self->pidl("}");
916                 }
917         } elsif ($l->{TYPE} eq "ARRAY") {
918                 my $pl = GetPrevLevel($e, $l);
919                 if ($pl && $pl->{TYPE} eq "POINTER") {
920                         $var_name = get_pointer_to($var_name);
921                 }
922
923                 if (is_charset_array($e, $l)) {
924                         $self->pidl("PY_CHECK_TYPE(&PyUnicode_Type, $py_var, $fail);");
925                         # FIXME: Use Unix charset setting rather than utf-8
926                         $self->pidl($var_name . " = PyString_AsString(PyUnicode_AsEncodedString($py_var, \"utf-8\", \"ignore\"));");
927                 } else {
928                         my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}";
929                         $self->pidl("PY_CHECK_TYPE(&PyList_Type, $py_var, $fail);");
930                         $self->pidl("{");
931                         $self->indent;
932                         $self->pidl("int $counter;");
933                         if (ArrayDynamicallyAllocated($e, $l)) {
934                                 $self->pidl("$var_name = talloc_array_ptrtype($mem_ctx, $var_name, PyList_Size($py_var));");
935                         }
936                         $self->pidl("for ($counter = 0; $counter < PyList_Size($py_var); $counter++) {");
937                         $self->indent;
938                         $self->ConvertObjectFromPythonLevel($env, $var_name, "PyList_GetItem($py_var, $counter)", $e, GetNextLevel($e, $l), $var_name."[$counter]", $fail);
939                         $self->deindent;
940                         $self->pidl("}");
941                         $self->deindent;
942                         $self->pidl("}");
943                 }
944         } elsif ($l->{TYPE} eq "DATA") {
945
946                 if (not Parse::Pidl::Typelist::is_scalar($l->{DATA_TYPE})) {
947                         $var_name = get_pointer_to($var_name);
948                 }
949                 $self->ConvertObjectFromPythonData($mem_ctx, $py_var, $l->{DATA_TYPE}, $var_name, $fail, $e->{ORIGINAL});
950         } elsif ($l->{TYPE} eq "SWITCH") {
951                 $var_name = get_pointer_to($var_name);
952                 my $switch = ParseExpr($l->{SWITCH_IS}, $env, $e);
953                 $self->assign($var_name, "py_export_" . GetNextLevel($e, $l)->{DATA_TYPE} . "($mem_ctx, $switch, $py_var)");
954         } elsif ($l->{TYPE} eq "SUBCONTEXT") {
955                 $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, GetNextLevel($e, $l), $var_name, $fail);
956         } else {
957                 fatal($e->{ORIGINAL}, "unknown level type $l->{TYPE}");
958         }
959 }
960
961 sub ConvertObjectFromPython($$$$$$$)
962 {
963         my ($self, $env, $mem_ctx, $ctype, $cvar, $target, $fail) = @_;
964
965         $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $cvar, $ctype, $ctype->{LEVELS}[0], $target, $fail);
966 }
967
968 sub ConvertScalarToPython($$$)
969 {
970         my ($self, $ctypename, $cvar) = @_;
971
972         die("expected string for $cvar, not $ctypename") if (ref($ctypename) eq "HASH");
973
974         $ctypename = expandAlias($ctypename);
975
976         if ($ctypename =~ /^(char|u?int[0-9]*|hyper|dlong|udlong|udlongr|time_t|NTTIME_hyper|NTTIME|NTTIME_1sec)$/) {
977                 return "PyInt_FromLong($cvar)";
978         }
979
980         if ($ctypename eq "DATA_BLOB") {
981                 return "PyString_FromStringAndSize((char *)($cvar).data, ($cvar).length)";
982         }
983
984         if ($ctypename eq "NTSTATUS") {
985                 return "PyErr_FromNTSTATUS($cvar)";
986         }
987
988         if ($ctypename eq "WERROR") {
989                 return "PyErr_FromWERROR($cvar)";
990         }
991
992         if (($ctypename eq "string" or $ctypename eq "nbt_string" or $ctypename eq "nbt_name" or $ctypename eq "wrepl_nbt_name")) {
993                 return "PyString_FromString($cvar)";
994         }
995
996         # Not yet supported
997         if ($ctypename eq "string_array") { return "PyCObject_FromVoidPtr($cvar)"; }
998         if ($ctypename eq "ipv4address") { return "PyString_FromString($cvar)"; }
999         if ($ctypename eq "pointer") {
1000                 return "PyCObject_FromVoidPtr($cvar, talloc_free)";
1001         }
1002
1003         die("Unknown scalar type $ctypename");
1004 }
1005
1006 sub ConvertObjectToPythonData($$$$$;$)
1007 {
1008         my ($self, $mem_ctx, $ctype, $cvar, $location) = @_;
1009
1010         die("undef type for $cvar") unless(defined($ctype));
1011
1012         $ctype = resolveType($ctype);
1013
1014         my $actual_ctype = $ctype;
1015         if ($actual_ctype->{TYPE} eq "TYPEDEF") {
1016                 $actual_ctype = $actual_ctype->{DATA};
1017         } 
1018         
1019         if ($actual_ctype->{TYPE} eq "ENUM") {
1020                 return $self->ConvertScalarToPython(Parse::Pidl::Typelist::enum_type_fn($actual_ctype), $cvar);
1021         } elsif ($actual_ctype->{TYPE} eq "BITMAP") {
1022                 return $self->ConvertScalarToPython(Parse::Pidl::Typelist::bitmap_type_fn($actual_ctype), $cvar);
1023         } elsif ($actual_ctype->{TYPE} eq "SCALAR") {
1024                 return $self->ConvertScalarToPython($actual_ctype->{NAME}, $cvar);
1025         } elsif ($actual_ctype->{TYPE} eq "UNION") {
1026                 fatal($ctype, "union without discriminant: " . mapTypeName($ctype) . ": $cvar");
1027         } elsif ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") {
1028                 my $ctype_name = $self->use_type_variable($ctype);
1029                 unless (defined($ctype_name)) {
1030                         error($location, "Unable to determine origin of type `" . mapTypeName($ctype) . "'");
1031                         return "NULL"; # FIXME!
1032                 }
1033                 return "py_talloc_import_ex($ctype_name, $mem_ctx, $cvar)";
1034         }
1035
1036         fatal($location, "unknown type $actual_ctype->{TYPE} for ".mapTypeName($ctype) . ": $cvar");
1037 }
1038
1039 sub fail_on_null($$$)
1040 {
1041         my ($self, $var, $fail) = @_;
1042         $self->pidl("if ($var == NULL) {");
1043         $self->indent;
1044         $self->pidl($fail);
1045         $self->deindent;
1046         $self->pidl("}");
1047 }
1048
1049 sub ConvertObjectToPythonLevel($$$$$$)
1050 {
1051         my ($self, $mem_ctx, $env, $e, $l, $var_name, $py_var, $fail) = @_;
1052         my $nl = GetNextLevel($e, $l);
1053
1054         if ($l->{TYPE} eq "POINTER") {
1055                 if ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) {
1056                         $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, $var_name, $py_var, $fail);
1057                         return;
1058                 }
1059                 if ($l->{POINTER_TYPE} ne "ref") {
1060                         $self->pidl("if ($var_name == NULL) {");
1061                         $self->indent;
1062                         $self->pidl("$py_var = Py_None;");
1063                         $self->deindent;
1064                         $self->pidl("} else {");
1065                         $self->indent;
1066                 }
1067                 $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, get_value_of($var_name), $py_var, $fail);
1068                 if ($l->{POINTER_TYPE} ne "ref") {
1069                         $self->deindent;
1070                         $self->pidl("}");
1071                 }
1072         } elsif ($l->{TYPE} eq "ARRAY") {
1073                 my $pl = GetPrevLevel($e, $l);
1074                 if ($pl && $pl->{TYPE} eq "POINTER") {
1075                         $var_name = get_pointer_to($var_name);
1076                 }
1077
1078                 if (is_charset_array($e, $l)) {
1079                         # FIXME: Use Unix charset setting rather than utf-8
1080                         $self->pidl("$py_var = PyUnicode_Decode($var_name, strlen($var_name), \"utf-8\", \"ignore\");");
1081                 } else {
1082                         die("No SIZE_IS for array $var_name") unless (defined($l->{SIZE_IS}));
1083                         my $length = $l->{SIZE_IS};
1084                         if (defined($l->{LENGTH_IS})) {
1085                                 $length = $l->{LENGTH_IS};
1086                         }
1087
1088                         $length = ParseExpr($length, $env, $e);
1089                         $self->pidl("$py_var = PyList_New($length);");
1090                         $self->fail_on_null($py_var, $fail);
1091                         $self->pidl("{");
1092                         $self->indent;
1093                         my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}";
1094                         $self->pidl("int $counter;");
1095                         $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
1096                         $self->indent;
1097                         my $member_var = "py_$e->{NAME}_$l->{LEVEL_INDEX}";
1098                         $self->pidl("PyObject *$member_var;");
1099                         $self->ConvertObjectToPythonLevel($var_name, $env, $e, GetNextLevel($e, $l), $var_name."[$counter]", $member_var, $fail);
1100                         $self->pidl("PyList_SetItem($py_var, $counter, $member_var);");
1101                         $self->deindent;
1102                         $self->pidl("}");
1103                         $self->deindent;
1104                         $self->pidl("}");
1105                 }
1106         } elsif ($l->{TYPE} eq "SWITCH") {
1107                 $var_name = get_pointer_to($var_name);
1108                 my $switch = ParseExpr($l->{SWITCH_IS}, $env, $e);
1109                 $self->pidl("$py_var = py_import_" . GetNextLevel($e, $l)->{DATA_TYPE} . "($mem_ctx, $switch, $var_name);");
1110                 $self->fail_on_null($py_var, $fail);
1111
1112         } elsif ($l->{TYPE} eq "DATA") {
1113                 if (not Parse::Pidl::Typelist::is_scalar($l->{DATA_TYPE})) {
1114                         $var_name = get_pointer_to($var_name);
1115                 }
1116                 my $conv = $self->ConvertObjectToPythonData($mem_ctx, $l->{DATA_TYPE}, $var_name, $e->{ORIGINAL});
1117                 $self->pidl("$py_var = $conv;");
1118         } elsif ($l->{TYPE} eq "SUBCONTEXT") {
1119                 $self->ConvertObjectToPythonLevel($mem_ctx, $env, $e, GetNextLevel($e, $l), $var_name, $py_var, $fail);
1120         } else {
1121                 fatal($e->{ORIGINAL}, "Unknown level type $l->{TYPE} $var_name");
1122         }
1123 }
1124
1125 sub ConvertObjectToPython($$$$$$)
1126 {
1127         my ($self, $mem_ctx, $env, $ctype, $cvar, $py_var, $fail) = @_;
1128
1129         $self->ConvertObjectToPythonLevel($mem_ctx, $env, $ctype, $ctype->{LEVELS}[0], $cvar, $py_var, $fail);
1130 }
1131
1132 sub Parse($$$$$)
1133 {
1134     my($self,$basename,$ndr,$ndr_hdr,$hdr) = @_;
1135
1136         $self->{BASENAME} = $basename;
1137
1138     $self->pidl_hdr("
1139 /* Python wrapper functions auto-generated by pidl */
1140 #include \"includes.h\"
1141 #include <Python.h>
1142 #include \"librpc/rpc/dcerpc.h\"
1143 #include \"lib/talloc/pytalloc.h\"
1144 #include \"librpc/rpc/pyrpc.h\"
1145 #include \"$hdr\"
1146 #include \"$ndr_hdr\"
1147
1148 #ifndef Py_RETURN_NONE
1149 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
1150 #endif
1151
1152 ");
1153
1154         foreach my $x (@$ndr) {
1155                 ($x->{TYPE} eq "IMPORT") && $self->Import(@{$x->{PATHS}});
1156             ($x->{TYPE} eq "INTERFACE") && $self->Interface($x, $basename);
1157         }
1158         
1159         $self->pidl("static PyMethodDef $basename\_methods[] = {");
1160         $self->indent;
1161         foreach (@{$self->{module_methods}}) {
1162                 my ($fn_name, $pyfn_name, $flags, $doc) = @$_;
1163                 $self->pidl("{ \"$fn_name\", (PyCFunction)$pyfn_name, $flags, $doc },");
1164         }
1165
1166         $self->pidl("{ NULL, NULL, 0, NULL }");
1167         $self->deindent;
1168         $self->pidl("};");
1169
1170         $self->pidl("");
1171
1172         $self->pidl("void init$basename(void)");
1173         $self->pidl("{");
1174         $self->indent;
1175         $self->pidl("PyObject *m;");
1176         foreach (@{$self->{module_imports}}) {
1177                 $self->pidl("PyObject *dep_$_;");
1178         }
1179         $self->pidl("");
1180
1181         foreach (@{$self->{module_imports}}) {
1182                 $self->pidl("dep_$_ = PyImport_ImportModule(\"samba.dcerpc.$_\");");
1183                 $self->pidl("if (dep_$_ == NULL)");
1184                 $self->pidl("\treturn;");
1185                 $self->pidl("");
1186         }
1187
1188         foreach (keys %{$self->{type_imports}}) {
1189                 my $basefile = $self->{type_imports}->{$_};
1190                 $self->pidl_hdr("static PyTypeObject *$_\_Type;\n");
1191                 my $pretty_name = PrettifyTypeName($_, $basefile);
1192                 $self->pidl("$_\_Type = (PyTypeObject *)PyObject_GetAttrString(dep_$basefile, \"$pretty_name\");");
1193                 $self->pidl("if ($_\_Type == NULL)");
1194                 $self->pidl("\treturn;");
1195                 $self->pidl("");
1196         }
1197
1198         foreach (@{$self->{ready_types}}) {
1199                 $self->pidl("if (PyType_Ready($_) < 0)");
1200                 $self->pidl("\treturn;");
1201         }
1202
1203         $self->pidl($_) foreach (@{$self->{readycode}});
1204
1205         foreach (@{$self->{patch_type_calls}}) {
1206                 my ($typename, $cvar) = @$_;
1207                 $self->pidl("#ifdef PY_".uc($typename)."_PATCH");
1208                 $self->pidl("PY_".uc($typename)."_PATCH($cvar);");
1209                 $self->pidl("#endif");
1210         }
1211
1212         $self->pidl("");
1213
1214         $self->pidl("m = Py_InitModule3(\"$basename\", $basename\_methods, \"$basename DCE/RPC\");");
1215         $self->pidl("if (m == NULL)");
1216         $self->pidl("\treturn;");
1217         $self->pidl("");
1218         foreach my $name (keys %{$self->{constants}}) {
1219                 my $py_obj;
1220                 my ($ctype, $cvar) = @{$self->{constants}->{$name}};
1221                 if ($cvar =~ /^[0-9]+$/ or $cvar =~ /^0x[0-9a-fA-F]+$/) {
1222                         $py_obj = "PyInt_FromLong($cvar)";
1223                 } elsif ($cvar =~ /^".*"$/) {
1224                         $py_obj = "PyString_FromString($cvar)";
1225                 } else {
1226                         $py_obj = $self->ConvertObjectToPythonData("NULL", expandAlias($ctype), $cvar, undef);
1227                 }
1228
1229                 $self->pidl("PyModule_AddObject(m, \"$name\", $py_obj);");
1230         }
1231
1232         foreach (@{$self->{module_objects}}) {
1233                 my ($object_name, $c_name) = @$_;
1234                 $self->pidl("Py_INCREF($c_name);");
1235                 $self->pidl("PyModule_AddObject(m, \"$object_name\", $c_name);");
1236         }
1237
1238         $self->pidl("#ifdef PY_MOD_".uc($basename)."_PATCH");
1239         $self->pidl("PY_MOD_".uc($basename)."_PATCH(m);");
1240         $self->pidl("#endif");
1241
1242         $self->pidl("");
1243         $self->deindent;
1244         $self->pidl("}");
1245     return ($self->{res_hdr} . $self->{res});
1246 }
1247
1248 1;