Skip to content

Commit 3012d8c

Browse files
committed
use wchar_t for Py_SetProgramName with Python 3.x, similar to #49
1 parent e6491aa commit 3012d8c

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/PyCall.jl

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,14 @@ function pyinitialize(libpy::Ptr{Void})
244244
libpython::Ptr{Void} = libpy == C_NULL ? ccall(:jl_load_dynamic_library, Ptr{Void}, (Ptr{Uint8},Cuint), C_NULL, 0) : libpy
245245
already_inited = 0 != ccall((@pysym :Py_IsInitialized), Cint, ())
246246
if !already_inited
247-
if !isempty(pyprogramname::ASCIIString)
248-
ccall((@pysym :Py_SetProgramName), Void, (Ptr{Uint8},),
249-
pyprogramname::ASCIIString)
247+
if !isempty(pyprogramname)
248+
if pyversion.major < 3
249+
ccall((@pysym :Py_SetProgramName), Void, (Ptr{Uint8},),
250+
pyprogramname::ASCIIString)
251+
else
252+
ccall((@pysym :Py_SetProgramName), Void, (Ptr{Cwchar_t},),
253+
pyprogramname::Array{Cwchar_t})
254+
end
250255
end
251256
ccall((@pysym :Py_InitializeEx), Void, (Cint,), 0)
252257
end
@@ -367,18 +372,33 @@ function dlopen_libpython(python::String)
367372
end
368373
end
369374

375+
# Python 3.x uses wchar_t arrays for some string arguments
376+
function wbytestring(s::String)
377+
if pyversion.major < 3
378+
bytestring(s)
379+
else
380+
n = length(s)
381+
w = Array(Cwchar_t, n + 1)
382+
for i = 1:n
383+
w[i] = s[i]
384+
end
385+
w[n+1] = 0
386+
w
387+
end
388+
end
389+
370390
# initialize the Python interpreter (no-op on subsequent calls)
371391
function pyinitialize(python::String)
372392
global initialized
373393
if !initialized::Bool
374394
libpy = try
375395
lib = dlopen_libpython(python)
376-
global pyprogramname = bytestring(pysys(python, "executable"))
396+
global pyprogramname = wbytestring(pysys(python, "executable"))
377397
lib
378398
catch
379399
# perhaps we were passed library name and not executable?
380400
lib = dlopen(python, RTLD_LAZY|RTLD_DEEPBIND|RTLD_GLOBAL)
381-
global pyprogramname = bytestring(python)
401+
global pyprogramname = wbytestring(python)
382402
lib
383403
end
384404
pyinitialize(libpy)

0 commit comments

Comments
 (0)