Skip to content

Commit ce0545f

Browse files
committed
Use unicode functions instead of string functions when building for python
3
1 parent 6963b12 commit ce0545f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/monoclr/pynetinit.c

+18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#include "dirent.h"
1616
#endif
1717

18+
#if PY_MAJOR_VERSION > 2
19+
#include <stdlib.h>
20+
#endif
21+
1822
// initialize Mono and PythonNet
1923
PyNet_Args* PyNet_Init(int ext) {
2024
PyNet_Args *pn_args;
@@ -102,11 +106,25 @@ void main_thread_handler (gpointer user_data) {
102106

103107
int ii = 0;
104108
for (ii = 0; ii < PyList_Size(syspath); ++ii) {
109+
#if PY_MAJOR_VERSION > 2
110+
Py_ssize_t wlen;
111+
wchar_t *wstr = PyUnicode_AsWideCharString(PyList_GetItem(syspath, ii), &wlen);
112+
char* pydir = (char*)malloc(wlen + 1);
113+
size_t mblen = wcstombs(pydir, wstr, wlen + 1);
114+
if (mblen > wlen)
115+
pydir[wlen] = '\0';
116+
PyMem_Free(wstr);
117+
#else
105118
const char* pydir = PyString_AsString(PyList_GetItem(syspath, ii));
119+
#endif
106120
char* curdir = (char*) malloc(1024);
107121
strncpy(curdir, strlen(pydir) > 0 ? pydir : ".", 1024);
108122
strncat(curdir, slash, 1024);
109123

124+
#if PY_MAJOR_VERSION > 2
125+
free(pydir);
126+
#endif
127+
110128
//look in this directory for the pn_args->pr_file
111129
DIR* dirp = opendir(curdir);
112130
if (dirp != NULL) {

0 commit comments

Comments
 (0)