Skip to content

Commit

Permalink
Add py 3.12 unicode fixes
Browse files Browse the repository at this point in the history
PyArg_ParseTupleAndKeywords does not support 'u' types starting from
Python 3.12.
These changes use the 's' type and then converts it to the former
required type using a new helper method ToWstring.
  • Loading branch information
petrutlucian94 authored and ader1990 committed Nov 3, 2023
1 parent 3279fcd commit 068164d
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 73 deletions.
32 changes: 16 additions & 16 deletions PyMI/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ static PyObject* Application_new(PyTypeObject *type, PyObject *args, PyObject *k

static int Application_init(Application *self, PyObject *args, PyObject *kwds)
{
wchar_t* appId = L"";
char* appId = "";
static char *kwlist[] = { "app_id", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|u", kwlist, &appId))
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s", kwlist, &appId))
return -1;

try
{
AllowThreads(&self->cs, [&]() {
self->app = std::make_shared<MI::Application>(appId);
self->app = std::make_shared<MI::Application>(ToWstring(appId).c_str());
});
return 0;
}
Expand All @@ -40,12 +40,12 @@ static int Application_init(Application *self, PyObject *args, PyObject *kwds)

static PyObject* Application_NewSession(Application *self, PyObject *args, PyObject *kwds)
{
wchar_t* protocol = L"";
wchar_t* computerName = L".";
char* protocol = "";
char* computerName = ".";
PyObject* destinationOptions = NULL;

static char *kwlist[] = { "protocol", "computer_name", "destination_options", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|uuO", kwlist, &protocol, &computerName, &destinationOptions))
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ssO", kwlist, &protocol, &computerName, &destinationOptions))
return NULL;

try
Expand All @@ -57,7 +57,7 @@ static PyObject* Application_NewSession(Application *self, PyObject *args, PyObj

std::shared_ptr<MI::Session> session;
AllowThreads(&self->cs, [&]() {
session = self->app->NewSession(protocol, computerName,
session = self->app->NewSession(ToWstring(protocol).c_str(), ToWstring(computerName).c_str(),
!CheckPyNone(destinationOptions) ? ((DestinationOptions*)destinationOptions)->destinationOptions : NULL);
});
return (PyObject*)Session_New(session);
Expand All @@ -84,10 +84,10 @@ static void Application_dealloc(Application* self)
static PyObject* Application_NewMethodInboundParameters(Application *self, PyObject *args, PyObject *kwds)
{
PyObject* pyClass = NULL;
wchar_t* methodName = NULL;
char* methodName = NULL;

static char *kwlist[] = { "mi_class", "method_name", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwds, "Ou", kwlist, &pyClass, &methodName))
if (!PyArg_ParseTupleAndKeywords(args, kwds, "Os", kwlist, &pyClass, &methodName))
return NULL;

try
Expand All @@ -97,7 +97,7 @@ static PyObject* Application_NewMethodInboundParameters(Application *self, PyObj

std::shared_ptr<MI::Instance> instance;
AllowThreads(&self->cs, [&]() {
instance = self->app->NewMethodParamsInstance(*((Class*)pyClass)->miClass, methodName);
instance = self->app->NewMethodParamsInstance(*((Class*)pyClass)->miClass, ToWstring(methodName).c_str());
});
return (PyObject*)Instance_New(instance);
}
Expand All @@ -110,16 +110,16 @@ static PyObject* Application_NewMethodInboundParameters(Application *self, PyObj

static PyObject* Application_NewInstance(Application *self, PyObject *args, PyObject *kwds)
{
wchar_t* className = NULL;
char* className = NULL;
static char *kwlist[] = { "class_name", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwds, "u", kwlist, &className))
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &className))
return NULL;

try
{
std::shared_ptr<MI::Instance> instance;
AllowThreads(&self->cs, [&]() {
instance = self->app->NewInstance(className);
instance = self->app->NewInstance(ToWstring(className).c_str());
});
return (PyObject*)Instance_New(instance);
}
Expand All @@ -132,10 +132,10 @@ static PyObject* Application_NewInstance(Application *self, PyObject *args, PyOb

static PyObject* Application_NewInstanceFromClass(Application *self, PyObject *args, PyObject *kwds)
{
wchar_t* className = NULL;
char* className = NULL;
PyObject* miClass = NULL;
static char *kwlist[] = { "class_name", "mi_class", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwds, "uO", kwlist, &className, &miClass))
if (!PyArg_ParseTupleAndKeywords(args, kwds, "sO", kwlist, &className, &miClass))
return NULL;

try
Expand All @@ -145,7 +145,7 @@ static PyObject* Application_NewInstanceFromClass(Application *self, PyObject *a

std::shared_ptr<MI::Instance> instance;
AllowThreads(&self->cs, [&]() {
instance = self->app->NewInstanceFromClass(className, *((Class*)miClass)->miClass);
instance = self->app->NewInstanceFromClass(ToWstring(className).c_str(), *((Class*)miClass)->miClass);
});
return (PyObject*)Instance_New(instance);
}
Expand Down
30 changes: 15 additions & 15 deletions PyMI/DestinationOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ static PyObject* DestinationOptions_GetUILocale(DestinationOptions* self)

static PyObject* DestinationOptions_SetUILocale(DestinationOptions* self, PyObject *args, PyObject *kwds)
{
wchar_t* locale = NULL;
char* locale = NULL;
static char *kwlist[] = { "locale_name", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwds, "u", kwlist, &locale))
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &locale))
return NULL;

try
{
AllowThreads(&self->cs, [&]() {
self->destinationOptions->SetUILocale(locale);
self->destinationOptions->SetUILocale(ToWstring(locale).c_str());
});
Py_RETURN_NONE;
}
Expand Down Expand Up @@ -139,15 +139,15 @@ static PyObject* DestinationOptions_SetTimeout(DestinationOptions* self, PyObjec

static PyObject* DestinationOptions_SetTransport(DestinationOptions* self, PyObject *args, PyObject *kwds)
{
wchar_t* transport = NULL;
char* transport = NULL;
static char *kwlist[] = { "transport", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwds, "u", kwlist, &transport))
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &transport))
return NULL;

try
{
AllowThreads(&self->cs, [&]() {
self->destinationOptions->SetTransport(transport);
self->destinationOptions->SetTransport(ToWstring(transport).c_str());
});
Py_RETURN_NONE;
}
Expand Down Expand Up @@ -178,26 +178,26 @@ static PyObject* DestinationOptions_GetTransport(DestinationOptions* self)

static PyObject* DestinationOptions_AddCredentials(DestinationOptions* self, PyObject *args, PyObject *kwds)
{
wchar_t* authType = NULL;
wchar_t* domain = NULL;
wchar_t* username = NULL;
wchar_t* password = NULL;
wchar_t* certThumbprint = NULL;
char* authType = NULL;
char* domain = NULL;
char* username = NULL;
char* password = NULL;
char* certThumbprint = NULL;

static char *kwlist[] = { "auth_type", "domain", "username", "password", "cert_thumbprint", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwds, "u|uuuu", kwlist,
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|ssss", kwlist,
&authType, &domain, &username, &password, &certThumbprint))
return NULL;

try
{
if (certThumbprint && wcslen(certThumbprint))
if (ToWstring(certThumbprint).c_str() && wcslen(ToWstring(certThumbprint).c_str()))
AllowThreads(&self->cs, [&]() {
self->destinationOptions->AddCredentials(authType, certThumbprint);
self->destinationOptions->AddCredentials(ToWstring(authType).c_str(), ToWstring(certThumbprint).c_str());
});
else
AllowThreads(&self->cs, [&]() {
self->destinationOptions->AddCredentials(authType, domain, username, password);
self->destinationOptions->AddCredentials(ToWstring(authType).c_str(), ToWstring(domain).c_str(), ToWstring(username).c_str(), ToWstring(password).c_str());
});
Py_RETURN_NONE;
}
Expand Down
6 changes: 3 additions & 3 deletions PyMI/OperationOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ static PyObject* OperationOptions_SetTimeout(OperationOptions* self, PyObject* t

static PyObject* OperationOptions_SetCustomOption(OperationOptions* self, PyObject *args, PyObject *kwds)
{
wchar_t* optionName = NULL;
char* optionName = NULL;
unsigned int optionValueType = 0;
PyObject* optionValue = NULL;
PyObject* mustComply = NULL;

static char *kwlist[] = { "name", "value_type", "value", "must_comply", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwds, "uIO|O", kwlist,
if (!PyArg_ParseTupleAndKeywords(args, kwds, "sIO|O", kwlist,
&optionName, &optionValueType,
&optionValue, &mustComply))
return NULL;
Expand All @@ -116,7 +116,7 @@ static PyObject* OperationOptions_SetCustomOption(OperationOptions* self, PyObj
{
auto miValue = Py2MI(optionValue, (MI_Type)optionValueType);
AllowThreads(&self->cs, [&]() {
self->operationOptions->SetCustomOption(optionName, (MI_Type)optionValueType,
self->operationOptions->SetCustomOption(ToWstring(optionName).c_str(), (MI_Type)optionValueType,
*miValue, PyObject_IsTrue(mustComply));
});
Py_RETURN_NONE;
Expand Down
Loading

0 comments on commit 068164d

Please sign in to comment.