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
;
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);
25 my $self = { res => "", res_hdr => "", tabs => "", constants => {},
26 module_methods => [], module_objects => [], ready_types => [],
27 module_imports => {}, type_imports => {},
28 patch_type_calls => [], prereadycode => [],
36 $self->{res_hdr} .= shift;
43 if ((!($d =~ /^#/))) {
44 $self->{res} .= $self->{tabs};
54 $self->{tabs} .= "\t";
60 $self->{tabs} = substr($self->{tabs}, 0, -1);
63 sub PrettifyTypeName($$)
65 my ($name, $basename) = @_;
67 $basename =~ s/^.*\.([^.]+)$/\1/;
69 $name =~ s/^$basename\_//;
82 $self->pidl_hdr("#include \"librpc/gen_ndr/$_\.h\"\n");
83 $self->register_module_import("samba.dcerpc.$_");
89 my ($self, $const) = @_;
90 $self->register_constant($const->{NAME}, $const->{DTYPE}, $const->{VALUE});
93 sub register_constant($$$$)
95 my ($self, $name, $type, $value) = @_;
97 $self->{constants}->{$name} = [$type, $value];
100 sub EnumAndBitmapConsts($$$)
102 my ($self, $name, $d) = @_;
104 return unless (defined($d->{ELEMENTS}));
106 foreach my $e (@{$d->{ELEMENTS}}) {
107 $e =~ /^([A-Za-z0-9_]+)/;
110 $self->register_constant($cname, $d, $cname);
114 sub FromUnionToPythonFunction($$$$)
116 my ($self, $mem_ctx, $type, $switch, $name) = @_;
118 $self->pidl("PyObject *ret;");
121 $self->pidl("switch ($switch) {");
124 foreach my $e (@{$type->{ELEMENTS}}) {
125 $self->pidl("$e->{CASE}:");
130 $self->ConvertObjectToPython($mem_ctx, {}, $e, "$name->$e->{NAME}", "ret", "return NULL;");
132 $self->pidl("ret = Py_None;");
133 $self->pidl("Py_INCREF(ret);");
136 $self->pidl("return ret;");
145 $self->pidl("PyErr_SetString(PyExc_TypeError, \"unknown union level\");");
146 $self->pidl("return NULL;");
149 sub FromPythonToUnionFunction($$$$$)
151 my ($self, $type, $typename, $switch, $mem_ctx, $name) = @_;
155 $self->pidl("$typename *ret = talloc_zero($mem_ctx, $typename);");
157 $self->pidl("switch ($switch) {");
160 foreach my $e (@{$type->{ELEMENTS}}) {
161 $self->pidl("$e->{CASE}:");
162 if ($e->{CASE} eq "default") { $has_default = 1; }
165 $self->ConvertObjectFromPython({}, $mem_ctx, $e, $name, "ret->$e->{NAME}", "talloc_free(ret); return NULL;");
167 $self->pidl("break;");
173 $self->pidl("default:");
175 $self->pidl("PyErr_SetString(PyExc_TypeError, \"invalid union level value\");");
176 $self->pidl("talloc_free(ret);");
177 $self->pidl("ret = NULL;");
184 $self->pidl("return ret;");
187 sub PythonStruct($$$$$$)
189 my ($self, $modulename, $prettyname, $name, $cname, $d) = @_;
191 my $env = GenerateStructEnv($d, "object");
195 my $getsetters = "NULL";
197 if ($#{$d->{ELEMENTS}} > -1) {
198 foreach my $e (@{$d->{ELEMENTS}}) {
199 my $varname = "object->$e->{NAME}";
200 $self->pidl("static PyObject *py_$name\_get_$e->{NAME}(PyObject *obj, void *closure)");
203 $self->pidl("$cname *object = ($cname *)py_talloc_get_ptr(obj);");
204 $self->pidl("PyObject *py_$e->{NAME};");
205 $self->ConvertObjectToPython("py_talloc_get_mem_ctx(obj)", $env, $e, $varname, "py_$e->{NAME}", "return NULL;");
206 $self->pidl("return py_$e->{NAME};");
211 $self->pidl("static int py_$name\_set_$e->{NAME}(PyObject *py_obj, PyObject *value, void *closure)");
214 $self->pidl("$cname *object = ($cname *)py_talloc_get_ptr(py_obj);");
215 my $mem_ctx = "py_talloc_get_mem_ctx(py_obj)";
216 my $l = $e->{LEVELS}[0];
217 my $nl = GetNextLevel($e, $l);
218 if ($l->{TYPE} eq "POINTER" and
219 not ($nl->{TYPE} eq "ARRAY" and ($nl->{IS_FIXED} or is_charset_array($e, $nl))) and
220 not ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE}))) {
221 $self->pidl("talloc_unlink(py_talloc_get_mem_ctx(py_obj), $varname);");
223 $self->ConvertObjectFromPython($env, $mem_ctx, $e, "value", $varname, "return -1;");
224 $self->pidl("return 0;");
230 $getsetters = "py_$name\_getsetters";
231 $self->pidl("static PyGetSetDef ".$getsetters."[] = {");
233 foreach my $e (@{$d->{ELEMENTS}}) {
234 $self->pidl("{ discard_const_p(char, \"$e->{NAME}\"), py_$name\_get_$e->{NAME}, py_$name\_set_$e->{NAME} },");
236 $self->pidl("{ NULL }");
242 $self->pidl("static PyObject *py_$name\_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)");
245 $self->pidl("return py_talloc_new($cname, type);");
250 my $py_methods = "NULL";
252 # If the struct is not public there ndr_pull/ndr_push functions will
253 # be static so not callable from here
254 if (has_property($d, "public")) {
255 $self->pidl("static PyObject *py_$name\_ndr_pack(PyObject *py_obj)");
258 $self->pidl("$cname *object = ($cname *)py_talloc_get_ptr(py_obj);");
259 $self->pidl("DATA_BLOB blob;");
260 $self->pidl("enum ndr_err_code err;");
261 $self->pidl("err = ndr_push_struct_blob(&blob, py_talloc_get_mem_ctx(py_obj), object, (ndr_push_flags_fn_t)ndr_push_$name);");
262 $self->pidl("if (err != NDR_ERR_SUCCESS) {");
264 $self->pidl("PyErr_SetNdrError(err);");
265 $self->pidl("return NULL;");
269 $self->pidl("return PyString_FromStringAndSize((char *)blob.data, blob.length);");
274 $self->pidl("static PyObject *py_$name\_ndr_unpack(PyObject *py_obj, PyObject *args)");
277 $self->pidl("$cname *object = ($cname *)py_talloc_get_ptr(py_obj);");
278 $self->pidl("DATA_BLOB blob;");
279 $self->pidl("enum ndr_err_code err;");
280 $self->pidl("if (!PyArg_ParseTuple(args, \"s#:__ndr_unpack__\", &blob.data, &blob.length))");
281 $self->pidl("\treturn NULL;");
283 $self->pidl("err = ndr_pull_struct_blob_all(&blob, py_talloc_get_mem_ctx(py_obj), object, (ndr_pull_flags_fn_t)ndr_pull_$name);");
284 $self->pidl("if (err != NDR_ERR_SUCCESS) {");
286 $self->pidl("PyErr_SetNdrError(err);");
287 $self->pidl("return NULL;");
291 $self->pidl("Py_RETURN_NONE;");
296 $self->pidl("static PyObject *py_$name\_ndr_print(PyObject *py_obj)");
299 $self->pidl("$cname *object = ($cname *)py_talloc_get_ptr(py_obj);");
300 $self->pidl("char *retstr;");
301 $self->pidl("retstr = ndr_print_struct_string(py_talloc_get_mem_ctx(py_obj), (ndr_print_fn_t)ndr_print_$name, \"$name\", object);");
302 $self->pidl("return PyString_FromString(retstr);");
307 $py_methods = "py_$name\_methods";
308 $self->pidl("static PyMethodDef $py_methods\[] = {");
310 $self->pidl("{ \"__ndr_pack__\", (PyCFunction)py_$name\_ndr_pack, METH_NOARGS, \"S.ndr_pack(object) -> blob\\nNDR pack\" },");
311 $self->pidl("{ \"__ndr_unpack__\", (PyCFunction)py_$name\_ndr_unpack, METH_VARARGS, \"S.ndr_unpack(class, blob) -> None\\nNDR unpack\" },");
312 $self->pidl("{ \"__ndr_print__\", (PyCFunction)py_$name\_ndr_print, METH_VARARGS, \"S.ndr_print(object) -> None\\nNDR print\" },");
313 $self->pidl("{ NULL, NULL, 0, NULL }");
319 $self->pidl_hdr("staticforward PyTypeObject $name\_Type;\n");
321 my $docstring = $self->DocString($d, $name);
322 my $typeobject = "$name\_Type";
323 $self->pidl("static PyTypeObject $typeobject = {");
325 $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
326 $self->pidl(".tp_name = \"$modulename.$prettyname\",");
327 $self->pidl(".tp_getset = $getsetters,");
329 $self->pidl(".tp_doc = $docstring,");
331 $self->pidl(".tp_methods = $py_methods,");
332 $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,");
333 $self->pidl(".tp_basicsize = sizeof(py_talloc_Object),");
334 $self->pidl(".tp_new = py_$name\_new,");
340 my $talloc_typename = $self->import_type_variable("talloc", "Object");
341 $self->register_module_prereadycode(["$name\_Type.tp_base = $talloc_typename;", ""]);
343 return "&$typeobject";
346 sub get_metadata_var($)
349 sub get_var($) { my $x = shift; $x =~ s/\*//g; return $x; }
351 if (has_property($e, "length_is")) {
352 return get_var($e->{PROPERTIES}->{length_is});
353 } elsif (has_property($e, "size_is")) {
354 return get_var($e->{PROPERTIES}->{size_is});
360 sub find_metadata_args($)
363 my $metadata_args = { in => {}, out => {} };
365 # Determine arguments that are metadata for other arguments (size_is/length_is)
366 foreach my $e (@{$fn->{ELEMENTS}}) {
367 foreach my $dir (@{$e->{DIRECTION}}) {
368 my $main = get_metadata_var($e);
370 $metadata_args->{$dir}->{$main} = $e->{NAME};
375 return $metadata_args;
378 sub PythonFunctionUnpackOut($$$)
380 my ($self, $fn, $fnname) = @_;
382 my $outfnname = "unpack_$fnname\_args_out";
385 my $metadata_args = find_metadata_args($fn);
387 my $env = GenerateFunctionOutEnv($fn, "r->");
390 $self->pidl("static PyObject *$outfnname(struct $fn->{NAME} *r)");
393 $self->pidl("PyObject *result;");
394 foreach my $e (@{$fn->{ELEMENTS}}) {
395 next unless (grep(/out/,@{$e->{DIRECTION}}));
396 next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or
397 ($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}}));
398 $self->pidl("PyObject *py_$e->{NAME};");
402 if ($fn->{RETURN_TYPE}) {
403 $result_size++ unless ($fn->{RETURN_TYPE} eq "WERROR" or $fn->{RETURN_TYPE} eq "NTSTATUS");
408 if ($result_size > 1) {
409 $self->pidl("result = PyTuple_New($result_size);");
411 } elsif ($result_size == 0) {
412 $self->pidl("result = Py_None;");
413 $self->pidl("Py_INCREF(result);");
414 $signature .= "None";
417 foreach my $e (@{$fn->{ELEMENTS}}) {
418 next if ($metadata_args->{out}->{$e->{NAME}});
419 my $py_name = "py_$e->{NAME}";
420 if (grep(/out/,@{$e->{DIRECTION}})) {
421 $self->ConvertObjectToPython("r", $env, $e, "r->out.$e->{NAME}", $py_name, "return NULL;");
422 if ($result_size > 1) {
423 $self->pidl("PyTuple_SetItem(result, $i, $py_name);");
425 $signature .= "$e->{NAME}, ";
427 $self->pidl("result = $py_name;");
428 $signature .= $e->{NAME};
433 if (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "NTSTATUS") {
434 $self->handle_ntstatus("r->out.result", "NULL", undef);
435 } elsif (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "WERROR") {
436 $self->handle_werror("r->out.result", "NULL", undef);
437 } elsif (defined($fn->{RETURN_TYPE})) {
438 my $conv = $self->ConvertObjectToPythonData("r", $fn->{RETURN_TYPE}, "r->out.result", $fn);
439 if ($result_size > 1) {
440 $self->pidl("PyTuple_SetItem(result, $i, $conv);");
442 $self->pidl("result = $conv;");
444 $signature .= "result";
447 if (substr($signature, -2) eq ", ") {
448 $signature = substr($signature, 0, -2);
450 if ($result_size > 1) {
454 $self->pidl("return result;");
459 return ($outfnname, $signature);
462 sub PythonFunctionPackIn($$$)
464 my ($self, $fn, $fnname) = @_;
465 my $metadata_args = find_metadata_args($fn);
467 my $infnname = "pack_$fnname\_args_in";
469 $self->pidl("static bool $infnname(PyObject *args, PyObject *kwargs, struct $fn->{NAME} *r)");
472 my $args_format = "";
473 my $args_string = "";
477 foreach my $e (@{$fn->{ELEMENTS}}) {
478 next unless (grep(/in/,@{$e->{DIRECTION}}));
479 next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or
480 ($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}}));
481 $self->pidl("PyObject *py_$e->{NAME};");
483 $args_string .= ", &py_$e->{NAME}";
484 $args_names .= "\"$e->{NAME}\", ";
485 $signature .= "$e->{NAME}, ";
487 if (substr($signature, -2) eq ", ") {
488 $signature = substr($signature, 0, -2);
490 $self->pidl("const char *kwnames[] = {");
492 $self->pidl($args_names . "NULL");
497 $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"$args_format:$fn->{NAME}\", discard_const_p(char *, kwnames)$args_string)) {");
499 $self->pidl("return false;");
504 my $env = GenerateFunctionInEnv($fn, "r->");
506 my $fail = "return false;";
507 foreach my $e (@{$fn->{ELEMENTS}}) {
508 next unless (grep(/in/,@{$e->{DIRECTION}}));
509 if ($metadata_args->{in}->{$e->{NAME}}) {
510 my $py_var = "py_".$metadata_args->{in}->{$e->{NAME}};
511 $self->pidl("PY_CHECK_TYPE(&PyList_Type, $py_var, $fail);");
512 my $val = "PyList_GET_SIZE($py_var)";
513 if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
514 $self->pidl("r->in.$e->{NAME} = talloc_ptrtype(r, r->in.$e->{NAME});");
515 $self->pidl("*r->in.$e->{NAME} = $val;");
517 $self->pidl("r->in.$e->{NAME} = $val;");
520 $self->ConvertObjectFromPython($env, "r", $e, "py_$e->{NAME}", "r->in.$e->{NAME}", $fail);
523 $self->pidl("return true;");
527 return ($infnname, $signature);
530 sub PythonFunction($$$)
532 my ($self, $fn, $iface, $prettyname) = @_;
534 my $fnname = "py_$fn->{NAME}";
535 my $docstring = $self->DocString($fn, $fn->{NAME});
537 my ($infn, $insignature) = $self->PythonFunctionPackIn($fn, $fnname);
538 my ($outfn, $outsignature) = $self->PythonFunctionUnpackOut($fn, $fnname);
539 my $signature = "S.$prettyname($insignature) -> $outsignature";
541 $docstring = "\"$signature\\n\\n\"$docstring";
543 $docstring = "\"$signature\"";
546 return ($infn, $outfn, $docstring);
549 sub handle_werror($$$$)
551 my ($self, $var, $retval, $mem_ctx) = @_;
553 $self->pidl("if (!W_ERROR_IS_OK($var)) {");
555 $self->pidl("PyErr_SetWERROR($var);");
556 $self->pidl("talloc_free($mem_ctx);") if ($mem_ctx);
557 $self->pidl("return $retval;");
563 sub handle_ntstatus($$$$)
565 my ($self, $var, $retval, $mem_ctx) = @_;
567 $self->pidl("if (NT_STATUS_IS_ERR($var)) {");
569 $self->pidl("PyErr_SetNTSTATUS($var);");
570 $self->pidl("talloc_free($mem_ctx);") if ($mem_ctx);
571 $self->pidl("return $retval;");
579 my ($self, $modulename, $d, $interface, $basename) = @_;
581 my $actual_ctype = $d;
582 if ($actual_ctype->{TYPE} eq "TYPEDEF") {
583 $actual_ctype = $actual_ctype->{DATA};
586 if ($actual_ctype->{TYPE} eq "STRUCT") {
588 my $fn_name = PrettifyTypeName($d->{NAME}, $basename);
590 if ($d->{TYPE} eq "STRUCT") {
591 $typeobject = $self->PythonStruct($modulename, $fn_name, $d->{NAME}, mapTypeName($d), $d);
593 $typeobject = $self->PythonStruct($modulename, $fn_name, $d->{NAME}, mapTypeName($d), $d->{DATA});
596 $self->register_module_typeobject($fn_name, $typeobject);
599 if ($d->{TYPE} eq "ENUM" or $d->{TYPE} eq "BITMAP") {
600 $self->EnumAndBitmapConsts($d->{NAME}, $d);
603 if ($d->{TYPE} eq "TYPEDEF" and ($d->{DATA}->{TYPE} eq "ENUM" or $d->{DATA}->{TYPE} eq "BITMAP")) {
604 $self->EnumAndBitmapConsts($d->{NAME}, $d->{DATA});
607 if ($actual_ctype->{TYPE} eq "UNION" and defined($actual_ctype->{ELEMENTS})) {
608 $self->pidl("PyObject *py_import_$d->{NAME}(TALLOC_CTX *mem_ctx, int level, " .mapTypeName($d) . " *in)");
611 $self->FromUnionToPythonFunction("mem_ctx", $actual_ctype, "level", "in") if ($actual_ctype->{TYPE} eq "UNION");
616 $self->pidl(mapTypeName($d) . " *py_export_$d->{NAME}(TALLOC_CTX *mem_ctx, int level, PyObject *in)");
619 $self->FromPythonToUnionFunction($actual_ctype, mapTypeName($d), "level", "mem_ctx", "in") if ($actual_ctype->{TYPE} eq "UNION");
628 my ($self, $d, $name) = @_;
629 if (has_property($d, "helpstring")) {
630 my $docstring = uc("py_doc_$name");
631 $self->pidl("#define $docstring ".has_property($d, "helpstring"));
640 my($self,$interface,$basename) = @_;
642 if (has_property($interface, "pyhelper")) {
643 $self->pidl("#include \"".unmake_str($interface->{PROPERTIES}->{pyhelper})."\"\n");
646 $self->Const($_) foreach (@{$interface->{CONSTS}});
648 foreach my $d (@{$interface->{TYPES}}) {
649 next if has_property($d, "nopython");
651 $self->PythonType($basename, $d, $interface, $basename);
654 if (defined $interface->{PROPERTIES}->{uuid}) {
655 $self->pidl_hdr("staticforward PyTypeObject $interface->{NAME}_InterfaceType;\n");
660 foreach my $d (@{$interface->{FUNCTIONS}}) {
661 next if has_property($d, "noopnum");
662 next if has_property($d, "nopython");
663 next if has_property($d, "todo");
665 my $prettyname = $d->{NAME};
667 $prettyname =~ s/^$interface->{NAME}_//;
668 $prettyname =~ s/^$basename\_//;
670 my ($infn, $outfn, $fndocstring) = $self->PythonFunction($d, $interface->{NAME}, $prettyname);
672 push (@fns, [$infn, $outfn, "dcerpc_$d->{NAME}_r", $prettyname, $fndocstring, $d->{OPNUM}]);
675 $self->pidl("const struct PyNdrRpcMethodDef py_ndr_$interface->{NAME}\_methods[] = {");
677 foreach my $d (@fns) {
678 my ($infn, $outfn, $callfn, $prettyname, $docstring, $opnum) = @$d;
679 $self->pidl("{ \"$prettyname\", $docstring, (py_dcerpc_call_fn)$callfn, (py_data_pack_fn)$infn, (py_data_unpack_fn)$outfn, $opnum, &ndr_table_$interface->{NAME} },");
681 $self->pidl("{ NULL }");
686 $self->pidl("static PyObject *interface_$interface->{NAME}_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)");
689 $self->pidl("return py_dcerpc_interface_init_helper(type, args, kwargs, &ndr_table_$interface->{NAME});");
696 "\"$interface->{NAME}(binding, lp_ctx=None, credentials=None) -> connection\\n\"
698 \"binding should be a DCE/RPC binding string (for example: ncacn_ip_tcp:127.0.0.1)\\n\"
699 \"lp_ctx should be a path to a smb.conf file or a param.LoadParm object\\n\"
700 \"credentials should be a credentials.Credentials object.\\n\\n\"";
702 my $docstring = $self->DocString($interface, $interface->{NAME});
705 $docstring = "$signature$docstring";
707 $docstring = $signature;
710 my $if_typename = "$interface->{NAME}_InterfaceType";
712 $self->pidl("static PyTypeObject $if_typename = {");
714 $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
715 $self->pidl(".tp_name = \"$basename.$interface->{NAME}\",");
716 $self->pidl(".tp_basicsize = sizeof(dcerpc_InterfaceObject),");
717 $self->pidl(".tp_doc = $docstring,");
718 $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,");
719 $self->pidl(".tp_new = interface_$interface->{NAME}_new,");
725 $self->register_module_typeobject($interface->{NAME}, "&$if_typename");
726 my $dcerpc_typename = $self->import_type_variable("samba.dcerpc.base", "ClientConnection");
727 $self->register_module_prereadycode(["$if_typename.tp_base = $dcerpc_typename;", ""]);
728 $self->register_module_postreadycode(["if (!PyInterface_AddNdrRpcMethods(&$if_typename, py_ndr_$interface->{NAME}\_methods))", "\treturn;", ""]);
731 $self->pidl_hdr("\n");
734 sub register_module_method($$$$$)
736 my ($self, $fn_name, $pyfn_name, $flags, $doc) = @_;
738 push (@{$self->{module_methods}}, [$fn_name, $pyfn_name, $flags, $doc])
741 sub register_module_typeobject($$$)
743 my ($self, $name, $py_name) = @_;
745 $self->register_module_object($name, "(PyObject *)(void *)$py_name");
747 $self->check_ready_type($py_name);
749 $self->register_patch_type_call($name, $py_name);
752 sub check_ready_type($$)
754 my ($self, $py_name) = @_;
755 push (@{$self->{ready_types}}, $py_name) unless (grep(/^$py_name$/,@{$self->{ready_types}}));
758 sub register_module_import($$)
760 my ($self, $module_path) = @_;
762 my $var_name = $module_path;
763 $var_name =~ s/\./_/g;
764 $var_name = "dep_$var_name";
766 $self->{module_imports}->{$var_name} = $module_path;
771 sub import_type_variable($$$)
773 my ($self, $module, $name) = @_;
775 $self->register_module_import($module);
776 unless (defined($self->{type_imports}->{$name})) {
777 $self->{type_imports}->{$name} = $module;
779 return "$name\_Type";
782 sub use_type_variable($$)
784 my ($self, $orig_ctype) = @_;
785 # FIXME: Have a global lookup table for types that look different on the
786 # wire than they are named in C?
787 if ($orig_ctype->{NAME} eq "dom_sid2" or $orig_ctype->{NAME} eq "dom_sid28") {
788 $orig_ctype->{NAME} = "dom_sid";
790 my $ctype = resolveType($orig_ctype);
791 unless (defined($ctype->{BASEFILE})) {
794 # If this is an external type, make sure we do the right imports.
795 if (($ctype->{BASEFILE} ne $self->{BASENAME})) {
796 return $self->import_type_variable("samba.dcerpc.$ctype->{BASEFILE}", $ctype->{NAME});
798 return "&$ctype->{NAME}_Type";
801 sub register_patch_type_call($$$)
803 my ($self, $typename, $cvar) = @_;
805 push(@{$self->{patch_type_calls}}, [$typename, $cvar]);
809 sub register_module_prereadycode($$)
811 my ($self, $code) = @_;
813 push (@{$self->{prereadycode}}, @$code);
816 sub register_module_postreadycode($$)
818 my ($self, $code) = @_;
820 push (@{$self->{postreadycode}}, @$code);
823 sub register_module_object($$$)
825 my ($self, $name, $py_name) = @_;
827 push (@{$self->{module_objects}}, [$name, $py_name])
832 my ($self, $dest, $src) = @_;
833 if ($dest =~ /^\&/ and $src eq "NULL") {
834 $self->pidl("memset($dest, 0, sizeof(" . get_value_of($dest) . "));");
835 } elsif ($dest =~ /^\&/) {
836 my $destvar = get_value_of($dest);
837 $self->pidl("$destvar = *$src;");
839 $self->pidl("$dest = $src;");
843 sub ConvertObjectFromPythonData($$$$$$;$)
845 my ($self, $mem_ctx, $cvar, $ctype, $target, $fail, $location) = @_;
847 fatal($location, "undef type for $cvar") unless(defined($ctype));
849 $ctype = resolveType($ctype);
851 my $actual_ctype = $ctype;
852 if ($actual_ctype->{TYPE} eq "TYPEDEF") {
853 $actual_ctype = $actual_ctype->{DATA};
856 if ($actual_ctype->{TYPE} eq "ENUM" or $actual_ctype->{TYPE} eq "BITMAP") {
857 $self->pidl("if (PyLong_Check($cvar)) {");
859 $self->pidl("$target = PyLong_AsLongLong($cvar);");
861 $self->pidl("} else if (PyInt_Check($cvar)) {");
863 $self->pidl("$target = PyInt_AsLong($cvar);");
865 $self->pidl("} else {");
867 $self->pidl("PyErr_Format(PyExc_TypeError, \"Expected type %s or %s\",\\");
868 $self->pidl(" PyInt_Type.tp_name, PyLong_Type.tp_name);");
874 if ($actual_ctype->{TYPE} eq "SCALAR" ) {
875 if (expandAlias($actual_ctype->{NAME}) =~ /^(u?int64|hyper|dlong|udlong|udlongr|NTTIME_hyper|NTTIME|NTTIME_1sec)$/) {
876 $self->pidl("if (PyLong_Check($cvar)) {");
878 $self->pidl("$target = PyLong_AsLongLong($cvar);");
880 $self->pidl("} else if (PyInt_Check($cvar)) {");
882 $self->pidl("$target = PyInt_AsLong($cvar);");
884 $self->pidl("} else {");
886 $self->pidl("PyErr_Format(PyExc_TypeError, \"Expected type %s or %s\",\\");
887 $self->pidl(" PyInt_Type.tp_name, PyLong_Type.tp_name);");
893 if (expandAlias($actual_ctype->{NAME}) =~ /^(char|u?int[0-9]*|time_t)$/) {
894 $self->pidl("PY_CHECK_TYPE(&PyInt_Type, $cvar, $fail);");
895 $self->pidl("$target = PyInt_AsLong($cvar);");
900 if ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") {
901 my $ctype_name = $self->use_type_variable($ctype);
902 unless (defined ($ctype_name)) {
903 error($location, "Unable to determine origin of type `" . mapTypeName($ctype) . "'");
904 $self->pidl("PyErr_SetString(PyExc_TypeError, \"Can not convert C Type " . mapTypeName($ctype) . " from Python\");");
907 $self->pidl("PY_CHECK_TYPE($ctype_name, $cvar, $fail);");
908 $self->pidl("if (talloc_reference($mem_ctx, py_talloc_get_mem_ctx($cvar)) == NULL) {");
910 $self->pidl("PyErr_NoMemory();");
911 $self->pidl("$fail");
914 $self->assign($target, "(".mapTypeName($ctype)." *)py_talloc_get_ptr($cvar)");
918 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "DATA_BLOB") {
919 $self->pidl("$target = data_blob_talloc($mem_ctx, PyString_AS_STRING($cvar), PyString_GET_SIZE($cvar));");
923 if ($actual_ctype->{TYPE} eq "SCALAR" and
924 ($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")) {
925 $self->pidl("$target = talloc_strdup($mem_ctx, PyString_AS_STRING($cvar));");
929 if ($actual_ctype->{TYPE} eq "SCALAR" and ($actual_ctype->{NAME} eq "dns_string" or $actual_ctype->{NAME} eq "dns_name")) {
930 $self->pidl("$target = talloc_strdup($mem_ctx, PyString_AS_STRING($cvar));");
934 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "ipv4address") {
935 $self->pidl("$target = PyString_AS_STRING($cvar);");
939 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "ipv6address") {
940 $self->pidl("$target = PyString_AsString($cvar);");
944 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "dnsp_name") {
945 $self->pidl("$target = PyString_AS_STRING($cvar);");
949 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "dnsp_string") {
950 $self->pidl("$target = PyString_AS_STRING($cvar);");
954 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "NTSTATUS") {
955 $self->pidl("$target = NT_STATUS(PyInt_AsLong($cvar));");
959 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "WERROR") {
960 $self->pidl("$target = W_ERROR(PyInt_AsLong($cvar));");
964 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "string_array") {
965 $self->pidl("$target = PyCObject_AsVoidPtr($cvar);");
969 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "pointer") {
970 $self->assign($target, "PyCObject_AsVoidPtr($cvar)");
974 fatal($location, "unknown type `$actual_ctype->{TYPE}' for ".mapTypeName($ctype) . ": $cvar");
978 sub ConvertObjectFromPythonLevel($$$$$$$$)
980 my ($self, $env, $mem_ctx, $py_var, $e, $l, $var_name, $fail) = @_;
981 my $nl = GetNextLevel($e, $l);
983 if ($l->{TYPE} eq "POINTER") {
984 if ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) {
985 $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, $nl, $var_name, $fail);
988 if ($l->{POINTER_TYPE} ne "ref") {
989 $self->pidl("if ($py_var == Py_None) {");
991 $self->pidl("$var_name = NULL;");
993 $self->pidl("} else {");
996 # if we want to handle more than one level of pointer in python interfaces
997 # then this is where we would need to allocate it
998 if ($l->{POINTER_TYPE} eq "ref") {
999 $self->pidl("$var_name = talloc_ptrtype($mem_ctx, $var_name);");
1001 $self->pidl("$var_name = NULL;");
1003 $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, $nl, get_value_of($var_name), $fail);
1004 if ($l->{POINTER_TYPE} ne "ref") {
1008 } elsif ($l->{TYPE} eq "ARRAY") {
1009 my $pl = GetPrevLevel($e, $l);
1010 if ($pl && $pl->{TYPE} eq "POINTER") {
1011 $var_name = get_pointer_to($var_name);
1014 if (is_charset_array($e, $l)) {
1015 $self->pidl("if (PyUnicode_Check($py_var)) {");
1017 # FIXME: Use Unix charset setting rather than utf-8
1018 $self->pidl($var_name . " = PyString_AS_STRING(PyUnicode_AsEncodedString($py_var, \"utf-8\", \"ignore\"));");
1020 $self->pidl("} else if (PyString_Check($py_var)) {");
1022 $self->pidl($var_name . " = PyString_AS_STRING($py_var);");
1024 $self->pidl("} else {");
1026 $self->pidl("PyErr_Format(PyExc_TypeError, \"Expected string or unicode object, got %s\", Py_TYPE($py_var)->tp_name);");
1027 $self->pidl("$fail;");
1031 my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}";
1032 $self->pidl("PY_CHECK_TYPE(&PyList_Type, $py_var, $fail);");
1035 $self->pidl("int $counter;");
1036 if (ArrayDynamicallyAllocated($e, $l)) {
1037 $self->pidl("$var_name = talloc_array_ptrtype($mem_ctx, $var_name, PyList_GET_SIZE($py_var));");
1038 $self->pidl("if (!$var_name) { $fail; }");
1039 $self->pidl("talloc_set_name_const($var_name, \"ARRAY: $var_name\");");
1041 $self->pidl("for ($counter = 0; $counter < PyList_GET_SIZE($py_var); $counter++) {");
1043 $self->ConvertObjectFromPythonLevel($env, $var_name, "PyList_GET_ITEM($py_var, $counter)", $e, GetNextLevel($e, $l), $var_name."[$counter]", $fail);
1049 } elsif ($l->{TYPE} eq "DATA") {
1050 if (not Parse::Pidl::Typelist::is_scalar($l->{DATA_TYPE})) {
1051 $var_name = get_pointer_to($var_name);
1053 $self->ConvertObjectFromPythonData($mem_ctx, $py_var, $l->{DATA_TYPE}, $var_name, $fail, $e->{ORIGINAL});
1054 } elsif ($l->{TYPE} eq "SWITCH") {
1055 $var_name = get_pointer_to($var_name);
1056 my $switch = ParseExpr($l->{SWITCH_IS}, $env, $e);
1057 my $switch_ptr = "$e->{NAME}_switch_$l->{LEVEL_INDEX}";
1060 my $union_type = mapTypeName(GetNextLevel($e, $l)->{DATA_TYPE});
1061 $self->pidl("$union_type *$switch_ptr;");
1062 $self->pidl("$switch_ptr = py_export_" . GetNextLevel($e, $l)->{DATA_TYPE} . "($mem_ctx, $switch, $py_var);");
1063 $self->pidl("if ($switch_ptr == NULL) { $fail }");
1064 $self->assign($var_name, "$switch_ptr");
1067 } elsif ($l->{TYPE} eq "SUBCONTEXT") {
1068 $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, GetNextLevel($e, $l), $var_name, $fail);
1070 fatal($e->{ORIGINAL}, "unknown level type $l->{TYPE}");
1074 sub ConvertObjectFromPython($$$$$$$)
1076 my ($self, $env, $mem_ctx, $ctype, $cvar, $target, $fail) = @_;
1078 $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $cvar, $ctype, $ctype->{LEVELS}[0], $target, $fail);
1081 sub ConvertScalarToPython($$$)
1083 my ($self, $ctypename, $cvar) = @_;
1085 die("expected string for $cvar, not $ctypename") if (ref($ctypename) eq "HASH");
1087 $ctypename = expandAlias($ctypename);
1089 if ($ctypename =~ /^(u?int64|hyper|dlong|udlong|udlongr|NTTIME_hyper|NTTIME|NTTIME_1sec)$/) {
1090 return "PyLong_FromLongLong($cvar)";
1093 if ($ctypename =~ /^(char|u?int[0-9]*|time_t)$/) {
1094 return "PyInt_FromLong($cvar)";
1097 if ($ctypename eq "DATA_BLOB") {
1098 return "PyString_FromStringAndSize((char *)($cvar).data, ($cvar).length)";
1101 if ($ctypename eq "NTSTATUS") {
1102 return "PyErr_FromNTSTATUS($cvar)";
1105 if ($ctypename eq "WERROR") {
1106 return "PyErr_FromWERROR($cvar)";
1109 if (($ctypename eq "string" or $ctypename eq "nbt_string" or $ctypename eq "nbt_name" or $ctypename eq "wrepl_nbt_name")) {
1110 return "PyString_FromString_check_null($cvar)";
1113 if (($ctypename eq "dns_string" or $ctypename eq "dns_name")) {
1114 return "PyString_FromString_check_null($cvar)";
1118 if ($ctypename eq "string_array") { return "PyCObject_FromTallocPtr($cvar)"; }
1119 if ($ctypename eq "ipv4address") { return "PyString_FromString_check_null($cvar)"; }
1120 if ($ctypename eq "ipv6address") { return "PyString_FromString_check_null($cvar)"; }
1121 if ($ctypename eq "dnsp_name") { return "PyString_FromString_check_null($cvar)"; }
1122 if ($ctypename eq "dnsp_string") { return "PyString_FromString_check_null($cvar)"; }
1123 if ($ctypename eq "pointer") {
1124 return "PyCObject_FromTallocPtr($cvar)";
1127 die("Unknown scalar type $ctypename");
1130 sub ConvertObjectToPythonData($$$$$;$)
1132 my ($self, $mem_ctx, $ctype, $cvar, $location) = @_;
1134 die("undef type for $cvar") unless(defined($ctype));
1136 $ctype = resolveType($ctype);
1138 my $actual_ctype = $ctype;
1139 if ($actual_ctype->{TYPE} eq "TYPEDEF") {
1140 $actual_ctype = $actual_ctype->{DATA};
1143 if ($actual_ctype->{TYPE} eq "ENUM") {
1144 return $self->ConvertScalarToPython(Parse::Pidl::Typelist::enum_type_fn($actual_ctype), $cvar);
1145 } elsif ($actual_ctype->{TYPE} eq "BITMAP") {
1146 return $self->ConvertScalarToPython(Parse::Pidl::Typelist::bitmap_type_fn($actual_ctype), $cvar);
1147 } elsif ($actual_ctype->{TYPE} eq "SCALAR") {
1148 return $self->ConvertScalarToPython($actual_ctype->{NAME}, $cvar);
1149 } elsif ($actual_ctype->{TYPE} eq "UNION") {
1150 fatal($ctype, "union without discriminant: " . mapTypeName($ctype) . ": $cvar");
1151 } elsif ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") {
1152 my $ctype_name = $self->use_type_variable($ctype);
1153 unless (defined($ctype_name)) {
1154 error($location, "Unable to determine origin of type `" . mapTypeName($ctype) . "'");
1155 return "NULL"; # FIXME!
1157 return "py_talloc_reference_ex($ctype_name, $mem_ctx, $cvar)";
1160 fatal($location, "unknown type $actual_ctype->{TYPE} for ".mapTypeName($ctype) . ": $cvar");
1163 sub fail_on_null($$$)
1165 my ($self, $var, $fail) = @_;
1166 $self->pidl("if ($var == NULL) {");
1173 sub ConvertObjectToPythonLevel($$$$$$)
1175 my ($self, $mem_ctx, $env, $e, $l, $var_name, $py_var, $fail) = @_;
1176 my $nl = GetNextLevel($e, $l);
1178 if ($l->{TYPE} eq "POINTER") {
1179 if ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) {
1180 $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, $var_name, $py_var, $fail);
1183 if ($l->{POINTER_TYPE} ne "ref") {
1184 $self->pidl("if ($var_name == NULL) {");
1186 $self->pidl("$py_var = Py_None;");
1187 $self->pidl("Py_INCREF($py_var);");
1189 $self->pidl("} else {");
1192 $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, get_value_of($var_name), $py_var, $fail);
1193 if ($l->{POINTER_TYPE} ne "ref") {
1197 } elsif ($l->{TYPE} eq "ARRAY") {
1198 my $pl = GetPrevLevel($e, $l);
1199 if ($pl && $pl->{TYPE} eq "POINTER") {
1200 $var_name = get_pointer_to($var_name);
1203 if (is_charset_array($e, $l)) {
1204 # FIXME: Use Unix charset setting rather than utf-8
1205 $self->pidl("if ($var_name == NULL) {");
1207 $self->pidl("$py_var = Py_None;");
1208 $self->pidl("Py_INCREF($py_var);");
1210 $self->pidl("} else {");
1212 $self->pidl("$py_var = PyUnicode_Decode($var_name, strlen($var_name), \"utf-8\", \"ignore\");");
1216 die("No SIZE_IS for array $var_name") unless (defined($l->{SIZE_IS}));
1217 my $length = $l->{SIZE_IS};
1218 if (defined($l->{LENGTH_IS})) {
1219 $length = $l->{LENGTH_IS};
1222 $length = ParseExpr($length, $env, $e);
1223 $self->pidl("$py_var = PyList_New($length);");
1224 $self->fail_on_null($py_var, $fail);
1227 my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}";
1228 $self->pidl("int $counter;");
1229 $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
1231 my $member_var = "py_$e->{NAME}_$l->{LEVEL_INDEX}";
1232 $self->pidl("PyObject *$member_var;");
1233 $self->ConvertObjectToPythonLevel($var_name, $env, $e, GetNextLevel($e, $l), $var_name."[$counter]", $member_var, $fail);
1234 $self->pidl("PyList_SetItem($py_var, $counter, $member_var);");
1240 } elsif ($l->{TYPE} eq "SWITCH") {
1241 $var_name = get_pointer_to($var_name);
1242 my $switch = ParseExpr($l->{SWITCH_IS}, $env, $e);
1243 $self->pidl("$py_var = py_import_" . GetNextLevel($e, $l)->{DATA_TYPE} . "($mem_ctx, $switch, $var_name);");
1244 $self->fail_on_null($py_var, $fail);
1246 } elsif ($l->{TYPE} eq "DATA") {
1247 if (not Parse::Pidl::Typelist::is_scalar($l->{DATA_TYPE})) {
1248 $var_name = get_pointer_to($var_name);
1250 my $conv = $self->ConvertObjectToPythonData($mem_ctx, $l->{DATA_TYPE}, $var_name, $e->{ORIGINAL});
1251 $self->pidl("$py_var = $conv;");
1252 } elsif ($l->{TYPE} eq "SUBCONTEXT") {
1253 $self->ConvertObjectToPythonLevel($mem_ctx, $env, $e, GetNextLevel($e, $l), $var_name, $py_var, $fail);
1255 fatal($e->{ORIGINAL}, "Unknown level type $l->{TYPE} $var_name");
1259 sub ConvertObjectToPython($$$$$$)
1261 my ($self, $mem_ctx, $env, $ctype, $cvar, $py_var, $fail) = @_;
1263 $self->ConvertObjectToPythonLevel($mem_ctx, $env, $ctype, $ctype->{LEVELS}[0], $cvar, $py_var, $fail);
1268 my($self,$basename,$ndr,$ndr_hdr,$hdr) = @_;
1270 $self->{BASENAME} = $basename;
1273 /* Python wrapper functions auto-generated by pidl */
1275 #include \"includes.h\"
1276 #include \"lib/talloc/pytalloc.h\"
1277 #include \"librpc/rpc/pyrpc.h\"
1278 #include \"librpc/rpc/pyrpc_util.h\"
1280 #include \"$ndr_hdr\"
1284 foreach my $x (@$ndr) {
1285 ($x->{TYPE} eq "IMPORT") && $self->Import(@{$x->{PATHS}});
1286 ($x->{TYPE} eq "INTERFACE") && $self->Interface($x, $basename);
1289 $self->pidl("static PyMethodDef $basename\_methods[] = {");
1291 foreach (@{$self->{module_methods}}) {
1292 my ($fn_name, $pyfn_name, $flags, $doc) = @$_;
1293 $self->pidl("{ \"$fn_name\", (PyCFunction)$pyfn_name, $flags, $doc },");
1296 $self->pidl("{ NULL, NULL, 0, NULL }");
1302 $self->pidl("void init$basename(void)");
1305 $self->pidl("PyObject *m;");
1306 foreach (keys %{$self->{module_imports}}) {
1307 $self->pidl("PyObject *$_;");
1311 foreach (keys %{$self->{module_imports}}) {
1313 my $module_path = $self->{module_imports}->{$var_name};
1314 $self->pidl("$var_name = PyImport_ImportModule(\"$module_path\");");
1315 $self->pidl("if ($var_name == NULL)");
1316 $self->pidl("\treturn;");
1320 foreach (keys %{$self->{type_imports}}) {
1321 my $type_var = "$_\_Type";
1322 my $module_path = $self->{type_imports}->{$_};
1323 $self->pidl_hdr("static PyTypeObject *$type_var;\n");
1324 my $pretty_name = PrettifyTypeName($_, $module_path);
1325 my $module_var = "dep_$module_path";
1326 $module_var =~ s/\./_/g;
1327 $self->pidl("$type_var = (PyTypeObject *)PyObject_GetAttrString($module_var, \"$pretty_name\");");
1328 $self->pidl("if ($type_var == NULL)");
1329 $self->pidl("\treturn;");
1333 $self->pidl($_) foreach (@{$self->{prereadycode}});
1335 foreach (@{$self->{ready_types}}) {
1336 $self->pidl("if (PyType_Ready($_) < 0)");
1337 $self->pidl("\treturn;");
1340 $self->pidl($_) foreach (@{$self->{postreadycode}});
1342 foreach (@{$self->{patch_type_calls}}) {
1343 my ($typename, $cvar) = @$_;
1344 $self->pidl("#ifdef PY_".uc($typename)."_PATCH");
1345 $self->pidl("PY_".uc($typename)."_PATCH($cvar);");
1346 $self->pidl("#endif");
1351 $self->pidl("m = Py_InitModule3(\"$basename\", $basename\_methods, \"$basename DCE/RPC\");");
1352 $self->pidl("if (m == NULL)");
1353 $self->pidl("\treturn;");
1355 foreach my $name (keys %{$self->{constants}}) {
1357 my ($ctype, $cvar) = @{$self->{constants}->{$name}};
1358 if ($cvar =~ /^[0-9]+$/ or $cvar =~ /^0x[0-9a-fA-F]+$/) {
1359 $py_obj = "PyInt_FromLong($cvar)";
1360 } elsif ($cvar =~ /^".*"$/) {
1361 $py_obj = "PyString_FromString($cvar)";
1363 $py_obj = $self->ConvertObjectToPythonData("NULL", expandAlias($ctype), $cvar, undef);
1366 $self->pidl("PyModule_AddObject(m, \"$name\", $py_obj);");
1369 foreach (@{$self->{module_objects}}) {
1370 my ($object_name, $c_name) = @$_;
1371 $self->pidl("Py_INCREF($c_name);");
1372 $self->pidl("PyModule_AddObject(m, \"$object_name\", $c_name);");
1375 $self->pidl("#ifdef PY_MOD_".uc($basename)."_PATCH");
1376 $self->pidl("PY_MOD_".uc($basename)."_PATCH(m);");
1377 $self->pidl("#endif");
1382 return ($self->{res_hdr} . $self->{res});