Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jul 22, 2023. It is now read-only.
/pythonnetPublic archive
forked frompythonnet/pythonnet

Commit0cfc67a

Browse files
committed
Fix get PYTHONHOME/PYTHONPATH marshal
Unlikepythonnet#413, the return type changes between PY2/PY3.Extended Custom Marshaler to convert IntPtr to Unicode String
1 parente200cae commit0cfc67a

File tree

3 files changed

+51
-27
lines changed

3 files changed

+51
-27
lines changed

‎src/runtime/CustomMarshaler.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,33 @@ public static ICustomMarshaler GetInstance(string cookie)
7272
{
7373
returnInstance;
7474
}
75+
76+
publicstaticstringPtrToStringUni(IntPtrp)
77+
{
78+
if(p==IntPtr.Zero)
79+
{
80+
returnnull;
81+
}
82+
83+
intsize=GetUnicodeByteLength(p);
84+
varbuffer=newbyte[size];
85+
Marshal.Copy(p,buffer,0,size);
86+
returnPyEncoding.GetString(buffer,0,size);
87+
}
88+
89+
publicstaticintGetUnicodeByteLength(IntPtrp)
90+
{
91+
varlen=0;
92+
while(true)
93+
{
94+
intc=Runtime.UCS==2
95+
?Marshal.ReadInt16(p,len*2)
96+
:Marshal.ReadInt32(p,len*4);
97+
98+
if(c==0)returnlen*Runtime.UCS;
99+
checked{++len;}
100+
}
101+
}
75102
}
76103

77104

‎src/runtime/pythonengine.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ public static string ProgramName
5757
{
5858
get
5959
{
60-
stringresult=Runtime.Py_GetProgramName();
61-
if(result==null)
62-
{
63-
return"";
64-
}
65-
returnresult;
60+
IntPtrp=Runtime.Py_GetProgramName();
61+
stringresult=Runtime.IsPython3
62+
?StrMarshaler.PtrToStringUni(p)
63+
:Marshal.PtrToStringAnsi(p);
64+
65+
returnresult??"";
6666
}
6767
set{Runtime.Py_SetProgramName(value);}
6868
}
@@ -71,12 +71,12 @@ public static string PythonHome
7171
{
7272
get
7373
{
74-
stringresult=Runtime.Py_GetPythonHome();
75-
if(result==null)
76-
{
77-
return"";
78-
}
79-
returnresult;
74+
IntPtrp=Runtime.Py_GetPythonHome();
75+
stringresult=Runtime.IsPython3
76+
?StrMarshaler.PtrToStringUni(p)
77+
:Marshal.PtrToStringAnsi(p);
78+
79+
returnresult??"";
8080
}
8181
set{Runtime.Py_SetPythonHome(value);}
8282
}
@@ -85,12 +85,12 @@ public static string PythonPath
8585
{
8686
get
8787
{
88-
stringresult=Runtime.Py_GetPath();
89-
if(result==null)
90-
{
91-
return"";
92-
}
93-
returnresult;
88+
IntPtrp=Runtime.Py_GetPath();
89+
stringresult=Runtime.IsPython3
90+
?StrMarshaler.PtrToStringUni(p)
91+
:Marshal.PtrToStringAnsi(p);
92+
93+
returnresult??"";
9494
}
9595
set{Runtime.Py_SetPath(value);}
9696
}

‎src/runtime/runtime.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -683,46 +683,43 @@ public static extern int Py_Main(
683683

684684
#ifPYTHON3
685685
[DllImport(PythonDll)]
686-
[return:MarshalAs(UnmanagedType.LPWStr)]
687-
internalstaticexternstringPy_GetProgramName();
686+
internalstaticexternIntPtr Py_GetProgramName();
688687

689688
[DllImport(PythonDll)]
690689
internalstaticexternvoidPy_SetProgramName(
691690
[MarshalAs(UnmanagedType.LPWStr)]stringname
692691
);
693692

694693
[DllImport(PythonDll)]
695-
[return:MarshalAs(UnmanagedType.LPWStr)]
696-
internalstaticexternstringPy_GetPythonHome();
694+
internalstaticexternIntPtr Py_GetPythonHome();
697695

698696
[DllImport(PythonDll)]
699697
internalstaticexternvoidPy_SetPythonHome(
700698
[MarshalAs(UnmanagedType.LPWStr)]stringhome
701699
);
702700

703701
[DllImport(PythonDll)]
704-
[return:MarshalAs(UnmanagedType.LPWStr)]
705-
internalstaticexternstringPy_GetPath();
702+
internalstaticexternIntPtr Py_GetPath();
706703

707704
[DllImport(PythonDll)]
708705
internalstaticexternvoidPy_SetPath(
709706
[MarshalAs(UnmanagedType.LPWStr)]stringhome
710707
);
711708
#elifPYTHON2
712709
[DllImport(PythonDll)]
713-
internalstaticexternstringPy_GetProgramName();
710+
internalstaticexternIntPtr Py_GetProgramName();
714711

715712
[DllImport(PythonDll)]
716713
internalstaticexternvoidPy_SetProgramName(stringname);
717714

718715
[DllImport(PythonDll)]
719-
internalstaticexternstringPy_GetPythonHome();
716+
internalstaticexternIntPtr Py_GetPythonHome();
720717

721718
[DllImport(PythonDll)]
722719
internalstaticexternvoidPy_SetPythonHome(stringhome);
723720

724721
[DllImport(PythonDll)]
725-
internalstaticexternstringPy_GetPath();
722+
internalstaticexternIntPtr Py_GetPath();
726723

727724
[DllImport(PythonDll)]
728725
internalstaticexternvoidPy_SetPath(stringhome);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp