From 46cbe76a2c8c60bf14df941b2fcf6b8c5cad10e5 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Thu, 23 Sep 2004 03:26:14 +0000 Subject: r2559: Python ints can't hold the full range of uint32 values so store them as Python longs. Also allow shorter width integer types to be initialised from long values. Their values are truncated if they are too long. (This used to be commit e9eb231d6441774d1b5227962bbe94aa29e20995) --- source4/scripting/swig/dcerpc.i | 65 ++++++++++++++++++++++------------ source4/scripting/swig/torture/samr.py | 12 +++++++ 2 files changed, 55 insertions(+), 22 deletions(-) diff --git a/source4/scripting/swig/dcerpc.i b/source4/scripting/swig/dcerpc.i index 7a8a0e4a83..eb8b065e33 100644 --- a/source4/scripting/swig/dcerpc.i +++ b/source4/scripting/swig/dcerpc.i @@ -58,12 +58,15 @@ uint8 uint8_from_python(PyObject *obj, char *name) return 0; } - if (!PyInt_Check(obj)) { - PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name); + if (!PyInt_Check(obj) && !PyLong_Check(obj)) { + PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name); return 0; } - return (uint8)PyInt_AsLong(obj); + if (PyLong_Check(obj)) + return (uint8)PyLong_AsLong(obj); + else + return (uint8)PyInt_AsLong(obj); } PyObject *uint8_to_python(uint8 obj) @@ -78,12 +81,15 @@ uint16 uint16_from_python(PyObject *obj, char *name) return 0; } - if (!PyInt_Check(obj)) { - PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name); + if (!PyInt_Check(obj) && !PyLong_Check(obj)) { + PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name); return 0; } - return (uint16)PyInt_AsLong(obj); + if (PyLong_Check(obj)) + return (uint16)PyLong_AsLong(obj); + else + return (uint16)PyInt_AsLong(obj); } PyObject *uint16_to_python(uint16 obj) @@ -98,17 +104,20 @@ uint32 uint32_from_python(PyObject *obj, char *name) return 0; } - if (!PyInt_Check(obj)) { - PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name); + if (!PyLong_Check(obj) && !PyInt_Check(obj)) { + PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name); return 0; } - return (uint32)PyInt_AsLong(obj); + if (PyLong_Check(obj)) + return (uint32)PyLong_AsLong(obj); + else + return (uint32)PyInt_AsLong(obj); } PyObject *uint32_to_python(uint32 obj) { - return PyInt_FromLong(obj); + return PyLong_FromLong(obj); } int64 int64_from_python(PyObject *obj, char *name) @@ -118,12 +127,15 @@ int64 int64_from_python(PyObject *obj, char *name) return 0; } - if (!PyLong_Check(obj)) { - PyErr_Format(PyExc_TypeError, "Expecting long value for %s", name); + if (!PyLong_Check(obj) && !PyInt_Check(obj)) { + PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name); return 0; } - return (int64)PyLong_AsLongLong(obj); + if (PyLong_Check(obj)) + return (int64)PyLong_AsLongLong(obj); + else + return (int64)PyInt_AsLong(obj); } PyObject *int64_to_python(int64 obj) @@ -138,12 +150,15 @@ uint64 uint64_from_python(PyObject *obj, char *name) return 0; } - if (!PyLong_Check(obj)) { - PyErr_Format(PyExc_TypeError, "Expecting long value for %s", name); + if (!PyLong_Check(obj) && !PyInt_Check(obj)) { + PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name); return 0; } - return (uint64)PyLong_AsUnsignedLongLong(obj); + if (PyLong_Check(obj)) + return (uint64)PyLong_AsUnsignedLongLong(obj); + else + return (uint64)PyInt_AsLong(obj); } PyObject *uint64_to_python(uint64 obj) @@ -158,12 +173,15 @@ NTTIME NTTIME_from_python(PyObject *obj, char *name) return 0; } - if (!PyLong_Check(obj)) { - PyErr_Format(PyExc_TypeError, "Expecting long value for %s", name); + if (!PyLong_Check(obj) && !PyInt_Check(obj)) { + PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name); return 0; } - return (NTTIME)PyLong_AsUnsignedLongLong(obj); + if (PyLong_Check(obj)) + return (NTTIME)PyLong_AsUnsignedLongLong(obj); + else + return (NTTIME)PyInt_AsUnsignedLongMask(obj); } PyObject *NTTIME_to_python(NTTIME obj) @@ -178,12 +196,15 @@ HYPER_T HYPER_T_from_python(PyObject *obj, char *name) return 0; } - if (!PyLong_Check(obj)) { - PyErr_Format(PyExc_TypeError, "Expecting long value for %s", name); + if (!PyLong_Check(obj) && !PyInt_Check(obj)) { + PyErr_Format(PyExc_TypeError, "Expecting int or long value for %s", name); return 0; } - return (HYPER_T)PyLong_AsUnsignedLongLong(obj); + if (PyLong_Check(obj)) + return (HYPER_T)PyLong_AsUnsignedLongLong(obj); + else + return (HYPER_T)PyInt_AsUnsignedLongMask(obj); } PyObject *HYPER_T_to_python(HYPER_T obj) diff --git a/source4/scripting/swig/torture/samr.py b/source4/scripting/swig/torture/samr.py index 68b065b677..e462087ca0 100755 --- a/source4/scripting/swig/torture/samr.py +++ b/source4/scripting/swig/torture/samr.py @@ -872,6 +872,16 @@ def test_EnumDomains(pipe, connect_handle): for domain in result['sam']['entries']: test_LookupDomain(pipe, handle, domain['name']['name']) +def test_LongInt(pipe): + + # Check that we can use long values for shorter width types + + r = {} + r['system_name'] = 0L; + r['access_mask'] = 0x02000000L + + result = dcerpc.samr_Connect(pipe, r) + # Parse command line parser = OptionParser() @@ -908,6 +918,8 @@ pipe = dcerpc.pipe_connect(binding, dcerpc.DCERPC_SAMR_UUID, dcerpc.DCERPC_SAMR_VERSION, domain, username, password) +test_LongInt(pipe) + handle = test_Connect(pipe) test_QuerySecurity(pipe, handle) -- cgit