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 ContainsPipe 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("PyObject *ret;");
301 $self->pidl("char *retstr;");
303 $self->pidl("retstr = ndr_print_struct_string(py_talloc_get_mem_ctx(py_obj), (ndr_print_fn_t)ndr_print_$name, \"$name\", object);");
304 $self->pidl("ret = PyString_FromString(retstr);");
305 $self->pidl("talloc_free(retstr);");
307 $self->pidl("return ret;");
312 $py_methods = "py_$name\_methods";
313 $self->pidl("static PyMethodDef $py_methods\[] = {");
315 $self->pidl("{ \"__ndr_pack__\", (PyCFunction)py_$name\_ndr_pack, METH_NOARGS, \"S.ndr_pack(object) -> blob\\nNDR pack\" },");
316 $self->pidl("{ \"__ndr_unpack__\", (PyCFunction)py_$name\_ndr_unpack, METH_VARARGS, \"S.ndr_unpack(class, blob) -> None\\nNDR unpack\" },");
317 $self->pidl("{ \"__ndr_print__\", (PyCFunction)py_$name\_ndr_print, METH_VARARGS, \"S.ndr_print(object) -> None\\nNDR print\" },");
318 $self->pidl("{ NULL, NULL, 0, NULL }");
324 $self->pidl_hdr("staticforward PyTypeObject $name\_Type;\n");
326 my $docstring = $self->DocString($d, $name);
327 my $typeobject = "$name\_Type";
328 $self->pidl("static PyTypeObject $typeobject = {");
330 $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
331 $self->pidl(".tp_name = \"$modulename.$prettyname\",");
332 $self->pidl(".tp_getset = $getsetters,");
334 $self->pidl(".tp_doc = $docstring,");
336 $self->pidl(".tp_methods = $py_methods,");
337 $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,");
338 $self->pidl(".tp_basicsize = sizeof(py_talloc_Object),");
339 $self->pidl(".tp_new = py_$name\_new,");
345 my $talloc_typename = $self->import_type_variable("talloc", "Object");
346 $self->register_module_prereadycode(["$name\_Type.tp_base = $talloc_typename;", ""]);
348 return "&$typeobject";
351 sub get_metadata_var($)
354 sub get_var($) { my $x = shift; $x =~ s/\*//g; return $x; }
356 if (has_property($e, "length_is")) {
357 return get_var($e->{PROPERTIES}->{length_is});
358 } elsif (has_property($e, "size_is")) {
359 return get_var($e->{PROPERTIES}->{size_is});
365 sub find_metadata_args($)
368 my $metadata_args = { in => {}, out => {} };
370 # Determine arguments that are metadata for other arguments (size_is/length_is)
371 foreach my $e (@{$fn->{ELEMENTS}}) {
372 foreach my $dir (@{$e->{DIRECTION}}) {
373 my $main = get_metadata_var($e);
375 $metadata_args->{$dir}->{$main} = $e->{NAME};
380 return $metadata_args;
383 sub PythonFunctionUnpackOut($$$)
385 my ($self, $fn, $fnname) = @_;
387 my $outfnname = "unpack_$fnname\_args_out";
390 my $metadata_args = find_metadata_args($fn);
392 my $env = GenerateFunctionOutEnv($fn, "r->");
395 $self->pidl("static PyObject *$outfnname(struct $fn->{NAME} *r)");
398 $self->pidl("PyObject *result;");
399 foreach my $e (@{$fn->{ELEMENTS}}) {
400 next unless (grep(/out/,@{$e->{DIRECTION}}));
401 next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or
402 ($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}}));
403 $self->pidl("PyObject *py_$e->{NAME};");
407 if ($fn->{RETURN_TYPE}) {
408 $result_size++ unless ($fn->{RETURN_TYPE} eq "WERROR" or $fn->{RETURN_TYPE} eq "NTSTATUS");
413 if ($result_size > 1) {
414 $self->pidl("result = PyTuple_New($result_size);");
416 } elsif ($result_size == 0) {
417 $self->pidl("result = Py_None;");
418 $self->pidl("Py_INCREF(result);");
419 $signature .= "None";
422 foreach my $e (@{$fn->{ELEMENTS}}) {
423 next if ($metadata_args->{out}->{$e->{NAME}});
424 my $py_name = "py_$e->{NAME}";
425 if (grep(/out/,@{$e->{DIRECTION}})) {
426 $self->ConvertObjectToPython("r", $env, $e, "r->out.$e->{NAME}", $py_name, "return NULL;");
427 if ($result_size > 1) {
428 $self->pidl("PyTuple_SetItem(result, $i, $py_name);");
430 $signature .= "$e->{NAME}, ";
432 $self->pidl("result = $py_name;");
433 $signature .= $e->{NAME};
438 if (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "NTSTATUS") {
439 $self->handle_ntstatus("r->out.result", "NULL", undef);
440 } elsif (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "WERROR") {
441 $self->handle_werror("r->out.result", "NULL", undef);
442 } elsif (defined($fn->{RETURN_TYPE})) {
443 my $conv = $self->ConvertObjectToPythonData("r", $fn->{RETURN_TYPE}, "r->out.result", $fn);
444 if ($result_size > 1) {
445 $self->pidl("PyTuple_SetItem(result, $i, $conv);");
447 $self->pidl("result = $conv;");
449 $signature .= "result";
452 if (substr($signature, -2) eq ", ") {
453 $signature = substr($signature, 0, -2);
455 if ($result_size > 1) {
459 $self->pidl("return result;");
464 return ($outfnname, $signature);
467 sub PythonFunctionPackIn($$$)
469 my ($self, $fn, $fnname) = @_;
470 my $metadata_args = find_metadata_args($fn);
472 my $infnname = "pack_$fnname\_args_in";
474 $self->pidl("static bool $infnname(PyObject *args, PyObject *kwargs, struct $fn->{NAME} *r)");
477 my $args_format = "";
478 my $args_string = "";
482 foreach my $e (@{$fn->{ELEMENTS}}) {
483 next unless (grep(/in/,@{$e->{DIRECTION}}));
484 next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or
485 ($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}}));
486 $self->pidl("PyObject *py_$e->{NAME};");
488 $args_string .= ", &py_$e->{NAME}";
489 $args_names .= "\"$e->{NAME}\", ";
490 $signature .= "$e->{NAME}, ";
492 if (substr($signature, -2) eq ", ") {
493 $signature = substr($signature, 0, -2);
495 $self->pidl("const char *kwnames[] = {");
497 $self->pidl($args_names . "NULL");
502 $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"$args_format:$fn->{NAME}\", discard_const_p(char *, kwnames)$args_string)) {");
504 $self->pidl("return false;");
509 my $env = GenerateFunctionInEnv($fn, "r->");
511 my $fail = "return false;";
512 foreach my $e (@{$fn->{ELEMENTS}}) {
513 next unless (grep(/in/,@{$e->{DIRECTION}}));
514 if ($metadata_args->{in}->{$e->{NAME}}) {
515 my $py_var = "py_".$metadata_args->{in}->{$e->{NAME}};
516 $self->pidl("PY_CHECK_TYPE(&PyList_Type, $py_var, $fail);");
517 my $val = "PyList_GET_SIZE($py_var)";
518 if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
519 $self->pidl("r->in.$e->{NAME} = talloc_ptrtype(r, r->in.$e->{NAME});");
520 $self->pidl("*r->in.$e->{NAME} = $val;");
522 $self->pidl("r->in.$e->{NAME} = $val;");
525 $self->ConvertObjectFromPython($env, "r", $e, "py_$e->{NAME}", "r->in.$e->{NAME}", $fail);
528 $self->pidl("return true;");
532 return ($infnname, $signature);
535 sub PythonFunction($$$)
537 my ($self, $fn, $iface, $prettyname) = @_;
539 my $fnname = "py_$fn->{NAME}";
540 my $docstring = $self->DocString($fn, $fn->{NAME});
542 my ($infn, $insignature) = $self->PythonFunctionPackIn($fn, $fnname);
543 my ($outfn, $outsignature) = $self->PythonFunctionUnpackOut($fn, $fnname);
544 my $signature = "S.$prettyname($insignature) -> $outsignature";
546 $docstring = "\"$signature\\n\\n\"$docstring";
548 $docstring = "\"$signature\"";
551 return ($infn, $outfn, $docstring);
554 sub handle_werror($$$$)
556 my ($self, $var, $retval, $mem_ctx) = @_;
558 $self->pidl("if (!W_ERROR_IS_OK($var)) {");
560 $self->pidl("PyErr_SetWERROR($var);");
561 $self->pidl("talloc_free($mem_ctx);") if ($mem_ctx);
562 $self->pidl("return $retval;");
568 sub handle_ntstatus($$$$)
570 my ($self, $var, $retval, $mem_ctx) = @_;
572 $self->pidl("if (NT_STATUS_IS_ERR($var)) {");
574 $self->pidl("PyErr_SetNTSTATUS($var);");
575 $self->pidl("talloc_free($mem_ctx);") if ($mem_ctx);
576 $self->pidl("return $retval;");
584 my ($self, $modulename, $d, $interface, $basename) = @_;
586 my $actual_ctype = $d;
587 if ($actual_ctype->{TYPE} eq "TYPEDEF") {
588 $actual_ctype = $actual_ctype->{DATA};
591 if ($actual_ctype->{TYPE} eq "STRUCT") {
593 my $fn_name = PrettifyTypeName($d->{NAME}, $basename);
595 if ($d->{TYPE} eq "STRUCT") {
596 $typeobject = $self->PythonStruct($modulename, $fn_name, $d->{NAME}, mapTypeName($d), $d);
598 $typeobject = $self->PythonStruct($modulename, $fn_name, $d->{NAME}, mapTypeName($d), $d->{DATA});
601 $self->register_module_typeobject($fn_name, $typeobject);
604 if ($d->{TYPE} eq "ENUM" or $d->{TYPE} eq "BITMAP") {
605 $self->EnumAndBitmapConsts($d->{NAME}, $d);
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});
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)");
616 $self->FromUnionToPythonFunction("mem_ctx", $actual_ctype, "level", "in") if ($actual_ctype->{TYPE} eq "UNION");
621 $self->pidl(mapTypeName($d) . " *py_export_$d->{NAME}(TALLOC_CTX *mem_ctx, int level, PyObject *in)");
624 $self->FromPythonToUnionFunction($actual_ctype, mapTypeName($d), "level", "mem_ctx", "in") if ($actual_ctype->{TYPE} eq "UNION");
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"));
645 my($self,$interface,$basename) = @_;
647 if (has_property($interface, "pyhelper")) {
648 $self->pidl("#include \"".unmake_str($interface->{PROPERTIES}->{pyhelper})."\"\n");
651 $self->Const($_) foreach (@{$interface->{CONSTS}});
653 foreach my $d (@{$interface->{TYPES}}) {
654 next if has_property($d, "nopython");
656 $self->PythonType($basename, $d, $interface, $basename);
659 if (defined $interface->{PROPERTIES}->{uuid}) {
660 $self->pidl_hdr("staticforward PyTypeObject $interface->{NAME}_InterfaceType;\n");
665 foreach my $d (@{$interface->{FUNCTIONS}}) {
666 next if has_property($d, "noopnum");
667 next if has_property($d, "nopython");
668 next if has_property($d, "todo");
671 foreach my $e (@{$d->{ELEMENTS}}) {
672 if (ContainsPipe($e, $e->{LEVELS}[0])) {
679 my $prettyname = $d->{NAME};
681 $prettyname =~ s/^$interface->{NAME}_//;
682 $prettyname =~ s/^$basename\_//;
684 my ($infn, $outfn, $fndocstring) = $self->PythonFunction($d, $interface->{NAME}, $prettyname);
686 push (@fns, [$infn, $outfn, "dcerpc_$d->{NAME}_r", $prettyname, $fndocstring, $d->{OPNUM}]);
689 $self->pidl("const struct PyNdrRpcMethodDef py_ndr_$interface->{NAME}\_methods[] = {");
691 foreach my $d (@fns) {
692 my ($infn, $outfn, $callfn, $prettyname, $docstring, $opnum) = @$d;
693 $self->pidl("{ \"$prettyname\", $docstring, (py_dcerpc_call_fn)$callfn, (py_data_pack_fn)$infn, (py_data_unpack_fn)$outfn, $opnum, &ndr_table_$interface->{NAME} },");
695 $self->pidl("{ NULL }");
700 $self->pidl("static PyObject *interface_$interface->{NAME}_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)");
703 $self->pidl("return py_dcerpc_interface_init_helper(type, args, kwargs, &ndr_table_$interface->{NAME});");
710 "\"$interface->{NAME}(binding, lp_ctx=None, credentials=None) -> connection\\n\"
712 \"binding should be a DCE/RPC binding string (for example: ncacn_ip_tcp:127.0.0.1)\\n\"
713 \"lp_ctx should be a path to a smb.conf file or a param.LoadParm object\\n\"
714 \"credentials should be a credentials.Credentials object.\\n\\n\"";
716 my $docstring = $self->DocString($interface, $interface->{NAME});
719 $docstring = "$signature$docstring";
721 $docstring = $signature;
724 my $if_typename = "$interface->{NAME}_InterfaceType";
726 $self->pidl("static PyTypeObject $if_typename = {");
728 $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
729 $self->pidl(".tp_name = \"$basename.$interface->{NAME}\",");
730 $self->pidl(".tp_basicsize = sizeof(dcerpc_InterfaceObject),");
731 $self->pidl(".tp_doc = $docstring,");
732 $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,");
733 $self->pidl(".tp_new = interface_$interface->{NAME}_new,");
739 $self->register_module_typeobject($interface->{NAME}, "&$if_typename");
740 my $dcerpc_typename = $self->import_type_variable("samba.dcerpc.base", "ClientConnection");
741 $self->register_module_prereadycode(["$if_typename.tp_base = $dcerpc_typename;", ""]);
742 $self->register_module_postreadycode(["if (!PyInterface_AddNdrRpcMethods(&$if_typename, py_ndr_$interface->{NAME}\_methods))", "\treturn;", ""]);
745 $self->pidl_hdr("\n");
748 sub register_module_method($$$$$)
750 my ($self, $fn_name, $pyfn_name, $flags, $doc) = @_;
752 push (@{$self->{module_methods}}, [$fn_name, $pyfn_name, $flags, $doc])
755 sub register_module_typeobject($$$)
757 my ($self, $name, $py_name) = @_;
759 $self->register_module_object($name, "(PyObject *)(void *)$py_name");
761 $self->check_ready_type($py_name);
763 $self->register_patch_type_call($name, $py_name);
766 sub check_ready_type($$)
768 my ($self, $py_name) = @_;
769 push (@{$self->{ready_types}}, $py_name) unless (grep(/^$py_name$/,@{$self->{ready_types}}));
772 sub register_module_import($$)
774 my ($self, $module_path) = @_;
776 my $var_name = $module_path;
777 $var_name =~ s/\./_/g;
778 $var_name = "dep_$var_name";
780 $self->{module_imports}->{$var_name} = $module_path;
785 sub import_type_variable($$$)
787 my ($self, $module, $name) = @_;
789 $self->register_module_import($module);
790 unless (defined($self->{type_imports}->{$name})) {
791 $self->{type_imports}->{$name} = $module;
793 return "$name\_Type";
796 sub use_type_variable($$)
798 my ($self, $orig_ctype) = @_;
799 # FIXME: Have a global lookup table for types that look different on the
800 # wire than they are named in C?
801 if ($orig_ctype->{NAME} eq "dom_sid2" or $orig_ctype->{NAME} eq "dom_sid28") {
802 $orig_ctype->{NAME} = "dom_sid";
804 my $ctype = resolveType($orig_ctype);
805 unless (defined($ctype->{BASEFILE})) {
808 # If this is an external type, make sure we do the right imports.
809 if (($ctype->{BASEFILE} ne $self->{BASENAME})) {
810 return $self->import_type_variable("samba.dcerpc.$ctype->{BASEFILE}", $ctype->{NAME});
812 return "&$ctype->{NAME}_Type";
815 sub register_patch_type_call($$$)
817 my ($self, $typename, $cvar) = @_;
819 push(@{$self->{patch_type_calls}}, [$typename, $cvar]);
823 sub register_module_prereadycode($$)
825 my ($self, $code) = @_;
827 push (@{$self->{prereadycode}}, @$code);
830 sub register_module_postreadycode($$)
832 my ($self, $code) = @_;
834 push (@{$self->{postreadycode}}, @$code);
837 sub register_module_object($$$)
839 my ($self, $name, $py_name) = @_;
841 push (@{$self->{module_objects}}, [$name, $py_name])
846 my ($self, $dest, $src) = @_;
847 if ($dest =~ /^\&/ and $src eq "NULL") {
848 $self->pidl("memset($dest, 0, sizeof(" . get_value_of($dest) . "));");
849 } elsif ($dest =~ /^\&/) {
850 my $destvar = get_value_of($dest);
851 $self->pidl("$destvar = *$src;");
853 $self->pidl("$dest = $src;");
857 sub ConvertObjectFromPythonData($$$$$$;$)
859 my ($self, $mem_ctx, $cvar, $ctype, $target, $fail, $location) = @_;
861 fatal($location, "undef type for $cvar") unless(defined($ctype));
863 $ctype = resolveType($ctype);
865 my $actual_ctype = $ctype;
866 if ($actual_ctype->{TYPE} eq "TYPEDEF") {
867 $actual_ctype = $actual_ctype->{DATA};
870 if ($actual_ctype->{TYPE} eq "ENUM" or $actual_ctype->{TYPE} eq "BITMAP") {
871 $self->pidl("if (PyLong_Check($cvar)) {");
873 $self->pidl("$target = PyLong_AsLongLong($cvar);");
875 $self->pidl("} else if (PyInt_Check($cvar)) {");
877 $self->pidl("$target = PyInt_AsLong($cvar);");
879 $self->pidl("} else {");
881 $self->pidl("PyErr_Format(PyExc_TypeError, \"Expected type %s or %s\",\\");
882 $self->pidl(" PyInt_Type.tp_name, PyLong_Type.tp_name);");
888 if ($actual_ctype->{TYPE} eq "SCALAR" ) {
889 if (expandAlias($actual_ctype->{NAME}) =~ /^(u?int64|hyper|dlong|udlong|udlongr|NTTIME_hyper|NTTIME|NTTIME_1sec)$/) {
890 $self->pidl("if (PyLong_Check($cvar)) {");
892 $self->pidl("$target = PyLong_AsLongLong($cvar);");
894 $self->pidl("} else if (PyInt_Check($cvar)) {");
896 $self->pidl("$target = PyInt_AsLong($cvar);");
898 $self->pidl("} else {");
900 $self->pidl("PyErr_Format(PyExc_TypeError, \"Expected type %s or %s\",\\");
901 $self->pidl(" PyInt_Type.tp_name, PyLong_Type.tp_name);");
907 if (expandAlias($actual_ctype->{NAME}) =~ /^(char|u?int[0-9]*|time_t|uid_t|gid_t)$/) {
908 $self->pidl("PY_CHECK_TYPE(&PyInt_Type, $cvar, $fail);");
909 $self->pidl("$target = PyInt_AsLong($cvar);");
914 if ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") {
915 my $ctype_name = $self->use_type_variable($ctype);
916 unless (defined ($ctype_name)) {
917 error($location, "Unable to determine origin of type `" . mapTypeName($ctype) . "'");
918 $self->pidl("PyErr_SetString(PyExc_TypeError, \"Can not convert C Type " . mapTypeName($ctype) . " from Python\");");
921 $self->pidl("PY_CHECK_TYPE($ctype_name, $cvar, $fail);");
922 $self->pidl("if (talloc_reference($mem_ctx, py_talloc_get_mem_ctx($cvar)) == NULL) {");
924 $self->pidl("PyErr_NoMemory();");
925 $self->pidl("$fail");
928 $self->assign($target, "(".mapTypeName($ctype)." *)py_talloc_get_ptr($cvar)");
932 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "DATA_BLOB") {
933 $self->pidl("$target = data_blob_talloc($mem_ctx, PyString_AS_STRING($cvar), PyString_GET_SIZE($cvar));");
937 if ($actual_ctype->{TYPE} eq "SCALAR" and
938 ($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")) {
939 $self->pidl("$target = talloc_strdup($mem_ctx, PyString_AS_STRING($cvar));");
943 if ($actual_ctype->{TYPE} eq "SCALAR" and ($actual_ctype->{NAME} eq "dns_string" or $actual_ctype->{NAME} eq "dns_name")) {
944 $self->pidl("$target = talloc_strdup($mem_ctx, PyString_AS_STRING($cvar));");
948 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "ipv4address") {
949 $self->pidl("$target = PyString_AS_STRING($cvar);");
953 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "ipv6address") {
954 $self->pidl("$target = PyString_AsString($cvar);");
958 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "dnsp_name") {
959 $self->pidl("$target = PyString_AS_STRING($cvar);");
963 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "dnsp_string") {
964 $self->pidl("$target = PyString_AS_STRING($cvar);");
968 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "NTSTATUS") {
969 $self->pidl("$target = NT_STATUS(PyInt_AsLong($cvar));");
973 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "WERROR") {
974 $self->pidl("$target = W_ERROR(PyInt_AsLong($cvar));");
978 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "string_array") {
979 $self->pidl("$target = PyCObject_AsVoidPtr($cvar);");
983 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "pointer") {
984 $self->assign($target, "PyCObject_AsVoidPtr($cvar)");
988 fatal($location, "unknown type `$actual_ctype->{TYPE}' for ".mapTypeName($ctype) . ": $cvar");
992 sub ConvertObjectFromPythonLevel($$$$$$$$)
994 my ($self, $env, $mem_ctx, $py_var, $e, $l, $var_name, $fail) = @_;
995 my $nl = GetNextLevel($e, $l);
996 if ($nl and $nl->{TYPE} eq "SUBCONTEXT") {
997 $nl = GetNextLevel($e, $nl);
999 my $pl = GetPrevLevel($e, $l);
1000 if ($pl and $pl->{TYPE} eq "SUBCONTEXT") {
1001 $pl = GetPrevLevel($e, $pl);
1004 if ($l->{TYPE} eq "POINTER") {
1005 if ($l->{POINTER_TYPE} ne "ref") {
1006 $self->pidl("if ($py_var == Py_None) {");
1008 $self->pidl("$var_name = NULL;");
1010 $self->pidl("} else {");
1013 # if we want to handle more than one level of pointer in python interfaces
1014 # then this is where we would need to allocate it
1015 if ($l->{POINTER_TYPE} eq "ref") {
1016 $self->pidl("$var_name = talloc_ptrtype($mem_ctx, $var_name);");
1017 } elsif ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::is_scalar($nl->{DATA_TYPE})
1018 and not Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) {
1019 $self->pidl("$var_name = talloc_ptrtype($mem_ctx, $var_name);");
1021 $self->pidl("$var_name = NULL;");
1023 unless ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) {
1024 $var_name = get_value_of($var_name);
1026 $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, $nl, $var_name, $fail);
1027 if ($l->{POINTER_TYPE} ne "ref") {
1031 } elsif ($l->{TYPE} eq "ARRAY") {
1032 if ($pl && $pl->{TYPE} eq "POINTER") {
1033 $var_name = get_pointer_to($var_name);
1036 if (is_charset_array($e, $l)) {
1037 $self->pidl("if (PyUnicode_Check($py_var)) {");
1039 # FIXME: Use Unix charset setting rather than utf-8
1040 $self->pidl($var_name . " = PyString_AS_STRING(PyUnicode_AsEncodedString($py_var, \"utf-8\", \"ignore\"));");
1042 $self->pidl("} else if (PyString_Check($py_var)) {");
1044 $self->pidl($var_name . " = PyString_AS_STRING($py_var);");
1046 $self->pidl("} else {");
1048 $self->pidl("PyErr_Format(PyExc_TypeError, \"Expected string or unicode object, got %s\", Py_TYPE($py_var)->tp_name);");
1049 $self->pidl("$fail");
1053 my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}";
1054 $self->pidl("PY_CHECK_TYPE(&PyList_Type, $py_var, $fail);");
1057 $self->pidl("int $counter;");
1058 if (ArrayDynamicallyAllocated($e, $l)) {
1059 $self->pidl("$var_name = talloc_array_ptrtype($mem_ctx, $var_name, PyList_GET_SIZE($py_var));");
1060 $self->pidl("if (!$var_name) { $fail; }");
1061 $self->pidl("talloc_set_name_const($var_name, \"ARRAY: $var_name\");");
1063 $self->pidl("for ($counter = 0; $counter < PyList_GET_SIZE($py_var); $counter++) {");
1065 $self->ConvertObjectFromPythonLevel($env, $var_name, "PyList_GET_ITEM($py_var, $counter)", $e, $nl, $var_name."[$counter]", $fail);
1071 } elsif ($l->{TYPE} eq "DATA") {
1072 if (not Parse::Pidl::Typelist::is_scalar($l->{DATA_TYPE})) {
1073 $var_name = get_pointer_to($var_name);
1075 $self->ConvertObjectFromPythonData($mem_ctx, $py_var, $l->{DATA_TYPE}, $var_name, $fail, $e->{ORIGINAL});
1076 } elsif ($l->{TYPE} eq "SWITCH") {
1077 $var_name = get_pointer_to($var_name);
1078 my $switch = ParseExpr($l->{SWITCH_IS}, $env, $e);
1079 my $switch_ptr = "$e->{NAME}_switch_$l->{LEVEL_INDEX}";
1082 my $union_type = mapTypeName($nl->{DATA_TYPE});
1083 $self->pidl("$union_type *$switch_ptr;");
1084 $self->pidl("$switch_ptr = py_export_" . $nl->{DATA_TYPE} . "($mem_ctx, $switch, $py_var);");
1085 $self->fail_on_null($switch_ptr, $fail);
1086 $self->assign($var_name, "$switch_ptr");
1089 } elsif ($l->{TYPE} eq "SUBCONTEXT") {
1090 $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, $nl, $var_name, $fail);
1092 fatal($e->{ORIGINAL}, "unknown level type $l->{TYPE}");
1096 sub ConvertObjectFromPython($$$$$$$)
1098 my ($self, $env, $mem_ctx, $ctype, $cvar, $target, $fail) = @_;
1100 $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $cvar, $ctype, $ctype->{LEVELS}[0], $target, $fail);
1103 sub ConvertScalarToPython($$$)
1105 my ($self, $ctypename, $cvar) = @_;
1107 die("expected string for $cvar, not $ctypename") if (ref($ctypename) eq "HASH");
1109 $ctypename = expandAlias($ctypename);
1111 if ($ctypename =~ /^(u?int64|hyper|dlong|udlong|udlongr|NTTIME_hyper|NTTIME|NTTIME_1sec)$/) {
1112 return "PyLong_FromLongLong($cvar)";
1115 if ($ctypename =~ /^(char|u?int[0-9]*|time_t|uid_t|gid_t)$/) {
1116 return "PyInt_FromLong($cvar)";
1119 if ($ctypename eq "DATA_BLOB") {
1120 return "PyString_FromStringAndSize((char *)($cvar).data, ($cvar).length)";
1123 if ($ctypename eq "NTSTATUS") {
1124 return "PyErr_FromNTSTATUS($cvar)";
1127 if ($ctypename eq "WERROR") {
1128 return "PyErr_FromWERROR($cvar)";
1131 if (($ctypename eq "string" or $ctypename eq "nbt_string" or $ctypename eq "nbt_name" or $ctypename eq "wrepl_nbt_name")) {
1132 return "PyString_FromStringOrNULL($cvar)";
1135 if (($ctypename eq "dns_string" or $ctypename eq "dns_name")) {
1136 return "PyString_FromStringOrNULL($cvar)";
1140 if ($ctypename eq "string_array") { return "PyCObject_FromTallocPtr($cvar)"; }
1141 if ($ctypename eq "ipv4address") { return "PyString_FromStringOrNULL($cvar)"; }
1142 if ($ctypename eq "ipv6address") { return "PyString_FromStringOrNULL($cvar)"; }
1143 if ($ctypename eq "dnsp_name") { return "PyString_FromStringOrNULL($cvar)"; }
1144 if ($ctypename eq "dnsp_string") { return "PyString_FromStringOrNULL($cvar)"; }
1145 if ($ctypename eq "pointer") {
1146 return "PyCObject_FromTallocPtr($cvar)";
1149 die("Unknown scalar type $ctypename");
1152 sub ConvertObjectToPythonData($$$$$;$)
1154 my ($self, $mem_ctx, $ctype, $cvar, $location) = @_;
1156 die("undef type for $cvar") unless(defined($ctype));
1158 $ctype = resolveType($ctype);
1160 my $actual_ctype = $ctype;
1161 if ($actual_ctype->{TYPE} eq "TYPEDEF") {
1162 $actual_ctype = $actual_ctype->{DATA};
1165 if ($actual_ctype->{TYPE} eq "ENUM") {
1166 return $self->ConvertScalarToPython(Parse::Pidl::Typelist::enum_type_fn($actual_ctype), $cvar);
1167 } elsif ($actual_ctype->{TYPE} eq "BITMAP") {
1168 return $self->ConvertScalarToPython(Parse::Pidl::Typelist::bitmap_type_fn($actual_ctype), $cvar);
1169 } elsif ($actual_ctype->{TYPE} eq "SCALAR") {
1170 return $self->ConvertScalarToPython($actual_ctype->{NAME}, $cvar);
1171 } elsif ($actual_ctype->{TYPE} eq "UNION") {
1172 fatal($ctype, "union without discriminant: " . mapTypeName($ctype) . ": $cvar");
1173 } elsif ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") {
1174 my $ctype_name = $self->use_type_variable($ctype);
1175 unless (defined($ctype_name)) {
1176 error($location, "Unable to determine origin of type `" . mapTypeName($ctype) . "'");
1177 return "NULL"; # FIXME!
1179 return "py_talloc_reference_ex($ctype_name, $mem_ctx, $cvar)";
1182 fatal($location, "unknown type $actual_ctype->{TYPE} for ".mapTypeName($ctype) . ": $cvar");
1185 sub fail_on_null($$$)
1187 my ($self, $var, $fail) = @_;
1188 $self->pidl("if ($var == NULL) {");
1195 sub ConvertObjectToPythonLevel($$$$$$)
1197 my ($self, $mem_ctx, $env, $e, $l, $var_name, $py_var, $fail) = @_;
1198 my $nl = GetNextLevel($e, $l);
1199 if ($nl and $nl->{TYPE} eq "SUBCONTEXT") {
1200 $nl = GetNextLevel($e, $nl);
1202 my $pl = GetPrevLevel($e, $l);
1203 if ($pl and $pl->{TYPE} eq "SUBCONTEXT") {
1204 $pl = GetPrevLevel($e, $pl);
1207 if ($l->{TYPE} eq "POINTER") {
1208 if ($l->{POINTER_TYPE} ne "ref") {
1209 $self->pidl("if ($var_name == NULL) {");
1211 $self->pidl("$py_var = Py_None;");
1212 $self->pidl("Py_INCREF($py_var);");
1214 $self->pidl("} else {");
1217 my $var_name2 = $var_name;
1218 unless ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) {
1219 $var_name2 = get_value_of($var_name);
1221 $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, $var_name2, $py_var, $fail);
1222 if ($l->{POINTER_TYPE} ne "ref") {
1226 } elsif ($l->{TYPE} eq "ARRAY") {
1227 if ($pl && $pl->{TYPE} eq "POINTER") {
1228 $var_name = get_pointer_to($var_name);
1231 if (is_charset_array($e, $l)) {
1232 # FIXME: Use Unix charset setting rather than utf-8
1233 $self->pidl("if ($var_name == NULL) {");
1235 $self->pidl("$py_var = Py_None;");
1236 $self->pidl("Py_INCREF($py_var);");
1238 $self->pidl("} else {");
1240 $self->pidl("$py_var = PyUnicode_Decode($var_name, strlen($var_name), \"utf-8\", \"ignore\");");
1244 die("No SIZE_IS for array $var_name") unless (defined($l->{SIZE_IS}));
1245 my $length = $l->{SIZE_IS};
1246 if (defined($l->{LENGTH_IS})) {
1247 $length = $l->{LENGTH_IS};
1250 $length = ParseExpr($length, $env, $e);
1251 $self->pidl("$py_var = PyList_New($length);");
1252 $self->fail_on_null($py_var, $fail);
1255 my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}";
1256 $self->pidl("int $counter;");
1257 $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
1259 my $member_var = "py_$e->{NAME}_$l->{LEVEL_INDEX}";
1260 $self->pidl("PyObject *$member_var;");
1261 $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, $var_name."[$counter]", $member_var, $fail);
1262 $self->pidl("PyList_SetItem($py_var, $counter, $member_var);");
1268 } elsif ($l->{TYPE} eq "SWITCH") {
1269 $var_name = get_pointer_to($var_name);
1270 my $switch = ParseExpr($l->{SWITCH_IS}, $env, $e);
1271 $self->pidl("$py_var = py_import_" . $nl->{DATA_TYPE} . "($mem_ctx, $switch, $var_name);");
1272 $self->fail_on_null($py_var, $fail);
1274 } elsif ($l->{TYPE} eq "DATA") {
1275 if (not Parse::Pidl::Typelist::is_scalar($l->{DATA_TYPE})) {
1276 $var_name = get_pointer_to($var_name);
1278 my $conv = $self->ConvertObjectToPythonData($mem_ctx, $l->{DATA_TYPE}, $var_name, $e->{ORIGINAL});
1279 $self->pidl("$py_var = $conv;");
1280 } elsif ($l->{TYPE} eq "SUBCONTEXT") {
1281 $self->ConvertObjectToPythonLevel($mem_ctx, $env, $e, $nl, $var_name, $py_var, $fail);
1283 fatal($e->{ORIGINAL}, "Unknown level type $l->{TYPE} $var_name");
1287 sub ConvertObjectToPython($$$$$$)
1289 my ($self, $mem_ctx, $env, $ctype, $cvar, $py_var, $fail) = @_;
1291 $self->ConvertObjectToPythonLevel($mem_ctx, $env, $ctype, $ctype->{LEVELS}[0], $cvar, $py_var, $fail);
1296 my($self,$basename,$ndr,$ndr_hdr,$hdr) = @_;
1298 $self->{BASENAME} = $basename;
1301 /* Python wrapper functions auto-generated by pidl */
1303 #include \"includes.h\"
1304 #include <pytalloc.h>
1305 #include \"librpc/rpc/pyrpc.h\"
1306 #include \"librpc/rpc/pyrpc_util.h\"
1308 #include \"$ndr_hdr\"
1312 foreach my $x (@$ndr) {
1313 ($x->{TYPE} eq "IMPORT") && $self->Import(@{$x->{PATHS}});
1314 ($x->{TYPE} eq "INTERFACE") && $self->Interface($x, $basename);
1317 $self->pidl("static PyMethodDef $basename\_methods[] = {");
1319 foreach (@{$self->{module_methods}}) {
1320 my ($fn_name, $pyfn_name, $flags, $doc) = @$_;
1321 $self->pidl("{ \"$fn_name\", (PyCFunction)$pyfn_name, $flags, $doc },");
1324 $self->pidl("{ NULL, NULL, 0, NULL }");
1330 $self->pidl_hdr("void init$basename(void);");
1331 $self->pidl("void init$basename(void)");
1334 $self->pidl("PyObject *m;");
1335 foreach (keys %{$self->{module_imports}}) {
1336 $self->pidl("PyObject *$_;");
1340 foreach (keys %{$self->{module_imports}}) {
1342 my $module_path = $self->{module_imports}->{$var_name};
1343 $self->pidl("$var_name = PyImport_ImportModule(\"$module_path\");");
1344 $self->pidl("if ($var_name == NULL)");
1345 $self->pidl("\treturn;");
1349 foreach (keys %{$self->{type_imports}}) {
1350 my $type_var = "$_\_Type";
1351 my $module_path = $self->{type_imports}->{$_};
1352 $self->pidl_hdr("static PyTypeObject *$type_var;\n");
1353 my $pretty_name = PrettifyTypeName($_, $module_path);
1354 my $module_var = "dep_$module_path";
1355 $module_var =~ s/\./_/g;
1356 $self->pidl("$type_var = (PyTypeObject *)PyObject_GetAttrString($module_var, \"$pretty_name\");");
1357 $self->pidl("if ($type_var == NULL)");
1358 $self->pidl("\treturn;");
1362 $self->pidl($_) foreach (@{$self->{prereadycode}});
1364 foreach (@{$self->{ready_types}}) {
1365 $self->pidl("if (PyType_Ready($_) < 0)");
1366 $self->pidl("\treturn;");
1369 $self->pidl($_) foreach (@{$self->{postreadycode}});
1371 foreach (@{$self->{patch_type_calls}}) {
1372 my ($typename, $cvar) = @$_;
1373 $self->pidl("#ifdef PY_".uc($typename)."_PATCH");
1374 $self->pidl("PY_".uc($typename)."_PATCH($cvar);");
1375 $self->pidl("#endif");
1380 $self->pidl("m = Py_InitModule3(\"$basename\", $basename\_methods, \"$basename DCE/RPC\");");
1381 $self->pidl("if (m == NULL)");
1382 $self->pidl("\treturn;");
1384 foreach my $name (keys %{$self->{constants}}) {
1386 my ($ctype, $cvar) = @{$self->{constants}->{$name}};
1387 if ($cvar =~ /^[0-9]+$/ or $cvar =~ /^0x[0-9a-fA-F]+$/) {
1388 $py_obj = "PyInt_FromLong($cvar)";
1389 } elsif ($cvar =~ /^".*"$/) {
1390 $py_obj = "PyString_FromString($cvar)";
1392 $py_obj = $self->ConvertObjectToPythonData("NULL", expandAlias($ctype), $cvar, undef);
1395 $self->pidl("PyModule_AddObject(m, \"$name\", $py_obj);");
1398 foreach (@{$self->{module_objects}}) {
1399 my ($object_name, $c_name) = @$_;
1400 $self->pidl("Py_INCREF($c_name);");
1401 $self->pidl("PyModule_AddObject(m, \"$object_name\", $c_name);");
1404 $self->pidl("#ifdef PY_MOD_".uc($basename)."_PATCH");
1405 $self->pidl("PY_MOD_".uc($basename)."_PATCH(m);");
1406 $self->pidl("#endif");
1411 return ($self->{res_hdr} . $self->{res});