| 1 |
################################################### |
| 2 |
# Python function wrapper generator |
| 3 |
# Copyright [email protected] 2007-2008 |
| 4 |
# released under the GNU GPL |
| 5 |
|
| 6 |
package Parse::Pidl::Samba4::Python; |
| 7 |
use parent Parse::Pidl::Base; |
| 8 |
|
| 9 |
use strict; |
| 10 |
use warnings; |
| 11 |
use Parse::Pidl qw(warning fatal error); |
| 12 |
use Parse::Pidl::Typelist qw(hasType resolveType getType mapTypeName expandAlias bitmap_type_fn enum_type_fn); |
| 13 |
use Parse::Pidl::Util qw(has_property ParseExpr unmake_str); |
| 14 |
use Parse::Pidl::NDR qw(ReturnTypeElement GetPrevLevel GetNextLevel ContainsDeferred ContainsPipe is_charset_array); |
| 15 |
use Parse::Pidl::CUtil qw(get_value_of get_pointer_to); |
| 16 |
use Parse::Pidl::Samba4 qw(ArrayDynamicallyAllocated); |
| 17 |
use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv GenerateFunctionOutEnv EnvSubstituteValue GenerateStructEnv); |
| 18 |
|
| 19 |
|
| 20 |
use vars qw($VERSION); |
| 21 |
$VERSION = '0.01'; |
| 22 |
|
| 23 |
sub new($) { |
| 24 |
my ($class) = @_; |
| 25 |
my $self = { res => "", res_hdr => "", tabs => "", |
| 26 |
constants => [], constants_uniq => {}, |
| 27 |
module_methods => [], |
| 28 |
module_objects => [], module_objects_uniq => {}, |
| 29 |
ready_types => [], |
| 30 |
module_imports => [], module_imports_uniq => {}, |
| 31 |
type_imports => [], type_imports_uniq => {}, |
| 32 |
patch_type_calls => [], prereadycode => [], |
| 33 |
postreadycode => []}; |
| 34 |
bless($self, $class); |
| 35 |
} |
| 36 |
|
| 37 |
sub PrettifyTypeName($$) |
| 38 |
{ |
| 39 |
my ($name, $basename) = @_; |
| 40 |
|
| 41 |
$basename =~ s/^.*\.([^.]+)$/$1/; |
| 42 |
|
| 43 |
$name =~ s/^$basename\_//; |
| 44 |
|
| 45 |
|
| 46 |
return $name; |
| 47 |
} |
| 48 |
|
| 49 |
sub Import |
| 50 |
{ |
| 51 |
my $self = shift; |
| 52 |
my @imports = @_; |
| 53 |
foreach (@imports) { |
| 54 |
$_ = unmake_str($_); |
| 55 |
s/\.idl$//; |
| 56 |
$self->pidl_hdr("#include \"librpc/gen_ndr/$_\.h\""); |
| 57 |
$self->register_module_import("samba.dcerpc.$_"); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
sub Const($$) |
| 62 |
{ |
| 63 |
my ($self, $const) = @_; |
| 64 |
$self->register_constant($const->{NAME}, $const->{DTYPE}, $const->{VALUE}); |
| 65 |
} |
| 66 |
|
| 67 |
sub register_constant($$$$) |
| 68 |
{ |
| 69 |
my ($self, $name, $type, $value) = @_; |
| 70 |
|
| 71 |
unless (defined $self->{constants_uniq}->{$name}) { |
| 72 |
my $h = {"key" => $name, "val" => [$type, $value]}; |
| 73 |
push @{$self->{constants}}, $h; |
| 74 |
$self->{constants_uniq}->{$name} = $h; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
sub EnumAndBitmapConsts($$$) |
| 79 |
{ |
| 80 |
my ($self, $name, $d) = @_; |
| 81 |
|
| 82 |
return unless (defined($d->{ELEMENTS})); |
| 83 |
|
| 84 |
foreach my $e (@{$d->{ELEMENTS}}) { |
| 85 |
$e =~ /^([A-Za-z0-9_]+)/; |
| 86 |
my $cname = $1; |
| 87 |
|
| 88 |
$self->register_constant($cname, $d, $cname); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
sub FromUnionToPythonFunction($$$$) |
| 93 |
{ |
| 94 |
my ($self, $mem_ctx, $type, $switch, $name) = @_; |
| 95 |
|
| 96 |
$self->pidl("PyObject *ret;"); |
| 97 |
$self->pidl(""); |
| 98 |
|
| 99 |
$self->pidl("switch ($switch) {"); |
| 100 |
$self->indent; |
| 101 |
|
| 102 |
foreach my $e (@{$type->{ELEMENTS}}) { |
| 103 |
$self->pidl("$e->{CASE}:"); |
| 104 |
|
| 105 |
$self->indent; |
| 106 |
|
| 107 |
if ($e->{NAME}) { |
| 108 |
$self->ConvertObjectToPython($mem_ctx, {}, $e, "$name->$e->{NAME}", "ret", "return NULL;"); |
| 109 |
} else { |
| 110 |
$self->pidl("ret = Py_None;"); |
| 111 |
$self->pidl("Py_INCREF(ret);"); |
| 112 |
} |
| 113 |
|
| 114 |
$self->pidl("return ret;"); |
| 115 |
$self->pidl(""); |
| 116 |
|
| 117 |
$self->deindent; |
| 118 |
} |
| 119 |
|
| 120 |
$self->deindent; |
| 121 |
$self->pidl("}"); |
| 122 |
|
| 123 |
$self->pidl("PyErr_SetString(PyExc_TypeError, \"unknown union level\");"); |
| 124 |
$self->pidl("return NULL;"); |
| 125 |
} |
| 126 |
|
| 127 |
sub FromPythonToUnionFunction($$$$$) |
| 128 |
{ |
| 129 |
my ($self, $type, $typename, $switch, $mem_ctx, $name) = @_; |
| 130 |
|
| 131 |
my $has_default = 0; |
| 132 |
|
| 133 |
$self->pidl("$typename *ret = talloc_zero($mem_ctx, $typename);"); |
| 134 |
|
| 135 |
$self->pidl("switch ($switch) {"); |
| 136 |
$self->indent; |
| 137 |
|
| 138 |
foreach my $e (@{$type->{ELEMENTS}}) { |
| 139 |
$self->pidl("$e->{CASE}:"); |
| 140 |
if ($e->{CASE} eq "default") { $has_default = 1; } |
| 141 |
$self->indent; |
| 142 |
if ($e->{NAME}) { |
| 143 |
$self->ConvertObjectFromPython({}, $mem_ctx, $e, $name, "ret->$e->{NAME}", "talloc_free(ret); return NULL;"); |
| 144 |
} |
| 145 |
$self->pidl("break;"); |
| 146 |
$self->deindent; |
| 147 |
$self->pidl(""); |
| 148 |
} |
| 149 |
|
| 150 |
if (!$has_default) { |
| 151 |
$self->pidl("default:"); |
| 152 |
$self->indent; |
| 153 |
$self->pidl("PyErr_SetString(PyExc_TypeError, \"invalid union level value\");"); |
| 154 |
$self->pidl("talloc_free(ret);"); |
| 155 |
$self->pidl("ret = NULL;"); |
| 156 |
$self->deindent; |
| 157 |
} |
| 158 |
|
| 159 |
$self->deindent; |
| 160 |
$self->pidl("}"); |
| 161 |
$self->pidl(""); |
| 162 |
$self->pidl("return ret;"); |
| 163 |
} |
| 164 |
|
| 165 |
sub PythonElementGetSet($$$$$$) { |
| 166 |
my ($self, $name, $cname, $ename, $e, $env) = @_; |
| 167 |
|
| 168 |
my $varname = "object->$ename"; |
| 169 |
$self->pidl("static PyObject *py_$name\_get_$e->{NAME}(PyObject *obj, void *closure)"); |
| 170 |
$self->pidl("{"); |
| 171 |
$self->indent; |
| 172 |
$self->pidl("$cname *object = pytalloc_get_ptr(obj);"); |
| 173 |
$self->pidl("PyObject *py_$e->{NAME};"); |
| 174 |
my $l = $e->{LEVELS}[0]; |
| 175 |
if ($l->{TYPE} eq "POINTER") { |
| 176 |
$self->pidl("if ($varname == NULL) {"); |
| 177 |
$self->indent; |
| 178 |
$self->pidl("Py_RETURN_NONE;"); |
| 179 |
$self->deindent; |
| 180 |
$self->pidl("}"); |
| 181 |
} |
| 182 |
$self->ConvertObjectToPython("pytalloc_get_mem_ctx(obj)", $env, $e, $varname, "py_$e->{NAME}", "return NULL;"); |
| 183 |
$self->pidl("return py_$e->{NAME};"); |
| 184 |
$self->deindent; |
| 185 |
$self->pidl("}"); |
| 186 |
$self->pidl(""); |
| 187 |
|
| 188 |
$self->pidl("static int py_$name\_set_$e->{NAME}(PyObject *py_obj, PyObject *value, void *closure)"); |
| 189 |
$self->pidl("{"); |
| 190 |
$self->indent; |
| 191 |
$self->pidl("$cname *object = pytalloc_get_ptr(py_obj);"); |
| 192 |
my $mem_ctx = "pytalloc_get_mem_ctx(py_obj)"; |
| 193 |
my $nl = GetNextLevel($e, $l); |
| 194 |
if ($l->{TYPE} eq "POINTER" and |
| 195 |
not ($nl->{TYPE} eq "ARRAY" and ($nl->{IS_FIXED} or is_charset_array($e, $nl))) and |
| 196 |
not ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE}))) { |
| 197 |
$self->pidl("talloc_unlink($mem_ctx, discard_const($varname));"); |
| 198 |
} |
| 199 |
$self->ConvertObjectFromPython($env, $mem_ctx, $e, "value", $varname, "return -1;"); |
| 200 |
$self->pidl("return 0;"); |
| 201 |
$self->deindent; |
| 202 |
$self->pidl("}"); |
| 203 |
$self->pidl(""); |
| 204 |
} |
| 205 |
|
| 206 |
sub PythonStruct($$$$$$) |
| 207 |
{ |
| 208 |
my ($self, $modulename, $prettyname, $name, $cname, $d) = @_; |
| 209 |
|
| 210 |
my $env = GenerateStructEnv($d, "object"); |
| 211 |
|
| 212 |
$self->pidl(""); |
| 213 |
|
| 214 |
my $getsetters = "NULL"; |
| 215 |
|
| 216 |
if ($#{$d->{ELEMENTS}} > -1) { |
| 217 |
foreach my $e (@{$d->{ELEMENTS}}) { |
| 218 |
$self->PythonElementGetSet($name, $cname, $e->{NAME}, $e, $env); |
| 219 |
} |
| 220 |
|
| 221 |
$getsetters = "py_$name\_getsetters"; |
| 222 |
$self->pidl("static PyGetSetDef ".$getsetters."[] = {"); |
| 223 |
$self->indent; |
| 224 |
foreach my $e (@{$d->{ELEMENTS}}) { |
| 225 |
my $etype = ""; |
| 226 |
if (ref($e->{TYPE}) eq "HASH") { |
| 227 |
$etype = $e->{TYPE}->{NAME}; |
| 228 |
} else { |
| 229 |
$etype = $e->{TYPE}; |
| 230 |
} |
| 231 |
$self->pidl("{"); |
| 232 |
$self->indent; |
| 233 |
$self->pidl(".name = discard_const_p(char, \"$e->{NAME}\"),"); |
| 234 |
$self->pidl(".get = py_$name\_get_$e->{NAME},"); |
| 235 |
$self->pidl(".set = py_$name\_set_$e->{NAME},"); |
| 236 |
$self->pidl(".doc = discard_const_p(char, \"PIDL-generated element of base type $etype\")"); |
| 237 |
$self->deindent; |
| 238 |
$self->pidl("},"); |
| 239 |
} |
| 240 |
$self->pidl("{ .name = NULL }"); |
| 241 |
$self->deindent; |
| 242 |
$self->pidl("};"); |
| 243 |
$self->pidl(""); |
| 244 |
} |
| 245 |
|
| 246 |
$self->pidl("static PyObject *py_$name\_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)"); |
| 247 |
$self->pidl("{"); |
| 248 |
$self->indent; |
| 249 |
$self->pidl("return pytalloc_new($cname, type);"); |
| 250 |
$self->deindent; |
| 251 |
$self->pidl("}"); |
| 252 |
$self->pidl(""); |
| 253 |
|
| 254 |
my $py_methods = "NULL"; |
| 255 |
|
| 256 |
# If the struct is not public there ndr_pull/ndr_push functions will |
| 257 |
# be static so not callable from here |
| 258 |
if (has_property($d, "public")) { |
| 259 |
$self->pidl("static PyObject *py_$name\_ndr_pack(PyObject *py_obj, PyObject *Py_UNUSED(ignored))"); |
| 260 |
$self->pidl("{"); |
| 261 |
$self->indent; |
| 262 |
$self->pidl("$cname *object = pytalloc_get_ptr(py_obj);"); |
| 263 |
$self->pidl("PyObject *ret = NULL;"); |
| 264 |
$self->pidl("DATA_BLOB blob;"); |
| 265 |
$self->pidl("enum ndr_err_code err;"); |
| 266 |
$self->pidl("TALLOC_CTX *tmp_ctx = talloc_new(pytalloc_get_mem_ctx(py_obj));"); |
| 267 |
$self->pidl("if (tmp_ctx == NULL) {"); |
| 268 |
$self->indent; |
| 269 |
$self->pidl("PyErr_SetNdrError(NDR_ERR_ALLOC);"); |
| 270 |
$self->pidl("return NULL;"); |
| 271 |
$self->deindent; |
| 272 |
$self->pidl("}"); |
| 273 |
$self->pidl("err = ndr_push_struct_blob(&blob, tmp_ctx, object, (ndr_push_flags_fn_t)ndr_push_$name);"); |
| 274 |
$self->pidl("if (!NDR_ERR_CODE_IS_SUCCESS(err)) {"); |
| 275 |
$self->indent; |
| 276 |
$self->pidl("TALLOC_FREE(tmp_ctx);"); |
| 277 |
$self->pidl("PyErr_SetNdrError(err);"); |
| 278 |
$self->pidl("return NULL;"); |
| 279 |
$self->deindent; |
| 280 |
$self->pidl("}"); |
| 281 |
$self->pidl(""); |
| 282 |
$self->pidl("ret = PyBytes_FromStringAndSize((char *)blob.data, blob.length);"); |
| 283 |
$self->pidl("TALLOC_FREE(tmp_ctx);"); |
| 284 |
$self->pidl("return ret;"); |
| 285 |
$self->deindent; |
| 286 |
$self->pidl("}"); |
| 287 |
$self->pidl(""); |
| 288 |
|
| 289 |
$self->pidl("static PyObject *py_$name\_ndr_unpack(PyObject *py_obj, PyObject *args, PyObject *kwargs)"); |
| 290 |
$self->pidl("{"); |
| 291 |
$self->indent; |
| 292 |
$self->pidl("$cname *object = pytalloc_get_ptr(py_obj);"); |
| 293 |
$self->pidl("DATA_BLOB blob = {.data = NULL, .length = 0};"); |
| 294 |
$self->pidl("Py_ssize_t blob_length = 0;"); |
| 295 |
$self->pidl("enum ndr_err_code err;"); |
| 296 |
$self->pidl("const char * const kwnames[] = { \"data_blob\", \"allow_remaining\", NULL };"); |
| 297 |
$self->pidl("PyObject *allow_remaining_obj = NULL;"); |
| 298 |
$self->pidl("bool allow_remaining = false;"); |
| 299 |
$self->pidl(""); |
| 300 |
$self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, PYARG_BYTES_LEN \"|O:__ndr_unpack__\","); |
| 301 |
$self->indent; |
| 302 |
$self->pidl("discard_const_p(char *, kwnames),"); |
| 303 |
$self->pidl("&blob.data, &blob_length,"); |
| 304 |
$self->pidl("&allow_remaining_obj)) {"); |
| 305 |
$self->deindent; |
| 306 |
$self->indent; |
| 307 |
$self->pidl("return NULL;"); |
| 308 |
$self->deindent; |
| 309 |
$self->pidl("}"); |
| 310 |
$self->pidl("blob.length = blob_length;"); |
| 311 |
$self->pidl(""); |
| 312 |
$self->pidl("if (allow_remaining_obj && PyObject_IsTrue(allow_remaining_obj)) {"); |
| 313 |
$self->indent; |
| 314 |
$self->pidl("allow_remaining = true;"); |
| 315 |
$self->deindent; |
| 316 |
$self->pidl("}"); |
| 317 |
$self->pidl(""); |
| 318 |
$self->pidl("if (allow_remaining) {"); |
| 319 |
$self->indent; |
| 320 |
$self->pidl("err = ndr_pull_struct_blob(&blob, pytalloc_get_mem_ctx(py_obj), object, (ndr_pull_flags_fn_t)ndr_pull_$name);"); |
| 321 |
$self->deindent; |
| 322 |
$self->pidl("} else {"); |
| 323 |
$self->indent; |
| 324 |
$self->pidl("err = ndr_pull_struct_blob_all(&blob, pytalloc_get_mem_ctx(py_obj), object, (ndr_pull_flags_fn_t)ndr_pull_$name);"); |
| 325 |
$self->deindent; |
| 326 |
$self->pidl("}"); |
| 327 |
$self->pidl("if (!NDR_ERR_CODE_IS_SUCCESS(err)) {"); |
| 328 |
$self->indent; |
| 329 |
$self->pidl("PyErr_SetNdrError(err);"); |
| 330 |
$self->pidl("return NULL;"); |
| 331 |
$self->deindent; |
| 332 |
$self->pidl("}"); |
| 333 |
$self->pidl(""); |
| 334 |
$self->pidl("Py_RETURN_NONE;"); |
| 335 |
$self->deindent; |
| 336 |
$self->pidl("}"); |
| 337 |
$self->pidl(""); |
| 338 |
|
| 339 |
$self->pidl("static PyObject *py_$name\_ndr_print(PyObject *py_obj, PyObject *args, PyObject *kwargs)"); |
| 340 |
$self->pidl("{"); |
| 341 |
$self->indent; |
| 342 |
$self->pidl("$cname *object = pytalloc_get_ptr(py_obj);"); |
| 343 |
$self->pidl("const char * const kwnames[] = { \"print_secrets\", NULL };"); |
| 344 |
$self->pidl("PyObject *py_print_secrets = NULL;"); |
| 345 |
$self->pidl("PyObject *ret;"); |
| 346 |
$self->pidl("char *retstr;"); |
| 347 |
$self->pidl("bool ok;"); |
| 348 |
$self->pidl(""); |
| 349 |
$self->pidl("ok = PyArg_ParseTupleAndKeywords(args, kwargs, \"|O:__ndr_print__\","); |
| 350 |
$self->indent; |
| 351 |
$self->pidl("discard_const_p(char *, kwnames),"); |
| 352 |
$self->pidl("&py_print_secrets);"); |
| 353 |
$self->deindent; |
| 354 |
$self->pidl("if (!ok) {"); |
| 355 |
$self->indent; |
| 356 |
$self->pidl("return NULL;"); |
| 357 |
$self->deindent; |
| 358 |
$self->pidl("}"); |
| 359 |
$self->pidl(""); |
| 360 |
$self->pidl("if (py_print_secrets != NULL && PyObject_IsTrue(py_print_secrets)) {"); |
| 361 |
$self->indent; |
| 362 |
$self->pidl("retstr = ndr_print_struct_secret_string(pytalloc_get_mem_ctx(py_obj), (ndr_print_fn_t)ndr_print_$name, \"$name\", object);"); |
| 363 |
$self->deindent; |
| 364 |
$self->pidl("} else {"); |
| 365 |
$self->indent; |
| 366 |
$self->pidl("retstr = ndr_print_struct_string(pytalloc_get_mem_ctx(py_obj), (ndr_print_fn_t)ndr_print_$name, \"$name\", object);"); |
| 367 |
$self->pidl("}"); |
| 368 |
$self->deindent; |
| 369 |
$self->pidl("ret = PyUnicode_FromString(retstr);"); |
| 370 |
$self->pidl("talloc_free(retstr);"); |
| 371 |
$self->pidl(""); |
| 372 |
$self->pidl("return ret;"); |
| 373 |
$self->deindent; |
| 374 |
$self->pidl("}"); |
| 375 |
$self->pidl(""); |
| 376 |
|
| 377 |
$py_methods = "py_$name\_methods"; |
| 378 |
$self->pidl("static PyMethodDef $py_methods\[] = {"); |
| 379 |
$self->indent; |
| 380 |
$self->pidl("{ \"__ndr_pack__\", (PyCFunction)py_$name\_ndr_pack, METH_NOARGS, \"S.ndr_pack(object) -> blob\\nNDR pack\" },"); |
| 381 |
$self->pidl("{ \"__ndr_unpack__\", PY_DISCARD_FUNC_SIG(PyCFunction,py_$name\_ndr_unpack), METH_VARARGS|METH_KEYWORDS, \"S.ndr_unpack(class, blob, allow_remaining=False) -> None\\nNDR unpack\" },"); |
| 382 |
$self->pidl("{ \"__ndr_print__\", PY_DISCARD_FUNC_SIG(PyCFunction,py_$name\_ndr_print), METH_VARARGS|METH_KEYWORDS, \"S.ndr_print(object, print_secrets=False) -> String\\nNDR print\" },"); |
| 383 |
$self->pidl("{ NULL, NULL, 0, NULL }"); |
| 384 |
$self->deindent; |
| 385 |
$self->pidl("};"); |
| 386 |
$self->pidl(""); |
| 387 |
} |
| 388 |
|
| 389 |
$self->pidl_hdr("static PyTypeObject $name\_Type;"); |
| 390 |
$self->pidl(""); |
| 391 |
my $docstring = $self->DocString($d, $name); |
| 392 |
my $typeobject = "$name\_Type"; |
| 393 |
$self->pidl("static PyTypeObject $typeobject = {"); |
| 394 |
$self->indent; |
| 395 |
$self->pidl("PyVarObject_HEAD_INIT(NULL, 0)"); |
| 396 |
$self->pidl(".tp_name = \"$modulename.$prettyname\","); |
| 397 |
$self->pidl(".tp_getset = $getsetters,"); |
| 398 |
if ($docstring) { |
| 399 |
$self->pidl(".tp_doc = $docstring,"); |
| 400 |
} |
| 401 |
$self->pidl(".tp_methods = $py_methods,"); |
| 402 |
$self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,"); |
| 403 |
$self->pidl(".tp_new = py_$name\_new,"); |
| 404 |
$self->deindent; |
| 405 |
$self->pidl("};"); |
| 406 |
|
| 407 |
$self->pidl(""); |
| 408 |
|
| 409 |
my $talloc_typename = $self->import_type_variable("talloc", "BaseObject"); |
| 410 |
$self->register_module_prereadycode(["$name\_Type.tp_base = $talloc_typename;", |
| 411 |
"$name\_Type.tp_basicsize = pytalloc_BaseObject_size();", |
| 412 |
""]); |
| 413 |
|
| 414 |
return "&$typeobject"; |
| 415 |
} |
| 416 |
|
| 417 |
sub PythonFunctionStruct($$$$) |
| 418 |
{ |
| 419 |
my ($self, $modulename, $fn, $iface, $prettyname) = @_; |
| 420 |
|
| 421 |
my $inenv = GenerateFunctionInEnv($fn, "object->"); |
| 422 |
my $outenv = GenerateFunctionOutEnv($fn, "object->"); |
| 423 |
|
| 424 |
my $name = "$fn->{NAME}"; |
| 425 |
my $cname = "struct $name"; |
| 426 |
|
| 427 |
$self->pidl(""); |
| 428 |
|
| 429 |
my $getsetters = "NULL"; |
| 430 |
|
| 431 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 432 |
if (grep(/in/,@{$e->{DIRECTION}})) { |
| 433 |
my $inname = "$name\_in"; |
| 434 |
my $ename = "in.$e->{NAME}"; |
| 435 |
$self->PythonElementGetSet($inname, $cname, $ename, $e, $inenv); |
| 436 |
} |
| 437 |
if (grep(/out/,@{$e->{DIRECTION}})) { |
| 438 |
my $outname = "$name\_out"; |
| 439 |
my $ename = "out.$e->{NAME}"; |
| 440 |
$self->PythonElementGetSet($outname, $cname, $ename, $e, $outenv); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
if (defined($fn->{RETURN_TYPE})) { |
| 445 |
my $e = ReturnTypeElement($fn); |
| 446 |
my $ename = "out.result"; |
| 447 |
$self->PythonElementGetSet($name, $cname, $ename, $e, $outenv); |
| 448 |
} |
| 449 |
|
| 450 |
$getsetters = "py_$name\_getsetters"; |
| 451 |
$self->pidl("static PyGetSetDef ".$getsetters."[] = {"); |
| 452 |
$self->indent; |
| 453 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 454 |
if (grep(/in/,@{$e->{DIRECTION}})) { |
| 455 |
$self->pidl("{"); |
| 456 |
$self->indent; |
| 457 |
$self->pidl(".name = discard_const_p(char, \"in_$e->{NAME}\"),"); |
| 458 |
$self->pidl(".get = py_$name\_in_get_$e->{NAME},"); |
| 459 |
$self->pidl(".set = py_$name\_in_set_$e->{NAME},"); |
| 460 |
$self->pidl(".doc = discard_const_p(char, \"PIDL-generated element of base type $e->{TYPE}\")"); |
| 461 |
$self->deindent; |
| 462 |
$self->pidl("},"); |
| 463 |
} |
| 464 |
if (grep(/out/,@{$e->{DIRECTION}})) { |
| 465 |
$self->pidl("{"); |
| 466 |
$self->indent; |
| 467 |
$self->pidl(".name = discard_const_p(char, \"out_$e->{NAME}\"),"); |
| 468 |
$self->pidl(".get = py_$name\_out_get_$e->{NAME},"); |
| 469 |
$self->pidl(".set = py_$name\_out_set_$e->{NAME},"); |
| 470 |
$self->pidl(".doc = discard_const_p(char, \"PIDL-generated element of base type $e->{TYPE}\")"); |
| 471 |
$self->deindent; |
| 472 |
$self->pidl("},"); |
| 473 |
} |
| 474 |
} |
| 475 |
if (defined($fn->{RETURN_TYPE})) { |
| 476 |
$self->pidl("{"); |
| 477 |
$self->indent; |
| 478 |
$self->pidl(".name = discard_const_p(char, \"result\"),"); |
| 479 |
$self->pidl(".get = py_$name\_get_result,"); |
| 480 |
$self->pidl(".set = py_$name\_set_result,"); |
| 481 |
$self->pidl(".doc = discard_const_p(char, \"PIDL-generated element of type $fn->{RETURN_TYPE}\")"); |
| 482 |
$self->deindent; |
| 483 |
$self->pidl("},"); |
| 484 |
} |
| 485 |
$self->pidl("{ .name = NULL }"); |
| 486 |
$self->deindent; |
| 487 |
$self->pidl("};"); |
| 488 |
$self->pidl(""); |
| 489 |
|
| 490 |
$self->pidl("static PyObject *py_$name\_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)"); |
| 491 |
$self->pidl("{"); |
| 492 |
$self->indent; |
| 493 |
|
| 494 |
# This creates a new, zeroed C structure and python object. |
| 495 |
# These may not be valid or sensible values, but this is as |
| 496 |
# well as we can do. |
| 497 |
|
| 498 |
$self->pidl("PyObject *self = pytalloc_new($cname, type);"); |
| 499 |
|
| 500 |
# If there are any children that are ref pointers, we need to |
| 501 |
# allocate something for them to point to just as the pull |
| 502 |
# routine will when parsing the structure from NDR. |
| 503 |
# |
| 504 |
# We then make those pointers point to zeroed memory |
| 505 |
# |
| 506 |
# A ref pointer is a pointer in the C structure but a scalar |
| 507 |
# on the wire. It is for a remote function like: |
| 508 |
# |
| 509 |
# int foo(int *i) |
| 510 |
# |
| 511 |
# This may be called with the pointer by reference eg foo(&i) |
| 512 |
# |
| 513 |
# That is why this only goes as far as the next level; deeply |
| 514 |
# nested pointer chains will end in a NULL. |
| 515 |
|
| 516 |
my @ref_elements; |
| 517 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 518 |
if (has_property($e, "ref") && ! has_property($e, "charset")) { |
| 519 |
if (!has_property($e, 'in') && !has_property($e, 'out')) { |
| 520 |
die "ref pointer that is not in or out"; |
| 521 |
} |
| 522 |
push @ref_elements, $e; |
| 523 |
} |
| 524 |
} |
| 525 |
if (@ref_elements) { |
| 526 |
$self->pidl("$cname *_self = ($cname *)pytalloc_get_ptr(self);"); |
| 527 |
$self->pidl("TALLOC_CTX *mem_ctx = pytalloc_get_mem_ctx(self);"); |
| 528 |
foreach my $e (@ref_elements) { |
| 529 |
my $ename = $e->{NAME}; |
| 530 |
my $t = mapTypeName($e->{TYPE}); |
| 531 |
my $p = $e->{ORIGINAL}->{POINTERS} // 1; |
| 532 |
if ($p > 1) { |
| 533 |
$self->pidl("/* a pointer to a NULL pointer */"); |
| 534 |
$t .= ' ' . '*' x ($p - 1); |
| 535 |
} |
| 536 |
|
| 537 |
# We checked in the loop above that each ref |
| 538 |
# pointer is in or out (or both) |
| 539 |
if (has_property($e, 'in')) { |
| 540 |
$self->pidl("_self->in.$ename = talloc_zero(mem_ctx, $t);"); |
| 541 |
} |
| 542 |
|
| 543 |
if (has_property($e, 'out')) { |
| 544 |
$self->pidl("_self->out.$ename = talloc_zero(mem_ctx, $t);"); |
| 545 |
} |
| 546 |
} |
| 547 |
} |
| 548 |
$self->pidl("return self;"); |
| 549 |
$self->deindent; |
| 550 |
$self->pidl("}"); |
| 551 |
$self->pidl(""); |
| 552 |
|
| 553 |
my $py_methods = "NULL"; |
| 554 |
|
| 555 |
my $ndr_call = "const struct ndr_interface_call *call = NULL;"; |
| 556 |
my $object_ptr = "$cname *object = pytalloc_get_ptr(py_obj);"; |
| 557 |
|
| 558 |
$self->pidl("static PyObject *py_$name\_ndr_opnum(PyTypeObject *type, PyObject *Py_UNUSED(ignored))"); |
| 559 |
$self->pidl("{"); |
| 560 |
$self->indent; |
| 561 |
$self->pidl(""); |
| 562 |
$self->pidl(""); |
| 563 |
$self->pidl("return PyLong_FromLong($fn->{OPNUM});"); |
| 564 |
$self->deindent; |
| 565 |
$self->pidl("}"); |
| 566 |
$self->pidl(""); |
| 567 |
|
| 568 |
$self->pidl("static PyObject *py_$name\_ndr_pack(PyObject *py_obj, ndr_flags_type ndr_inout_flags, libndr_flags ndr_push_flags)"); |
| 569 |
$self->pidl("{"); |
| 570 |
$self->indent; |
| 571 |
$self->pidl("$ndr_call"); |
| 572 |
$self->pidl("$object_ptr"); |
| 573 |
$self->pidl("PyObject *ret = NULL;"); |
| 574 |
$self->pidl("struct ndr_push *push = NULL;"); |
| 575 |
$self->pidl("DATA_BLOB blob;"); |
| 576 |
$self->pidl("enum ndr_err_code err;"); |
| 577 |
$self->pidl(""); |
| 578 |
$self->pidl("if (ndr_table_$iface\.num_calls < " . ($fn->{OPNUM}+1) . |
| 579 |
") {"); |
| 580 |
$self->indent; |
| 581 |
$self->pidl("PyErr_SetString(PyExc_TypeError, \"Internal Error, ndr_interface_call missing for py_$name\_ndr_pack\");"); |
| 582 |
$self->pidl("return NULL;"); |
| 583 |
$self->deindent; |
| 584 |
$self->pidl("}"); |
| 585 |
$self->pidl("call = &ndr_table_$iface\.calls[$fn->{OPNUM}];"); |
| 586 |
$self->pidl(""); |
| 587 |
$self->pidl("push = ndr_push_init_ctx(pytalloc_get_mem_ctx(py_obj));"); |
| 588 |
$self->pidl("if (push == NULL) {"); |
| 589 |
$self->indent; |
| 590 |
$self->pidl("PyErr_SetNdrError(NDR_ERR_ALLOC);"); |
| 591 |
$self->pidl("return NULL;"); |
| 592 |
$self->deindent; |
| 593 |
$self->pidl("}"); |
| 594 |
$self->pidl(""); |
| 595 |
$self->pidl("push->flags |= ndr_push_flags;"); |
| 596 |
$self->pidl(""); |
| 597 |
$self->pidl("err = call->ndr_push(push, ndr_inout_flags, object);"); |
| 598 |
$self->pidl("if (!NDR_ERR_CODE_IS_SUCCESS(err)) {"); |
| 599 |
$self->indent; |
| 600 |
$self->pidl("TALLOC_FREE(push);"); |
| 601 |
$self->pidl("PyErr_SetNdrError(err);"); |
| 602 |
$self->pidl("return NULL;"); |
| 603 |
$self->deindent; |
| 604 |
$self->pidl("}"); |
| 605 |
$self->pidl("blob = ndr_push_blob(push);"); |
| 606 |
$self->pidl("ret = PyBytes_FromStringAndSize((char *)blob.data, blob.length);"); |
| 607 |
$self->pidl("TALLOC_FREE(push);"); |
| 608 |
$self->pidl("return ret;"); |
| 609 |
$self->deindent; |
| 610 |
$self->pidl("}"); |
| 611 |
$self->pidl(""); |
| 612 |
|
| 613 |
$self->pidl("static PyObject *py_$name\_ndr_pack_in(PyObject *py_obj, PyObject *args, PyObject *kwargs)"); |
| 614 |
$self->pidl("{"); |
| 615 |
$self->indent; |
| 616 |
$self->pidl("const char * const kwnames[] = { \"bigendian\", \"ndr64\", NULL };"); |
| 617 |
$self->pidl("PyObject *bigendian_obj = NULL;"); |
| 618 |
$self->pidl("PyObject *ndr64_obj = NULL;"); |
| 619 |
$self->pidl("libndr_flags ndr_push_flags = 0;"); |
| 620 |
$self->pidl(""); |
| 621 |
$self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"|OO:__ndr_pack_in__\","); |
| 622 |
$self->indent; |
| 623 |
$self->pidl("discard_const_p(char *, kwnames),"); |
| 624 |
$self->pidl("&bigendian_obj,"); |
| 625 |
$self->pidl("&ndr64_obj)) {"); |
| 626 |
$self->deindent; |
| 627 |
$self->indent; |
| 628 |
$self->pidl("return NULL;"); |
| 629 |
$self->deindent; |
| 630 |
$self->pidl("}"); |
| 631 |
$self->pidl(""); |
| 632 |
$self->pidl("if (bigendian_obj && PyObject_IsTrue(bigendian_obj)) {"); |
| 633 |
$self->indent; |
| 634 |
$self->pidl("ndr_push_flags |= LIBNDR_FLAG_BIGENDIAN;"); |
| 635 |
$self->deindent; |
| 636 |
$self->pidl("}"); |
| 637 |
$self->pidl("if (ndr64_obj && PyObject_IsTrue(ndr64_obj)) {"); |
| 638 |
$self->indent; |
| 639 |
$self->pidl("ndr_push_flags |= LIBNDR_FLAG_NDR64;"); |
| 640 |
$self->deindent; |
| 641 |
$self->pidl("}"); |
| 642 |
$self->pidl(""); |
| 643 |
$self->pidl("return py_$name\_ndr_pack(py_obj, NDR_IN, ndr_push_flags);"); |
| 644 |
$self->deindent; |
| 645 |
$self->pidl("}"); |
| 646 |
$self->pidl(""); |
| 647 |
|
| 648 |
$self->pidl("static PyObject *py_$name\_ndr_pack_out(PyObject *py_obj, PyObject *args, PyObject *kwargs)"); |
| 649 |
$self->pidl("{"); |
| 650 |
$self->indent; |
| 651 |
$self->pidl("const char * const kwnames[] = { \"bigendian\", \"ndr64\", NULL };"); |
| 652 |
$self->pidl("PyObject *bigendian_obj = NULL;"); |
| 653 |
$self->pidl("PyObject *ndr64_obj = NULL;"); |
| 654 |
$self->pidl("libndr_flags ndr_push_flags = 0;"); |
| 655 |
$self->pidl(""); |
| 656 |
$self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"|OO:__ndr_pack_out__\","); |
| 657 |
$self->indent; |
| 658 |
$self->pidl("discard_const_p(char *, kwnames),"); |
| 659 |
$self->pidl("&bigendian_obj,"); |
| 660 |
$self->pidl("&ndr64_obj)) {"); |
| 661 |
$self->deindent; |
| 662 |
$self->indent; |
| 663 |
$self->pidl("return NULL;"); |
| 664 |
$self->deindent; |
| 665 |
$self->pidl("}"); |
| 666 |
$self->pidl(""); |
| 667 |
$self->pidl("if (bigendian_obj && PyObject_IsTrue(bigendian_obj)) {"); |
| 668 |
$self->indent; |
| 669 |
$self->pidl("ndr_push_flags |= LIBNDR_FLAG_BIGENDIAN;"); |
| 670 |
$self->deindent; |
| 671 |
$self->pidl("}"); |
| 672 |
$self->pidl("if (ndr64_obj && PyObject_IsTrue(ndr64_obj)) {"); |
| 673 |
$self->indent; |
| 674 |
$self->pidl("ndr_push_flags |= LIBNDR_FLAG_NDR64;"); |
| 675 |
$self->deindent; |
| 676 |
$self->pidl("}"); |
| 677 |
$self->pidl(""); |
| 678 |
$self->pidl("return py_$name\_ndr_pack(py_obj, NDR_OUT, ndr_push_flags);"); |
| 679 |
$self->deindent; |
| 680 |
$self->pidl("}"); |
| 681 |
$self->pidl(""); |
| 682 |
|
| 683 |
$self->pidl("static PyObject *py_$name\_ndr_unpack(PyObject *py_obj, const DATA_BLOB *blob, ndr_flags_type ndr_inout_flags, libndr_flags ndr_pull_flags, bool allow_remaining)"); |
| 684 |
$self->pidl("{"); |
| 685 |
$self->indent; |
| 686 |
$self->pidl("$ndr_call"); |
| 687 |
$self->pidl("$object_ptr"); |
| 688 |
$self->pidl("struct ndr_pull *pull = NULL;"); |
| 689 |
$self->pidl("enum ndr_err_code err;"); |
| 690 |
$self->pidl(""); |
| 691 |
$self->pidl("if (ndr_table_$iface\.num_calls < " . ($fn->{OPNUM}+1) . |
| 692 |
") {"); |
| 693 |
$self->indent; |
| 694 |
$self->pidl("PyErr_SetString(PyExc_TypeError, \"Internal Error, ndr_interface_call missing for py_$name\_ndr_unpack\");"); |
| 695 |
$self->pidl("return NULL;"); |
| 696 |
$self->deindent; |
| 697 |
$self->pidl("}"); |
| 698 |
$self->pidl("call = &ndr_table_$iface\.calls[$fn->{OPNUM}];"); |
| 699 |
$self->pidl(""); |
| 700 |
$self->pidl("pull = ndr_pull_init_blob(blob, object);"); |
| 701 |
$self->pidl("if (pull == NULL) {"); |
| 702 |
$self->indent; |
| 703 |
$self->pidl("PyErr_SetNdrError(NDR_ERR_ALLOC);"); |
| 704 |
$self->pidl("return NULL;"); |
| 705 |
$self->deindent; |
| 706 |
$self->pidl("}"); |
| 707 |
$self->pidl(""); |
| 708 |
$self->pidl("pull->flags |= ndr_pull_flags;"); |
| 709 |
$self->pidl(""); |
| 710 |
$self->pidl("err = call->ndr_pull(pull, ndr_inout_flags, object);"); |
| 711 |
$self->pidl("if (!NDR_ERR_CODE_IS_SUCCESS(err)) {"); |
| 712 |
$self->indent; |
| 713 |
$self->pidl("TALLOC_FREE(pull);"); |
| 714 |
$self->pidl("PyErr_SetNdrError(err);"); |
| 715 |
$self->pidl("return NULL;"); |
| 716 |
$self->deindent; |
| 717 |
$self->pidl("}"); |
| 718 |
$self->pidl("if (!allow_remaining) {"); |
| 719 |
$self->indent; |
| 720 |
$self->pidl("uint32_t highest_ofs;"); |
| 721 |
$self->pidl(""); |
| 722 |
$self->pidl("if (pull->offset > pull->relative_highest_offset) {"); |
| 723 |
$self->indent; |
| 724 |
$self->pidl("highest_ofs = pull->offset;"); |
| 725 |
$self->deindent; |
| 726 |
$self->pidl("} else {"); |
| 727 |
$self->indent; |
| 728 |
$self->pidl("highest_ofs = pull->relative_highest_offset;"); |
| 729 |
$self->deindent; |
| 730 |
$self->pidl("}"); |
| 731 |
$self->pidl("if (highest_ofs < pull->data_size) {"); |
| 732 |
$self->indent; |
| 733 |
$self->pidl("err = ndr_pull_error(pull, NDR_ERR_UNREAD_BYTES,"); |
| 734 |
$self->indent; |
| 735 |
$self->pidl("\"not all bytes consumed ofs[%u] size[%u]\","); |
| 736 |
$self->pidl("highest_ofs, pull->data_size);"); |
| 737 |
$self->deindent; |
| 738 |
$self->pidl("TALLOC_FREE(pull);"); |
| 739 |
$self->pidl("PyErr_SetNdrError(err);"); |
| 740 |
$self->pidl("return NULL;"); |
| 741 |
$self->deindent; |
| 742 |
$self->pidl("}"); |
| 743 |
$self->deindent; |
| 744 |
$self->pidl("}"); |
| 745 |
$self->pidl(""); |
| 746 |
$self->pidl("TALLOC_FREE(pull);"); |
| 747 |
$self->pidl("Py_RETURN_NONE;"); |
| 748 |
$self->deindent; |
| 749 |
$self->pidl("}"); |
| 750 |
$self->pidl(""); |
| 751 |
|
| 752 |
$self->pidl("static PyObject *py_$name\_ndr_unpack_in(PyObject *py_obj, PyObject *args, PyObject *kwargs)"); |
| 753 |
$self->pidl("{"); |
| 754 |
$self->indent; |
| 755 |
$self->pidl("DATA_BLOB blob;"); |
| 756 |
$self->pidl("Py_ssize_t blob_length = 0;"); |
| 757 |
$self->pidl("const char * const kwnames[] = { \"data_blob\", \"bigendian\", \"ndr64\", \"allow_remaining\", NULL };"); |
| 758 |
$self->pidl("PyObject *bigendian_obj = NULL;"); |
| 759 |
$self->pidl("PyObject *ndr64_obj = NULL;"); |
| 760 |
$self->pidl("libndr_flags ndr_pull_flags = LIBNDR_FLAG_REF_ALLOC;"); |
| 761 |
$self->pidl("PyObject *allow_remaining_obj = NULL;"); |
| 762 |
$self->pidl("bool allow_remaining = false;"); |
| 763 |
$self->pidl(""); |
| 764 |
$self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, PYARG_BYTES_LEN \"|OOO:__ndr_unpack_in__\","); |
| 765 |
$self->indent; |
| 766 |
$self->pidl("discard_const_p(char *, kwnames),"); |
| 767 |
$self->pidl("&blob.data, &blob_length,"); |
| 768 |
$self->pidl("&bigendian_obj,"); |
| 769 |
$self->pidl("&ndr64_obj,"); |
| 770 |
$self->pidl("&allow_remaining_obj)) {"); |
| 771 |
$self->deindent; |
| 772 |
$self->indent; |
| 773 |
$self->pidl("return NULL;"); |
| 774 |
$self->deindent; |
| 775 |
$self->pidl("}"); |
| 776 |
$self->pidl("blob.length = blob_length;"); |
| 777 |
$self->pidl(""); |
| 778 |
$self->pidl("if (bigendian_obj && PyObject_IsTrue(bigendian_obj)) {"); |
| 779 |
$self->indent; |
| 780 |
$self->pidl("ndr_pull_flags |= LIBNDR_FLAG_BIGENDIAN;"); |
| 781 |
$self->deindent; |
| 782 |
$self->pidl("}"); |
| 783 |
$self->pidl("if (ndr64_obj && PyObject_IsTrue(ndr64_obj)) {"); |
| 784 |
$self->indent; |
| 785 |
$self->pidl("ndr_pull_flags |= LIBNDR_FLAG_NDR64;"); |
| 786 |
$self->deindent; |
| 787 |
$self->pidl("}"); |
| 788 |
$self->pidl(""); |
| 789 |
$self->pidl("if (allow_remaining_obj && PyObject_IsTrue(allow_remaining_obj)) {"); |
| 790 |
$self->indent; |
| 791 |
$self->pidl("allow_remaining = true;"); |
| 792 |
$self->deindent; |
| 793 |
$self->pidl("}"); |
| 794 |
$self->pidl(""); |
| 795 |
$self->pidl("return py_$name\_ndr_unpack(py_obj, &blob, NDR_IN, ndr_pull_flags, allow_remaining);"); |
| 796 |
$self->deindent; |
| 797 |
$self->pidl("}"); |
| 798 |
$self->pidl(""); |
| 799 |
|
| 800 |
$self->pidl("static PyObject *py_$name\_ndr_unpack_out(PyObject *py_obj, PyObject *args, PyObject *kwargs)"); |
| 801 |
$self->pidl("{"); |
| 802 |
$self->indent; |
| 803 |
$self->pidl("DATA_BLOB blob;"); |
| 804 |
$self->pidl("Py_ssize_t blob_length = 0;"); |
| 805 |
$self->pidl("const char * const kwnames[] = { \"data_blob\", \"bigendian\", \"ndr64\", \"allow_remaining\", NULL };"); |
| 806 |
$self->pidl("PyObject *bigendian_obj = NULL;"); |
| 807 |
$self->pidl("PyObject *ndr64_obj = NULL;"); |
| 808 |
$self->pidl("libndr_flags ndr_pull_flags = LIBNDR_FLAG_REF_ALLOC;"); |
| 809 |
$self->pidl("PyObject *allow_remaining_obj = NULL;"); |
| 810 |
$self->pidl("bool allow_remaining = false;"); |
| 811 |
$self->pidl(""); |
| 812 |
$self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, PYARG_BYTES_LEN \"|OOO:__ndr_unpack_out__\","); |
| 813 |
$self->indent; |
| 814 |
$self->pidl("discard_const_p(char *, kwnames),"); |
| 815 |
$self->pidl("&blob.data, &blob_length,"); |
| 816 |
$self->pidl("&bigendian_obj,"); |
| 817 |
$self->pidl("&ndr64_obj,"); |
| 818 |
$self->pidl("&allow_remaining_obj)) {"); |
| 819 |
$self->deindent; |
| 820 |
$self->indent; |
| 821 |
$self->pidl("return NULL;"); |
| 822 |
$self->deindent; |
| 823 |
$self->pidl("}"); |
| 824 |
$self->pidl("blob.length = blob_length;"); |
| 825 |
$self->pidl(""); |
| 826 |
$self->pidl("if (bigendian_obj && PyObject_IsTrue(bigendian_obj)) {"); |
| 827 |
$self->indent; |
| 828 |
$self->pidl("ndr_pull_flags |= LIBNDR_FLAG_BIGENDIAN;"); |
| 829 |
$self->deindent; |
| 830 |
$self->pidl("}"); |
| 831 |
$self->pidl("if (ndr64_obj && PyObject_IsTrue(ndr64_obj)) {"); |
| 832 |
$self->indent; |
| 833 |
$self->pidl("ndr_pull_flags |= LIBNDR_FLAG_NDR64;"); |
| 834 |
$self->deindent; |
| 835 |
$self->pidl("}"); |
| 836 |
$self->pidl(""); |
| 837 |
$self->pidl("if (allow_remaining_obj && PyObject_IsTrue(allow_remaining_obj)) {"); |
| 838 |
$self->indent; |
| 839 |
$self->pidl("allow_remaining = true;"); |
| 840 |
$self->deindent; |
| 841 |
$self->pidl("}"); |
| 842 |
$self->pidl(""); |
| 843 |
$self->pidl("return py_$name\_ndr_unpack(py_obj, &blob, NDR_OUT, ndr_pull_flags, allow_remaining);"); |
| 844 |
$self->deindent; |
| 845 |
$self->pidl("}"); |
| 846 |
$self->pidl(""); |
| 847 |
|
| 848 |
$self->pidl("static PyObject *py_$name\_ndr_print(PyObject *py_obj, PyObject *args, PyObject *kwargs, const char *name, ndr_flags_type ndr_inout_flags)"); |
| 849 |
$self->pidl("{"); |
| 850 |
$self->indent; |
| 851 |
$self->pidl("$ndr_call"); |
| 852 |
$self->pidl("$object_ptr"); |
| 853 |
$self->pidl("const char * const kwnames[] = { \"print_secrets\", NULL };"); |
| 854 |
$self->pidl("PyObject *py_print_secrets = NULL;"); |
| 855 |
$self->pidl("PyObject *ret;"); |
| 856 |
$self->pidl("char *retstr;"); |
| 857 |
$self->pidl("bool ok;"); |
| 858 |
$self->pidl(""); |
| 859 |
$self->pidl("ok = PyArg_ParseTupleAndKeywords(args, kwargs, \"|O:__ndr_print__\","); |
| 860 |
$self->indent; |
| 861 |
$self->pidl("discard_const_p(char *, kwnames),"); |
| 862 |
$self->pidl("&py_print_secrets);"); |
| 863 |
$self->deindent; |
| 864 |
$self->pidl("if (!ok) {"); |
| 865 |
$self->indent; |
| 866 |
$self->pidl("return NULL;"); |
| 867 |
$self->deindent; |
| 868 |
$self->pidl("}"); |
| 869 |
$self->pidl(""); |
| 870 |
$self->pidl("if (ndr_table_$iface\.num_calls < " . ($fn->{OPNUM}+1) . |
| 871 |
") {"); |
| 872 |
$self->indent; |
| 873 |
$self->pidl("PyErr_SetString(PyExc_TypeError, \"Internal Error, ndr_interface_call missing for py_$name\_ndr_print\");"); |
| 874 |
$self->pidl("return NULL;"); |
| 875 |
$self->deindent; |
| 876 |
$self->pidl("}"); |
| 877 |
$self->pidl("call = &ndr_table_$iface\.calls[$fn->{OPNUM}];"); |
| 878 |
$self->pidl(""); |
| 879 |
$self->pidl("if (py_print_secrets != NULL && PyObject_IsTrue(py_print_secrets)) {"); |
| 880 |
$self->indent; |
| 881 |
$self->pidl("retstr = ndr_print_function_secret_string(pytalloc_get_mem_ctx(py_obj), call->ndr_print, name, ndr_inout_flags, object);"); |
| 882 |
$self->deindent; |
| 883 |
$self->pidl("} else {"); |
| 884 |
$self->indent; |
| 885 |
$self->pidl("retstr = ndr_print_function_string(pytalloc_get_mem_ctx(py_obj), call->ndr_print, name, ndr_inout_flags, object);"); |
| 886 |
$self->deindent; |
| 887 |
$self->pidl("}"); |
| 888 |
$self->pidl("ret = PyUnicode_FromString(retstr);"); |
| 889 |
$self->pidl("TALLOC_FREE(retstr);"); |
| 890 |
$self->pidl(""); |
| 891 |
$self->pidl("return ret;"); |
| 892 |
$self->deindent; |
| 893 |
$self->pidl("}"); |
| 894 |
$self->pidl(""); |
| 895 |
|
| 896 |
$self->pidl("static PyObject *py_$name\_ndr_print_in(PyObject *py_obj, PyObject *args, PyObject *kwargs)"); |
| 897 |
$self->pidl("{"); |
| 898 |
$self->indent; |
| 899 |
$self->pidl("return py_$name\_ndr_print(py_obj, args, kwargs, \"$name\_in\", NDR_IN);"); |
| 900 |
$self->deindent; |
| 901 |
$self->pidl("}"); |
| 902 |
$self->pidl(""); |
| 903 |
|
| 904 |
$self->pidl("static PyObject *py_$name\_ndr_print_out(PyObject *py_obj, PyObject *args, PyObject *kwargs)"); |
| 905 |
$self->pidl("{"); |
| 906 |
$self->indent; |
| 907 |
$self->pidl("return py_$name\_ndr_print(py_obj, args, kwargs, \"$name\_out\", NDR_OUT);"); |
| 908 |
$self->deindent; |
| 909 |
$self->pidl("}"); |
| 910 |
$self->pidl(""); |
| 911 |
|
| 912 |
$py_methods = "py_$name\_methods"; |
| 913 |
$self->pidl("static PyMethodDef $py_methods\[] = {"); |
| 914 |
$self->indent; |
| 915 |
$self->pidl("{ \"opnum\", (PyCFunction)py_$name\_ndr_opnum, METH_NOARGS|METH_CLASS,"); |
| 916 |
$self->indent; |
| 917 |
$self->pidl("\"$modulename.$prettyname.opnum() -> ".sprintf("%d (0x%02x)", $fn->{OPNUM}, $fn->{OPNUM})." \" },"); |
| 918 |
$self->deindent; |
| 919 |
$self->pidl("{ \"__ndr_pack_in__\", PY_DISCARD_FUNC_SIG(PyCFunction,py_$name\_ndr_pack_in), METH_VARARGS|METH_KEYWORDS,"); |
| 920 |
$self->indent; |
| 921 |
$self->pidl("\"S.ndr_pack_in(object, bigendian=False, ndr64=False) -> blob\\nNDR pack input\" },"); |
| 922 |
$self->deindent; |
| 923 |
$self->pidl("{ \"__ndr_pack_out__\", PY_DISCARD_FUNC_SIG(PyCFunction,py_$name\_ndr_pack_out), METH_VARARGS|METH_KEYWORDS,"); |
| 924 |
$self->indent; |
| 925 |
$self->pidl("\"S.ndr_pack_out(object, bigendian=False, ndr64=False) -> blob\\nNDR pack output\" },"); |
| 926 |
$self->deindent; |
| 927 |
$self->pidl("{ \"__ndr_unpack_in__\", PY_DISCARD_FUNC_SIG(PyCFunction,py_$name\_ndr_unpack_in), METH_VARARGS|METH_KEYWORDS,"); |
| 928 |
$self->indent; |
| 929 |
$self->pidl("\"S.ndr_unpack_in(class, blob, bigendian=False, ndr64=False, allow_remaining=False) -> None\\nNDR unpack input\" },"); |
| 930 |
$self->deindent; |
| 931 |
$self->pidl("{ \"__ndr_unpack_out__\", PY_DISCARD_FUNC_SIG(PyCFunction,py_$name\_ndr_unpack_out), METH_VARARGS|METH_KEYWORDS,"); |
| 932 |
$self->indent; |
| 933 |
$self->pidl("\"S.ndr_unpack_out(class, blob, bigendian=False, ndr64=False, allow_remaining=False) -> None\\nNDR unpack output\" },"); |
| 934 |
$self->deindent; |
| 935 |
$self->pidl("{ \"__ndr_print_in__\", PY_DISCARD_FUNC_SIG(PyCFunction,py_$name\_ndr_print_in), METH_VARARGS|METH_KEYWORDS,"); |
| 936 |
$self->indent; |
| 937 |
$self->pidl("\"S.ndr_print_in(object, print_secrets=False) -> String\\nNDR print input\" },"); |
| 938 |
$self->deindent; |
| 939 |
$self->pidl("{ \"__ndr_print_out__\", PY_DISCARD_FUNC_SIG(PyCFunction,py_$name\_ndr_print_out), METH_VARARGS|METH_KEYWORDS,"); |
| 940 |
$self->indent; |
| 941 |
$self->pidl("\"S.ndr_print_out(object, print_secrets=False) -> String\\nNDR print output\" },"); |
| 942 |
$self->deindent; |
| 943 |
$self->pidl("{ NULL, NULL, 0, NULL }"); |
| 944 |
$self->deindent; |
| 945 |
$self->pidl("};"); |
| 946 |
$self->pidl(""); |
| 947 |
|
| 948 |
$self->pidl_hdr("static PyTypeObject $name\_Type;"); |
| 949 |
$self->pidl(""); |
| 950 |
my $docstring = $self->DocString($fn, $name); |
| 951 |
my $typeobject = "$name\_Type"; |
| 952 |
$self->pidl("static PyTypeObject $typeobject = {"); |
| 953 |
$self->indent; |
| 954 |
$self->pidl("PyVarObject_HEAD_INIT(NULL, 0)"); |
| 955 |
$self->pidl(".tp_name = \"$modulename.$prettyname\","); |
| 956 |
$self->pidl(".tp_getset = $getsetters,"); |
| 957 |
if ($docstring) { |
| 958 |
$self->pidl(".tp_doc = $docstring,"); |
| 959 |
} |
| 960 |
$self->pidl(".tp_methods = $py_methods,"); |
| 961 |
$self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,"); |
| 962 |
$self->pidl(".tp_new = py_$name\_new,"); |
| 963 |
$self->deindent; |
| 964 |
$self->pidl("};"); |
| 965 |
|
| 966 |
$self->pidl(""); |
| 967 |
|
| 968 |
my $talloc_typename = $self->import_type_variable("talloc", "BaseObject"); |
| 969 |
$self->register_module_prereadycode(["$name\_Type.tp_base = $talloc_typename;", |
| 970 |
"$name\_Type.tp_basicsize = pytalloc_BaseObject_size();", |
| 971 |
""]); |
| 972 |
|
| 973 |
return "&$typeobject"; |
| 974 |
} |
| 975 |
|
| 976 |
sub get_metadata_var($) |
| 977 |
{ |
| 978 |
my ($e) = @_; |
| 979 |
sub get_var($) { my $x = shift; $x =~ s/\*//g; return $x; } |
| 980 |
|
| 981 |
if (has_property($e, "length_is")) { |
| 982 |
return get_var($e->{PROPERTIES}->{length_is}); |
| 983 |
} elsif (has_property($e, "size_is")) { |
| 984 |
return get_var($e->{PROPERTIES}->{size_is}); |
| 985 |
} |
| 986 |
|
| 987 |
return undef; |
| 988 |
} |
| 989 |
|
| 990 |
sub find_metadata_args($) |
| 991 |
{ |
| 992 |
my ($fn) = @_; |
| 993 |
my $metadata_args = { in => {}, out => {} }; |
| 994 |
|
| 995 |
# Determine arguments that are metadata for other arguments (size_is/length_is) |
| 996 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 997 |
foreach my $dir (@{$e->{DIRECTION}}) { |
| 998 |
my $main = get_metadata_var($e); |
| 999 |
if ($main) { |
| 1000 |
$metadata_args->{$dir}->{$main} = $e->{NAME}; |
| 1001 |
} |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
return $metadata_args; |
| 1006 |
} |
| 1007 |
|
| 1008 |
sub PythonFunctionUnpackOut($$$) |
| 1009 |
{ |
| 1010 |
my ($self, $fn, $fnname) = @_; |
| 1011 |
|
| 1012 |
my $outfnname = "unpack_$fnname\_args_out"; |
| 1013 |
my $signature = ""; |
| 1014 |
|
| 1015 |
my $metadata_args = find_metadata_args($fn); |
| 1016 |
|
| 1017 |
my $env = GenerateFunctionOutEnv($fn, "r->"); |
| 1018 |
my $result_size = 0; |
| 1019 |
|
| 1020 |
$self->pidl("static PyObject *$outfnname(struct $fn->{NAME} *r, bool raise_result_exception)"); |
| 1021 |
$self->pidl("{"); |
| 1022 |
$self->indent; |
| 1023 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 1024 |
next unless (grep(/out/,@{$e->{DIRECTION}})); |
| 1025 |
next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or |
| 1026 |
($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}})); |
| 1027 |
$self->pidl("PyObject *py_$e->{NAME} = NULL;"); |
| 1028 |
$result_size++; |
| 1029 |
} |
| 1030 |
|
| 1031 |
my $is_raisable_return = 0; |
| 1032 |
if ($fn->{RETURN_TYPE} and ($fn->{RETURN_TYPE} eq "WERROR" or $fn->{RETURN_TYPE} eq "NTSTATUS")) { |
| 1033 |
$is_raisable_return = 1; |
| 1034 |
} |
| 1035 |
|
| 1036 |
if ($fn->{RETURN_TYPE} and not $is_raisable_return) { |
| 1037 |
$result_size++; |
| 1038 |
} |
| 1039 |
|
| 1040 |
my $max_result_size = $result_size; |
| 1041 |
my $alloc_size = "$result_size"; |
| 1042 |
if ($fn->{RETURN_TYPE} and $is_raisable_return) { |
| 1043 |
$max_result_size++; |
| 1044 |
} |
| 1045 |
$self->pidl("PyObject *result = NULL;"); |
| 1046 |
if ($max_result_size != $result_size) { |
| 1047 |
$self->pidl("size_t result_size = $result_size;"); |
| 1048 |
$alloc_size = "result_size"; |
| 1049 |
} |
| 1050 |
$self->pidl(""); |
| 1051 |
|
| 1052 |
if ($result_size > 1) { |
| 1053 |
$signature .= "("; |
| 1054 |
} elsif ($result_size == 0) { |
| 1055 |
$signature .= "None"; |
| 1056 |
} |
| 1057 |
|
| 1058 |
if ($fn->{RETURN_TYPE} and $is_raisable_return) { |
| 1059 |
$self->pidl("if (raise_result_exception) {"); |
| 1060 |
$self->indent; |
| 1061 |
if (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "NTSTATUS") { |
| 1062 |
$self->handle_ntstatus("r->out.result", "NULL", undef); |
| 1063 |
} elsif (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "WERROR") { |
| 1064 |
$self->handle_werror("r->out.result", "NULL", undef); |
| 1065 |
} |
| 1066 |
$self->deindent; |
| 1067 |
$self->pidl("} else {"); |
| 1068 |
$self->indent; |
| 1069 |
$self->pidl("/* $fn->{RETURN_TYPE} will be part of the results */"); |
| 1070 |
$self->pidl("result_size += 1;"); |
| 1071 |
$self->deindent; |
| 1072 |
$self->pidl("}"); |
| 1073 |
$self->pidl(""); |
| 1074 |
} |
| 1075 |
|
| 1076 |
if ($max_result_size > 1) { |
| 1077 |
if ($max_result_size != $result_size) { |
| 1078 |
$self->pidl("if (result_size > 1) {"); |
| 1079 |
$self->indent; |
| 1080 |
} |
| 1081 |
$self->pidl("result = PyTuple_New($alloc_size);"); |
| 1082 |
$self->pidl("if (result == NULL) {"); |
| 1083 |
$self->indent; |
| 1084 |
$self->pidl("return NULL;"); |
| 1085 |
$self->deindent; |
| 1086 |
$self->pidl("}"); |
| 1087 |
if ($max_result_size != $result_size) { |
| 1088 |
$self->deindent; |
| 1089 |
$self->pidl("}"); |
| 1090 |
} |
| 1091 |
$self->pidl(""); |
| 1092 |
} |
| 1093 |
|
| 1094 |
my $i = 0; |
| 1095 |
|
| 1096 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 1097 |
next if ($metadata_args->{out}->{$e->{NAME}}); |
| 1098 |
my $py_name = "py_$e->{NAME}"; |
| 1099 |
if (grep(/out/,@{$e->{DIRECTION}})) { |
| 1100 |
$self->ConvertObjectToPython("r", $env, $e, "r->out.$e->{NAME}", $py_name, "return NULL;"); |
| 1101 |
if ($result_size > 1) { |
| 1102 |
$signature .= "$e->{NAME}, "; |
| 1103 |
} else { |
| 1104 |
$signature .= $e->{NAME}; |
| 1105 |
} |
| 1106 |
|
| 1107 |
if ($max_result_size > 1) { |
| 1108 |
if ($max_result_size != $result_size and $result_size == 1) { |
| 1109 |
$self->pidl("if (result_size > 1) {"); |
| 1110 |
$self->indent; |
| 1111 |
} |
| 1112 |
$self->pidl("PyTuple_SetItem(result, $i, $py_name);"); |
| 1113 |
if ($max_result_size != $result_size and $result_size == 1) { |
| 1114 |
$self->deindent; |
| 1115 |
$self->pidl("}"); |
| 1116 |
} |
| 1117 |
} |
| 1118 |
if ($result_size == 1) { |
| 1119 |
if ($max_result_size != $result_size) { |
| 1120 |
$self->pidl("if (result_size == 1) {"); |
| 1121 |
$self->indent; |
| 1122 |
} |
| 1123 |
$self->pidl("result = $py_name;"); |
| 1124 |
if ($max_result_size != $result_size) { |
| 1125 |
$self->deindent; |
| 1126 |
$self->pidl("}"); |
| 1127 |
} |
| 1128 |
} |
| 1129 |
$self->pidl(""); |
| 1130 |
$i++; |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
if ($fn->{RETURN_TYPE} and $is_raisable_return) { |
| 1135 |
$self->pidl("if (!raise_result_exception) {"); |
| 1136 |
$self->indent; |
| 1137 |
} |
| 1138 |
|
| 1139 |
if ($fn->{RETURN_TYPE}) { |
| 1140 |
my $conv = $self->ConvertObjectToPythonData("r", $fn->{RETURN_TYPE}, "r->out.result", $fn); |
| 1141 |
if ($max_result_size > 1) { |
| 1142 |
$self->pidl("PyTuple_SetItem(result, $i, $conv);"); |
| 1143 |
} elsif ($max_result_size == 1) { |
| 1144 |
$self->pidl("result = $conv;"); |
| 1145 |
} else { |
| 1146 |
fatal($fn->{ORIGINAL}, "Internal error max_result_size=$max_result_size"); |
| 1147 |
} |
| 1148 |
|
| 1149 |
if (not $is_raisable_return) { |
| 1150 |
$signature .= "result"; |
| 1151 |
} |
| 1152 |
} |
| 1153 |
|
| 1154 |
if ($fn->{RETURN_TYPE} and $is_raisable_return) { |
| 1155 |
$self->deindent; |
| 1156 |
$self->pidl("}"); |
| 1157 |
$self->pidl(""); |
| 1158 |
} elsif ($fn->{RETURN_TYPE}) { |
| 1159 |
$self->pidl(""); |
| 1160 |
} |
| 1161 |
|
| 1162 |
if (substr($signature, -2) eq ", ") { |
| 1163 |
$signature = substr($signature, 0, -2); |
| 1164 |
} |
| 1165 |
if ($result_size > 1) { |
| 1166 |
$signature .= ")"; |
| 1167 |
} |
| 1168 |
|
| 1169 |
if ($result_size == 0) { |
| 1170 |
if ($max_result_size != $result_size) { |
| 1171 |
$self->pidl("if (result_size == 0) {"); |
| 1172 |
$self->indent; |
| 1173 |
} |
| 1174 |
$self->pidl("result = Py_None;"); |
| 1175 |
$self->pidl("Py_INCREF(result);"); |
| 1176 |
if ($max_result_size != $result_size) { |
| 1177 |
$self->deindent; |
| 1178 |
$self->pidl("}"); |
| 1179 |
} |
| 1180 |
$self->pidl(""); |
| 1181 |
} |
| 1182 |
|
| 1183 |
$self->pidl("return result;"); |
| 1184 |
$self->deindent; |
| 1185 |
$self->pidl("}"); |
| 1186 |
$self->pidl(""); |
| 1187 |
|
| 1188 |
return ($outfnname, $signature); |
| 1189 |
} |
| 1190 |
|
| 1191 |
sub PythonFunctionPackIn($$$) |
| 1192 |
{ |
| 1193 |
my ($self, $fn, $fnname) = @_; |
| 1194 |
my $metadata_args = find_metadata_args($fn); |
| 1195 |
|
| 1196 |
my $infnname = "pack_$fnname\_args_in"; |
| 1197 |
|
| 1198 |
$self->pidl("static bool $infnname(PyObject *args, PyObject *kwargs, struct $fn->{NAME} *r)"); |
| 1199 |
$self->pidl("{"); |
| 1200 |
$self->indent; |
| 1201 |
my $args_format = ""; |
| 1202 |
my $args_string = ""; |
| 1203 |
my $args_names = ""; |
| 1204 |
my $signature = ""; |
| 1205 |
|
| 1206 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 1207 |
next unless (grep(/in/,@{$e->{DIRECTION}})); |
| 1208 |
next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or |
| 1209 |
($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}})); |
| 1210 |
$self->pidl("PyObject *py_$e->{NAME};"); |
| 1211 |
$args_format .= "O"; |
| 1212 |
$args_string .= ", &py_$e->{NAME}"; |
| 1213 |
$args_names .= "\"$e->{NAME}\", "; |
| 1214 |
$signature .= "$e->{NAME}, "; |
| 1215 |
} |
| 1216 |
if (substr($signature, -2) eq ", ") { |
| 1217 |
$signature = substr($signature, 0, -2); |
| 1218 |
} |
| 1219 |
$self->pidl("const char *kwnames[] = {"); |
| 1220 |
$self->indent; |
| 1221 |
$self->pidl($args_names . "NULL"); |
| 1222 |
$self->deindent; |
| 1223 |
$self->pidl("};"); |
| 1224 |
|
| 1225 |
$self->pidl(""); |
| 1226 |
$self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"$args_format:$fn->{NAME}\", discard_const_p(char *, kwnames)$args_string)) {"); |
| 1227 |
$self->indent; |
| 1228 |
$self->pidl("return false;"); |
| 1229 |
$self->deindent; |
| 1230 |
$self->pidl("}"); |
| 1231 |
$self->pidl(""); |
| 1232 |
|
| 1233 |
my $env = GenerateFunctionInEnv($fn, "r->"); |
| 1234 |
|
| 1235 |
my $fail = "return false;"; |
| 1236 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 1237 |
next unless (grep(/in/,@{$e->{DIRECTION}})); |
| 1238 |
if ($metadata_args->{in}->{$e->{NAME}}) { |
| 1239 |
my $py_var = "py_".$metadata_args->{in}->{$e->{NAME}}; |
| 1240 |
$self->pidl("PY_CHECK_TYPE(&PyList_Type, $py_var, $fail);"); |
| 1241 |
my $val = "PyList_GET_SIZE($py_var)"; |
| 1242 |
if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") { |
| 1243 |
$self->pidl("r->in.$e->{NAME} = talloc_ptrtype(r, r->in.$e->{NAME});"); |
| 1244 |
$self->pidl("if (r->in.$e->{NAME} == NULL) {"); |
| 1245 |
$self->indent; |
| 1246 |
$self->pidl("PyErr_NoMemory();"); |
| 1247 |
$self->pidl($fail); |
| 1248 |
$self->deindent; |
| 1249 |
$self->pidl("}"); |
| 1250 |
$self->pidl("*r->in.$e->{NAME} = $val;"); |
| 1251 |
} else { |
| 1252 |
$self->pidl("r->in.$e->{NAME} = $val;"); |
| 1253 |
} |
| 1254 |
} else { |
| 1255 |
$self->ConvertObjectFromPython($env, "r", $e, "py_$e->{NAME}", "r->in.$e->{NAME}", $fail); |
| 1256 |
} |
| 1257 |
} |
| 1258 |
$self->pidl("return true;"); |
| 1259 |
$self->deindent; |
| 1260 |
$self->pidl("}"); |
| 1261 |
$self->pidl(""); |
| 1262 |
return ($infnname, $signature); |
| 1263 |
} |
| 1264 |
|
| 1265 |
sub PythonFunction($$$) |
| 1266 |
{ |
| 1267 |
my ($self, $fn, $iface, $prettyname) = @_; |
| 1268 |
|
| 1269 |
my $fnname = "py_$fn->{NAME}"; |
| 1270 |
my $docstring = $self->DocString($fn, $fn->{NAME}); |
| 1271 |
|
| 1272 |
my ($infn, $insignature) = $self->PythonFunctionPackIn($fn, $fnname); |
| 1273 |
my ($outfn, $outsignature) = $self->PythonFunctionUnpackOut($fn, $fnname); |
| 1274 |
my $signature = "S.$prettyname($insignature) -> $outsignature"; |
| 1275 |
if ($docstring) { |
| 1276 |
$docstring = "\"$signature\\n\\n\"$docstring"; |
| 1277 |
} else { |
| 1278 |
$docstring = "\"$signature\""; |
| 1279 |
} |
| 1280 |
|
| 1281 |
return ($infn, $outfn, $docstring); |
| 1282 |
} |
| 1283 |
|
| 1284 |
sub handle_werror($$$$) |
| 1285 |
{ |
| 1286 |
my ($self, $var, $retval, $mem_ctx) = @_; |
| 1287 |
|
| 1288 |
$self->pidl("if (!W_ERROR_IS_OK($var)) {"); |
| 1289 |
$self->indent; |
| 1290 |
$self->pidl("PyErr_SetWERROR($var);"); |
| 1291 |
$self->pidl("talloc_free($mem_ctx);") if ($mem_ctx); |
| 1292 |
$self->pidl("return $retval;"); |
| 1293 |
$self->deindent; |
| 1294 |
$self->pidl("}"); |
| 1295 |
$self->pidl(""); |
| 1296 |
} |
| 1297 |
|
| 1298 |
sub handle_ntstatus($$$$) |
| 1299 |
{ |
| 1300 |
my ($self, $var, $retval, $mem_ctx) = @_; |
| 1301 |
|
| 1302 |
$self->pidl("if (NT_STATUS_IS_ERR($var)) {"); |
| 1303 |
$self->indent; |
| 1304 |
$self->pidl("PyErr_SetNTSTATUS($var);"); |
| 1305 |
$self->pidl("talloc_free($mem_ctx);") if ($mem_ctx); |
| 1306 |
$self->pidl("return $retval;"); |
| 1307 |
$self->deindent; |
| 1308 |
$self->pidl("}"); |
| 1309 |
$self->pidl(""); |
| 1310 |
} |
| 1311 |
|
| 1312 |
sub PythonType($$$$) |
| 1313 |
{ |
| 1314 |
my ($self, $modulename, $d, $interface, $basename) = @_; |
| 1315 |
|
| 1316 |
my $actual_ctype = $d; |
| 1317 |
if ($actual_ctype->{TYPE} eq "TYPEDEF") { |
| 1318 |
$actual_ctype = $actual_ctype->{DATA}; |
| 1319 |
} |
| 1320 |
|
| 1321 |
if ($actual_ctype->{TYPE} eq "STRUCT") { |
| 1322 |
my $typeobject; |
| 1323 |
my $fn_name = PrettifyTypeName($d->{NAME}, $basename); |
| 1324 |
|
| 1325 |
if ($d->{TYPE} eq "STRUCT") { |
| 1326 |
$typeobject = $self->PythonStruct($modulename, $fn_name, $d->{NAME}, mapTypeName($d), $d); |
| 1327 |
} else { |
| 1328 |
$typeobject = $self->PythonStruct($modulename, $fn_name, $d->{NAME}, mapTypeName($d), $d->{DATA}); |
| 1329 |
} |
| 1330 |
|
| 1331 |
$self->register_module_typeobject($fn_name, $typeobject, $d->{ORIGINAL}); |
| 1332 |
} |
| 1333 |
|
| 1334 |
if ($d->{TYPE} eq "ENUM" or $d->{TYPE} eq "BITMAP") { |
| 1335 |
$self->EnumAndBitmapConsts($d->{NAME}, $d); |
| 1336 |
} |
| 1337 |
|
| 1338 |
if ($d->{TYPE} eq "TYPEDEF" and ($d->{DATA}->{TYPE} eq "ENUM" or $d->{DATA}->{TYPE} eq "BITMAP")) { |
| 1339 |
$self->EnumAndBitmapConsts($d->{NAME}, $d->{DATA}); |
| 1340 |
} |
| 1341 |
|
| 1342 |
if ($actual_ctype->{TYPE} eq "UNION" and defined($actual_ctype->{ELEMENTS})) { |
| 1343 |
my $prettyname = PrettifyTypeName($d->{NAME}, $basename); |
| 1344 |
my $typeobject = "$d->{NAME}\_Type"; |
| 1345 |
my $docstring = $self->DocString($d, $d->{NAME}); |
| 1346 |
my $cname = "union $d->{NAME}"; |
| 1347 |
|
| 1348 |
$self->pidl("static PyObject *py_import_$d->{NAME}(TALLOC_CTX *mem_ctx, int level, " .mapTypeName($d) . " *in)"); |
| 1349 |
$self->pidl("{"); |
| 1350 |
$self->indent; |
| 1351 |
$self->FromUnionToPythonFunction("mem_ctx", $actual_ctype, "level", "in") if ($actual_ctype->{TYPE} eq "UNION"); |
| 1352 |
$self->deindent; |
| 1353 |
$self->pidl("}"); |
| 1354 |
$self->pidl(""); |
| 1355 |
|
| 1356 |
$self->pidl("static ".mapTypeName($d) . " *py_export_$d->{NAME}(TALLOC_CTX *mem_ctx, int level, PyObject *in)"); |
| 1357 |
$self->pidl("{"); |
| 1358 |
$self->indent; |
| 1359 |
$self->FromPythonToUnionFunction($actual_ctype, mapTypeName($d), "level", "mem_ctx", "in") if ($actual_ctype->{TYPE} eq "UNION"); |
| 1360 |
$self->deindent; |
| 1361 |
$self->pidl("}"); |
| 1362 |
$self->pidl(""); |
| 1363 |
|
| 1364 |
my $getsetters = "NULL"; |
| 1365 |
my $py_methods = "NULL"; |
| 1366 |
my $typename = mapTypeName($d); |
| 1367 |
|
| 1368 |
$self->pidl("static PyObject *py_$d->{NAME}\_import(PyTypeObject *type, PyObject *args, PyObject *kwargs)"); |
| 1369 |
$self->pidl("{"); |
| 1370 |
$self->indent; |
| 1371 |
$self->pidl("const char * const kwnames[] = { \"mem_ctx\", \"level\", \"in\", NULL };"); |
| 1372 |
$self->pidl("PyObject *mem_ctx_obj = NULL;"); |
| 1373 |
$self->pidl("TALLOC_CTX *mem_ctx = NULL;"); |
| 1374 |
$self->pidl("int level = 0;"); |
| 1375 |
$self->pidl("PyObject *in_obj = NULL;"); |
| 1376 |
$self->pidl("$typename *in = NULL;"); |
| 1377 |
$self->pidl(""); |
| 1378 |
$self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"OiO:import\","); |
| 1379 |
$self->indent; |
| 1380 |
$self->pidl("discard_const_p(char *, kwnames),"); |
| 1381 |
$self->pidl("&mem_ctx_obj,"); |
| 1382 |
$self->pidl("&level,"); |
| 1383 |
$self->pidl("&in_obj)) {"); |
| 1384 |
$self->deindent; |
| 1385 |
$self->indent; |
| 1386 |
$self->pidl("return NULL;"); |
| 1387 |
$self->deindent; |
| 1388 |
$self->pidl("}"); |
| 1389 |
$self->pidl("mem_ctx = pytalloc_get_ptr(mem_ctx_obj);"); |
| 1390 |
$self->pidl("if (mem_ctx == NULL) {"); |
| 1391 |
$self->indent; |
| 1392 |
$self->pidl("PyErr_SetString(PyExc_TypeError, \"mem_ctx is NULL)!\");"); |
| 1393 |
$self->pidl("return NULL;"); |
| 1394 |
$self->deindent; |
| 1395 |
$self->pidl("}"); |
| 1396 |
$self->pidl("in = ($typename *)pytalloc_get_ptr(in_obj);"); |
| 1397 |
$self->pidl("if (in == NULL) {"); |
| 1398 |
$self->indent; |
| 1399 |
$self->pidl("PyErr_Format(PyExc_TypeError, \"in needs to be a pointer to $typename!\");"); |
| 1400 |
$self->pidl("return NULL;"); |
| 1401 |
$self->deindent; |
| 1402 |
$self->pidl("}"); |
| 1403 |
$self->pidl(""); |
| 1404 |
$self->pidl("return py_import_$d->{NAME}(mem_ctx, level, in);"); |
| 1405 |
$self->deindent; |
| 1406 |
$self->pidl("}"); |
| 1407 |
$self->pidl(""); |
| 1408 |
|
| 1409 |
$self->pidl("static PyObject *py_$d->{NAME}\_export(PyTypeObject *type, PyObject *args, PyObject *kwargs)"); |
| 1410 |
$self->pidl("{"); |
| 1411 |
$self->indent; |
| 1412 |
$self->pidl("const char * const kwnames[] = { \"mem_ctx\", \"level\", \"in\", NULL };"); |
| 1413 |
$self->pidl("PyObject *mem_ctx_obj = NULL;"); |
| 1414 |
$self->pidl("TALLOC_CTX *mem_ctx = NULL;"); |
| 1415 |
$self->pidl("int level = 0;"); |
| 1416 |
$self->pidl("PyObject *in = NULL;"); |
| 1417 |
$self->pidl("$typename *out = NULL;"); |
| 1418 |
$self->pidl(""); |
| 1419 |
$self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"OiO:export\","); |
| 1420 |
$self->indent; |
| 1421 |
$self->pidl("discard_const_p(char *, kwnames),"); |
| 1422 |
$self->pidl("&mem_ctx_obj,"); |
| 1423 |
$self->pidl("&level,"); |
| 1424 |
$self->pidl("&in)) {"); |
| 1425 |
$self->deindent; |
| 1426 |
$self->indent; |
| 1427 |
$self->pidl("return NULL;"); |
| 1428 |
$self->deindent; |
| 1429 |
$self->pidl("}"); |
| 1430 |
$self->pidl("mem_ctx = pytalloc_get_ptr(mem_ctx_obj);"); |
| 1431 |
$self->pidl("if (mem_ctx == NULL) {"); |
| 1432 |
$self->indent; |
| 1433 |
$self->pidl("PyErr_SetString(PyExc_TypeError, \"mem_ctx is NULL)!\");"); |
| 1434 |
$self->pidl("return NULL;"); |
| 1435 |
$self->deindent; |
| 1436 |
$self->pidl("}"); |
| 1437 |
$self->pidl(""); |
| 1438 |
$self->pidl("out = py_export_$d->{NAME}(mem_ctx, level, in);"); |
| 1439 |
$self->pidl("if (out == NULL) {"); |
| 1440 |
$self->indent; |
| 1441 |
$self->pidl("return NULL;"); |
| 1442 |
$self->deindent; |
| 1443 |
$self->pidl("}"); |
| 1444 |
$self->pidl(""); |
| 1445 |
$self->pidl("return pytalloc_GenericObject_reference(out);"); |
| 1446 |
$self->deindent; |
| 1447 |
$self->pidl("}"); |
| 1448 |
$self->pidl(""); |
| 1449 |
|
| 1450 |
$py_methods = "py_$d->{NAME}_methods"; |
| 1451 |
$self->pidl("static PyMethodDef $py_methods\[] = {"); |
| 1452 |
$self->indent; |
| 1453 |
$self->pidl("{ \"__import__\", PY_DISCARD_FUNC_SIG(PyCFunction,py_$d->{NAME}\_import),"); |
| 1454 |
$self->indent; |
| 1455 |
$self->pidl("METH_VARARGS|METH_KEYWORDS|METH_CLASS,"); |
| 1456 |
$self->pidl("\"T.__import__(mem_ctx, level, in) => ret.\" },"); |
| 1457 |
$self->deindent; |
| 1458 |
$self->pidl("{ \"__export__\", PY_DISCARD_FUNC_SIG(PyCFunction,py_$d->{NAME}\_export),"); |
| 1459 |
$self->indent; |
| 1460 |
$self->pidl("METH_VARARGS|METH_KEYWORDS|METH_CLASS,"); |
| 1461 |
$self->pidl("\"T.__export__(mem_ctx, level, in) => ret.\" },"); |
| 1462 |
$self->deindent; |
| 1463 |
$self->pidl("{ NULL, NULL, 0, NULL }"); |
| 1464 |
$self->deindent; |
| 1465 |
$self->pidl("};"); |
| 1466 |
$self->pidl(""); |
| 1467 |
|
| 1468 |
$self->pidl("static PyObject *py_$d->{NAME}\_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)"); |
| 1469 |
$self->pidl("{"); |
| 1470 |
$self->indent; |
| 1471 |
$self->pidl("PyErr_Format(PyExc_TypeError, \"New %s Objects are not supported\", type->tp_name);"); |
| 1472 |
$self->pidl("return NULL;"); |
| 1473 |
$self->deindent; |
| 1474 |
$self->pidl("}"); |
| 1475 |
$self->pidl(""); |
| 1476 |
|
| 1477 |
$self->pidl(""); |
| 1478 |
$self->pidl_hdr("static PyTypeObject $typeobject;"); |
| 1479 |
$self->pidl("static PyTypeObject $typeobject = {"); |
| 1480 |
$self->indent; |
| 1481 |
$self->pidl("PyVarObject_HEAD_INIT(NULL, 0)"); |
| 1482 |
$self->pidl(".tp_name = \"$modulename.$prettyname\","); |
| 1483 |
$self->pidl(".tp_getset = $getsetters,"); |
| 1484 |
if ($docstring) { |
| 1485 |
$self->pidl(".tp_doc = $docstring,"); |
| 1486 |
} |
| 1487 |
$self->pidl(".tp_methods = $py_methods,"); |
| 1488 |
$self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,"); |
| 1489 |
$self->pidl(".tp_new = py_$d->{NAME}\_new,"); |
| 1490 |
$self->deindent; |
| 1491 |
$self->pidl("};"); |
| 1492 |
|
| 1493 |
$self->pidl(""); |
| 1494 |
|
| 1495 |
my $talloc_typename = $self->import_type_variable("talloc", "BaseObject"); |
| 1496 |
$self->register_module_prereadycode(["$typeobject.tp_base = $talloc_typename;", |
| 1497 |
"$typeobject.tp_basicsize = pytalloc_BaseObject_size();", |
| 1498 |
""]); |
| 1499 |
|
| 1500 |
$self->register_module_typeobject($prettyname, "&$typeobject", $d->{ORIGINAL}); |
| 1501 |
} |
| 1502 |
} |
| 1503 |
|
| 1504 |
sub DocString($$$) |
| 1505 |
{ |
| 1506 |
my ($self, $d, $name) = @_; |
| 1507 |
if (has_property($d, "helpstring")) { |
| 1508 |
my $docstring = uc("py_doc_$name"); |
| 1509 |
$self->pidl("#define $docstring ".has_property($d, "helpstring")); |
| 1510 |
return $docstring; |
| 1511 |
} |
| 1512 |
|
| 1513 |
return undef; |
| 1514 |
} |
| 1515 |
|
| 1516 |
sub Interface($$$) |
| 1517 |
{ |
| 1518 |
my($self,$interface,$basename) = @_; |
| 1519 |
|
| 1520 |
if (has_property($interface, "pyhelper")) { |
| 1521 |
$self->pidl("#include \"".unmake_str($interface->{PROPERTIES}->{pyhelper})."\"\n"); |
| 1522 |
} |
| 1523 |
|
| 1524 |
$self->Const($_) foreach (@{$interface->{CONSTS}}); |
| 1525 |
|
| 1526 |
foreach my $d (@{$interface->{TYPES}}) { |
| 1527 |
next if has_property($d, "nopython"); |
| 1528 |
|
| 1529 |
$self->PythonType($basename, $d, $interface, $basename); |
| 1530 |
} |
| 1531 |
|
| 1532 |
if (defined $interface->{PROPERTIES}->{uuid}) { |
| 1533 |
$self->pidl_hdr("static PyTypeObject $interface->{NAME}_InterfaceType;"); |
| 1534 |
$self->pidl(""); |
| 1535 |
|
| 1536 |
my @fns = (); |
| 1537 |
|
| 1538 |
foreach my $d (@{$interface->{FUNCTIONS}}) { |
| 1539 |
next if has_property($d, "noopnum"); |
| 1540 |
next if has_property($d, "nopython"); |
| 1541 |
next if has_property($d, "todo"); |
| 1542 |
|
| 1543 |
my $skip = 0; |
| 1544 |
foreach my $e (@{$d->{ELEMENTS}}) { |
| 1545 |
if (ContainsPipe($e, $e->{LEVELS}[0])) { |
| 1546 |
$skip = 1; |
| 1547 |
last; |
| 1548 |
} |
| 1549 |
} |
| 1550 |
next if $skip; |
| 1551 |
|
| 1552 |
my $prettyname = $d->{NAME}; |
| 1553 |
|
| 1554 |
$prettyname =~ s/^$interface->{NAME}_//; |
| 1555 |
$prettyname =~ s/^$basename\_//; |
| 1556 |
|
| 1557 |
my $typeobject = $self->PythonFunctionStruct($basename, $d, $interface->{NAME}, $prettyname); |
| 1558 |
$self->register_module_typeobject($prettyname, $typeobject, $d->{ORIGINAL}); |
| 1559 |
|
| 1560 |
my ($infn, $outfn, $fndocstring) = $self->PythonFunction($d, $interface->{NAME}, $prettyname); |
| 1561 |
|
| 1562 |
push (@fns, [$infn, $outfn, "dcerpc_$d->{NAME}_r", $prettyname, $fndocstring, $d->{OPNUM}]); |
| 1563 |
} |
| 1564 |
|
| 1565 |
$self->pidl("const struct PyNdrRpcMethodDef py_ndr_$interface->{NAME}\_methods[] = {"); |
| 1566 |
$self->indent; |
| 1567 |
foreach my $d (@fns) { |
| 1568 |
my ($infn, $outfn, $callfn, $prettyname, $docstring, $opnum) = @$d; |
| 1569 |
$self->pidl("{"); |
| 1570 |
$self->indent; |
| 1571 |
$self->pidl(".name = \"$prettyname\","); |
| 1572 |
$self->pidl(".doc = $docstring,"); |
| 1573 |
$self->pidl(".call = (py_dcerpc_call_fn)$callfn,"); |
| 1574 |
$self->pidl(".pack_in_data = (py_data_pack_fn)$infn,"); |
| 1575 |
$self->pidl(".unpack_out_data = (py_data_unpack_fn)$outfn,"); |
| 1576 |
$self->pidl(".opnum = $opnum,"); |
| 1577 |
$self->pidl(".table = &ndr_table_$interface->{NAME},"); |
| 1578 |
$self->deindent; |
| 1579 |
$self->pidl("},"); |
| 1580 |
} |
| 1581 |
$self->pidl("{"); |
| 1582 |
$self->indent; |
| 1583 |
$self->pidl(".name = NULL,"); |
| 1584 |
$self->deindent; |
| 1585 |
$self->pidl("},"); |
| 1586 |
$self->deindent; |
| 1587 |
$self->pidl("};"); |
| 1588 |
$self->pidl(""); |
| 1589 |
|
| 1590 |
$self->pidl("static PyObject *interface_$interface->{NAME}_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)"); |
| 1591 |
$self->pidl("{"); |
| 1592 |
$self->indent; |
| 1593 |
$self->pidl("return py_dcerpc_interface_init_helper(type, args, kwargs, &ndr_table_$interface->{NAME});"); |
| 1594 |
$self->deindent; |
| 1595 |
$self->pidl("}"); |
| 1596 |
|
| 1597 |
$self->pidl(""); |
| 1598 |
|
| 1599 |
my $signature = |
| 1600 |
"\"$interface->{NAME}(binding, lp_ctx=None, credentials=None, basis_connection=None) -> connection\\n\" |
| 1601 |
\"\\n\\n\" |
| 1602 |
\"Parameters\\n\" |
| 1603 |
\"----------\\n\" |
| 1604 |
\"binding : str\\n\" |
| 1605 |
\" A DCE/RPC binding string (for example: ncacn_ip_tcp:127.0.0.1)\\n\" |
| 1606 |
\"lp_ctx : param.LoadParm\\n\" |
| 1607 |
\" Should be a path to a smb.conf file or a param.LoadParm object\\n\" |
| 1608 |
\"credentials : credentials.Credentials, optional\\n\" |
| 1609 |
\" A credentials.Credentials object (default is None).\\n\" |
| 1610 |
\"basis_connection : samba.dcerpc.ClientConnection, optional\\n\" |
| 1611 |
\" A $interface->{NAME} client connection object (default is None).\\n\" |
| 1612 |
\"\\n\\n\" |
| 1613 |
\"Returns\\n\" |
| 1614 |
\"-------\\n\" |
| 1615 |
\"samba.dcerpc.ClientConnection\\n\" |
| 1616 |
\" A ClientConnection object\\n\" |
| 1617 |
\"\\n\\n\" |
| 1618 |
\"Raises\\n\" |
| 1619 |
\"------\\n\" |
| 1620 |
\"samba.NTSTATUSError\\n\" |
| 1621 |
\" An NTSTATUS error\\n\" |
| 1622 |
\"\\n\""; |
| 1623 |
|
| 1624 |
|
| 1625 |
my $docstring = $self->DocString($interface, $interface->{NAME}); |
| 1626 |
|
| 1627 |
if ($docstring) { |
| 1628 |
$docstring = "$signature$docstring"; |
| 1629 |
} else { |
| 1630 |
$docstring = $signature; |
| 1631 |
} |
| 1632 |
|
| 1633 |
my $if_typename = "$interface->{NAME}_InterfaceType"; |
| 1634 |
|
| 1635 |
$self->pidl("static PyTypeObject $if_typename = {"); |
| 1636 |
$self->indent; |
| 1637 |
$self->pidl("PyVarObject_HEAD_INIT(NULL, 0)"); |
| 1638 |
$self->pidl(".tp_name = \"$basename.$interface->{NAME}\","); |
| 1639 |
$self->pidl(".tp_basicsize = sizeof(dcerpc_InterfaceObject),"); |
| 1640 |
$self->pidl(".tp_doc = $docstring,"); |
| 1641 |
$self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,"); |
| 1642 |
$self->pidl(".tp_new = interface_$interface->{NAME}_new,"); |
| 1643 |
$self->deindent; |
| 1644 |
$self->pidl("};"); |
| 1645 |
|
| 1646 |
$self->pidl(""); |
| 1647 |
|
| 1648 |
$self->register_module_typeobject($interface->{NAME}, "&$if_typename", $interface->{ORIGINAL}); |
| 1649 |
my $dcerpc_typename = $self->import_type_variable("samba.dcerpc.base", "ClientConnection"); |
| 1650 |
$self->register_module_prereadycode(["$if_typename.tp_base = $dcerpc_typename;", ""]); |
| 1651 |
$self->register_module_postreadycode(["if (!PyInterface_AddNdrRpcMethods(&$if_typename, py_ndr_$interface->{NAME}\_methods))", "\treturn NULL;", ""]); |
| 1652 |
|
| 1653 |
|
| 1654 |
$self->pidl("static PyObject *syntax_$interface->{NAME}_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)"); |
| 1655 |
$self->pidl("{"); |
| 1656 |
$self->indent; |
| 1657 |
$self->pidl("return py_dcerpc_syntax_init_helper(type, args, kwargs, &ndr_table_$interface->{NAME}.syntax_id);"); |
| 1658 |
$self->deindent; |
| 1659 |
$self->pidl("}"); |
| 1660 |
|
| 1661 |
$self->pidl(""); |
| 1662 |
|
| 1663 |
$signature = "\"$interface->{NAME}_abstract_syntax()\\n\""; |
| 1664 |
|
| 1665 |
$docstring = $self->DocString($interface, $interface->{NAME}."_syntax"); |
| 1666 |
|
| 1667 |
if ($docstring) { |
| 1668 |
$docstring = "$signature$docstring"; |
| 1669 |
} else { |
| 1670 |
$docstring = $signature; |
| 1671 |
} |
| 1672 |
|
| 1673 |
my $syntax_typename = "$interface->{NAME}_SyntaxType"; |
| 1674 |
|
| 1675 |
$self->pidl("static PyTypeObject $syntax_typename = {"); |
| 1676 |
$self->indent; |
| 1677 |
$self->pidl("PyVarObject_HEAD_INIT(NULL, 0)"); |
| 1678 |
$self->pidl(".tp_name = \"$basename.$interface->{NAME}_abstract_syntax\","); |
| 1679 |
$self->pidl(".tp_doc = $docstring,"); |
| 1680 |
$self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,"); |
| 1681 |
$self->pidl(".tp_new = syntax_$interface->{NAME}_new,"); |
| 1682 |
$self->deindent; |
| 1683 |
$self->pidl("};"); |
| 1684 |
|
| 1685 |
$self->pidl(""); |
| 1686 |
|
| 1687 |
$self->register_module_typeobject("$interface->{NAME}_abstract_syntax", "&$syntax_typename", $interface->{ORIGINAL}); |
| 1688 |
if (not defined($self->existing_module_object("abstract_syntax"))) { |
| 1689 |
# Only the first syntax gets registered with the legacy |
| 1690 |
# "abstract_syntax" name |
| 1691 |
$self->register_module_typeobject("abstract_syntax", "&$syntax_typename", $interface->{ORIGINAL}); |
| 1692 |
} |
| 1693 |
my $ndr_typename = $self->import_type_variable("samba.dcerpc.misc", "ndr_syntax_id"); |
| 1694 |
$self->register_module_prereadycode(["$syntax_typename.tp_base = $ndr_typename;", |
| 1695 |
"$syntax_typename.tp_basicsize = pytalloc_BaseObject_size();", |
| 1696 |
""]); |
| 1697 |
} |
| 1698 |
|
| 1699 |
$self->pidl_hdr(""); |
| 1700 |
} |
| 1701 |
|
| 1702 |
sub register_module_method($$$$$) |
| 1703 |
{ |
| 1704 |
my ($self, $fn_name, $pyfn_name, $flags, $doc) = @_; |
| 1705 |
|
| 1706 |
push (@{$self->{module_methods}}, [$fn_name, $pyfn_name, $flags, $doc]) |
| 1707 |
} |
| 1708 |
|
| 1709 |
sub register_module_typeobject($$$$) |
| 1710 |
{ |
| 1711 |
my ($self, $name, $py_name, $location) = @_; |
| 1712 |
|
| 1713 |
$self->register_module_object($name, "(PyObject *)(void *)$py_name", $location); |
| 1714 |
|
| 1715 |
$self->check_ready_type($py_name); |
| 1716 |
|
| 1717 |
$self->register_patch_type_call($name, $py_name); |
| 1718 |
} |
| 1719 |
|
| 1720 |
sub check_ready_type($$) |
| 1721 |
{ |
| 1722 |
my ($self, $py_name) = @_; |
| 1723 |
push (@{$self->{ready_types}}, $py_name) unless (grep(/^$py_name$/,@{$self->{ready_types}})); |
| 1724 |
} |
| 1725 |
|
| 1726 |
sub register_module_import($$) |
| 1727 |
{ |
| 1728 |
my ($self, $module_path) = @_; |
| 1729 |
|
| 1730 |
my $var_name = $module_path; |
| 1731 |
$var_name =~ s/\./_/g; |
| 1732 |
$var_name = "dep_$var_name"; |
| 1733 |
|
| 1734 |
unless (defined $self->{module_imports_uniq}->{$var_name}) { |
| 1735 |
my $h = { "key" => $var_name, "val" => $module_path}; |
| 1736 |
push @{$self->{module_imports}}, $h; |
| 1737 |
$self->{module_imports_uniq}->{$var_name} = $h; |
| 1738 |
} |
| 1739 |
return $var_name; |
| 1740 |
} |
| 1741 |
|
| 1742 |
sub import_type_variable($$$) |
| 1743 |
{ |
| 1744 |
my ($self, $module, $name) = @_; |
| 1745 |
|
| 1746 |
$self->register_module_import($module); |
| 1747 |
unless (defined $self->{type_imports_uniq}->{$name}) { |
| 1748 |
my $h = { "key" => $name, "val" => $module}; |
| 1749 |
push @{$self->{type_imports}}, $h; |
| 1750 |
$self->{type_imports_uniq}->{$name} = $h; |
| 1751 |
} |
| 1752 |
return "$name\_Type"; |
| 1753 |
} |
| 1754 |
|
| 1755 |
sub use_type_variable($$) |
| 1756 |
{ |
| 1757 |
my ($self, $orig_ctype) = @_; |
| 1758 |
# FIXME: Have a global lookup table for types that look different on the |
| 1759 |
# wire than they are named in C? |
| 1760 |
if ($orig_ctype->{NAME} eq "dom_sid2" or |
| 1761 |
$orig_ctype->{NAME} eq "dom_sid28" or |
| 1762 |
$orig_ctype->{NAME} eq "dom_sid0") { |
| 1763 |
$orig_ctype->{NAME} = "dom_sid"; |
| 1764 |
} |
| 1765 |
if ($orig_ctype->{NAME} eq "spoolss_security_descriptor") { |
| 1766 |
$orig_ctype->{NAME} = "security_descriptor"; |
| 1767 |
} |
| 1768 |
|
| 1769 |
my $ctype = resolveType($orig_ctype); |
| 1770 |
unless (defined($ctype->{BASEFILE})) { |
| 1771 |
return undef; |
| 1772 |
} |
| 1773 |
# If this is an external type, make sure we do the right imports. |
| 1774 |
if (($ctype->{BASEFILE} ne $self->{BASENAME})) { |
| 1775 |
return $self->import_type_variable("samba.dcerpc.$ctype->{BASEFILE}", $ctype->{NAME}); |
| 1776 |
} |
| 1777 |
return "&$ctype->{NAME}_Type"; |
| 1778 |
} |
| 1779 |
|
| 1780 |
sub register_patch_type_call($$$) |
| 1781 |
{ |
| 1782 |
my ($self, $typename, $cvar) = @_; |
| 1783 |
|
| 1784 |
push(@{$self->{patch_type_calls}}, [$typename, $cvar]); |
| 1785 |
|
| 1786 |
} |
| 1787 |
|
| 1788 |
sub register_module_prereadycode($$) |
| 1789 |
{ |
| 1790 |
my ($self, $code) = @_; |
| 1791 |
|
| 1792 |
push (@{$self->{prereadycode}}, @$code); |
| 1793 |
} |
| 1794 |
|
| 1795 |
sub register_module_postreadycode($$) |
| 1796 |
{ |
| 1797 |
my ($self, $code) = @_; |
| 1798 |
|
| 1799 |
push (@{$self->{postreadycode}}, @$code); |
| 1800 |
} |
| 1801 |
|
| 1802 |
sub existing_module_object($$) |
| 1803 |
{ |
| 1804 |
my ($self, $name) = @_; |
| 1805 |
|
| 1806 |
if (defined($self->{module_object_uniq}->{$name})) { |
| 1807 |
return $self->{module_object_uniq}->{$name}; |
| 1808 |
} |
| 1809 |
|
| 1810 |
return undef; |
| 1811 |
} |
| 1812 |
|
| 1813 |
sub register_module_object($$$$) |
| 1814 |
{ |
| 1815 |
my ($self, $name, $py_name, $location) = @_; |
| 1816 |
|
| 1817 |
my $existing = $self->existing_module_object($name); |
| 1818 |
fatal($location, "module_object($name, $py_name) registered twice! $existing.") if defined($existing); |
| 1819 |
|
| 1820 |
push (@{$self->{module_objects}}, [$name, $py_name]); |
| 1821 |
$self->{module_object_uniq}->{$name} = $py_name; |
| 1822 |
} |
| 1823 |
|
| 1824 |
sub assign($$$) |
| 1825 |
{ |
| 1826 |
my ($self, $dest, $src) = @_; |
| 1827 |
if ($dest =~ /^\&/ and $src eq "NULL") { |
| 1828 |
$self->pidl("memset($dest, 0, sizeof(" . get_value_of($dest) . "));"); |
| 1829 |
} elsif ($dest =~ /^\&/) { |
| 1830 |
my $destvar = get_value_of($dest); |
| 1831 |
$self->pidl("$destvar = *$src;"); |
| 1832 |
} else { |
| 1833 |
$self->pidl("$dest = $src;"); |
| 1834 |
} |
| 1835 |
} |
| 1836 |
|
| 1837 |
sub ConvertStringFromPythonData($$$$$) |
| 1838 |
{ |
| 1839 |
my ($self, $mem_ctx, $py_var, $target, $fail) = @_; |
| 1840 |
|
| 1841 |
$self->pidl("{"); |
| 1842 |
$self->indent; |
| 1843 |
$self->pidl("const char *test_str;"); |
| 1844 |
$self->pidl("const char *talloc_str;"); |
| 1845 |
$self->pidl("PyObject *unicode = NULL;"); |
| 1846 |
$self->pidl("if (PyUnicode_Check($py_var)) {"); |
| 1847 |
$self->indent; |
| 1848 |
# FIXME: Use Unix charset setting rather than utf-8 |
| 1849 |
$self->pidl("unicode = PyUnicode_AsEncodedString($py_var, \"utf-8\", \"ignore\");"); |
| 1850 |
$self->pidl("if (unicode == NULL) {"); |
| 1851 |
$self->indent; |
| 1852 |
$self->pidl("$fail"); |
| 1853 |
$self->deindent; |
| 1854 |
$self->pidl("}"); |
| 1855 |
|
| 1856 |
$self->pidl("test_str = PyBytes_AS_STRING(unicode);"); |
| 1857 |
$self->deindent; |
| 1858 |
$self->pidl("} else if (PyBytes_Check($py_var)) {"); |
| 1859 |
$self->indent; |
| 1860 |
$self->pidl("test_str = PyBytes_AS_STRING($py_var);"); |
| 1861 |
$self->deindent; |
| 1862 |
$self->pidl("} else {"); |
| 1863 |
$self->indent; |
| 1864 |
$self->pidl("PyErr_Format(PyExc_TypeError, \"Expected string or unicode object, got %s\", Py_TYPE($py_var)->tp_name);"); |
| 1865 |
$self->pidl("$fail"); |
| 1866 |
$self->deindent; |
| 1867 |
$self->pidl("}"); |
| 1868 |
$self->pidl("talloc_str = talloc_strdup($mem_ctx, test_str);"); |
| 1869 |
$self->pidl("if (unicode != NULL) {"); |
| 1870 |
$self->indent; |
| 1871 |
$self->pidl("Py_DECREF(unicode);"); |
| 1872 |
$self->deindent; |
| 1873 |
$self->pidl("}"); |
| 1874 |
$self->pidl("if (talloc_str == NULL) {"); |
| 1875 |
$self->indent; |
| 1876 |
$self->pidl("PyErr_NoMemory();"); |
| 1877 |
$self->pidl("$fail"); |
| 1878 |
$self->deindent; |
| 1879 |
$self->pidl("}"); |
| 1880 |
$self->pidl("$target = talloc_str;"); |
| 1881 |
$self->deindent; |
| 1882 |
$self->pidl("}"); |
| 1883 |
} |
| 1884 |
|
| 1885 |
sub ConvertU16StringFromPythonData($$$$$) |
| 1886 |
{ |
| 1887 |
my ($self, $mem_ctx, $py_var, $target, $fail) = @_; |
| 1888 |
|
| 1889 |
$self->pidl("{"); |
| 1890 |
$self->indent; |
| 1891 |
$self->pidl("unsigned char *str = NULL;"); |
| 1892 |
$self->pidl(""); |
| 1893 |
$self->pidl("str = PyUtf16String_FromBytes("); |
| 1894 |
$self->pidl(" $mem_ctx, $py_var);"); |
| 1895 |
$self->pidl("if (str == NULL) {"); |
| 1896 |
$self->indent; |
| 1897 |
$self->pidl("$fail"); |
| 1898 |
$self->deindent; |
| 1899 |
$self->pidl("}"); |
| 1900 |
$self->pidl(""); |
| 1901 |
$self->pidl("$target = str;"); |
| 1902 |
$self->deindent; |
| 1903 |
$self->pidl("}"); |
| 1904 |
} |
| 1905 |
|
| 1906 |
sub ConvertObjectFromPythonData($$$$$$;$$) |
| 1907 |
{ |
| 1908 |
my ($self, $mem_ctx, $cvar, $ctype, $target, $fail, $location, $switch) = @_; |
| 1909 |
|
| 1910 |
fatal($location, "undef type for $cvar") unless(defined($ctype)); |
| 1911 |
|
| 1912 |
$ctype = resolveType($ctype); |
| 1913 |
|
| 1914 |
my $actual_ctype = $ctype; |
| 1915 |
if ($actual_ctype->{TYPE} eq "TYPEDEF") { |
| 1916 |
$actual_ctype = $actual_ctype->{DATA}; |
| 1917 |
} |
| 1918 |
|
| 1919 |
# We need to cover ENUMs, BITMAPS and SCALAR values here, as |
| 1920 |
# all could otherwise be assigned invalid integer values |
| 1921 |
my $ctype_alias = ""; |
| 1922 |
my $uint_max = ""; |
| 1923 |
if ($actual_ctype->{TYPE} eq "ENUM") { |
| 1924 |
# Importantly, ENUM values are unsigned in pidl, and |
| 1925 |
# typically map to uint32 |
| 1926 |
$ctype_alias = enum_type_fn($actual_ctype); |
| 1927 |
} elsif ($actual_ctype->{TYPE} eq "BITMAP") { |
| 1928 |
$ctype_alias = bitmap_type_fn($actual_ctype); |
| 1929 |
} elsif ($actual_ctype->{TYPE} eq "SCALAR") { |
| 1930 |
$ctype_alias = expandAlias($actual_ctype->{NAME}); |
| 1931 |
} |
| 1932 |
|
| 1933 |
# This is the unsigned Python Integer -> C integer validation |
| 1934 |
# case. The signed case is below. |
| 1935 |
if ($ctype_alias =~ /^(uint[0-9]*|hyper|udlong|udlongr |
| 1936 |
|NTTIME_hyper|NTTIME|NTTIME_1sec |
| 1937 |
|uid_t|gid_t)$/x) { |
| 1938 |
$self->pidl("{"); |
| 1939 |
$self->indent; |
| 1940 |
$self->pidl("const unsigned long long uint_max = ndr_sizeof2uintmax(sizeof($target));"); |
| 1941 |
$self->pidl("if (PyLong_Check($cvar)) {"); |
| 1942 |
$self->indent; |
| 1943 |
$self->pidl("unsigned long long test_var;"); |
| 1944 |
$self->pidl("test_var = PyLong_AsUnsignedLongLong($cvar);"); |
| 1945 |
$self->pidl("if (PyErr_Occurred() != NULL) {"); |
| 1946 |
$self->indent; |
| 1947 |
$self->pidl($fail); |
| 1948 |
$self->deindent; |
| 1949 |
$self->pidl("}"); |
| 1950 |
$self->pidl("if (test_var > uint_max) {"); |
| 1951 |
$self->indent; |
| 1952 |
$self->pidl("PyErr_Format(PyExc_OverflowError, \"Expected type %s within range 0 - %llu, got %llu\","); |
| 1953 |
$self->pidl(" PyLong_Type.tp_name, uint_max, test_var);"); |
| 1954 |
$self->pidl($fail); |
| 1955 |
$self->deindent; |
| 1956 |
$self->pidl("}"); |
| 1957 |
$self->pidl("$target = test_var;"); |
| 1958 |
$self->deindent; |
| 1959 |
$self->pidl("} else {"); |
| 1960 |
$self->indent; |
| 1961 |
$self->pidl("PyErr_Format(PyExc_TypeError, \"Expected type %s\","); |
| 1962 |
$self->pidl(" PyLong_Type.tp_name);"); |
| 1963 |
$self->pidl($fail); |
| 1964 |
$self->deindent; |
| 1965 |
$self->pidl("}"); |
| 1966 |
$self->deindent; |
| 1967 |
$self->pidl("}"); |
| 1968 |
return; |
| 1969 |
} |
| 1970 |
|
| 1971 |
# Confirm the signed python integer fits in the C type |
| 1972 |
# correctly. It is subtly different from the unsigned case |
| 1973 |
# above, so while it looks like a duplicate, it is not |
| 1974 |
# actually a duplicate. |
| 1975 |
if ($ctype_alias =~ /^(dlong|char|int[0-9]*|time_t)$/x) { |
| 1976 |
$self->pidl("{"); |
| 1977 |
$self->indent; |
| 1978 |
$self->pidl("const long long int_max = ndr_sizeof2intmax(sizeof($target));"); |
| 1979 |
$self->pidl("const long long int_min = -int_max - 1;"); |
| 1980 |
$self->pidl("if (PyLong_Check($cvar)) {"); |
| 1981 |
$self->indent; |
| 1982 |
$self->pidl("long long test_var;"); |
| 1983 |
$self->pidl("test_var = PyLong_AsLongLong($cvar);"); |
| 1984 |
$self->pidl("if (PyErr_Occurred() != NULL) {"); |
| 1985 |
$self->indent; |
| 1986 |
$self->pidl($fail); |
| 1987 |
$self->deindent; |
| 1988 |
$self->pidl("}"); |
| 1989 |
$self->pidl("if (test_var < int_min || test_var > int_max) {"); |
| 1990 |
$self->indent; |
| 1991 |
$self->pidl("PyErr_Format(PyExc_OverflowError, \"Expected type %s within range %lld - %lld, got %lld\","); |
| 1992 |
$self->pidl(" PyLong_Type.tp_name, int_min, int_max, test_var);"); |
| 1993 |
$self->pidl($fail); |
| 1994 |
$self->deindent; |
| 1995 |
$self->pidl("}"); |
| 1996 |
$self->pidl("$target = test_var;"); |
| 1997 |
$self->deindent; |
| 1998 |
$self->pidl("} else {"); |
| 1999 |
$self->indent; |
| 2000 |
$self->pidl("PyErr_Format(PyExc_TypeError, \"Expected type %s\","); |
| 2001 |
$self->pidl(" PyLong_Type.tp_name);"); |
| 2002 |
$self->pidl($fail); |
| 2003 |
$self->deindent; |
| 2004 |
$self->pidl("}"); |
| 2005 |
$self->deindent; |
| 2006 |
$self->pidl("}"); |
| 2007 |
return; |
| 2008 |
} |
| 2009 |
|
| 2010 |
if ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") { |
| 2011 |
my $ctype_name = $self->use_type_variable($ctype); |
| 2012 |
unless (defined ($ctype_name)) { |
| 2013 |
error($location, "Unable to determine origin of type `" . mapTypeName($ctype) . "'"); |
| 2014 |
$self->pidl("PyErr_SetString(PyExc_TypeError, \"Cannot convert Python object to NDR $target\");"); |
| 2015 |
$self->pidl("$fail"); |
| 2016 |
return; |
| 2017 |
} |
| 2018 |
$self->pidl("PY_CHECK_TYPE($ctype_name, $cvar, $fail);"); |
| 2019 |
$self->pidl("if (talloc_reference($mem_ctx, pytalloc_get_mem_ctx($cvar)) == NULL) {"); |
| 2020 |
$self->indent; |
| 2021 |
$self->pidl("PyErr_NoMemory();"); |
| 2022 |
$self->pidl("$fail"); |
| 2023 |
$self->deindent; |
| 2024 |
$self->pidl("}"); |
| 2025 |
$self->assign($target, "(".mapTypeName($ctype)." *)pytalloc_get_ptr($cvar)"); |
| 2026 |
return; |
| 2027 |
} |
| 2028 |
|
| 2029 |
if ($actual_ctype->{TYPE} eq "UNION") { |
| 2030 |
my $ctype_name = $self->use_type_variable($ctype); |
| 2031 |
unless (defined ($ctype_name)) { |
| 2032 |
error($location, "Unable to determine origin of type `" . mapTypeName($ctype) . "'"); |
| 2033 |
$self->pidl("PyErr_SetString(PyExc_TypeError, \"Can not convert C Type " . mapTypeName($ctype) . " from Python\");"); |
| 2034 |
return; |
| 2035 |
} |
| 2036 |
my $export = "pyrpc_export_union($ctype_name, $mem_ctx, $switch, $cvar, \"".mapTypeName($ctype)."\")"; |
| 2037 |
$self->assign($target, "(".mapTypeName($ctype)." *)$export"); |
| 2038 |
return; |
| 2039 |
} |
| 2040 |
|
| 2041 |
if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "DATA_BLOB") { |
| 2042 |
$self->pidl("if (!PyBytes_Check($cvar)) {"); |
| 2043 |
$self->indent; |
| 2044 |
$self->pidl("PyErr_Format(PyExc_TypeError, \"Expected bytes object, got %s\", Py_TYPE($cvar)->tp_name);"); |
| 2045 |
$self->pidl("$fail"); |
| 2046 |
$self->deindent; |
| 2047 |
$self->pidl("}"); |
| 2048 |
$self->pidl("$target = data_blob_talloc($mem_ctx, PyBytes_AS_STRING($cvar), PyBytes_GET_SIZE($cvar));"); |
| 2049 |
return; |
| 2050 |
} |
| 2051 |
|
| 2052 |
if ($actual_ctype->{TYPE} eq "SCALAR" and |
| 2053 |
($actual_ctype->{NAME} eq "string" |
| 2054 |
or $actual_ctype->{NAME} eq "nbt_string" |
| 2055 |
or $actual_ctype->{NAME} eq "nbt_name" |
| 2056 |
or $actual_ctype->{NAME} eq "wrepl_nbt_name" |
| 2057 |
or $actual_ctype->{NAME} eq "dns_string" |
| 2058 |
or $actual_ctype->{NAME} eq "dnsp_string" |
| 2059 |
or $actual_ctype->{NAME} eq "dns_name" |
| 2060 |
or $actual_ctype->{NAME} eq "ipv4address" |
| 2061 |
or $actual_ctype->{NAME} eq "ipv6address" |
| 2062 |
or $actual_ctype->{NAME} eq "dnsp_name")) { |
| 2063 |
$self->ConvertStringFromPythonData($mem_ctx, $cvar, $target, $fail); |
| 2064 |
return; |
| 2065 |
} |
| 2066 |
|
| 2067 |
if ($actual_ctype->{TYPE} eq "SCALAR" and |
| 2068 |
$actual_ctype->{NAME} eq "u16string") { |
| 2069 |
$self->ConvertU16StringFromPythonData($mem_ctx, $cvar, $target, $fail); |
| 2070 |
return; |
| 2071 |
} |
| 2072 |
|
| 2073 |
if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "NTSTATUS") { |
| 2074 |
$self->pidl("$target = NT_STATUS(PyLong_AsLong($cvar));"); |
| 2075 |
return; |
| 2076 |
} |
| 2077 |
|
| 2078 |
if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "WERROR") { |
| 2079 |
$self->pidl("$target = W_ERROR(PyLong_AsLong($cvar));"); |
| 2080 |
return; |
| 2081 |
} |
| 2082 |
|
| 2083 |
if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "HRESULT") { |
| 2084 |
$self->pidl("$target = HRES_ERROR(PyLong_AsLong($cvar));"); |
| 2085 |
return; |
| 2086 |
} |
| 2087 |
|
| 2088 |
if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "string_array") { |
| 2089 |
$self->pidl("$target = pytalloc_get_ptr($cvar);"); |
| 2090 |
return; |
| 2091 |
} |
| 2092 |
|
| 2093 |
if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "pointer") { |
| 2094 |
$self->assign($target, "pytalloc_get_ptr($cvar)"); |
| 2095 |
return; |
| 2096 |
} |
| 2097 |
|
| 2098 |
fatal($location, "unknown type `$actual_ctype->{TYPE}' for ".mapTypeName($ctype) . ": $cvar"); |
| 2099 |
|
| 2100 |
} |
| 2101 |
|
| 2102 |
sub ConvertObjectFromPythonLevel($$$$$$$$$) |
| 2103 |
{ |
| 2104 |
my ($self, $env, $mem_ctx, $py_var, $e, $l, $var_name, $fail, $recurse) = @_; |
| 2105 |
my $nl = GetNextLevel($e, $l); |
| 2106 |
if ($nl and $nl->{TYPE} eq "SUBCONTEXT") { |
| 2107 |
$nl = GetNextLevel($e, $nl); |
| 2108 |
} |
| 2109 |
my $pl = GetPrevLevel($e, $l); |
| 2110 |
if ($pl and $pl->{TYPE} eq "SUBCONTEXT") { |
| 2111 |
$pl = GetPrevLevel($e, $pl); |
| 2112 |
} |
| 2113 |
|
| 2114 |
if ($recurse == 0) { |
| 2115 |
$self->pidl("if ($py_var == NULL) {"); |
| 2116 |
$self->indent; |
| 2117 |
$self->pidl("PyErr_Format(PyExc_AttributeError, \"Cannot delete NDR object: $var_name\");"); |
| 2118 |
$self->pidl($fail); |
| 2119 |
$self->deindent; |
| 2120 |
$self->pidl("}"); |
| 2121 |
} |
| 2122 |
$recurse = $recurse + 1; |
| 2123 |
|
| 2124 |
if ($l->{TYPE} eq "POINTER") { |
| 2125 |
my $need_deindent = 0; |
| 2126 |
my $need_deref = 0; |
| 2127 |
|
| 2128 |
if ($l->{POINTER_TYPE} ne "ref") { |
| 2129 |
$self->pidl("if ($py_var == Py_None) {"); |
| 2130 |
$self->indent; |
| 2131 |
$self->pidl("$var_name = NULL;"); |
| 2132 |
$self->deindent; |
| 2133 |
$self->pidl("} else {"); |
| 2134 |
$self->indent; |
| 2135 |
$need_deindent = 1; |
| 2136 |
if ($nl->{TYPE} eq "POINTER") { |
| 2137 |
$need_deref = 1; |
| 2138 |
} |
| 2139 |
} |
| 2140 |
|
| 2141 |
if ($l->{POINTER_TYPE} eq "ref" or $need_deref == 1) { |
| 2142 |
$self->pidl("$var_name = talloc_ptrtype($mem_ctx, $var_name);"); |
| 2143 |
$self->pidl("if ($var_name == NULL) {"); |
| 2144 |
$self->indent; |
| 2145 |
$self->pidl("PyErr_NoMemory();"); |
| 2146 |
$self->pidl($fail); |
| 2147 |
$self->deindent; |
| 2148 |
$self->pidl("}"); |
| 2149 |
} elsif ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::is_scalar($nl->{DATA_TYPE}) |
| 2150 |
and not Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) { |
| 2151 |
$self->pidl("$var_name = talloc_ptrtype($mem_ctx, $var_name);"); |
| 2152 |
$self->pidl("if ($var_name == NULL) {"); |
| 2153 |
$self->indent; |
| 2154 |
$self->pidl("PyErr_NoMemory();"); |
| 2155 |
$self->pidl($fail); |
| 2156 |
$self->deindent; |
| 2157 |
$self->pidl("}"); |
| 2158 |
} else { |
| 2159 |
$self->pidl("$var_name = NULL;"); |
| 2160 |
} |
| 2161 |
if ($need_deref == 1) { |
| 2162 |
my $ndr_pointer_typename = $self->import_type_variable("samba.dcerpc.base", "ndr_pointer"); |
| 2163 |
$self->pidl("$py_var = py_dcerpc_ndr_pointer_deref($ndr_pointer_typename, $py_var);"); |
| 2164 |
$self->pidl("if ($py_var == NULL) {"); |
| 2165 |
$self->indent; |
| 2166 |
$self->pidl($fail); |
| 2167 |
$self->deindent; |
| 2168 |
$self->pidl("}"); |
| 2169 |
} |
| 2170 |
unless ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) { |
| 2171 |
$var_name = get_value_of($var_name); |
| 2172 |
} |
| 2173 |
$self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, $nl, $var_name, $fail, $recurse); |
| 2174 |
if ($need_deindent == 1) { |
| 2175 |
$self->deindent; |
| 2176 |
$self->pidl("}"); |
| 2177 |
} |
| 2178 |
} elsif ($l->{TYPE} eq "ARRAY") { |
| 2179 |
if ($pl && $pl->{TYPE} eq "POINTER") { |
| 2180 |
$var_name = get_pointer_to($var_name); |
| 2181 |
} |
| 2182 |
|
| 2183 |
if (is_charset_array($e, $l)) { |
| 2184 |
$self->ConvertStringFromPythonData($mem_ctx, $py_var, $var_name, $fail); |
| 2185 |
} else { |
| 2186 |
my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}"; |
| 2187 |
$self->pidl("PY_CHECK_TYPE(&PyList_Type, $py_var, $fail);"); |
| 2188 |
$self->pidl("{"); |
| 2189 |
$self->indent; |
| 2190 |
$self->pidl("int $counter;"); |
| 2191 |
if (ArrayDynamicallyAllocated($e, $l)) { |
| 2192 |
$self->pidl("$var_name = talloc_array_ptrtype($mem_ctx, $var_name, PyList_GET_SIZE($py_var));"); |
| 2193 |
$self->pidl("if (!$var_name) { $fail }"); |
| 2194 |
$self->pidl("talloc_set_name_const($var_name, \"ARRAY: $var_name\");"); |
| 2195 |
} else { |
| 2196 |
$self->pidl("if (ARRAY_SIZE($var_name) != PyList_GET_SIZE($py_var)) {"); |
| 2197 |
$self->indent; |
| 2198 |
$self->pidl("PyErr_Format(PyExc_TypeError, \"Expected list of type %s, length %zu, got %zd\", Py_TYPE($py_var)->tp_name, ARRAY_SIZE($var_name), PyList_GET_SIZE($py_var));"); |
| 2199 |
$self->pidl("$fail"); |
| 2200 |
$self->deindent; |
| 2201 |
$self->pidl("}"); |
| 2202 |
} |
| 2203 |
$self->pidl("for ($counter = 0; $counter < PyList_GET_SIZE($py_var); $counter++) {"); |
| 2204 |
$self->indent; |
| 2205 |
if (ArrayDynamicallyAllocated($e, $l)) { |
| 2206 |
$self->ConvertObjectFromPythonLevel($env, $var_name, "PyList_GET_ITEM($py_var, $counter)", $e, $nl, "($var_name)"."[$counter]", $fail, 0); |
| 2207 |
} else { |
| 2208 |
$self->ConvertObjectFromPythonLevel($env, $mem_ctx, "PyList_GET_ITEM($py_var, $counter)", $e, $nl, "($var_name)"."[$counter]", $fail, 0); |
| 2209 |
} |
| 2210 |
$self->deindent; |
| 2211 |
$self->pidl("}"); |
| 2212 |
$self->deindent; |
| 2213 |
$self->pidl("}"); |
| 2214 |
} |
| 2215 |
} elsif ($l->{TYPE} eq "DATA") { |
| 2216 |
if (not Parse::Pidl::Typelist::is_scalar($l->{DATA_TYPE})) { |
| 2217 |
$var_name = get_pointer_to($var_name); |
| 2218 |
} |
| 2219 |
$self->ConvertObjectFromPythonData($mem_ctx, $py_var, $l->{DATA_TYPE}, $var_name, $fail, $e->{ORIGINAL}); |
| 2220 |
} elsif ($l->{TYPE} eq "SWITCH") { |
| 2221 |
$var_name = get_pointer_to($var_name); |
| 2222 |
my $switch = ParseExpr($l->{SWITCH_IS}, $env, $e); |
| 2223 |
my $switch_ptr = "$e->{NAME}_switch_$l->{LEVEL_INDEX}"; |
| 2224 |
$self->pidl("{"); |
| 2225 |
$self->indent; |
| 2226 |
my $union_type = mapTypeName($nl->{DATA_TYPE}); |
| 2227 |
$self->pidl("$union_type *$switch_ptr;"); |
| 2228 |
$self->ConvertObjectFromPythonData($mem_ctx, $py_var, $nl->{DATA_TYPE}, $switch_ptr, $fail, $e->{ORIGINAL}, $switch); |
| 2229 |
$self->fail_on_null($switch_ptr, $fail); |
| 2230 |
$self->assign($var_name, "$switch_ptr"); |
| 2231 |
$self->deindent; |
| 2232 |
$self->pidl("}"); |
| 2233 |
} elsif ($l->{TYPE} eq "SUBCONTEXT") { |
| 2234 |
$self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, $nl, $var_name, $fail, $recurse); |
| 2235 |
} else { |
| 2236 |
fatal($e->{ORIGINAL}, "unknown level type $l->{TYPE}"); |
| 2237 |
} |
| 2238 |
} |
| 2239 |
|
| 2240 |
sub ConvertObjectFromPython($$$$$$$) |
| 2241 |
{ |
| 2242 |
my ($self, $env, $mem_ctx, $ctype, $cvar, $target, $fail) = @_; |
| 2243 |
my $recurse = 0; |
| 2244 |
|
| 2245 |
$self->ConvertObjectFromPythonLevel($env, $mem_ctx, $cvar, $ctype, $ctype->{LEVELS}[0], $target, $fail, $recurse); |
| 2246 |
} |
| 2247 |
|
| 2248 |
sub ConvertScalarToPython($$$$) |
| 2249 |
{ |
| 2250 |
my ($self, $ctypename, $cvar, $mem_ctx) = @_; |
| 2251 |
|
| 2252 |
die("expected string for $cvar, not $ctypename") if (ref($ctypename) eq "HASH"); |
| 2253 |
|
| 2254 |
$ctypename = expandAlias($ctypename); |
| 2255 |
|
| 2256 |
if ($ctypename =~ /^(int64|dlong)$/) { |
| 2257 |
return "PyLong_FromLongLong($cvar)"; |
| 2258 |
} |
| 2259 |
|
| 2260 |
if ($ctypename =~ /^(uint64|hyper|NTTIME_hyper|NTTIME|NTTIME_1sec|udlong|udlongr|uid_t|gid_t)$/) { |
| 2261 |
return "PyLong_FromUnsignedLongLong($cvar)"; |
| 2262 |
} |
| 2263 |
|
| 2264 |
if ($ctypename =~ /^(char|int|int8|int16|int32|time_t)$/) { |
| 2265 |
return "PyLong_FromLong($cvar)"; |
| 2266 |
} |
| 2267 |
|
| 2268 |
# Needed to ensure unsigned values in a 32 or 16 bit enum is |
| 2269 |
# cast correctly to a uint32_t, not sign extended to a a |
| 2270 |
# possibly 64 bit unsigned long. (enums are signed in C, |
| 2271 |
# unsigned in NDR) |
| 2272 |
if ($ctypename =~ /^(uint32|uint3264)$/) { |
| 2273 |
return "PyLong_FromUnsignedLongLong((uint32_t)($cvar))"; |
| 2274 |
} |
| 2275 |
|
| 2276 |
if ($ctypename =~ /^(uint|uint8|uint16|uint1632)$/) { |
| 2277 |
return "PyLong_FromLong((uint16_t)($cvar))"; |
| 2278 |
} |
| 2279 |
|
| 2280 |
if ($ctypename eq "DATA_BLOB") { |
| 2281 |
return "PyBytes_FromStringAndSize((char *)($cvar).data, ($cvar).length)"; |
| 2282 |
} |
| 2283 |
|
| 2284 |
if ($ctypename eq "NTSTATUS") { |
| 2285 |
return "PyErr_FromNTSTATUS($cvar)"; |
| 2286 |
} |
| 2287 |
|
| 2288 |
if ($ctypename eq "WERROR") { |
| 2289 |
return "PyErr_FromWERROR($cvar)"; |
| 2290 |
} |
| 2291 |
|
| 2292 |
if ($ctypename eq "HRESULT") { |
| 2293 |
return "PyErr_FromHRESULT($cvar)"; |
| 2294 |
} |
| 2295 |
|
| 2296 |
if (($ctypename eq "string" or $ctypename eq "nbt_string" or $ctypename eq "nbt_name" or $ctypename eq "wrepl_nbt_name")) { |
| 2297 |
return "PyString_FromStringOrNULL($cvar)"; |
| 2298 |
} |
| 2299 |
|
| 2300 |
if (($ctypename eq "dns_string" or $ctypename eq "dns_name")) { |
| 2301 |
return "PyString_FromStringOrNULL($cvar)"; |
| 2302 |
} |
| 2303 |
|
| 2304 |
if ($ctypename eq "u16string") { |
| 2305 |
return "PyBytes_FromUtf16StringOrNULL($cvar)"; |
| 2306 |
} |
| 2307 |
|
| 2308 |
# Not yet supported |
| 2309 |
if ($ctypename eq "string_array") { |
| 2310 |
return "pytalloc_GenericObject_reference_ex($mem_ctx, $cvar)"; |
| 2311 |
} |
| 2312 |
if ($ctypename eq "ipv4address") { return "PyString_FromStringOrNULL($cvar)"; } |
| 2313 |
if ($ctypename eq "ipv6address") { return "PyString_FromStringOrNULL($cvar)"; } |
| 2314 |
if ($ctypename eq "dnsp_name") { return "PyString_FromStringOrNULL($cvar)"; } |
| 2315 |
if ($ctypename eq "dnsp_string") { return "PyString_FromStringOrNULL($cvar)"; } |
| 2316 |
if ($ctypename eq "pointer") { |
| 2317 |
return "pytalloc_GenericObject_reference_ex($mem_ctx, $cvar)"; |
| 2318 |
} |
| 2319 |
|
| 2320 |
die("Unknown scalar type $ctypename"); |
| 2321 |
} |
| 2322 |
|
| 2323 |
sub ConvertObjectToPythonData($$$$$;$$) |
| 2324 |
{ |
| 2325 |
my ($self, $mem_ctx, $ctype, $cvar, $location, $switch) = @_; |
| 2326 |
|
| 2327 |
die("undef type for $cvar") unless(defined($ctype)); |
| 2328 |
|
| 2329 |
$ctype = resolveType($ctype); |
| 2330 |
|
| 2331 |
my $actual_ctype = $ctype; |
| 2332 |
if ($actual_ctype->{TYPE} eq "TYPEDEF") { |
| 2333 |
$actual_ctype = $actual_ctype->{DATA}; |
| 2334 |
} |
| 2335 |
|
| 2336 |
if ($actual_ctype->{TYPE} eq "ENUM") { |
| 2337 |
return $self->ConvertScalarToPython(Parse::Pidl::Typelist::enum_type_fn($actual_ctype), $cvar, $mem_ctx); |
| 2338 |
} elsif ($actual_ctype->{TYPE} eq "BITMAP") { |
| 2339 |
return $self->ConvertScalarToPython(Parse::Pidl::Typelist::bitmap_type_fn($actual_ctype), $cvar, $mem_ctx); |
| 2340 |
} elsif ($actual_ctype->{TYPE} eq "SCALAR") { |
| 2341 |
return $self->ConvertScalarToPython($actual_ctype->{NAME}, $cvar, $mem_ctx); |
| 2342 |
} elsif ($actual_ctype->{TYPE} eq "UNION") { |
| 2343 |
my $ctype_name = $self->use_type_variable($ctype); |
| 2344 |
unless (defined($ctype_name)) { |
| 2345 |
error($location, "Unable to determine origin of type `" . mapTypeName($ctype) . "'"); |
| 2346 |
return "NULL"; # FIXME! |
| 2347 |
} |
| 2348 |
return "pyrpc_import_union($ctype_name, $mem_ctx, $switch, $cvar, \"".mapTypeName($ctype)."\")"; |
| 2349 |
} elsif ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") { |
| 2350 |
my $ctype_name = $self->use_type_variable($ctype); |
| 2351 |
unless (defined($ctype_name)) { |
| 2352 |
error($location, "Unable to determine origin of type `" . mapTypeName($ctype) . "'"); |
| 2353 |
return "NULL"; # FIXME! |
| 2354 |
} |
| 2355 |
return "pytalloc_reference_ex($ctype_name, $mem_ctx, $cvar)"; |
| 2356 |
} |
| 2357 |
|
| 2358 |
fatal($location, "unknown type $actual_ctype->{TYPE} for ".mapTypeName($ctype) . ": $cvar"); |
| 2359 |
} |
| 2360 |
|
| 2361 |
sub fail_on_null($$$) |
| 2362 |
{ |
| 2363 |
my ($self, $var, $fail) = @_; |
| 2364 |
$self->pidl("if ($var == NULL) {"); |
| 2365 |
$self->indent; |
| 2366 |
$self->pidl($fail); |
| 2367 |
$self->deindent; |
| 2368 |
$self->pidl("}"); |
| 2369 |
} |
| 2370 |
|
| 2371 |
sub ConvertObjectToPythonLevel($$$$$$$) |
| 2372 |
{ |
| 2373 |
my ($self, $mem_ctx, $env, $e, $l, $var_name, $py_var, $fail, $recurse) = @_; |
| 2374 |
my $nl = GetNextLevel($e, $l); |
| 2375 |
if ($nl and $nl->{TYPE} eq "SUBCONTEXT") { |
| 2376 |
$nl = GetNextLevel($e, $nl); |
| 2377 |
} |
| 2378 |
my $pl = GetPrevLevel($e, $l); |
| 2379 |
if ($pl and $pl->{TYPE} eq "SUBCONTEXT") { |
| 2380 |
$pl = GetPrevLevel($e, $pl); |
| 2381 |
} |
| 2382 |
|
| 2383 |
if ($l->{TYPE} eq "POINTER") { |
| 2384 |
my $need_wrap = 0; |
| 2385 |
if ($l->{POINTER_TYPE} ne "ref" and $nl->{TYPE} eq "POINTER") { |
| 2386 |
$need_wrap = 1; |
| 2387 |
} |
| 2388 |
if ($l->{POINTER_TYPE} ne "ref") { |
| 2389 |
if ($recurse == 0) { |
| 2390 |
$self->pidl("if ($var_name == NULL) {"); |
| 2391 |
$self->indent; |
| 2392 |
$self->pidl("$py_var = Py_None;"); |
| 2393 |
$self->pidl("Py_INCREF($py_var);"); |
| 2394 |
$self->deindent; |
| 2395 |
$self->pidl("} else {"); |
| 2396 |
$self->indent; |
| 2397 |
} else { |
| 2398 |
$self->pidl("{"); |
| 2399 |
$self->indent; |
| 2400 |
} |
| 2401 |
$recurse = $recurse + 1; |
| 2402 |
} |
| 2403 |
my $var_name2 = $var_name; |
| 2404 |
my $recurse2 = $recurse; |
| 2405 |
unless ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) { |
| 2406 |
$var_name2 = get_value_of($var_name); |
| 2407 |
$recurse2 = 0; |
| 2408 |
} |
| 2409 |
$self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, $var_name2, $py_var, $fail, $recurse2); |
| 2410 |
if ($l->{POINTER_TYPE} ne "ref") { |
| 2411 |
$self->deindent; |
| 2412 |
$self->pidl("}"); |
| 2413 |
} |
| 2414 |
if ($need_wrap) { |
| 2415 |
my $py_var_wrap = undef; |
| 2416 |
$need_wrap = 1; |
| 2417 |
$self->pidl("{"); |
| 2418 |
$self->indent; |
| 2419 |
$py_var_wrap = "py_$e->{NAME}_level_$l->{LEVEL_INDEX}"; |
| 2420 |
$self->pidl("PyObject *$py_var_wrap = $py_var;"); |
| 2421 |
my $ndr_pointer_typename = $self->import_type_variable("samba.dcerpc.base", "ndr_pointer"); |
| 2422 |
$self->pidl("$py_var = py_dcerpc_ndr_pointer_wrap($ndr_pointer_typename, $py_var_wrap);"); |
| 2423 |
$self->pidl("Py_XDECREF($py_var_wrap);"); |
| 2424 |
$self->deindent; |
| 2425 |
$self->pidl("}"); |
| 2426 |
} |
| 2427 |
} elsif ($l->{TYPE} eq "ARRAY") { |
| 2428 |
if ($pl && $pl->{TYPE} eq "POINTER") { |
| 2429 |
$var_name = get_pointer_to($var_name); |
| 2430 |
} |
| 2431 |
|
| 2432 |
if (is_charset_array($e, $l)) { |
| 2433 |
# FIXME: Use Unix charset setting rather than utf-8 |
| 2434 |
$self->pidl("if ($var_name == NULL) {"); |
| 2435 |
$self->indent; |
| 2436 |
$self->pidl("$py_var = Py_None;"); |
| 2437 |
$self->pidl("Py_INCREF($py_var);"); |
| 2438 |
$self->deindent; |
| 2439 |
$self->pidl("} else {"); |
| 2440 |
$self->indent; |
| 2441 |
$self->pidl("$py_var = PyUnicode_Decode($var_name, strlen($var_name), \"utf-8\", \"ignore\");"); |
| 2442 |
$self->deindent; |
| 2443 |
$self->pidl("}"); |
| 2444 |
} else { |
| 2445 |
die("No SIZE_IS for array $var_name") unless (defined($l->{SIZE_IS})); |
| 2446 |
my $length = $l->{SIZE_IS}; |
| 2447 |
if (defined($l->{LENGTH_IS})) { |
| 2448 |
$length = $l->{LENGTH_IS}; |
| 2449 |
} |
| 2450 |
|
| 2451 |
$length = ParseExpr($length, $env, $e); |
| 2452 |
$self->pidl("$py_var = PyList_New($length);"); |
| 2453 |
$self->fail_on_null($py_var, $fail); |
| 2454 |
$self->pidl("{"); |
| 2455 |
$self->indent; |
| 2456 |
my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}"; |
| 2457 |
$self->pidl("int $counter;"); |
| 2458 |
$self->pidl("for ($counter = 0; $counter < ($length); $counter++) {"); |
| 2459 |
$self->indent; |
| 2460 |
my $member_var = "py_$e->{NAME}_$l->{LEVEL_INDEX}"; |
| 2461 |
$self->pidl("PyObject *$member_var;"); |
| 2462 |
if (ArrayDynamicallyAllocated($e, $l)) { |
| 2463 |
$self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, "($var_name)"."[$counter]", $member_var, $fail, $recurse); |
| 2464 |
} else { |
| 2465 |
$self->ConvertObjectToPythonLevel($mem_ctx, $env, $e, $nl, "($var_name)"."[$counter]", $member_var, $fail, $recurse); |
| 2466 |
} |
| 2467 |
$self->pidl("PyList_SetItem($py_var, $counter, $member_var);"); |
| 2468 |
$self->deindent; |
| 2469 |
$self->pidl("}"); |
| 2470 |
$self->deindent; |
| 2471 |
$self->pidl("}"); |
| 2472 |
} |
| 2473 |
} elsif ($l->{TYPE} eq "SWITCH") { |
| 2474 |
$var_name = get_pointer_to($var_name); |
| 2475 |
my $switch = ParseExpr($l->{SWITCH_IS}, $env, $e); |
| 2476 |
my $conv = $self->ConvertObjectToPythonData($mem_ctx, $nl->{DATA_TYPE}, $var_name, $e->{ORIGINAL}, $switch); |
| 2477 |
$self->pidl("$py_var = $conv;"); |
| 2478 |
$self->fail_on_null($py_var, $fail); |
| 2479 |
|
| 2480 |
} elsif ($l->{TYPE} eq "DATA") { |
| 2481 |
if (not Parse::Pidl::Typelist::is_scalar($l->{DATA_TYPE})) { |
| 2482 |
$var_name = get_pointer_to($var_name); |
| 2483 |
} |
| 2484 |
my $conv = $self->ConvertObjectToPythonData($mem_ctx, $l->{DATA_TYPE}, $var_name, $e->{ORIGINAL}); |
| 2485 |
$self->pidl("$py_var = $conv;"); |
| 2486 |
if ($conv eq "NULL") { |
| 2487 |
$self->pidl("PyErr_SetString(PyExc_NotImplementedError, \"Cannot convert NDR $var_name to Python\");"); |
| 2488 |
$self->pidl("$fail"); |
| 2489 |
} |
| 2490 |
} elsif ($l->{TYPE} eq "SUBCONTEXT") { |
| 2491 |
$self->ConvertObjectToPythonLevel($mem_ctx, $env, $e, $nl, $var_name, $py_var, $fail, $recurse); |
| 2492 |
} else { |
| 2493 |
fatal($e->{ORIGINAL}, "Unknown level type $l->{TYPE} $var_name"); |
| 2494 |
} |
| 2495 |
} |
| 2496 |
|
| 2497 |
sub ConvertObjectToPython($$$$$$) |
| 2498 |
{ |
| 2499 |
my ($self, $mem_ctx, $env, $ctype, $cvar, $py_var, $fail) = @_; |
| 2500 |
my $recurse = 0; |
| 2501 |
|
| 2502 |
$self->ConvertObjectToPythonLevel($mem_ctx, $env, $ctype, $ctype->{LEVELS}[0], $cvar, $py_var, $fail, $recurse); |
| 2503 |
} |
| 2504 |
|
| 2505 |
sub Parse($$$$$) |
| 2506 |
{ |
| 2507 |
my($self,$basename,$ndr,$ndr_hdr,$hdr) = @_; |
| 2508 |
|
| 2509 |
$self->{BASENAME} = $basename; |
| 2510 |
|
| 2511 |
my $ndr_hdr_include = ""; |
| 2512 |
if (defined($ndr_hdr)) { |
| 2513 |
$ndr_hdr_include = "#include \"$ndr_hdr\""; |
| 2514 |
} |
| 2515 |
$self->pidl_hdr(" |
| 2516 |
/* Python wrapper functions auto-generated by pidl */ |
| 2517 |
#define PY_SSIZE_T_CLEAN 1 /* We use Py_ssize_t for PyArg_ParseTupleAndKeywords */ |
| 2518 |
#include \"lib/replace/system/python.h\" |
| 2519 |
#include \"python/py3compat.h\" |
| 2520 |
#include \"includes.h\" |
| 2521 |
#include \"python/modules.h\" |
| 2522 |
#include <pytalloc.h> |
| 2523 |
#include \"librpc/rpc/pyrpc.h\" |
| 2524 |
#include \"librpc/rpc/pyrpc_util.h\" |
| 2525 |
#include \"$hdr\" |
| 2526 |
$ndr_hdr_include |
| 2527 |
|
| 2528 |
/* |
| 2529 |
* Suppress compiler warnings if the generated code does not call these |
| 2530 |
* functions |
| 2531 |
*/ |
| 2532 |
#ifndef _MAYBE_UNUSED_ |
| 2533 |
#ifdef __has_attribute |
| 2534 |
#if __has_attribute(unused) |
| 2535 |
#define _MAYBE_UNUSED_ __attribute__ ((unused)) |
| 2536 |
#else |
| 2537 |
#define _MAYBE_UNUSED_ |
| 2538 |
#endif |
| 2539 |
#endif |
| 2540 |
#endif |
| 2541 |
/* |
| 2542 |
* These functions are here to ensure they can be optimized out by |
| 2543 |
* the compiler based on the constant input values |
| 2544 |
*/ |
| 2545 |
|
| 2546 |
static inline unsigned long long ndr_sizeof2uintmax(size_t var_size) |
| 2547 |
{ |
| 2548 |
switch (var_size) { |
| 2549 |
case 8: |
| 2550 |
return UINT64_MAX; |
| 2551 |
case 4: |
| 2552 |
return UINT32_MAX; |
| 2553 |
case 2: |
| 2554 |
return UINT16_MAX; |
| 2555 |
case 1: |
| 2556 |
return UINT8_MAX; |
| 2557 |
} |
| 2558 |
|
| 2559 |
return 0; |
| 2560 |
} |
| 2561 |
|
| 2562 |
static inline _MAYBE_UNUSED_ long long ndr_sizeof2intmax(size_t var_size) |
| 2563 |
{ |
| 2564 |
switch (var_size) { |
| 2565 |
case 8: |
| 2566 |
return INT64_MAX; |
| 2567 |
case 4: |
| 2568 |
return INT32_MAX; |
| 2569 |
case 2: |
| 2570 |
return INT16_MAX; |
| 2571 |
case 1: |
| 2572 |
return INT8_MAX; |
| 2573 |
} |
| 2574 |
|
| 2575 |
return 0; |
| 2576 |
} |
| 2577 |
"); |
| 2578 |
|
| 2579 |
foreach my $x (@$ndr) { |
| 2580 |
($x->{TYPE} eq "IMPORT") && $self->Import(@{$x->{PATHS}}); |
| 2581 |
($x->{TYPE} eq "INTERFACE") && $self->Interface($x, $basename); |
| 2582 |
} |
| 2583 |
|
| 2584 |
$self->pidl("static PyMethodDef $basename\_methods[] = {"); |
| 2585 |
$self->indent; |
| 2586 |
foreach (@{$self->{module_methods}}) { |
| 2587 |
my ($fn_name, $pyfn_name, $flags, $doc) = @$_; |
| 2588 |
$self->pidl("{ \"$fn_name\", (PyCFunction)$pyfn_name, $flags, $doc },"); |
| 2589 |
} |
| 2590 |
|
| 2591 |
$self->pidl("{ NULL, NULL, 0, NULL }"); |
| 2592 |
$self->deindent; |
| 2593 |
$self->pidl("};"); |
| 2594 |
|
| 2595 |
$self->pidl(""); |
| 2596 |
|
| 2597 |
$self->pidl("static struct PyModuleDef moduledef = {"); |
| 2598 |
$self->indent; |
| 2599 |
$self->pidl("PyModuleDef_HEAD_INIT,"); |
| 2600 |
$self->pidl(".m_name = \"$basename\","); |
| 2601 |
$self->pidl(".m_doc = \"$basename DCE/RPC\","); |
| 2602 |
$self->pidl(".m_size = -1,"); |
| 2603 |
$self->pidl(".m_methods = $basename\_methods,"); |
| 2604 |
$self->deindent; |
| 2605 |
$self->pidl("};"); |
| 2606 |
|
| 2607 |
$self->pidl("MODULE_INIT_FUNC($basename)"); |
| 2608 |
$self->pidl("{"); |
| 2609 |
$self->indent; |
| 2610 |
$self->pidl("PyObject *m = NULL;"); |
| 2611 |
foreach my $h (@{$self->{module_imports}}) { |
| 2612 |
$self->pidl("PyObject *$h->{'key'} = NULL;"); |
| 2613 |
} |
| 2614 |
$self->pidl(""); |
| 2615 |
|
| 2616 |
foreach my $h (@{$self->{module_imports}}) { |
| 2617 |
my $var_name = $h->{'key'}; |
| 2618 |
my $module_path = $h->{'val'}; |
| 2619 |
$self->pidl("$var_name = PyImport_ImportModule(\"$module_path\");"); |
| 2620 |
$self->pidl("if ($var_name == NULL)"); |
| 2621 |
$self->pidl("\tgoto out;"); |
| 2622 |
$self->pidl(""); |
| 2623 |
} |
| 2624 |
|
| 2625 |
foreach my $h (@{$self->{type_imports}}) { |
| 2626 |
my $type_var = "$h->{'key'}\_Type"; |
| 2627 |
my $module_path = $h->{'val'}; |
| 2628 |
$self->pidl_hdr("static PyTypeObject *$type_var;"); |
| 2629 |
my $pretty_name = PrettifyTypeName($h->{'key'}, $module_path); |
| 2630 |
my $module_var = "dep_$module_path"; |
| 2631 |
$module_var =~ s/\./_/g; |
| 2632 |
$self->pidl("$type_var = (PyTypeObject *)PyObject_GetAttrString($module_var, \"$pretty_name\");"); |
| 2633 |
$self->pidl("if ($type_var == NULL)"); |
| 2634 |
$self->pidl("\tgoto out;"); |
| 2635 |
$self->pidl(""); |
| 2636 |
} |
| 2637 |
|
| 2638 |
$self->pidl($_) foreach (@{$self->{prereadycode}}); |
| 2639 |
|
| 2640 |
foreach (@{$self->{ready_types}}) { |
| 2641 |
$self->pidl("if (PyType_Ready($_) < 0)"); |
| 2642 |
$self->pidl("\tgoto out;"); |
| 2643 |
} |
| 2644 |
|
| 2645 |
$self->pidl($_) foreach (@{$self->{postreadycode}}); |
| 2646 |
|
| 2647 |
foreach (@{$self->{patch_type_calls}}) { |
| 2648 |
my ($typename, $cvar) = @$_; |
| 2649 |
$self->pidl("#ifdef PY_".uc($typename)."_PATCH"); |
| 2650 |
$self->pidl("PY_".uc($typename)."_PATCH($cvar);"); |
| 2651 |
$self->pidl("#endif"); |
| 2652 |
} |
| 2653 |
|
| 2654 |
$self->pidl(""); |
| 2655 |
|
| 2656 |
$self->pidl("m = PyModule_Create(&moduledef);"); |
| 2657 |
$self->pidl("if (m == NULL)"); |
| 2658 |
$self->pidl("\tgoto out;"); |
| 2659 |
$self->pidl(""); |
| 2660 |
foreach my $h (@{$self->{constants}}) { |
| 2661 |
my $pretty_name = PrettifyTypeName($h->{'key'}, $basename); |
| 2662 |
my $py_obj; |
| 2663 |
my ($ctype, $cvar) = @{$h->{'val'}}; |
| 2664 |
if ($cvar =~ /^[0-9]+$/ or $cvar =~ /^0x[0-9a-fA-F]+$/) { |
| 2665 |
$py_obj = "PyLong_FromUnsignedLongLong($cvar)"; |
| 2666 |
} elsif ($cvar =~ /^".*"$/) { |
| 2667 |
$py_obj = "PyUnicode_FromString($cvar)"; |
| 2668 |
} else { |
| 2669 |
$py_obj = $self->ConvertObjectToPythonData("NULL", expandAlias($ctype), $cvar, undef); |
| 2670 |
} |
| 2671 |
|
| 2672 |
$self->pidl("PyModule_AddObject(m, \"$pretty_name\", $py_obj);"); |
| 2673 |
} |
| 2674 |
|
| 2675 |
foreach (@{$self->{module_objects}}) { |
| 2676 |
my ($object_name, $c_name) = @$_; |
| 2677 |
$self->pidl("Py_INCREF($c_name);"); |
| 2678 |
$self->pidl("PyModule_AddObject(m, \"$object_name\", $c_name);"); |
| 2679 |
} |
| 2680 |
|
| 2681 |
$self->pidl("#ifdef PY_MOD_".uc($basename)."_PATCH"); |
| 2682 |
$self->pidl("PY_MOD_".uc($basename)."_PATCH(m);"); |
| 2683 |
$self->pidl("#endif"); |
| 2684 |
$self->pidl("out:"); |
| 2685 |
foreach my $h (@{$self->{module_imports}}) { |
| 2686 |
my $mod_var = $h->{'key'}; |
| 2687 |
$self->pidl("Py_XDECREF($mod_var);"); |
| 2688 |
} |
| 2689 |
$self->pidl("return m;"); |
| 2690 |
$self->pidl(""); |
| 2691 |
$self->deindent; |
| 2692 |
$self->pidl("}"); |
| 2693 |
return ($self->{res_hdr} . $self->{res}); |
| 2694 |
} |
| 2695 |
|
| 2696 |
1; |