diff options
author | Jelmer Vernooij <jelmer@samba.org> | 2008-12-22 01:51:38 +0100 |
---|---|---|
committer | Jelmer Vernooij <jelmer@samba.org> | 2008-12-22 01:51:38 +0100 |
commit | 2c58c9497cb6a15d03626864585f0437949ee6f6 (patch) | |
tree | 85a267c746e06f3cde94dc91c12aa53e7ab08b4a | |
parent | 9a7c64f1dce7560c94df99c45eb5c75903cf0e6b (diff) | |
download | samba-2c58c9497cb6a15d03626864585f0437949ee6f6.tar.gz samba-2c58c9497cb6a15d03626864585f0437949ee6f6.tar.bz2 samba-2c58c9497cb6a15d03626864585f0437949ee6f6.zip |
Import types from other Python mechanisms using the Python import mechanism, to ensure they are initialized.
-rw-r--r-- | pidl/lib/Parse/Pidl/ODL.pm | 4 | ||||
-rw-r--r-- | pidl/lib/Parse/Pidl/Samba4/Python.pm | 101 | ||||
-rw-r--r-- | pidl/lib/Parse/Pidl/Typelist.pm | 23 | ||||
-rwxr-xr-x | pidl/pidl | 5 | ||||
-rw-r--r-- | source4/librpc/rpc/pyrpc.h | 4 |
5 files changed, 88 insertions, 49 deletions
diff --git a/pidl/lib/Parse/Pidl/ODL.pm b/pidl/lib/Parse/Pidl/ODL.pm index b323a6a98a..74d9ac7c04 100644 --- a/pidl/lib/Parse/Pidl/ODL.pm +++ b/pidl/lib/Parse/Pidl/ODL.pm @@ -8,6 +8,7 @@ use Parse::Pidl qw(error); use Parse::Pidl::IDL; use Parse::Pidl::Util qw(has_property unmake_str); use Parse::Pidl::Typelist qw(hasType getType); +use File::Basename; use strict; use vars qw($VERSION); @@ -71,8 +72,9 @@ sub ODL2IDL my $podl = Parse::Pidl::IDL::parse_file($idl_path, $opt_incdirs); if (defined(@$podl)) { require Parse::Pidl::Typelist; + my $basename = basename($idl_path, ".idl"); - Parse::Pidl::Typelist::LoadIdl($podl); + Parse::Pidl::Typelist::LoadIdl($podl, $basename); my $pidl = ODL2IDL($podl, $basedir, $opt_incdirs); foreach my $y (@$pidl) { diff --git a/pidl/lib/Parse/Pidl/Samba4/Python.pm b/pidl/lib/Parse/Pidl/Samba4/Python.pm index 73ae8350b1..c2ee63dbde 100644 --- a/pidl/lib/Parse/Pidl/Samba4/Python.pm +++ b/pidl/lib/Parse/Pidl/Samba4/Python.pm @@ -24,6 +24,7 @@ sub new($) { my ($class) = @_; my $self = { res => "", res_hdr => "", tabs => "", constants => {}, module_methods => [], module_objects => [], ready_types => [], + module_imports => [], type_imports => {}, patch_type_calls => [], readycode => [] }; bless($self, $class); } @@ -65,7 +66,8 @@ sub Import foreach (@imports) { $_ = unmake_str($_); s/\.idl$//; - $self->pidl_hdr("#include \"librpc/gen_ndr/py_$_\.h\"\n"); + $self->pidl_hdr("#include \"librpc/gen_ndr/$_\.h\"\n"); + $self->register_module_import($_); } } @@ -287,14 +289,11 @@ sub PythonStruct($$$$$$) $self->pidl(""); } - $self->pidl_hdr("PyAPI_DATA(PyTypeObject) $name\_Type;\n"); - $self->pidl_hdr("#define $name\_Check(op) PyObject_TypeCheck(op, &$name\_Type)\n"); - $self->pidl_hdr("#define $name\_CheckExact(op) ((op)->ob_type == &$name\_Type)\n"); - $self->pidl_hdr("\n"); + $self->pidl_hdr("staticforward PyTypeObject $name\_Type;\n"); $self->pidl(""); my $docstring = $self->DocString($d, $name); my $typeobject = "$name\_Type"; - $self->pidl("PyTypeObject $typeobject = {"); + $self->pidl("static PyTypeObject $typeobject = {"); $self->indent; $self->pidl("PyObject_HEAD_INIT(NULL) 0,"); $self->pidl(".tp_name = \"$modulename.$prettyname\","); @@ -479,7 +478,7 @@ sub PythonFunctionPackIn($$$) next unless (grep(/in/,@{$e->{DIRECTION}})); if ($metadata_args->{in}->{$e->{NAME}}) { my $py_var = "py_".$metadata_args->{in}->{$e->{NAME}}; - $self->pidl("PY_CHECK_TYPE(PyList, $py_var, $fail);"); + $self->pidl("PY_CHECK_TYPE(&PyList_Type, $py_var, $fail);"); my $val = "PyList_Size($py_var)"; if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") { $self->pidl("r->in.$e->{NAME} = talloc_ptrtype(r, r->in.$e->{NAME});"); @@ -623,13 +622,8 @@ sub Interface($$$) { my($self,$interface,$basename) = @_; - $self->pidl_hdr("#ifndef _HEADER_PYTHON_$interface->{NAME}\n"); - $self->pidl_hdr("#define _HEADER_PYTHON_$interface->{NAME}\n\n"); - - $self->pidl_hdr("\n"); - if (has_property($interface, "pyhelper")) { - $self->pidl("#include \"".unmake_str($interface->{PROPERTIES}->{pyhelper})."\"\n"); + $self->pidl_hdr("#include \"".unmake_str($interface->{PROPERTIES}->{pyhelper})."\"\n"); } $self->Const($_) foreach (@{$interface->{CONSTS}}); @@ -641,7 +635,7 @@ sub Interface($$$) } if (defined $interface->{PROPERTIES}->{uuid}) { - $self->pidl_hdr("PyAPI_DATA(PyTypeObject) $interface->{NAME}_InterfaceType;\n"); + $self->pidl_hdr("staticforward PyTypeObject $interface->{NAME}_InterfaceType;\n"); $self->pidl(""); my @fns = (); @@ -661,7 +655,6 @@ sub Interface($$$) } $self->pidl("const struct PyNdrRpcMethodDef py_ndr_$interface->{NAME}\_methods[] = {"); - $self->pidl_hdr("extern const struct PyNdrRpcMethodDef py_ndr_$interface->{NAME}\_methods[];"); $self->indent; foreach my $d (@fns) { my ($infn, $outfn, $callfn, $prettyname, $docstring, $opnum) = @$d; @@ -775,7 +768,7 @@ sub Interface($$$) $docstring = $signature; } - $self->pidl("PyTypeObject $interface->{NAME}_InterfaceType = {"); + $self->pidl("static PyTypeObject $interface->{NAME}_InterfaceType = {"); $self->indent; $self->pidl("PyObject_HEAD_INIT(NULL) 0,"); $self->pidl(".tp_name = \"$basename.$interface->{NAME}\","); @@ -794,7 +787,6 @@ sub Interface($$$) } $self->pidl_hdr("\n"); - $self->pidl_hdr("#endif /* _HEADER_NDR_$interface->{NAME} */\n"); } sub register_module_method($$$$$) @@ -821,6 +813,30 @@ sub check_ready_type($$) push (@{$self->{ready_types}}, $py_name) unless (grep(/^$py_name$/,@{$self->{ready_types}})); } +sub register_module_import($$) +{ + my ($self, $basename) = @_; + + push (@{$self->{module_imports}}, $basename); +} + +sub use_type_variable($$) +{ + my ($self, $orig_ctype) = @_; + my $ctype = resolveType($orig_ctype); + unless (defined($ctype->{BASEFILE})) { + fatal($orig_ctype, "Unable to determine origin of type " . mapTypeName($orig_ctype)); + } + # If this is an external type, make sure we do the right imports. + if (($ctype->{BASEFILE} ne $self->{BASENAME})) { + unless (defined($self->{type_imports}->{$ctype->{NAME}})) { + $self->{type_imports}->{$ctype->{NAME}} = $ctype->{BASEFILE}; + } + return "$ctype->{NAME}_Type" + } + return "&$ctype->{NAME}_Type"; +} + sub register_patch_type_call($$$) { my ($self, $typename, $cvar) = @_; @@ -862,20 +878,21 @@ sub ConvertObjectFromPythonData($$$$$$) $ctype = resolveType($ctype); my $actual_ctype = $ctype; - if ($ctype->{TYPE} eq "TYPEDEF") { - $actual_ctype = $ctype->{DATA}; + if ($actual_ctype->{TYPE} eq "TYPEDEF") { + $actual_ctype = $actual_ctype->{DATA}; } if ($actual_ctype->{TYPE} eq "ENUM" or $actual_ctype->{TYPE} eq "BITMAP" or $actual_ctype->{TYPE} eq "SCALAR" and ( expandAlias($actual_ctype->{NAME}) =~ /^(u?int[0-9]*|hyper|NTTIME|time_t|NTTIME_hyper|NTTIME_1sec|dlong|udlong|udlongr)$/)) { - $self->pidl("PY_CHECK_TYPE(PyInt, $cvar, $fail);"); + $self->pidl("PY_CHECK_TYPE(&PyInt_Type, $cvar, $fail);"); $self->pidl("$target = PyInt_AsLong($cvar);"); return; } if ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") { - $self->pidl("PY_CHECK_TYPE($ctype->{NAME}, $cvar, $fail);"); + my $ctype_name = $self->use_type_variable($ctype); + $self->pidl("PY_CHECK_TYPE($ctype_name, $cvar, $fail);"); $self->assign($target, "py_talloc_get_ptr($cvar)"); return; } @@ -952,12 +969,12 @@ sub ConvertObjectFromPythonLevel($$$$$$$$) } if (is_charset_array($e, $l)) { - $self->pidl("PY_CHECK_TYPE(PyUnicode, $py_var, $fail);"); + $self->pidl("PY_CHECK_TYPE(&PyUnicode_Type, $py_var, $fail);"); # FIXME: Use Unix charset setting rather than utf-8 $self->pidl($var_name . " = PyString_AsString(PyUnicode_AsEncodedString($py_var, \"utf-8\", \"ignore\"));"); } else { my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}"; - $self->pidl("PY_CHECK_TYPE(PyList, $py_var, $fail);"); + $self->pidl("PY_CHECK_TYPE(&PyList_Type, $py_var, $fail);"); $self->pidl("{"); $self->indent; $self->pidl("int $counter;"); @@ -1043,8 +1060,8 @@ sub ConvertObjectToPythonData($$$$$) $ctype = resolveType($ctype); my $actual_ctype = $ctype; - if ($ctype->{TYPE} eq "TYPEDEF") { - $actual_ctype = $ctype->{DATA}; + while ($actual_ctype->{TYPE} eq "TYPEDEF") { + $actual_ctype = $actual_ctype->{DATA}; } if ($actual_ctype->{TYPE} eq "ENUM") { @@ -1056,7 +1073,8 @@ sub ConvertObjectToPythonData($$$$$) } elsif ($actual_ctype->{TYPE} eq "UNION") { fatal($ctype, "union without discriminant: " . mapTypeName($ctype) . ": $cvar"); } elsif ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") { - return "py_talloc_import_ex(&$ctype->{NAME}_Type, $mem_ctx, $cvar)"; + my $ctype_name = $self->use_type_variable($ctype); + return "py_talloc_import_ex($ctype_name, $mem_ctx, $cvar)"; } fatal($ctype, "unknown type $actual_ctype->{TYPE} for ".mapTypeName($ctype) . ": $cvar"); @@ -1158,13 +1176,10 @@ sub ConvertObjectToPython($$$$$$) sub Parse($$$$$) { my($self,$basename,$ndr,$ndr_hdr,$hdr) = @_; - - my $py_hdr = $hdr; - $py_hdr =~ s/ndr_([^\/]+)$/py_$1/g; - $self->pidl_hdr("/* header auto-generated by pidl */\n\n"); - - $self->pidl(" + $self->{BASENAME} = $basename; + + $self->pidl_hdr(" /* Python wrapper functions auto-generated by pidl */ #include \"includes.h\" #include <Python.h> @@ -1174,7 +1189,6 @@ sub Parse($$$$$) #include \"lib/events/events.h\" #include \"$hdr\" #include \"$ndr_hdr\" -#include \"$py_hdr\" "); @@ -1200,8 +1214,27 @@ sub Parse($$$$$) $self->pidl("{"); $self->indent; $self->pidl("PyObject *m;"); + foreach (@{$self->{module_imports}}) { + $self->pidl("PyObject *dep_$_;"); + } $self->pidl(""); + foreach (@{$self->{module_imports}}) { + $self->pidl("dep_$_ = PyImport_ImportModule(\"samba.dcerpc.$_\");"); + $self->pidl("if (dep_$_ == NULL)"); + $self->pidl("\treturn;"); + $self->pidl(""); + } + + foreach (keys %{$self->{type_imports}}) { + my $basefile = $self->{type_imports}->{$_}; + $self->pidl_hdr("static PyTypeObject *$_\_Type;\n"); + $self->pidl("$_\_Type = (PyTypeObject *)PyObject_GetAttrString(dep_$basefile, \"$_\");"); + $self->pidl("if ($_\_Type == NULL)"); + $self->pidl("\treturn;"); + $self->pidl(""); + } + foreach (@{$self->{ready_types}}) { $self->pidl("if (PyType_Ready($_) < 0)"); $self->pidl("\treturn;"); @@ -1249,7 +1282,7 @@ sub Parse($$$$$) $self->pidl(""); $self->deindent; $self->pidl("}"); - return ($self->{res_hdr}, $self->{res}); + return ($self->{res_hdr} . $self->{res}); } 1; diff --git a/pidl/lib/Parse/Pidl/Typelist.pm b/pidl/lib/Parse/Pidl/Typelist.pm index c5c458ac6b..0e3fd8de44 100644 --- a/pidl/lib/Parse/Pidl/Typelist.pm +++ b/pidl/lib/Parse/Pidl/Typelist.pm @@ -177,6 +177,7 @@ sub RegisterScalars() addType({ NAME => $_, TYPE => "TYPEDEF", + BASEFILE => "<builtin>", DATA => { TYPE => "SCALAR", NAME => $_ @@ -266,9 +267,10 @@ sub mapTypeName($) return mapType($dt, $dt->{NAME}); } -sub LoadIdl($) +sub LoadIdl($;$) { - my ($idl) = @_; + my $idl = shift; + my $basename = shift; foreach my $x (@{$idl}) { next if $x->{TYPE} ne "INTERFACE"; @@ -277,16 +279,19 @@ sub LoadIdl($) addType({ NAME => $x->{NAME}, TYPE => "TYPEDEF", - DATA => $x + DATA => $x, + BASEFILE => $basename, }) if (has_property($x, "object")); foreach my $y (@{$x->{DATA}}) { - addType($y) if ( - $y->{TYPE} eq "TYPEDEF" - or $y->{TYPE} eq "UNION" - or $y->{TYPE} eq "STRUCT" - or $y->{TYPE} eq "ENUM" - or $y->{TYPE} eq "BITMAP"); + if ($y->{TYPE} eq "TYPEDEF" + or $y->{TYPE} eq "UNION" + or $y->{TYPE} eq "STRUCT" + or $y->{TYPE} eq "ENUM" + or $y->{TYPE} eq "BITMAP") { + $y->{BASEFILE} = $basename; + addType($y); + } } } } @@ -609,7 +609,7 @@ sub process_file($) } require Parse::Pidl::Typelist; - Parse::Pidl::Typelist::LoadIdl($pidl); + Parse::Pidl::Typelist::LoadIdl($pidl, $basename); if (defined($opt_dump_idl_tree)) { my($pidl_file) = ($opt_dump_idl_tree or "$outputdir/$basename.pidl"); @@ -697,10 +697,9 @@ sub process_file($) if (defined($opt_python)) { require Parse::Pidl::Samba4::Python; my $generator = new Parse::Pidl::Samba4::Python(); - my ($hdr,$prsr) = $generator->Parse($basename, $ndr, + my ($prsr) = $generator->Parse($basename, $ndr, "$outputdir/ndr_$basename\_c.h", $h_filename); FileSave("$outputdir/py_$basename.c", $prsr); - FileSave("$outputdir/py_$basename.h", $hdr); } if (defined($opt_server)) { diff --git a/source4/librpc/rpc/pyrpc.h b/source4/librpc/rpc/pyrpc.h index af9ca728d8..3b2162ce1b 100644 --- a/source4/librpc/rpc/pyrpc.h +++ b/source4/librpc/rpc/pyrpc.h @@ -24,8 +24,8 @@ #include "librpc/rpc/dcerpc.h" #define PY_CHECK_TYPE(type, var, fail) \ - if (!type ## _Check(var)) {\ - PyErr_Format(PyExc_TypeError, "Expected type %s", type ## _Type.tp_name); \ + if (!PyObject_TypeCheck(var, type)) {\ + PyErr_Format(PyExc_TypeError, "Expected type %s", (type)->tp_name); \ fail; \ } |