pidl:Samba/Python: don't generate methods for functions marked as [todo]
[Samba/nascimento.git] / pidl / lib / Parse / Pidl / Samba4 / Python.pm
bloba6c2738ddca4ce6e9c19df3c9f37b2f22a4152e2
1 ###################################################
2 # Python function wrapper generator
3 # Copyright jelmer@samba.org 2007-2008
4 # released under the GNU GPL
6 package Parse::Pidl::Samba4::Python;
8 use Exporter;
9 @ISA = qw(Exporter);
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);
20 use vars qw($VERSION);
21 $VERSION = '0.01';
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);
32 sub pidl_hdr ($$)
34 my $self = shift;
35 $self->{res_hdr} .= shift;
38 sub pidl($$)
40 my ($self, $d) = @_;
41 if ($d) {
42 if ((!($d =~ /^#/))) {
43 $self->{res} .= $self->{tabs};
45 $self->{res} .= $d;
47 $self->{res} .= "\n";
50 sub indent($)
52 my ($self) = @_;
53 $self->{tabs} .= "\t";
56 sub deindent($)
58 my ($self) = @_;
59 $self->{tabs} = substr($self->{tabs}, 0, -1);
62 sub PrettifyTypeName($$)
64 my ($name, $basename) = @_;
66 $name =~ s/^$basename\_//;
68 return $name;
71 sub Import
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($_);
83 sub Const($$)
85 my ($self, $const) = @_;
86 $self->register_constant($const->{NAME}, $const->{DTYPE}, $const->{VALUE});
89 sub register_constant($$$$)
91 my ($self, $name, $type, $value) = @_;
93 $self->{constants}->{$name} = [$type, $value];
96 sub EnumAndBitmapConsts($$$)
98 my ($self, $name, $d) = @_;
100 return unless (defined($d->{ELEMENTS}));
102 foreach my $e (@{$d->{ELEMENTS}}) {
103 $e =~ /^([A-Za-z0-9_]+)/;
104 my $cname = $1;
106 $self->register_constant($cname, $d, $cname);
110 sub FromUnionToPythonFunction($$$$)
112 my ($self, $mem_ctx, $type, $switch, $name) = @_;
114 $self->pidl("PyObject *ret;");
115 $self->pidl("");
117 $self->pidl("switch ($switch) {");
118 $self->indent;
120 foreach my $e (@{$type->{ELEMENTS}}) {
121 $self->pidl("$e->{CASE}:");
123 $self->indent;
125 if ($e->{NAME}) {
126 $self->ConvertObjectToPython($mem_ctx, {}, $e, "$name->$e->{NAME}", "ret", "return NULL;");
127 } else {
128 $self->pidl("ret = Py_None;");
131 $self->pidl("return ret;");
132 $self->pidl("");
134 $self->deindent;
137 $self->deindent;
138 $self->pidl("}");
140 $self->pidl("PyErr_SetString(PyExc_TypeError, \"unknown union level\");");
141 $self->pidl("return NULL;");
144 sub FromPythonToUnionFunction($$$$$)
146 my ($self, $type, $typename, $switch, $mem_ctx, $name) = @_;
148 my $has_default = 0;
150 $self->pidl("$typename *ret = talloc_zero($mem_ctx, $typename);");
152 $self->pidl("switch ($switch) {");
153 $self->indent;
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;");
162 $self->pidl("break;");
163 $self->deindent;
164 $self->pidl("");
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;
176 $self->deindent;
177 $self->pidl("}");
178 $self->pidl("");
179 $self->pidl("return ret;");
182 sub PythonStruct($$$$$$)
184 my ($self, $modulename, $prettyname, $name, $cname, $d) = @_;
186 my $env = GenerateStructEnv($d, "object");
188 $self->pidl("");
190 my $getsetters = "NULL";
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("");
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);");
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("");
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} },");
231 $self->pidl("{ NULL }");
232 $self->deindent;
233 $self->pidl("};");
234 $self->pidl("");
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("");
245 my $py_methods = "NULL";
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("");
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 $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);");
279 $self->pidl("if (err != NDR_ERR_SUCCESS) {");
280 $self->indent;
281 $self->pidl("PyErr_SetNdrError(err);");
282 $self->pidl("return NULL;");
283 $self->deindent;
284 $self->pidl("}");
285 $self->pidl("");
286 $self->pidl("Py_RETURN_NONE;");
287 $self->deindent;
288 $self->pidl("}");
289 $self->pidl("");
290 $py_methods = "py_$name\_methods";
291 $self->pidl("static PyMethodDef $py_methods\[] = {");
292 $self->indent;
293 $self->pidl("{ \"__ndr_pack__\", (PyCFunction)py_$name\_ndr_pack, METH_NOARGS, \"S.pack() -> blob\\nNDR pack\" },");
294 $self->pidl("{ \"__ndr_unpack__\", (PyCFunction)py_$name\_ndr_unpack, METH_VARARGS, \"S.unpack(blob) -> None\\nNDR unpack\" },");
295 $self->pidl("{ NULL, NULL, 0, NULL }");
296 $self->deindent;
297 $self->pidl("};");
298 $self->pidl("");
301 $self->pidl_hdr("staticforward PyTypeObject $name\_Type;\n");
302 $self->pidl("");
303 my $docstring = $self->DocString($d, $name);
304 my $typeobject = "$name\_Type";
305 $self->pidl("static PyTypeObject $typeobject = {");
306 $self->indent;
307 $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
308 $self->pidl(".tp_name = \"$modulename.$prettyname\",");
309 $self->pidl(".tp_basicsize = sizeof(py_talloc_Object),");
310 $self->pidl(".tp_dealloc = py_talloc_dealloc,");
311 $self->pidl(".tp_getset = $getsetters,");
312 $self->pidl(".tp_repr = py_talloc_default_repr,");
313 if ($docstring) {
314 $self->pidl(".tp_doc = $docstring,");
316 $self->pidl(".tp_methods = $py_methods,");
317 $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,");
318 $self->pidl(".tp_new = py_$name\_new,");
319 $self->deindent;
320 $self->pidl("};");
322 $self->pidl("");
324 return "&$typeobject";
327 sub get_metadata_var($)
329 my ($e) = @_;
330 sub get_var($) { my $x = shift; $x =~ s/\*//g; return $x; }
332 if (has_property($e, "length_is")) {
333 return get_var($e->{PROPERTIES}->{length_is});
334 } elsif (has_property($e, "size_is")) {
335 return get_var($e->{PROPERTIES}->{size_is});
338 return undef;
341 sub find_metadata_args($)
343 my ($fn) = @_;
344 my $metadata_args = { in => {}, out => {} };
346 # Determine arguments that are metadata for other arguments (size_is/length_is)
347 foreach my $e (@{$fn->{ELEMENTS}}) {
348 foreach my $dir (@{$e->{DIRECTION}}) {
349 my $main = get_metadata_var($e);
350 if ($main) {
351 $metadata_args->{$dir}->{$main} = $e->{NAME};
356 return $metadata_args;
359 sub PythonFunctionUnpackOut($$$)
361 my ($self, $fn, $fnname) = @_;
363 my $outfnname = "unpack_$fnname\_args_out";
364 my $signature = "";
366 my $metadata_args = find_metadata_args($fn);
368 my $env = GenerateFunctionOutEnv($fn, "r->");
369 my $result_size = 0;
371 $self->pidl("static PyObject *$outfnname(struct $fn->{NAME} *r)");
372 $self->pidl("{");
373 $self->indent;
374 $self->pidl("PyObject *result = Py_None;");
375 foreach my $e (@{$fn->{ELEMENTS}}) {
376 next unless (grep(/out/,@{$e->{DIRECTION}}));
377 next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or
378 ($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}}));
379 $self->pidl("PyObject *py_$e->{NAME};");
380 $result_size++;
383 if ($fn->{RETURN_TYPE}) {
384 $result_size++ unless ($fn->{RETURN_TYPE} eq "WERROR" or $fn->{RETURN_TYPE} eq "NTSTATUS");
387 my $i = 0;
389 if ($result_size > 1) {
390 $self->pidl("result = PyTuple_New($result_size);");
391 $signature .= "(";
392 } elsif ($result_size == 0) {
393 $signature .= "None";
396 foreach my $e (@{$fn->{ELEMENTS}}) {
397 next if ($metadata_args->{out}->{$e->{NAME}});
398 my $py_name = "py_$e->{NAME}";
399 if (grep(/out/,@{$e->{DIRECTION}})) {
400 $self->ConvertObjectToPython("r", $env, $e, "r->out.$e->{NAME}", $py_name, "return NULL;");
401 if ($result_size > 1) {
402 $self->pidl("PyTuple_SetItem(result, $i, $py_name);");
403 $i++;
404 $signature .= "$e->{NAME}, ";
405 } else {
406 $self->pidl("result = $py_name;");
407 $signature .= $e->{NAME};
412 if (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "NTSTATUS") {
413 $self->handle_ntstatus("r->out.result", "NULL", undef);
414 } elsif (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "WERROR") {
415 $self->handle_werror("r->out.result", "NULL", undef);
416 } elsif (defined($fn->{RETURN_TYPE})) {
417 my $conv = $self->ConvertObjectToPythonData("r", $fn->{RETURN_TYPE}, "r->out.result", $fn);
418 if ($result_size > 1) {
419 $self->pidl("PyTuple_SetItem(result, $i, $conv);");
420 } else {
421 $self->pidl("result = $conv;");
423 $signature .= "result";
426 if (substr($signature, -2) eq ", ") {
427 $signature = substr($signature, 0, -2);
429 if ($result_size > 1) {
430 $signature .= ")";
433 $self->pidl("return result;");
434 $self->deindent;
435 $self->pidl("}");
436 $self->pidl("");
438 return ($outfnname, $signature);
441 sub PythonFunctionPackIn($$$)
443 my ($self, $fn, $fnname) = @_;
444 my $metadata_args = find_metadata_args($fn);
446 my $infnname = "pack_$fnname\_args_in";
448 $self->pidl("static bool $infnname(PyObject *args, PyObject *kwargs, struct $fn->{NAME} *r)");
449 $self->pidl("{");
450 $self->indent;
451 my $args_format = "";
452 my $args_string = "";
453 my $args_names = "";
454 my $signature = "";
456 foreach my $e (@{$fn->{ELEMENTS}}) {
457 next unless (grep(/in/,@{$e->{DIRECTION}}));
458 next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or
459 ($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}}));
460 $self->pidl("PyObject *py_$e->{NAME};");
461 $args_format .= "O";
462 $args_string .= ", &py_$e->{NAME}";
463 $args_names .= "\"$e->{NAME}\", ";
464 $signature .= "$e->{NAME}, ";
466 if (substr($signature, -2) eq ", ") {
467 $signature = substr($signature, 0, -2);
469 $self->pidl("const char *kwnames[] = {");
470 $self->indent;
471 $self->pidl($args_names . "NULL");
472 $self->deindent;
473 $self->pidl("};");
475 $self->pidl("");
476 $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"$args_format:$fn->{NAME}\", discard_const_p(char *, kwnames)$args_string)) {");
477 $self->indent;
478 $self->pidl("return false;");
479 $self->deindent;
480 $self->pidl("}");
481 $self->pidl("");
483 my $env = GenerateFunctionInEnv($fn, "r->");
485 my $fail = "return false;";
486 foreach my $e (@{$fn->{ELEMENTS}}) {
487 next unless (grep(/in/,@{$e->{DIRECTION}}));
488 if ($metadata_args->{in}->{$e->{NAME}}) {
489 my $py_var = "py_".$metadata_args->{in}->{$e->{NAME}};
490 $self->pidl("PY_CHECK_TYPE(&PyList_Type, $py_var, $fail);");
491 my $val = "PyList_Size($py_var)";
492 if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
493 $self->pidl("r->in.$e->{NAME} = talloc_ptrtype(r, r->in.$e->{NAME});");
494 $self->pidl("*r->in.$e->{NAME} = $val;");
495 } else {
496 $self->pidl("r->in.$e->{NAME} = $val;");
498 } else {
499 $self->ConvertObjectFromPython($env, "r", $e, "py_$e->{NAME}", "r->in.$e->{NAME}", $fail);
502 $self->pidl("return true;");
503 $self->deindent;
504 $self->pidl("}");
505 $self->pidl("");
506 return ($infnname, $signature);
509 sub PythonFunction($$$)
511 my ($self, $fn, $iface, $prettyname) = @_;
513 my $fnname = "py_$fn->{NAME}";
514 my $docstring = $self->DocString($fn, $fn->{NAME});
516 my ($insignature, $outsignature);
517 my ($infn, $outfn);
519 if (has_property($fn, "todo")) {
520 unless ($docstring) { $docstring = "NULL"; }
521 $infn = "NULL";
522 $outfn = "NULL";
523 } else {
524 ($infn, $insignature) = $self->PythonFunctionPackIn($fn, $fnname);
525 ($outfn, $outsignature) = $self->PythonFunctionUnpackOut($fn, $fnname);
526 my $signature = "S.$prettyname($insignature) -> $outsignature";
527 if ($docstring) {
528 $docstring = "\"$signature\\n\\n\"$docstring";
529 } else {
530 $docstring = "\"$signature\"";
534 return ($infn, $outfn, $docstring);
537 sub handle_werror($$$$)
539 my ($self, $var, $retval, $mem_ctx) = @_;
541 $self->pidl("if (!W_ERROR_IS_OK($var)) {");
542 $self->indent;
543 $self->pidl("PyErr_SetWERROR($var);");
544 $self->pidl("talloc_free($mem_ctx);") if ($mem_ctx);
545 $self->pidl("return $retval;");
546 $self->deindent;
547 $self->pidl("}");
548 $self->pidl("");
551 sub handle_ntstatus($$$$)
553 my ($self, $var, $retval, $mem_ctx) = @_;
555 $self->pidl("if (NT_STATUS_IS_ERR($var)) {");
556 $self->indent;
557 $self->pidl("PyErr_SetNTSTATUS($var);");
558 $self->pidl("talloc_free($mem_ctx);") if ($mem_ctx);
559 $self->pidl("return $retval;");
560 $self->deindent;
561 $self->pidl("}");
562 $self->pidl("");
565 sub PythonType($$$$)
567 my ($self, $modulename, $d, $interface, $basename) = @_;
569 my $actual_ctype = $d;
570 if ($actual_ctype->{TYPE} eq "TYPEDEF") {
571 $actual_ctype = $actual_ctype->{DATA};
574 if ($actual_ctype->{TYPE} eq "STRUCT") {
575 my $typeobject;
576 my $fn_name = PrettifyTypeName($d->{NAME}, $basename);
578 if ($d->{TYPE} eq "STRUCT") {
579 $typeobject = $self->PythonStruct($modulename, $fn_name, $d->{NAME}, mapTypeName($d), $d);
580 } else {
581 $typeobject = $self->PythonStruct($modulename, $fn_name, $d->{NAME}, mapTypeName($d), $d->{DATA});
584 $self->register_module_typeobject($fn_name, $typeobject);
587 if ($d->{TYPE} eq "ENUM" or $d->{TYPE} eq "BITMAP") {
588 $self->EnumAndBitmapConsts($d->{NAME}, $d);
591 if ($d->{TYPE} eq "TYPEDEF" and ($d->{DATA}->{TYPE} eq "ENUM" or $d->{DATA}->{TYPE} eq "BITMAP")) {
592 $self->EnumAndBitmapConsts($d->{NAME}, $d->{DATA});
595 if ($actual_ctype->{TYPE} eq "UNION" and defined($actual_ctype->{ELEMENTS})) {
596 $self->pidl("PyObject *py_import_$d->{NAME}(TALLOC_CTX *mem_ctx, int level, " .mapTypeName($d) . " *in)");
597 $self->pidl("{");
598 $self->indent;
599 $self->FromUnionToPythonFunction("mem_ctx", $actual_ctype, "level", "in") if ($actual_ctype->{TYPE} eq "UNION");
600 $self->deindent;
601 $self->pidl("}");
602 $self->pidl("");
604 $self->pidl(mapTypeName($d) . " *py_export_$d->{NAME}(TALLOC_CTX *mem_ctx, int level, PyObject *in)");
605 $self->pidl("{");
606 $self->indent;
607 $self->FromPythonToUnionFunction($actual_ctype, mapTypeName($d), "level", "mem_ctx", "in") if ($actual_ctype->{TYPE} eq "UNION");
608 $self->deindent;
609 $self->pidl("}");
610 $self->pidl("");
614 sub DocString($$$)
616 my ($self, $d, $name) = @_;
617 if (has_property($d, "helpstring")) {
618 my $docstring = uc("py_doc_$name");
619 $self->pidl("#define $docstring ".has_property($d, "helpstring"));
620 return $docstring;
623 return undef;
626 sub Interface($$$)
628 my($self,$interface,$basename) = @_;
630 if (has_property($interface, "pyhelper")) {
631 $self->pidl("#include \"".unmake_str($interface->{PROPERTIES}->{pyhelper})."\"\n");
634 $self->Const($_) foreach (@{$interface->{CONSTS}});
636 foreach my $d (@{$interface->{TYPES}}) {
637 next if has_property($d, "nopython");
639 $self->PythonType($basename, $d, $interface, $basename);
642 if (defined $interface->{PROPERTIES}->{uuid}) {
643 $self->pidl_hdr("staticforward PyTypeObject $interface->{NAME}_InterfaceType;\n");
644 $self->pidl("");
646 my @fns = ();
648 foreach my $d (@{$interface->{FUNCTIONS}}) {
649 next if not defined($d->{OPNUM});
650 next if has_property($d, "nopython");
651 next if has_property($d, "todo");
653 my $prettyname = $d->{NAME};
655 $prettyname =~ s/^$interface->{NAME}_//;
656 $prettyname =~ s/^$basename\_//;
658 my ($infn, $outfn, $fndocstring) = $self->PythonFunction($d, $interface->{NAME}, $prettyname);
660 push (@fns, [$infn, $outfn, "dcerpc_$d->{NAME}", $prettyname, $fndocstring, $d->{OPNUM}]);
663 $self->pidl("const struct PyNdrRpcMethodDef py_ndr_$interface->{NAME}\_methods[] = {");
664 $self->indent;
665 foreach my $d (@fns) {
666 my ($infn, $outfn, $callfn, $prettyname, $docstring, $opnum) = @$d;
667 $self->pidl("{ \"$prettyname\", $docstring, (dcerpc_call_fn)$callfn, (py_data_pack_fn)$infn, (py_data_unpack_fn)$outfn, $opnum, &ndr_table_$interface->{NAME} },");
669 $self->pidl("{ NULL }");
670 $self->deindent;
671 $self->pidl("};");
672 $self->pidl("");
674 $self->pidl("static PyObject *interface_$interface->{NAME}_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)");
675 $self->pidl("{");
676 $self->indent;
677 $self->pidl("return py_dcerpc_interface_init_helper(type, args, kwargs, &ndr_table_$interface->{NAME});");
678 $self->deindent;
679 $self->pidl("}");
681 $self->pidl("");
683 my $signature =
684 "\"$interface->{NAME}(binding, lp_ctx=None, credentials=None) -> connection\\n\"
685 \"\\n\"
686 \"binding should be a DCE/RPC binding string (for example: ncacn_ip_tcp:127.0.0.1)\\n\"
687 \"lp_ctx should be a path to a smb.conf file or a param.LoadParm object\\n\"
688 \"credentials should be a credentials.Credentials object.\\n\\n\"";
690 my $docstring = $self->DocString($interface, $interface->{NAME});
692 if ($docstring) {
693 $docstring = "$signature$docstring";
694 } else {
695 $docstring = $signature;
698 $self->pidl("static PyTypeObject $interface->{NAME}_InterfaceType = {");
699 $self->indent;
700 $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
701 $self->pidl(".tp_name = \"$basename.$interface->{NAME}\",");
702 $self->pidl(".tp_basicsize = sizeof(dcerpc_InterfaceObject),");
703 $self->pidl(".tp_base = &dcerpc_InterfaceType,");
704 $self->pidl(".tp_doc = $docstring,");
705 $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,");
706 $self->pidl(".tp_new = interface_$interface->{NAME}_new,");
707 $self->deindent;
708 $self->pidl("};");
710 $self->pidl("");
712 $self->register_module_typeobject($interface->{NAME}, "&$interface->{NAME}_InterfaceType");
713 $self->register_module_readycode(["if (!PyInterface_AddNdrRpcMethods(&$interface->{NAME}_InterfaceType, py_ndr_$interface->{NAME}\_methods))", "\treturn;", ""]);
716 $self->pidl_hdr("\n");
719 sub register_module_method($$$$$)
721 my ($self, $fn_name, $pyfn_name, $flags, $doc) = @_;
723 push (@{$self->{module_methods}}, [$fn_name, $pyfn_name, $flags, $doc])
726 sub register_module_typeobject($$$)
728 my ($self, $name, $py_name) = @_;
730 $self->register_module_object($name, "(PyObject *)$py_name");
732 $self->check_ready_type($py_name);
734 $self->register_patch_type_call($name, $py_name);
737 sub check_ready_type($$)
739 my ($self, $py_name) = @_;
740 push (@{$self->{ready_types}}, $py_name) unless (grep(/^$py_name$/,@{$self->{ready_types}}));
743 sub register_module_import($$)
745 my ($self, $basename) = @_;
747 push (@{$self->{module_imports}}, $basename) unless (grep(/^$basename$/,@{$self->{module_imports}}));
750 sub use_type_variable($$)
752 my ($self, $orig_ctype) = @_;
753 # FIXME: Have a global lookup table for types that look different on the
754 # wire than they are named in C?
755 if ($orig_ctype->{NAME} eq "dom_sid2") {
756 $orig_ctype->{NAME} = "dom_sid";
758 my $ctype = resolveType($orig_ctype);
759 unless (defined($ctype->{BASEFILE})) {
760 return undef;
762 # If this is an external type, make sure we do the right imports.
763 if (($ctype->{BASEFILE} ne $self->{BASENAME})) {
764 $self->register_module_import($ctype->{BASEFILE});
765 unless (defined($self->{type_imports}->{$ctype->{NAME}})) {
766 $self->{type_imports}->{$ctype->{NAME}} = $ctype->{BASEFILE};
768 return "$ctype->{NAME}_Type";
770 return "&$ctype->{NAME}_Type";
773 sub register_patch_type_call($$$)
775 my ($self, $typename, $cvar) = @_;
777 push(@{$self->{patch_type_calls}}, [$typename, $cvar]);
781 sub register_module_readycode($$)
783 my ($self, $code) = @_;
785 push (@{$self->{readycode}}, @$code);
788 sub register_module_object($$$)
790 my ($self, $name, $py_name) = @_;
792 push (@{$self->{module_objects}}, [$name, $py_name])
795 sub assign($$$)
797 my ($self, $dest, $src) = @_;
798 if ($dest =~ /^\&/ and $src eq "NULL") {
799 $self->pidl("memset($dest, 0, sizeof(" . get_value_of($dest) . "));");
800 } elsif ($dest =~ /^\&/) {
801 $self->pidl("memcpy($dest, $src, sizeof(" . get_value_of($dest) . "));");
802 } else {
803 $self->pidl("$dest = $src;");
807 sub ConvertObjectFromPythonData($$$$$$;$)
809 my ($self, $mem_ctx, $cvar, $ctype, $target, $fail, $location) = @_;
811 fatal($location, "undef type for $cvar") unless(defined($ctype));
813 $ctype = resolveType($ctype);
815 my $actual_ctype = $ctype;
816 if ($actual_ctype->{TYPE} eq "TYPEDEF") {
817 $actual_ctype = $actual_ctype->{DATA};
820 if ($actual_ctype->{TYPE} eq "ENUM" or $actual_ctype->{TYPE} eq "BITMAP" or
821 $actual_ctype->{TYPE} eq "SCALAR" and (
822 expandAlias($actual_ctype->{NAME}) =~ /^(u?int[0-9]*|hyper|NTTIME|time_t|NTTIME_hyper|NTTIME_1sec|dlong|udlong|udlongr)$/)) {
823 $self->pidl("PY_CHECK_TYPE(&PyInt_Type, $cvar, $fail);");
824 $self->pidl("$target = PyInt_AsLong($cvar);");
825 return;
828 if ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") {
829 my $ctype_name = $self->use_type_variable($ctype);
830 unless (defined ($ctype_name)) {
831 error($location, "Unable to determine origin of type `" . mapTypeName($ctype) . "'");
832 $self->pidl("PyErr_SetString(PyExc_TypeError, \"Can not convert C Type " . mapTypeName($ctype) . " to Python\");");
833 return;
835 $self->pidl("PY_CHECK_TYPE($ctype_name, $cvar, $fail);");
836 $self->assign($target, "py_talloc_get_ptr($cvar)");
837 return;
840 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "DATA_BLOB") {
841 $self->pidl("$target = data_blob_talloc($mem_ctx, PyString_AsString($cvar), PyString_Size($cvar));");
842 return;
845 if ($actual_ctype->{TYPE} eq "SCALAR" and
846 ($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")) {
847 $self->pidl("$target = talloc_strdup($mem_ctx, PyString_AsString($cvar));");
848 return;
851 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "ipv4address") {
852 $self->pidl("$target = PyString_AsString($cvar);");
853 return;
857 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "NTSTATUS") {
858 $self->pidl("$target = NT_STATUS(PyInt_AsLong($cvar));");
859 return;
862 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "WERROR") {
863 $self->pidl("$target = W_ERROR(PyInt_AsLong($cvar));");
864 return;
867 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "string_array") {
868 $self->pidl("$target = PyCObject_AsVoidPtr($cvar);");
869 return;
872 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "pointer") {
873 $self->assign($target, "PyCObject_AsVoidPtr($cvar)");
874 return;
877 fatal($location, "unknown type `$actual_ctype->{TYPE}' for ".mapTypeName($ctype) . ": $cvar");
881 sub ConvertObjectFromPythonLevel($$$$$$$$)
883 my ($self, $env, $mem_ctx, $py_var, $e, $l, $var_name, $fail) = @_;
884 my $nl = GetNextLevel($e, $l);
886 if ($l->{TYPE} eq "POINTER") {
887 if ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) {
888 $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, $nl, $var_name, $fail);
889 return;
891 if ($l->{POINTER_TYPE} ne "ref") {
892 $self->pidl("if ($py_var == Py_None) {");
893 $self->indent;
894 $self->pidl("$var_name = NULL;");
895 $self->deindent;
896 $self->pidl("} else {");
897 $self->indent;
899 $self->pidl("$var_name = talloc_ptrtype($mem_ctx, $var_name);");
900 $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, $nl, get_value_of($var_name), $fail);
901 if ($l->{POINTER_TYPE} ne "ref") {
902 $self->deindent;
903 $self->pidl("}");
905 } elsif ($l->{TYPE} eq "ARRAY") {
906 my $pl = GetPrevLevel($e, $l);
907 if ($pl && $pl->{TYPE} eq "POINTER") {
908 $var_name = get_pointer_to($var_name);
911 if (is_charset_array($e, $l)) {
912 $self->pidl("PY_CHECK_TYPE(&PyUnicode_Type, $py_var, $fail);");
913 # FIXME: Use Unix charset setting rather than utf-8
914 $self->pidl($var_name . " = PyString_AsString(PyUnicode_AsEncodedString($py_var, \"utf-8\", \"ignore\"));");
915 } else {
916 my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}";
917 $self->pidl("PY_CHECK_TYPE(&PyList_Type, $py_var, $fail);");
918 $self->pidl("{");
919 $self->indent;
920 $self->pidl("int $counter;");
921 if (ArrayDynamicallyAllocated($e, $l)) {
922 $self->pidl("$var_name = talloc_array_ptrtype($mem_ctx, $var_name, PyList_Size($py_var));");
924 $self->pidl("for ($counter = 0; $counter < PyList_Size($py_var); $counter++) {");
925 $self->indent;
926 $self->ConvertObjectFromPythonLevel($env, $var_name, "PyList_GetItem($py_var, $counter)", $e, GetNextLevel($e, $l), $var_name."[$counter]", $fail);
927 $self->deindent;
928 $self->pidl("}");
929 $self->deindent;
930 $self->pidl("}");
932 } elsif ($l->{TYPE} eq "DATA") {
934 if (not Parse::Pidl::Typelist::is_scalar($l->{DATA_TYPE})) {
935 $var_name = get_pointer_to($var_name);
937 $self->ConvertObjectFromPythonData($mem_ctx, $py_var, $l->{DATA_TYPE}, $var_name, $fail, $e->{ORIGINAL});
938 } elsif ($l->{TYPE} eq "SWITCH") {
939 $var_name = get_pointer_to($var_name);
940 my $switch = ParseExpr($l->{SWITCH_IS}, $env, $e);
941 $self->assign($var_name, "py_export_" . GetNextLevel($e, $l)->{DATA_TYPE} . "($mem_ctx, $switch, $py_var)");
942 } elsif ($l->{TYPE} eq "SUBCONTEXT") {
943 $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, GetNextLevel($e, $l), $var_name, $fail);
944 } else {
945 fatal($e->{ORIGINAL}, "unknown level type $l->{TYPE}");
949 sub ConvertObjectFromPython($$$$$$$)
951 my ($self, $env, $mem_ctx, $ctype, $cvar, $target, $fail) = @_;
953 $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $cvar, $ctype, $ctype->{LEVELS}[0], $target, $fail);
956 sub ConvertScalarToPython($$$)
958 my ($self, $ctypename, $cvar) = @_;
960 die("expected string for $cvar, not $ctypename") if (ref($ctypename) eq "HASH");
962 $ctypename = expandAlias($ctypename);
964 if ($ctypename =~ /^(char|u?int[0-9]*|hyper|dlong|udlong|udlongr|time_t|NTTIME_hyper|NTTIME|NTTIME_1sec)$/) {
965 return "PyInt_FromLong($cvar)";
968 if ($ctypename eq "DATA_BLOB") {
969 return "PyString_FromStringAndSize((char *)($cvar).data, ($cvar).length)";
972 if ($ctypename eq "NTSTATUS") {
973 return "PyErr_FromNTSTATUS($cvar)";
976 if ($ctypename eq "WERROR") {
977 return "PyErr_FromWERROR($cvar)";
980 if (($ctypename eq "string" or $ctypename eq "nbt_string" or $ctypename eq "nbt_name" or $ctypename eq "wrepl_nbt_name")) {
981 return "PyString_FromString($cvar)";
984 # Not yet supported
985 if ($ctypename eq "string_array") { return "PyCObject_FromTallocPtr($cvar)"; }
986 if ($ctypename eq "ipv4address") { return "PyString_FromString($cvar)"; }
987 if ($ctypename eq "pointer") {
988 return "PyCObject_FromTallocPtr($cvar)";
991 die("Unknown scalar type $ctypename");
994 sub ConvertObjectToPythonData($$$$$;$)
996 my ($self, $mem_ctx, $ctype, $cvar, $location) = @_;
998 die("undef type for $cvar") unless(defined($ctype));
1000 $ctype = resolveType($ctype);
1002 my $actual_ctype = $ctype;
1003 if ($actual_ctype->{TYPE} eq "TYPEDEF") {
1004 $actual_ctype = $actual_ctype->{DATA};
1007 if ($actual_ctype->{TYPE} eq "ENUM") {
1008 return $self->ConvertScalarToPython(Parse::Pidl::Typelist::enum_type_fn($actual_ctype), $cvar);
1009 } elsif ($actual_ctype->{TYPE} eq "BITMAP") {
1010 return $self->ConvertScalarToPython(Parse::Pidl::Typelist::bitmap_type_fn($actual_ctype), $cvar);
1011 } elsif ($actual_ctype->{TYPE} eq "SCALAR") {
1012 return $self->ConvertScalarToPython($actual_ctype->{NAME}, $cvar);
1013 } elsif ($actual_ctype->{TYPE} eq "UNION") {
1014 fatal($ctype, "union without discriminant: " . mapTypeName($ctype) . ": $cvar");
1015 } elsif ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") {
1016 my $ctype_name = $self->use_type_variable($ctype);
1017 unless (defined($ctype_name)) {
1018 error($location, "Unable to determine origin of type `" . mapTypeName($ctype) . "'");
1019 return "NULL"; # FIXME!
1021 return "py_talloc_reference_ex($ctype_name, $mem_ctx, $cvar)";
1024 fatal($location, "unknown type $actual_ctype->{TYPE} for ".mapTypeName($ctype) . ": $cvar");
1027 sub fail_on_null($$$)
1029 my ($self, $var, $fail) = @_;
1030 $self->pidl("if ($var == NULL) {");
1031 $self->indent;
1032 $self->pidl($fail);
1033 $self->deindent;
1034 $self->pidl("}");
1037 sub ConvertObjectToPythonLevel($$$$$$)
1039 my ($self, $mem_ctx, $env, $e, $l, $var_name, $py_var, $fail) = @_;
1040 my $nl = GetNextLevel($e, $l);
1042 if ($l->{TYPE} eq "POINTER") {
1043 if ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) {
1044 $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, $var_name, $py_var, $fail);
1045 return;
1047 if ($l->{POINTER_TYPE} ne "ref") {
1048 $self->pidl("if ($var_name == NULL) {");
1049 $self->indent;
1050 $self->pidl("$py_var = Py_None;");
1051 $self->deindent;
1052 $self->pidl("} else {");
1053 $self->indent;
1055 $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, get_value_of($var_name), $py_var, $fail);
1056 if ($l->{POINTER_TYPE} ne "ref") {
1057 $self->deindent;
1058 $self->pidl("}");
1060 } elsif ($l->{TYPE} eq "ARRAY") {
1061 my $pl = GetPrevLevel($e, $l);
1062 if ($pl && $pl->{TYPE} eq "POINTER") {
1063 $var_name = get_pointer_to($var_name);
1066 if (is_charset_array($e, $l)) {
1067 # FIXME: Use Unix charset setting rather than utf-8
1068 $self->pidl("$py_var = PyUnicode_Decode($var_name, strlen($var_name), \"utf-8\", \"ignore\");");
1069 } else {
1070 die("No SIZE_IS for array $var_name") unless (defined($l->{SIZE_IS}));
1071 my $length = $l->{SIZE_IS};
1072 if (defined($l->{LENGTH_IS})) {
1073 $length = $l->{LENGTH_IS};
1076 $length = ParseExpr($length, $env, $e);
1077 $self->pidl("$py_var = PyList_New($length);");
1078 $self->fail_on_null($py_var, $fail);
1079 $self->pidl("{");
1080 $self->indent;
1081 my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}";
1082 $self->pidl("int $counter;");
1083 $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
1084 $self->indent;
1085 my $member_var = "py_$e->{NAME}_$l->{LEVEL_INDEX}";
1086 $self->pidl("PyObject *$member_var;");
1087 $self->ConvertObjectToPythonLevel($var_name, $env, $e, GetNextLevel($e, $l), $var_name."[$counter]", $member_var, $fail);
1088 $self->pidl("PyList_SetItem($py_var, $counter, $member_var);");
1089 $self->deindent;
1090 $self->pidl("}");
1091 $self->deindent;
1092 $self->pidl("}");
1094 } elsif ($l->{TYPE} eq "SWITCH") {
1095 $var_name = get_pointer_to($var_name);
1096 my $switch = ParseExpr($l->{SWITCH_IS}, $env, $e);
1097 $self->pidl("$py_var = py_import_" . GetNextLevel($e, $l)->{DATA_TYPE} . "($mem_ctx, $switch, $var_name);");
1098 $self->fail_on_null($py_var, $fail);
1100 } elsif ($l->{TYPE} eq "DATA") {
1101 if (not Parse::Pidl::Typelist::is_scalar($l->{DATA_TYPE})) {
1102 $var_name = get_pointer_to($var_name);
1104 my $conv = $self->ConvertObjectToPythonData($mem_ctx, $l->{DATA_TYPE}, $var_name, $e->{ORIGINAL});
1105 $self->pidl("$py_var = $conv;");
1106 } elsif ($l->{TYPE} eq "SUBCONTEXT") {
1107 $self->ConvertObjectToPythonLevel($mem_ctx, $env, $e, GetNextLevel($e, $l), $var_name, $py_var, $fail);
1108 } else {
1109 fatal($e->{ORIGINAL}, "Unknown level type $l->{TYPE} $var_name");
1113 sub ConvertObjectToPython($$$$$$)
1115 my ($self, $mem_ctx, $env, $ctype, $cvar, $py_var, $fail) = @_;
1117 $self->ConvertObjectToPythonLevel($mem_ctx, $env, $ctype, $ctype->{LEVELS}[0], $cvar, $py_var, $fail);
1120 sub Parse($$$$$)
1122 my($self,$basename,$ndr,$ndr_hdr,$hdr) = @_;
1124 $self->{BASENAME} = $basename;
1126 $self->pidl_hdr("
1127 /* Python wrapper functions auto-generated by pidl */
1128 #include <Python.h>
1129 #include \"includes.h\"
1130 #include \"librpc/rpc/dcerpc.h\"
1131 #include \"lib/talloc/pytalloc.h\"
1132 #include \"librpc/rpc/pyrpc.h\"
1133 #include \"$hdr\"
1134 #include \"$ndr_hdr\"
1138 foreach my $x (@$ndr) {
1139 ($x->{TYPE} eq "IMPORT") && $self->Import(@{$x->{PATHS}});
1140 ($x->{TYPE} eq "INTERFACE") && $self->Interface($x, $basename);
1143 $self->pidl("static PyMethodDef $basename\_methods[] = {");
1144 $self->indent;
1145 foreach (@{$self->{module_methods}}) {
1146 my ($fn_name, $pyfn_name, $flags, $doc) = @$_;
1147 $self->pidl("{ \"$fn_name\", (PyCFunction)$pyfn_name, $flags, $doc },");
1150 $self->pidl("{ NULL, NULL, 0, NULL }");
1151 $self->deindent;
1152 $self->pidl("};");
1154 $self->pidl("");
1156 $self->pidl("void init$basename(void)");
1157 $self->pidl("{");
1158 $self->indent;
1159 $self->pidl("PyObject *m;");
1160 foreach (@{$self->{module_imports}}) {
1161 $self->pidl("PyObject *dep_$_;");
1163 $self->pidl("");
1165 foreach (@{$self->{module_imports}}) {
1166 $self->pidl("dep_$_ = PyImport_ImportModule(\"samba.dcerpc.$_\");");
1167 $self->pidl("if (dep_$_ == NULL)");
1168 $self->pidl("\treturn;");
1169 $self->pidl("");
1172 foreach (keys %{$self->{type_imports}}) {
1173 my $basefile = $self->{type_imports}->{$_};
1174 $self->pidl_hdr("static PyTypeObject *$_\_Type;\n");
1175 my $pretty_name = PrettifyTypeName($_, $basefile);
1176 $self->pidl("$_\_Type = (PyTypeObject *)PyObject_GetAttrString(dep_$basefile, \"$pretty_name\");");
1177 $self->pidl("if ($_\_Type == NULL)");
1178 $self->pidl("\treturn;");
1179 $self->pidl("");
1182 foreach (@{$self->{ready_types}}) {
1183 $self->pidl("if (PyType_Ready($_) < 0)");
1184 $self->pidl("\treturn;");
1187 $self->pidl($_) foreach (@{$self->{readycode}});
1189 foreach (@{$self->{patch_type_calls}}) {
1190 my ($typename, $cvar) = @$_;
1191 $self->pidl("#ifdef PY_".uc($typename)."_PATCH");
1192 $self->pidl("PY_".uc($typename)."_PATCH($cvar);");
1193 $self->pidl("#endif");
1196 $self->pidl("");
1198 $self->pidl("m = Py_InitModule3(\"$basename\", $basename\_methods, \"$basename DCE/RPC\");");
1199 $self->pidl("if (m == NULL)");
1200 $self->pidl("\treturn;");
1201 $self->pidl("");
1202 foreach my $name (keys %{$self->{constants}}) {
1203 my $py_obj;
1204 my ($ctype, $cvar) = @{$self->{constants}->{$name}};
1205 if ($cvar =~ /^[0-9]+$/ or $cvar =~ /^0x[0-9a-fA-F]+$/) {
1206 $py_obj = "PyInt_FromLong($cvar)";
1207 } elsif ($cvar =~ /^".*"$/) {
1208 $py_obj = "PyString_FromString($cvar)";
1209 } else {
1210 $py_obj = $self->ConvertObjectToPythonData("NULL", expandAlias($ctype), $cvar, undef);
1213 $self->pidl("PyModule_AddObject(m, \"$name\", $py_obj);");
1216 foreach (@{$self->{module_objects}}) {
1217 my ($object_name, $c_name) = @$_;
1218 $self->pidl("Py_INCREF($c_name);");
1219 $self->pidl("PyModule_AddObject(m, \"$object_name\", $c_name);");
1222 $self->pidl("#ifdef PY_MOD_".uc($basename)."_PATCH");
1223 $self->pidl("PY_MOD_".uc($basename)."_PATCH(m);");
1224 $self->pidl("#endif");
1226 $self->pidl("");
1227 $self->deindent;
1228 $self->pidl("}");
1229 return ($self->{res_hdr} . $self->{res});