Skip to content

Commit c6a9502

Browse files
petrutlucian94alexpilotti
authored andcommitted
Add py 3.12 unicode fixes
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.
1 parent 9ff1121 commit c6a9502

6 files changed

Lines changed: 102 additions & 73 deletions

File tree

PyMI/Application.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ static PyObject* Application_new(PyTypeObject *type, PyObject *args, PyObject *k
1919

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

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

4141
static PyObject* Application_NewSession(Application *self, PyObject *args, PyObject *kwds)
4242
{
43-
wchar_t* protocol = L"";
44-
wchar_t* computerName = L".";
43+
char* protocol = "";
44+
char* computerName = ".";
4545
PyObject* destinationOptions = NULL;
4646

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

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

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

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

9393
try
@@ -97,7 +97,7 @@ static PyObject* Application_NewMethodInboundParameters(Application *self, PyObj
9797

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

111111
static PyObject* Application_NewInstance(Application *self, PyObject *args, PyObject *kwds)
112112
{
113-
wchar_t* className = NULL;
113+
char* className = NULL;
114114
static char *kwlist[] = { "class_name", NULL };
115-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "u", kwlist, &className))
115+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &className))
116116
return NULL;
117117

118118
try
119119
{
120120
std::shared_ptr<MI::Instance> instance;
121121
AllowThreads(&self->cs, [&]() {
122-
instance = self->app->NewInstance(className);
122+
instance = self->app->NewInstance(ToWstring(className).c_str());
123123
});
124124
return (PyObject*)Instance_New(instance);
125125
}
@@ -132,10 +132,10 @@ static PyObject* Application_NewInstance(Application *self, PyObject *args, PyOb
132132

133133
static PyObject* Application_NewInstanceFromClass(Application *self, PyObject *args, PyObject *kwds)
134134
{
135-
wchar_t* className = NULL;
135+
char* className = NULL;
136136
PyObject* miClass = NULL;
137137
static char *kwlist[] = { "class_name", "mi_class", NULL };
138-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "uO", kwlist, &className, &miClass))
138+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "sO", kwlist, &className, &miClass))
139139
return NULL;
140140

141141
try
@@ -145,7 +145,7 @@ static PyObject* Application_NewInstanceFromClass(Application *self, PyObject *a
145145

146146
std::shared_ptr<MI::Instance> instance;
147147
AllowThreads(&self->cs, [&]() {
148-
instance = self->app->NewInstanceFromClass(className, *((Class*)miClass)->miClass);
148+
instance = self->app->NewInstanceFromClass(ToWstring(className).c_str(), *((Class*)miClass)->miClass);
149149
});
150150
return (PyObject*)Instance_New(instance);
151151
}

PyMI/DestinationOptions.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ static PyObject* DestinationOptions_GetUILocale(DestinationOptions* self)
7575

7676
static PyObject* DestinationOptions_SetUILocale(DestinationOptions* self, PyObject *args, PyObject *kwds)
7777
{
78-
wchar_t* locale = NULL;
78+
char* locale = NULL;
7979
static char *kwlist[] = { "locale_name", NULL };
80-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "u", kwlist, &locale))
80+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &locale))
8181
return NULL;
8282

8383
try
8484
{
8585
AllowThreads(&self->cs, [&]() {
86-
self->destinationOptions->SetUILocale(locale);
86+
self->destinationOptions->SetUILocale(ToWstring(locale).c_str());
8787
});
8888
Py_RETURN_NONE;
8989
}
@@ -139,15 +139,15 @@ static PyObject* DestinationOptions_SetTimeout(DestinationOptions* self, PyObjec
139139

140140
static PyObject* DestinationOptions_SetTransport(DestinationOptions* self, PyObject *args, PyObject *kwds)
141141
{
142-
wchar_t* transport = NULL;
142+
char* transport = NULL;
143143
static char *kwlist[] = { "transport", NULL };
144-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "u", kwlist, &transport))
144+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &transport))
145145
return NULL;
146146

147147
try
148148
{
149149
AllowThreads(&self->cs, [&]() {
150-
self->destinationOptions->SetTransport(transport);
150+
self->destinationOptions->SetTransport(ToWstring(transport).c_str());
151151
});
152152
Py_RETURN_NONE;
153153
}
@@ -178,26 +178,26 @@ static PyObject* DestinationOptions_GetTransport(DestinationOptions* self)
178178

179179
static PyObject* DestinationOptions_AddCredentials(DestinationOptions* self, PyObject *args, PyObject *kwds)
180180
{
181-
wchar_t* authType = NULL;
182-
wchar_t* domain = NULL;
183-
wchar_t* username = NULL;
184-
wchar_t* password = NULL;
185-
wchar_t* certThumbprint = NULL;
181+
char* authType = NULL;
182+
char* domain = NULL;
183+
char* username = NULL;
184+
char* password = NULL;
185+
char* certThumbprint = NULL;
186186

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

192192
try
193193
{
194-
if (certThumbprint && wcslen(certThumbprint))
194+
if (ToWstring(certThumbprint).c_str() && wcslen(ToWstring(certThumbprint).c_str()))
195195
AllowThreads(&self->cs, [&]() {
196-
self->destinationOptions->AddCredentials(authType, certThumbprint);
196+
self->destinationOptions->AddCredentials(ToWstring(authType).c_str(), ToWstring(certThumbprint).c_str());
197197
});
198198
else
199199
AllowThreads(&self->cs, [&]() {
200-
self->destinationOptions->AddCredentials(authType, domain, username, password);
200+
self->destinationOptions->AddCredentials(ToWstring(authType).c_str(), ToWstring(domain).c_str(), ToWstring(username).c_str(), ToWstring(password).c_str());
201201
});
202202
Py_RETURN_NONE;
203203
}

PyMI/OperationOptions.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ static PyObject* OperationOptions_SetTimeout(OperationOptions* self, PyObject* t
101101

102102
static PyObject* OperationOptions_SetCustomOption(OperationOptions* self, PyObject *args, PyObject *kwds)
103103
{
104-
wchar_t* optionName = NULL;
104+
char* optionName = NULL;
105105
unsigned int optionValueType = 0;
106106
PyObject* optionValue = NULL;
107107
PyObject* mustComply = NULL;
108108

109109
static char *kwlist[] = { "name", "value_type", "value", "must_comply", NULL };
110-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "uIO|O", kwlist,
110+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "sIO|O", kwlist,
111111
&optionName, &optionValueType,
112112
&optionValue, &mustComply))
113113
return NULL;
@@ -116,7 +116,7 @@ static PyObject* OperationOptions_SetCustomOption(OperationOptions* self, PyObj
116116
{
117117
auto miValue = Py2MI(optionValue, (MI_Type)optionValueType);
118118
AllowThreads(&self->cs, [&]() {
119-
self->operationOptions->SetCustomOption(optionName, (MI_Type)optionValueType,
119+
self->operationOptions->SetCustomOption(ToWstring(optionName).c_str(), (MI_Type)optionValueType,
120120
*miValue, PyObject_IsTrue(mustComply));
121121
});
122122
Py_RETURN_NONE;

0 commit comments

Comments
 (0)