Skip to content

Commit

Permalink
Mark PySID.cpp as PY_SSIZE_T_CLEAN and nuke some py2 code
Browse files Browse the repository at this point in the history
  • Loading branch information
mhammond committed May 30, 2021
1 parent 0cd17a9 commit a921128
Showing 1 changed file with 7 additions and 32 deletions.
39 changes: 7 additions & 32 deletions win32/src/PySID.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//
// @doc

#define PY_SSIZE_T_CLEAN
#include "PyWinTypes.h"
#include "PyWinObjects.h"
#include "PySecurityObjects.h"
Expand All @@ -11,9 +12,9 @@
PyObject *PyWinMethod_NewSID(PyObject *self, PyObject *args)
{
void *buf = NULL;
int bufSize = 32; // xxxxxx64 - should be Py_ssize_t - but passed as 'i'
Py_ssize_t bufSize = 32;
// @pyparm int|bufSize|32|Size for the SID buffer
if (!PyArg_ParseTuple(args, "|i:SID", &bufSize)) {
if (!PyArg_ParseTuple(args, "|n:SID", &bufSize)) {
PyErr_Clear();
// @pyparmalt1 string|buffer||A raw data buffer, assumed to hold the SID data.
if (!PyArg_ParseTuple(args, "s#:SID", &buf, &bufSize)) {
Expand Down Expand Up @@ -53,6 +54,10 @@ PyObject *PyWinMethod_NewSID(PyObject *self, PyObject *args)
return PyWin_SetAPIError("AllocateAndInitializeSid");
return new PySID(pNew);
}
}
if (bufSize > INT_MAX) {
PyErr_SetString(PyExc_ValueError, "SID buffer size beyond INT_MAX");
return NULL;
}
return new PySID(bufSize, buf);
}
Expand Down Expand Up @@ -192,34 +197,6 @@ struct PyMethodDef PySID::methods[] = {
// SID_IDENTIFIER_AUTHORITY constants)
{NULL}};

#if (PY_VERSION_HEX < 0x03000000)
/*static*/ Py_ssize_t PySID::getreadbuf(PyObject *self, Py_ssize_t index, void **ptr)
{
if (index != 0) {
PyErr_SetString(PyExc_SystemError, "accessing non-existent SID segment");
return -1;
}
PySID *pysid = (PySID *)self;
*ptr = pysid->m_psid;
return GetLengthSid(pysid->m_psid);
}

/*static*/ Py_ssize_t PySID::getsegcount(PyObject *self, Py_ssize_t *lenp)
{
if (lenp)
*lenp = GetLengthSid(((PySID *)self)->m_psid);
return 1;
}

static PyBufferProcs PySID_as_buffer = {
PySID::getreadbuf,
0,
PySID::getsegcount,
0,
};

#else // New buffer interface in Py3k

/*static*/ int PySID::getbufferinfo(PyObject *self, Py_buffer *view, int flags)
{
PySID *pysid = (PySID *)self;
Expand All @@ -231,8 +208,6 @@ static PyBufferProcs PySID_as_buffer = {
NULL, // Does not have any allocated mem in Py_buffer struct
};

#endif // PY_VERSION_HEX < 0x03000000

PYWINTYPES_EXPORT PyTypeObject PySIDType = {
PYWIN_OBJECT_HEAD "PySID", sizeof(PySID), 0, PySID::deallocFunc, /* tp_dealloc */
0, /* tp_print */
Expand Down

0 comments on commit a921128

Please sign in to comment.