1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-24 21:34:56 +03:00

Add convenience functions for setting Python objects from errors.

This commit is contained in:
Jelmer Vernooij 2008-05-24 22:13:32 +02:00
parent 4bcb92d2d4
commit f1de723b89
5 changed files with 18 additions and 8 deletions

View File

@ -20,10 +20,14 @@
#ifndef __PYERRORS_H__
#define __PYERRORS_H__
#define PyErr_FromWERROR(err) Py_BuildValue("(i,s)", W_ERROR_V(err), discard_const_p(char, win_errstr(err)))
#define PyErr_FromNTSTATUS(status) Py_BuildValue("(i,s)", NT_STATUS_V(status), discard_const_p(char, nt_errstr(status)))
#define PyErr_SetWERROR(err) \
PyErr_SetObject(PyExc_RuntimeError, Py_BuildValue((char *)"(i,s)", W_ERROR_V(err), discard_const_p(char, win_errstr(err))))
PyErr_SetObject(PyExc_RuntimeError, PyErr_FromWERROR(err))
#define PyErr_SetNTSTATUS(status) \
PyErr_SetObject(PyExc_RuntimeError, Py_BuildValue((char *)"(i,s)", NT_STATUS_V(status), discard_const_p(char, nt_errstr(status))))
PyErr_SetObject(PyExc_RuntimeError, PyErr_FromNTSTATUS(status))
#endif /* __PYERRORS_H__ */

View File

@ -22,7 +22,6 @@
#include "librpc/rpc/pyrpc.h"
#include "librpc/rpc/dcerpc.h"
#include "lib/events/events.h"
#include "libcli/util/pyerrors.h"
static PyObject *py_iface_server_name(PyObject *obj, void *closure)
{

View File

@ -20,6 +20,8 @@
#ifndef _PYRPC_H_
#define _PYRPC_H_
#include "libcli/util/pyerrors.h"
#define PY_CHECK_TYPE(type, var, fail) \
if (!type ## _Check(var)) {\
PyErr_Format(PyExc_TypeError, "Expected type %s", type ## _Type.tp_name); \

View File

@ -257,7 +257,7 @@ sub PythonStruct($$$$$$)
$self->pidl("$cname *object = py_talloc_get_ptr(py_obj);");
$self->pidl("DATA_BLOB blob;");
$self->pidl("enum ndr_err_code err;");
$self->pidl("if (!PyArg_ParseTuple(args, \"(s#):__ndr_unpack__\", &blob.data, &blob.length))");
$self->pidl("if (!PyArg_ParseTuple(args, \"s#:__ndr_unpack__\", &blob.data, &blob.length))");
$self->pidl("\treturn NULL;");
$self->pidl("");
$self->pidl("err = ndr_pull_struct_blob_all(&blob, py_talloc_get_mem_ctx(py_obj), NULL, object, (ndr_pull_flags_fn_t)ndr_pull_$name);");
@ -501,7 +501,7 @@ sub handle_werror($$$$)
$self->pidl("if (!W_ERROR_IS_OK($var)) {");
$self->indent;
$self->pidl("PyErr_SetString(PyExc_RuntimeError, win_errstr($var));");
$self->pidl("PyErr_SetWERROR($var);");
$self->pidl("talloc_free($mem_ctx);") if ($mem_ctx);
$self->pidl("return $retval;");
$self->deindent;
@ -515,7 +515,7 @@ sub handle_ntstatus($$$$)
$self->pidl("if (NT_STATUS_IS_ERR($var)) {");
$self->indent;
$self->pidl("PyErr_SetString(PyExc_RuntimeError, nt_errstr($var));");
$self->pidl("PyErr_SetNTSTATUS($var);");
$self->pidl("talloc_free($mem_ctx);") if ($mem_ctx);
$self->pidl("return $retval;");
$self->deindent;
@ -926,11 +926,11 @@ sub ConvertScalarToPython($$$)
}
if ($ctypename eq "NTSTATUS") {
return "PyInt_FromLong(NT_STATUS_V($cvar))";
return "PyErr_FromNTSTATUS($cvar)";
}
if ($ctypename eq "WERROR") {
return "PyInt_FromLong(W_ERROR_V($cvar))";
return "PyErr_FromWERROR($cvar)";
}
if (($ctypename eq "string" or $ctypename eq "nbt_string" or $ctypename eq "nbt_name" or $ctypename eq "wrepl_nbt_name")) {

View File

@ -42,6 +42,11 @@ class RpcEchoTests(RpcInterfaceTestCase):
y = self.conn.TestSurrounding(surrounding_struct)
self.assertEquals(8 * [0], y.surrounding)
def test_manual_request(self):
self.assertEquals("\x01\x00\x00\x00", self.conn.request(0, chr(0) * 4))
def test_server_name(self):
self.assertEquals(None, self.conn.server_name)
class NdrEchoTests(unittest.TestCase):
def test_info1_push(self):