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);
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 => [],
34 $self->{res_hdr} .= shift;
41 $self->{res} .= $self->{tabs};
50 $self->{tabs} .= "\t";
56 $self->{tabs} = substr($self->{tabs}, 0, -1);
66 $self->pidl_hdr("#include \"librpc/gen_ndr/py_$_\.h\"\n");
72 my ($self, $const) = @_;
73 $self->register_constant($const->{NAME}, $const->{DTYPE}, $const->{VALUE});
76 sub register_constant($$$$)
78 my ($self, $name, $type, $value) = @_;
80 $self->{constants}->{$name} = [$type, $value];
83 sub EnumAndBitmapConsts($$$)
85 my ($self, $name, $d) = @_;
87 return unless (defined($d->{ELEMENTS}));
89 foreach my $e (@{$d->{ELEMENTS}}) {
90 $e =~ /^([A-Za-z0-9_]+)/;
93 $self->register_constant($cname, $d, $cname);
97 sub FromUnionToPythonFunction($$$$)
99 my ($self, $mem_ctx, $type, $switch, $name) = @_;
101 $self->pidl("PyObject *ret;");
104 $self->pidl("switch ($switch) {");
107 foreach my $e (@{$type->{ELEMENTS}}) {
108 $self->pidl("$e->{CASE}:");
113 $self->ConvertObjectToPython($mem_ctx, {}, $e, "$name->$e->{NAME}", "ret", "return NULL;");
115 $self->pidl("ret = Py_None;");
118 $self->pidl("return ret;");
127 $self->pidl("PyErr_SetString(PyExc_TypeError, \"unknown union level\");");
128 $self->pidl("return NULL;");
131 sub FromPythonToUnionFunction($$$$$)
133 my ($self, $type, $typename, $switch, $mem_ctx, $name) = @_;
137 $self->pidl("$typename *ret = talloc_zero($mem_ctx, $typename);");
139 $self->pidl("switch ($switch) {");
142 foreach my $e (@{$type->{ELEMENTS}}) {
143 $self->pidl("$e->{CASE}:");
144 if ($e->{CASE} eq "default") { $has_default = 1; }
147 $self->ConvertObjectFromPython({}, $mem_ctx, $e, $name, "ret->$e->{NAME}", "talloc_free(ret); return NULL;");
149 $self->pidl("break;");
155 $self->pidl("default:");
157 $self->pidl("PyErr_SetString(PyExc_TypeError, \"invalid union level value\");");
158 $self->pidl("talloc_free(ret);");
159 $self->pidl("ret = NULL;");
166 $self->pidl("return ret;");
169 sub PythonStruct($$$$$$)
171 my ($self, $modulename, $prettyname, $name, $cname, $d) = @_;
173 my $env = GenerateStructEnv($d, "object");
177 my $getsetters = "NULL";
179 if ($#{$d->{ELEMENTS}} > -1) {
180 foreach my $e (@{$d->{ELEMENTS}}) {
181 my $varname = "object->$e->{NAME}";
182 $self->pidl("static PyObject *py_$name\_get_$e->{NAME}(PyObject *obj, void *closure)");
185 $self->pidl("$cname *object = py_talloc_get_ptr(obj);");
186 $self->pidl("PyObject *py_$e->{NAME};");
187 $self->ConvertObjectToPython("py_talloc_get_mem_ctx(obj)", $env, $e, $varname, "py_$e->{NAME}", "return NULL;");
188 $self->pidl("return py_$e->{NAME};");
193 $self->pidl("static int py_$name\_set_$e->{NAME}(PyObject *py_obj, PyObject *value, void *closure)");
196 $self->pidl("$cname *object = py_talloc_get_ptr(py_obj);");
197 my $mem_ctx = "py_talloc_get_mem_ctx(py_obj)";
198 my $l = $e->{LEVELS}[0];
199 my $nl = GetNextLevel($e, $l);
200 if ($l->{TYPE} eq "POINTER" and
201 not ($nl->{TYPE} eq "ARRAY" and ($nl->{IS_FIXED} or is_charset_array($e, $nl))) and
202 not ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE}))) {
203 $self->pidl("talloc_free($varname);");
205 $self->ConvertObjectFromPython($env, $mem_ctx, $e, "value", $varname, "return -1;");
206 $self->pidl("return 0;");
212 $getsetters = "py_$name\_getsetters";
213 $self->pidl("static PyGetSetDef ".$getsetters."[] = {");
215 foreach my $e (@{$d->{ELEMENTS}}) {
216 $self->pidl("{ discard_const_p(char, \"$e->{NAME}\"), py_$name\_get_$e->{NAME}, py_$name\_set_$e->{NAME} },");
218 $self->pidl("{ NULL }");
224 $self->pidl("static PyObject *py_$name\_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)");
227 $self->pidl("$cname *ret = talloc_zero(NULL, $cname);");
228 $self->pidl("return py_talloc_import(type, ret);");
233 my $py_methods = "NULL";
235 # If the struct is not public there ndr_pull/ndr_push functions will
236 # be static so not callable from here
237 if (has_property($d, "public")) {
238 $self->pidl("static PyObject *py_$name\_ndr_pack(PyObject *py_obj)");
241 $self->pidl("$cname *object = py_talloc_get_ptr(py_obj);");
242 $self->pidl("DATA_BLOB blob;");
243 $self->pidl("enum ndr_err_code err;");
244 $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);");
245 $self->pidl("if (err != NDR_ERR_SUCCESS) {");
247 $self->pidl("PyErr_SetNdrError(err);");
248 $self->pidl("return NULL;");
252 $self->pidl("return PyString_FromStringAndSize((char *)blob.data, blob.length);");
257 $self->pidl("static PyObject *py_$name\_ndr_unpack(PyObject *py_obj, PyObject *args)");
260 $self->pidl("$cname *object = py_talloc_get_ptr(py_obj);");
261 $self->pidl("DATA_BLOB blob;");
262 $self->pidl("enum ndr_err_code err;");
263 $self->pidl("if (!PyArg_ParseTuple(args, \"s#:__ndr_unpack__\", &blob.data, &blob.length))");
264 $self->pidl("\treturn NULL;");
266 $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);");
267 $self->pidl("if (err != NDR_ERR_SUCCESS) {");
269 $self->pidl("PyErr_SetNdrError(err);");
270 $self->pidl("return NULL;");
274 $self->pidl("return Py_None;");
278 $py_methods = "py_$name\_methods";
279 $self->pidl("static PyMethodDef $py_methods\[] = {");
281 $self->pidl("{ \"__ndr_pack__\", (PyCFunction)py_$name\_ndr_pack, METH_NOARGS, \"S.pack() -> blob\\nNDR pack\" },");
282 $self->pidl("{ \"__ndr_unpack__\", (PyCFunction)py_$name\_ndr_unpack, METH_VARARGS, \"S.unpack(blob) -> None\\nNDR unpack\" },");
284 $self->pidl("#ifdef ".uc("py_$name\_extra_methods"));
285 $self->pidl("\t" .uc("py_$name\_extra_methods"));
286 $self->pidl("#endif");
288 $self->pidl("{ NULL, NULL, 0, NULL }");
294 $self->pidl_hdr("PyAPI_DATA(PyTypeObject) $name\_Type;\n");
295 $self->pidl_hdr("#define $name\_Check(op) PyObject_TypeCheck(op, &$name\_Type)\n");
296 $self->pidl_hdr("#define $name\_CheckExact(op) ((op)->ob_type == &$name\_Type)\n");
297 $self->pidl_hdr("\n");
298 $self->pidl("#ifndef ".uc("py_$name\_repr"));
299 $self->pidl("#define ".uc("py_$name\_repr") . " py_talloc_default_repr");
300 $self->pidl("#endif");
302 my $docstring = ($self->DocString($d, $name) or "NULL");
303 my $typeobject = "$name\_Type";
304 $self->pidl("PyTypeObject $typeobject = {");
306 $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
307 $self->pidl(".tp_name = \"$modulename.$prettyname\",");
308 $self->pidl(".tp_basicsize = sizeof(py_talloc_Object),");
309 $self->pidl(".tp_dealloc = py_talloc_dealloc,");
310 $self->pidl(".tp_getset = $getsetters,");
311 $self->pidl(".tp_repr = ".uc("py_$name\_repr").",");
312 $self->pidl(".tp_doc = $docstring,");
313 $self->pidl(".tp_methods = $py_methods,");
314 $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,");
315 $self->pidl(".tp_new = py_$name\_new,");
321 return "&$typeobject";
324 sub get_metadata_var($)
327 sub get_var($) { my $x = shift; $x =~ s/\*//g; return $x; }
329 if (has_property($e, "length_is")) {
330 return get_var($e->{PROPERTIES}->{length_is});
331 } elsif (has_property($e, "size_is")) {
332 return get_var($e->{PROPERTIES}->{size_is});
338 sub find_metadata_args($)
341 my $metadata_args = { in => {}, out => {} };
343 # Determine arguments that are metadata for other arguments (size_is/length_is)
344 foreach my $e (@{$fn->{ELEMENTS}}) {
345 foreach my $dir (@{$e->{DIRECTION}}) {
346 my $main = get_metadata_var($e);
348 $metadata_args->{$dir}->{$main} = $e->{NAME};
353 return $metadata_args;
356 sub PythonFunctionUnpackOut($$$)
358 my ($self, $fn, $fnname) = @_;
360 my $outfnname = "unpack_$fnname\_args_out";
363 my $metadata_args = find_metadata_args($fn);
365 my $env = GenerateFunctionOutEnv($fn, "r->");
368 $self->pidl("static PyObject *$outfnname(struct $fn->{NAME} *r)");
371 $self->pidl("PyObject *result = Py_None;");
372 foreach my $e (@{$fn->{ELEMENTS}}) {
373 next unless (grep(/out/,@{$e->{DIRECTION}}));
374 next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or
375 ($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}}));
376 $self->pidl("PyObject *py_$e->{NAME};");
380 if ($fn->{RETURN_TYPE}) {
381 $result_size++ unless ($fn->{RETURN_TYPE} eq "WERROR" or $fn->{RETURN_TYPE} eq "NTSTATUS");
386 if ($result_size > 1) {
387 $self->pidl("result = PyTuple_New($result_size);");
389 } elsif ($result_size == 0) {
390 $signature .= "None";
393 foreach my $e (@{$fn->{ELEMENTS}}) {
394 next if ($metadata_args->{out}->{$e->{NAME}});
395 my $py_name = "py_$e->{NAME}";
396 if (grep(/out/,@{$e->{DIRECTION}})) {
397 $self->ConvertObjectToPython("r", $env, $e, "r->out.$e->{NAME}", $py_name, "return NULL;");
398 if ($result_size > 1) {
399 $self->pidl("PyTuple_SetItem(result, $i, $py_name);");
401 $signature .= "$e->{NAME}, ";
403 $self->pidl("result = $py_name;");
404 $signature .= $e->{NAME};
409 if (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "NTSTATUS") {
410 $self->handle_ntstatus("r->out.result", "NULL", undef);
411 } elsif (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "WERROR") {
412 $self->handle_werror("r->out.result", "NULL", undef);
413 } elsif (defined($fn->{RETURN_TYPE})) {
414 my $conv = $self->ConvertObjectToPythonData("r", $fn->{RETURN_TYPE}, "r->out.result");
415 if ($result_size > 1) {
416 $self->pidl("PyTuple_SetItem(result, $i, $conv);");
418 $self->pidl("result = $conv;");
420 $signature .= "result";
423 if (substr($signature, -2) eq ", ") {
424 $signature = substr($signature, 0, -2);
426 if ($result_size > 1) {
430 $self->pidl("return result;");
435 return ($outfnname, $signature);
438 sub PythonFunctionPackIn($$$)
440 my ($self, $fn, $fnname) = @_;
441 my $metadata_args = find_metadata_args($fn);
443 my $infnname = "pack_$fnname\_args_in";
445 $self->pidl("static bool $infnname(PyObject *args, PyObject *kwargs, struct $fn->{NAME} *r)");
448 my $args_format = "";
449 my $args_string = "";
453 foreach my $e (@{$fn->{ELEMENTS}}) {
454 next unless (grep(/in/,@{$e->{DIRECTION}}));
455 next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or
456 ($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}}));
457 $self->pidl("PyObject *py_$e->{NAME};");
459 $args_string .= ", &py_$e->{NAME}";
460 $args_names .= "\"$e->{NAME}\", ";
461 $signature .= "$e->{NAME}, ";
463 if (substr($signature, -2) eq ", ") {
464 $signature = substr($signature, 0, -2);
466 $self->pidl("const char *kwnames[] = {");
468 $self->pidl($args_names . "NULL");
473 $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"$args_format:$fn->{NAME}\", discard_const_p(char *, kwnames)$args_string)) {");
475 $self->pidl("return false;");
480 my $env = GenerateFunctionInEnv($fn, "r->");
482 my $fail = "return false;";
483 foreach my $e (@{$fn->{ELEMENTS}}) {
484 next unless (grep(/in/,@{$e->{DIRECTION}}));
485 if ($metadata_args->{in}->{$e->{NAME}}) {
486 my $py_var = "py_".$metadata_args->{in}->{$e->{NAME}};
487 $self->pidl("PY_CHECK_TYPE(PyList, $py_var, $fail);");
488 my $val = "PyList_Size($py_var)";
489 if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
490 $self->pidl("r->in.$e->{NAME} = talloc_ptrtype(r, r->in.$e->{NAME});");
491 $self->pidl("*r->in.$e->{NAME} = $val;");
493 $self->pidl("r->in.$e->{NAME} = $val;");
496 $self->ConvertObjectFromPython($env, "r", $e, "py_$e->{NAME}", "r->in.$e->{NAME}", $fail);
499 $self->pidl("return true;");
503 return ($infnname, $signature);
506 sub PythonFunction($$$)
508 my ($self, $fn, $iface, $prettyname) = @_;
510 my $fnname = "py_$fn->{NAME}";
511 my $docstring = $self->DocString($fn, $fn->{NAME});
513 my ($insignature, $outsignature);
516 if (has_property($fn, "todo")) {
517 unless ($docstring) { $docstring = "NULL"; }
521 ($infn, $insignature) = $self->PythonFunctionPackIn($fn, $fnname);
522 ($outfn, $outsignature) = $self->PythonFunctionUnpackOut($fn, $fnname);
523 my $signature = "S.$prettyname($insignature) -> $outsignature";
525 $docstring = "\"$signature\\n\\n\"$docstring";
527 $docstring = "\"$signature\"";
531 return ($infn, $outfn, $docstring);
534 sub handle_werror($$$$)
536 my ($self, $var, $retval, $mem_ctx) = @_;
538 $self->pidl("if (!W_ERROR_IS_OK($var)) {");
540 $self->pidl("PyErr_SetWERROR($var);");
541 $self->pidl("talloc_free($mem_ctx);") if ($mem_ctx);
542 $self->pidl("return $retval;");
548 sub handle_ntstatus($$$$)
550 my ($self, $var, $retval, $mem_ctx) = @_;
552 $self->pidl("if (NT_STATUS_IS_ERR($var)) {");
554 $self->pidl("PyErr_SetNTSTATUS($var);");
555 $self->pidl("talloc_free($mem_ctx);") if ($mem_ctx);
556 $self->pidl("return $retval;");
564 my ($self, $modulename, $d, $interface, $basename) = @_;
566 my $actual_ctype = $d;
567 if ($actual_ctype->{TYPE} eq "TYPEDEF") {
568 $actual_ctype = $actual_ctype->{DATA};
571 if ($actual_ctype->{TYPE} eq "STRUCT") {
573 my $fn_name = $d->{NAME};
575 $fn_name =~ s/^$interface->{NAME}_//;
576 $fn_name =~ s/^$basename\_//;
579 if ($d->{TYPE} eq "STRUCT") {
580 $typeobject = $self->PythonStruct($modulename, $fn_name, $d->{NAME}, mapTypeName($d), $d);
582 $typeobject = $self->PythonStruct($modulename, $fn_name, $d->{NAME}, mapTypeName($d), $d->{DATA});
585 $self->register_module_typeobject($fn_name, $typeobject);
588 if ($d->{TYPE} eq "ENUM" or $d->{TYPE} eq "BITMAP") {
589 $self->EnumAndBitmapConsts($d->{NAME}, $d);
592 if ($d->{TYPE} eq "TYPEDEF" and ($d->{DATA}->{TYPE} eq "ENUM" or $d->{DATA}->{TYPE} eq "BITMAP")) {
593 $self->EnumAndBitmapConsts($d->{NAME}, $d->{DATA});
596 if ($actual_ctype->{TYPE} eq "UNION" and defined($actual_ctype->{ELEMENTS})) {
597 $self->pidl("PyObject *py_import_$d->{NAME}(TALLOC_CTX *mem_ctx, int level, " .mapTypeName($d) . " *in)");
600 $self->FromUnionToPythonFunction("mem_ctx", $actual_ctype, "level", "in") if ($actual_ctype->{TYPE} eq "UNION");
605 $self->pidl(mapTypeName($d) . " *py_export_$d->{NAME}(TALLOC_CTX *mem_ctx, int level, PyObject *in)");
608 $self->FromPythonToUnionFunction($actual_ctype, mapTypeName($d), "level", "mem_ctx", "in") if ($actual_ctype->{TYPE} eq "UNION");
617 my ($self, $d, $name) = @_;
618 if (has_property($d, "helpstring")) {
619 my $docstring = uc("py_doc_$name");
620 $self->pidl("#define $docstring ".has_property($d, "helpstring"));
629 my($self,$interface,$basename) = @_;
631 $self->pidl_hdr("#ifndef _HEADER_PYTHON_$interface->{NAME}\n");
632 $self->pidl_hdr("#define _HEADER_PYTHON_$interface->{NAME}\n\n");
634 $self->pidl_hdr("\n");
636 if (has_property($interface, "pyhelper")) {
637 $self->pidl("#include \"".unmake_str($interface->{PROPERTIES}->{pyhelper})."\"\n");
640 $self->Const($_) foreach (@{$interface->{CONSTS}});
642 foreach my $d (@{$interface->{TYPES}}) {
643 next if has_property($d, "nopython");
645 $self->PythonType($basename, $d, $interface, $basename);
648 if (defined $interface->{PROPERTIES}->{uuid}) {
649 $self->pidl_hdr("PyAPI_DATA(PyTypeObject) $interface->{NAME}_InterfaceType;\n");
654 foreach my $d (@{$interface->{FUNCTIONS}}) {
655 next if not defined($d->{OPNUM});
656 next if has_property($d, "nopython");
658 my $prettyname = $d->{NAME};
660 $prettyname =~ s/^$interface->{NAME}_//;
661 $prettyname =~ s/^$basename\_//;
663 my ($infn, $outfn, $fndocstring) = $self->PythonFunction($d, $interface->{NAME}, $prettyname);
665 push (@fns, [$infn, $outfn, "dcerpc_$d->{NAME}", $prettyname, $fndocstring, $d->{OPNUM}]);
668 $self->pidl("const struct PyNdrRpcMethodDef py_ndr_$interface->{NAME}\_methods[] = {");
669 $self->pidl_hdr("extern const struct PyNdrRpcMethodDef py_ndr_$interface->{NAME}\_methods[];");
671 foreach my $d (@fns) {
672 my ($infn, $outfn, $callfn, $prettyname, $docstring, $opnum) = @$d;
673 $self->pidl("{ \"$prettyname\", $docstring, (dcerpc_call_fn)$callfn, (py_data_pack_fn)$infn, (py_data_unpack_fn)$outfn, $opnum, &ndr_table_$interface->{NAME} },");
675 $self->pidl("{ NULL }");
680 $self->pidl("static PyObject *interface_$interface->{NAME}_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)");
683 $self->pidl("dcerpc_InterfaceObject *ret;");
684 $self->pidl("const char *binding_string;");
685 $self->pidl("struct cli_credentials *credentials;");
686 $self->pidl("struct loadparm_context *lp_ctx = NULL;");
687 $self->pidl("PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None, *py_basis = Py_None;");
688 $self->pidl("TALLOC_CTX *mem_ctx = NULL;");
689 $self->pidl("struct event_context *event_ctx;");
690 $self->pidl("NTSTATUS status;");
692 $self->pidl("const char *kwnames[] = {");
694 $self->pidl("\"binding\", \"lp_ctx\", \"credentials\", \"basis_connection\", NULL");
697 $self->pidl("extern struct loadparm_context *lp_from_py_object(PyObject *py_obj);");
698 $self->pidl("extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj);");
700 $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"s|OOO:$interface->{NAME}\", discard_const_p(char *, kwnames), &binding_string, &py_lp_ctx, &py_credentials, &py_basis)) {");
702 $self->pidl("return NULL;");
706 $self->pidl("lp_ctx = lp_from_py_object(py_lp_ctx);");
707 $self->pidl("if (lp_ctx == NULL) {");
709 $self->pidl("PyErr_SetString(PyExc_TypeError, \"Expected loadparm context\");");
710 $self->pidl("return NULL;");
715 $self->pidl("status = dcerpc_init(lp_ctx);");
716 $self->pidl("if (!NT_STATUS_IS_OK(status)) {");
718 $self->pidl("PyErr_SetNTSTATUS(status);");
719 $self->pidl("return NULL;");
723 $self->pidl("credentials = cli_credentials_from_py_object(py_credentials);");
724 $self->pidl("if (credentials == NULL) {");
726 $self->pidl("PyErr_SetString(PyExc_TypeError, \"Expected credentials\");");
727 $self->pidl("return NULL;");
731 $self->pidl("ret = PyObject_New(dcerpc_InterfaceObject, &$interface->{NAME}_InterfaceType);");
733 $self->pidl("event_ctx = event_context_init(mem_ctx);");
736 $self->pidl("if (py_basis != Py_None) {");
738 $self->pidl("struct dcerpc_pipe *base_pipe;");
740 $self->pidl("if (!PyObject_TypeCheck(py_basis, &dcerpc_InterfaceType)) {");
742 $self->pidl("PyErr_SetString(PyExc_ValueError, \"basis_connection must be a DCE/RPC connection\");");
743 $self->pidl("talloc_free(mem_ctx);");
744 $self->pidl("return NULL;");
748 $self->pidl("base_pipe = ((dcerpc_InterfaceObject *)py_basis)->pipe;");
750 $self->pidl("status = dcerpc_secondary_context(base_pipe, &ret->pipe, &ndr_table_$interface->{NAME});");
752 $self->pidl("} else {");
754 $self->pidl("status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string, ");
755 $self->pidl(" &ndr_table_$interface->{NAME}, credentials, event_ctx, lp_ctx);");
758 $self->handle_ntstatus("status", "NULL", "mem_ctx");
760 $self->pidl("ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;");
762 $self->pidl("return (PyObject *)ret;");
769 "\"$interface->{NAME}(binding, lp_ctx=None, credentials=None) -> connection\\n\"
771 \"binding should be a DCE/RPC binding string (for example: ncacn_ip_tcp:127.0.0.1)\\n\"
772 \"lp_ctx should be a path to a smb.conf file or a param.LoadParm object\\n\"
773 \"credentials should be a credentials.Credentials object.\\n\\n\"";
775 my $docstring = $self->DocString($interface, $interface->{NAME});
778 $docstring = "$signature$docstring";
780 $docstring = $signature;
783 $self->pidl("PyTypeObject $interface->{NAME}_InterfaceType = {");
785 $self->pidl("PyObject_HEAD_INIT(NULL) 0,");
786 $self->pidl(".tp_name = \"$basename.$interface->{NAME}\",");
787 $self->pidl(".tp_basicsize = sizeof(dcerpc_InterfaceObject),");
788 $self->pidl(".tp_base = &dcerpc_InterfaceType,");
789 $self->pidl(".tp_doc = $docstring,");
790 $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,");
791 $self->pidl(".tp_new = interface_$interface->{NAME}_new,");
797 $self->register_module_typeobject($interface->{NAME}, "&$interface->{NAME}_InterfaceType");
798 $self->register_module_readycode(["if (!PyInterface_AddNdrRpcMethods(&$interface->{NAME}_InterfaceType, py_ndr_$interface->{NAME}\_methods))", "\treturn;", ""]);
801 $self->pidl_hdr("\n");
802 $self->pidl_hdr("#endif /* _HEADER_NDR_$interface->{NAME} */\n");
805 sub register_module_method($$$$$)
807 my ($self, $fn_name, $pyfn_name, $flags, $doc) = @_;
809 push (@{$self->{module_methods}}, [$fn_name, $pyfn_name, $flags, $doc])
812 sub register_module_typeobject($$$)
814 my ($self, $name, $py_name) = @_;
816 $self->register_module_object($name, "(PyObject *)$py_name");
818 $self->check_ready_type($py_name);
821 sub check_ready_type($$)
823 my ($self, $py_name) = @_;
824 push (@{$self->{ready_types}}, $py_name) unless (grep(/^$py_name$/,@{$self->{ready_types}}));
827 sub register_module_readycode($$)
829 my ($self, $code) = @_;
831 push (@{$self->{readycode}}, @$code);
834 sub register_module_object($$$)
836 my ($self, $name, $py_name) = @_;
838 push (@{$self->{module_objects}}, [$name, $py_name])
843 my ($self, $dest, $src) = @_;
844 if ($dest =~ /^\&/) {
845 $self->pidl("memcpy($dest, $src, sizeof(" . get_value_of($dest) . "));");
847 $self->pidl("$dest = $src;");
851 sub ConvertObjectFromPythonData($$$$$$)
853 my ($self, $mem_ctx, $cvar, $ctype, $target, $fail) = @_;
855 die("undef type for $cvar") unless(defined($ctype));
857 $ctype = resolveType($ctype);
859 my $actual_ctype = $ctype;
860 if ($ctype->{TYPE} eq "TYPEDEF") {
861 $actual_ctype = $ctype->{DATA};
864 if ($actual_ctype->{TYPE} eq "ENUM" or $actual_ctype->{TYPE} eq "BITMAP" or
865 $actual_ctype->{TYPE} eq "SCALAR" and (
866 expandAlias($actual_ctype->{NAME}) =~ /^(u?int[0-9]*|hyper|NTTIME|time_t|NTTIME_hyper|NTTIME_1sec|dlong|udlong|udlongr)$/)) {
867 $self->pidl("PY_CHECK_TYPE(PyInt, $cvar, $fail);");
868 $self->pidl("$target = PyInt_AsLong($cvar);");
872 if ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") {
873 $self->pidl("PY_CHECK_TYPE($ctype->{NAME}, $cvar, $fail);");
874 $self->assign($target, "py_talloc_get_ptr($cvar)");
878 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "DATA_BLOB") {
879 $self->pidl("$target = data_blob_talloc($mem_ctx, PyString_AsString($cvar), PyString_Size($cvar));");
883 if ($actual_ctype->{TYPE} eq "SCALAR" and
884 ($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")) {
885 $self->pidl("$target = talloc_strdup($mem_ctx, PyString_AsString($cvar));");
889 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "ipv4address") {
890 $self->pidl("$target = PyString_AsString($cvar);");
895 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "NTSTATUS") {
896 $self->pidl("$target = NT_STATUS(PyInt_AsLong($cvar));");
900 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "WERROR") {
901 $self->pidl("$target = W_ERROR(PyInt_AsLong($cvar));");
905 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "string_array") {
906 $self->pidl("$target = PyCObject_AsVoidPtr($cvar);");
910 if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "pointer") {
911 $self->assign($target, "PyCObject_AsVoidPtr($cvar)");
915 fatal($ctype, "unknown type $actual_ctype->{TYPE} for ".mapTypeName($ctype) . ": $cvar");
919 sub ConvertObjectFromPythonLevel($$$$$$$$)
921 my ($self, $env, $mem_ctx, $py_var, $e, $l, $var_name, $fail) = @_;
922 my $nl = GetNextLevel($e, $l);
924 if ($l->{TYPE} eq "POINTER") {
925 if ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) {
926 $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, $nl, $var_name, $fail);
929 if ($l->{POINTER_TYPE} ne "ref") {
930 $self->pidl("if ($py_var == Py_None) {");
932 $self->pidl("$var_name = NULL;");
934 $self->pidl("} else {");
937 $self->pidl("$var_name = talloc_ptrtype($mem_ctx, $var_name);");
938 $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, $nl, get_value_of($var_name), $fail);
939 if ($l->{POINTER_TYPE} ne "ref") {
943 } elsif ($l->{TYPE} eq "ARRAY") {
944 my $pl = GetPrevLevel($e, $l);
945 if ($pl && $pl->{TYPE} eq "POINTER") {
946 $var_name = get_pointer_to($var_name);
949 if (is_charset_array($e, $l)) {
950 $self->pidl("PY_CHECK_TYPE(PyUnicode, $py_var, $fail);");
951 # FIXME: Use Unix charset setting rather than utf-8
952 $self->pidl($var_name . " = PyString_AsString(PyUnicode_AsEncodedString($py_var, \"utf-8\", \"ignore\"));");
954 my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}";
955 $self->pidl("PY_CHECK_TYPE(PyList, $py_var, $fail);");
958 $self->pidl("int $counter;");
959 if (ArrayDynamicallyAllocated($e, $l)) {
960 $self->pidl("$var_name = talloc_array_ptrtype($mem_ctx, $var_name, PyList_Size($py_var));");
962 $self->pidl("for ($counter = 0; $counter < PyList_Size($py_var); $counter++) {");
964 $self->ConvertObjectFromPythonLevel($env, $var_name, "PyList_GetItem($py_var, $counter)", $e, GetNextLevel($e, $l), $var_name."[$counter]", $fail);
970 } elsif ($l->{TYPE} eq "DATA") {
972 if (not Parse::Pidl::Typelist::is_scalar($l->{DATA_TYPE})) {
973 $var_name = get_pointer_to($var_name);
975 $self->ConvertObjectFromPythonData($mem_ctx, $py_var, $l->{DATA_TYPE}, $var_name, $fail);
976 } elsif ($l->{TYPE} eq "SWITCH") {
977 $var_name = get_pointer_to($var_name);
978 my $switch = ParseExpr($l->{SWITCH_IS}, $env, $e);
979 $self->assign($var_name, "py_export_" . GetNextLevel($e, $l)->{DATA_TYPE} . "($mem_ctx, $switch, $py_var)");
980 } elsif ($l->{TYPE} eq "SUBCONTEXT") {
981 $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, GetNextLevel($e, $l), $var_name, $fail);
983 die("unknown level type $l->{TYPE}");
987 sub ConvertObjectFromPython($$$$$$$)
989 my ($self, $env, $mem_ctx, $ctype, $cvar, $target, $fail) = @_;
991 $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $cvar, $ctype, $ctype->{LEVELS}[0], $target, $fail);
994 sub ConvertScalarToPython($$$)
996 my ($self, $ctypename, $cvar) = @_;
998 die("expected string for $cvar, not $ctypename") if (ref($ctypename) eq "HASH");
1000 $ctypename = expandAlias($ctypename);
1002 if ($ctypename =~ /^(char|u?int[0-9]*|hyper|dlong|udlong|udlongr|time_t|NTTIME_hyper|NTTIME|NTTIME_1sec)$/) {
1003 return "PyInt_FromLong($cvar)";
1006 if ($ctypename eq "DATA_BLOB") {
1007 return "PyString_FromStringAndSize((char *)($cvar).data, ($cvar).length)";
1010 if ($ctypename eq "NTSTATUS") {
1011 return "PyErr_FromNTSTATUS($cvar)";
1014 if ($ctypename eq "WERROR") {
1015 return "PyErr_FromWERROR($cvar)";
1018 if (($ctypename eq "string" or $ctypename eq "nbt_string" or $ctypename eq "nbt_name" or $ctypename eq "wrepl_nbt_name")) {
1019 return "PyString_FromString($cvar)";
1023 if ($ctypename eq "string_array") { return "PyCObject_FromVoidPtr($cvar)"; }
1024 if ($ctypename eq "ipv4address") { return "PyString_FromString($cvar)"; }
1025 if ($ctypename eq "pointer") {
1026 return "PyCObject_FromVoidPtr($cvar, talloc_free)";
1029 die("Unknown scalar type $ctypename");
1032 sub ConvertObjectToPythonData($$$$$)
1034 my ($self, $mem_ctx, $ctype, $cvar) = @_;
1036 die("undef type for $cvar") unless(defined($ctype));
1038 $ctype = resolveType($ctype);
1040 my $actual_ctype = $ctype;
1041 if ($ctype->{TYPE} eq "TYPEDEF") {
1042 $actual_ctype = $ctype->{DATA};
1045 if ($actual_ctype->{TYPE} eq "ENUM") {
1046 return $self->ConvertScalarToPython(Parse::Pidl::Typelist::enum_type_fn($actual_ctype), $cvar);
1047 } elsif ($actual_ctype->{TYPE} eq "BITMAP") {
1048 return $self->ConvertScalarToPython(Parse::Pidl::Typelist::bitmap_type_fn($actual_ctype), $cvar);
1049 } elsif ($actual_ctype->{TYPE} eq "SCALAR") {
1050 return $self->ConvertScalarToPython($actual_ctype->{NAME}, $cvar);
1051 } elsif ($actual_ctype->{TYPE} eq "UNION") {
1052 fatal($ctype, "union without discriminant: " . mapTypeName($ctype) . ": $cvar");
1053 } elsif ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") {
1054 return "py_talloc_import_ex(&$ctype->{NAME}_Type, $mem_ctx, $cvar)";
1057 fatal($ctype, "unknown type $actual_ctype->{TYPE} for ".mapTypeName($ctype) . ": $cvar");
1060 sub fail_on_null($$$)
1062 my ($self, $var, $fail) = @_;
1063 $self->pidl("if ($var == NULL) {");
1070 sub ConvertObjectToPythonLevel($$$$$$)
1072 my ($self, $mem_ctx, $env, $e, $l, $var_name, $py_var, $fail) = @_;
1073 my $nl = GetNextLevel($e, $l);
1075 if ($l->{TYPE} eq "POINTER") {
1076 if ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) {
1077 $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, $var_name, $py_var, $fail);
1080 if ($l->{POINTER_TYPE} ne "ref") {
1081 $self->pidl("if ($var_name == NULL) {");
1083 $self->pidl("$py_var = Py_None;");
1085 $self->pidl("} else {");
1088 $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, get_value_of($var_name), $py_var, $fail);
1089 if ($l->{POINTER_TYPE} ne "ref") {
1093 } elsif ($l->{TYPE} eq "ARRAY") {
1094 my $pl = GetPrevLevel($e, $l);
1095 if ($pl && $pl->{TYPE} eq "POINTER") {
1096 $var_name = get_pointer_to($var_name);
1099 if (is_charset_array($e, $l)) {
1100 # FIXME: Use Unix charset setting rather than utf-8
1101 $self->pidl("$py_var = PyUnicode_Decode($var_name, strlen($var_name), \"utf-8\", \"ignore\");");
1103 die("No SIZE_IS for array $var_name") unless (defined($l->{SIZE_IS}));
1104 my $length = $l->{SIZE_IS};
1105 if (defined($l->{LENGTH_IS})) {
1106 $length = $l->{LENGTH_IS};
1109 $length = ParseExpr($length, $env, $e);
1110 $self->pidl("$py_var = PyList_New($length);");
1111 $self->fail_on_null($py_var, $fail);
1114 my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}";
1115 $self->pidl("int $counter;");
1116 $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
1118 my $member_var = "py_$e->{NAME}_$l->{LEVEL_INDEX}";
1119 $self->pidl("PyObject *$member_var;");
1120 $self->ConvertObjectToPythonLevel($var_name, $env, $e, GetNextLevel($e, $l), $var_name."[$counter]", $member_var, $fail);
1121 $self->pidl("PyList_SetItem($py_var, $counter, $member_var);");
1127 } elsif ($l->{TYPE} eq "SWITCH") {
1128 $var_name = get_pointer_to($var_name);
1129 my $switch = ParseExpr($l->{SWITCH_IS}, $env, $e);
1130 $self->pidl("$py_var = py_import_" . GetNextLevel($e, $l)->{DATA_TYPE} . "($mem_ctx, $switch, $var_name);");
1131 $self->fail_on_null($py_var, $fail);
1133 } elsif ($l->{TYPE} eq "DATA") {
1134 if (not Parse::Pidl::Typelist::is_scalar($l->{DATA_TYPE})) {
1135 $var_name = get_pointer_to($var_name);
1137 my $conv = $self->ConvertObjectToPythonData($mem_ctx, $l->{DATA_TYPE}, $var_name);
1138 $self->pidl("$py_var = $conv;");
1139 } elsif ($l->{TYPE} eq "SUBCONTEXT") {
1140 $self->ConvertObjectToPythonLevel($mem_ctx, $env, $e, GetNextLevel($e, $l), $var_name, $py_var, $fail);
1142 die("Unknown level type $l->{TYPE} $var_name");
1146 sub ConvertObjectToPython($$$$$$)
1148 my ($self, $mem_ctx, $env, $ctype, $cvar, $py_var, $fail) = @_;
1150 $self->ConvertObjectToPythonLevel($mem_ctx, $env, $ctype, $ctype->{LEVELS}[0], $cvar, $py_var, $fail);
1155 my($self,$basename,$ndr,$ndr_hdr,$hdr) = @_;
1158 $py_hdr =~ s/ndr_([^\/]+)$/py_$1/g;
1160 $self->pidl_hdr("/* header auto-generated by pidl */\n\n");
1163 /* Python wrapper functions auto-generated by pidl */
1164 #include \"includes.h\"
1166 #include \"librpc/rpc/dcerpc.h\"
1167 #include \"lib/talloc/pytalloc.h\"
1168 #include \"librpc/rpc/pyrpc.h\"
1169 #include \"lib/events/events.h\"
1171 #include \"$ndr_hdr\"
1172 #include \"$py_hdr\"
1176 foreach my $x (@$ndr) {
1177 ($x->{TYPE} eq "IMPORT") && $self->Import(@{$x->{PATHS}});
1178 ($x->{TYPE} eq "INTERFACE") && $self->Interface($x, $basename);
1181 $self->pidl("static PyMethodDef $basename\_methods[] = {");
1183 foreach (@{$self->{module_methods}}) {
1184 my ($fn_name, $pyfn_name, $flags, $doc) = @$_;
1185 $self->pidl("{ \"$fn_name\", (PyCFunction)$pyfn_name, $flags, $doc },");
1188 $self->pidl("{ NULL, NULL, 0, NULL }");
1194 $self->pidl("void init$basename(void)");
1197 $self->pidl("PyObject *m;");
1200 foreach (@{$self->{ready_types}}) {
1201 $self->pidl("if (PyType_Ready($_) < 0)");
1202 $self->pidl("\treturn;");
1205 $self->pidl($_) foreach (@{$self->{readycode}});
1209 $self->pidl("m = Py_InitModule3(\"$basename\", $basename\_methods, \"$basename DCE/RPC\");");
1210 $self->pidl("if (m == NULL)");
1211 $self->pidl("\treturn;");
1213 foreach my $name (keys %{$self->{constants}}) {
1215 my ($ctype, $cvar) = @{$self->{constants}->{$name}};
1216 if ($cvar =~ /^[0-9]+$/ or $cvar =~ /^0x[0-9a-fA-F]+$/) {
1217 $py_obj = "PyInt_FromLong($cvar)";
1218 } elsif ($cvar =~ /^".*"$/) {
1219 $py_obj = "PyString_FromString($cvar)";
1221 $py_obj = $self->ConvertObjectToPythonData("NULL", expandAlias($ctype), $cvar);
1224 $self->pidl("PyModule_AddObject(m, \"$name\", $py_obj);");
1227 foreach (@{$self->{module_objects}}) {
1228 my ($object_name, $c_name) = @$_;
1229 $self->pidl("Py_INCREF($c_name);");
1230 $self->pidl("PyModule_AddObject(m, \"$object_name\", $c_name);");
1236 return ($self->{res_hdr}, $self->{res});