File tree Expand file tree Collapse file tree 3 files changed +51
-27
lines changed Expand file tree Collapse file tree 3 files changed +51
-27
lines changed Original file line number Diff line number Diff line change @@ -72,6 +72,33 @@ public static ICustomMarshaler GetInstance(string cookie)
72
72
{
73
73
return Instance ;
74
74
}
75
+
76
+ public static string PtrToStringUni ( IntPtr p )
77
+ {
78
+ if ( p == IntPtr . Zero )
79
+ {
80
+ return null ;
81
+ }
82
+
83
+ int size = GetUnicodeByteLength ( p ) ;
84
+ var buffer = new byte [ size ] ;
85
+ Marshal . Copy ( p , buffer , 0 , size ) ;
86
+ return PyEncoding . GetString ( buffer , 0 , size ) ;
87
+ }
88
+
89
+ public static int GetUnicodeByteLength ( IntPtr p )
90
+ {
91
+ var len = 0 ;
92
+ while ( true )
93
+ {
94
+ int c = Runtime . UCS == 2
95
+ ? Marshal . ReadInt16 ( p , len * 2 )
96
+ : Marshal . ReadInt32 ( p , len * 4 ) ;
97
+
98
+ if ( c == 0 ) return len * Runtime . UCS ;
99
+ checked { ++ len ; }
100
+ }
101
+ }
75
102
}
76
103
77
104
Original file line number Diff line number Diff line change @@ -57,12 +57,12 @@ public static string ProgramName
57
57
{
58
58
get
59
59
{
60
- string result = Runtime . Py_GetProgramName ( ) ;
61
- if ( result == null )
62
- {
63
- return "" ;
64
- }
65
- return result ;
60
+ IntPtr p = Runtime . Py_GetProgramName ( ) ;
61
+ string result = Runtime . IsPython3
62
+ ? StrMarshaler . PtrToStringUni ( p )
63
+ : Marshal . PtrToStringAnsi ( p ) ;
64
+
65
+ return result ?? "" ;
66
66
}
67
67
set { Runtime . Py_SetProgramName ( value ) ; }
68
68
}
@@ -71,12 +71,12 @@ public static string PythonHome
71
71
{
72
72
get
73
73
{
74
- string result = Runtime . Py_GetPythonHome ( ) ;
75
- if ( result == null )
76
- {
77
- return "" ;
78
- }
79
- return result ;
74
+ IntPtr p = Runtime . Py_GetPythonHome ( ) ;
75
+ string result = Runtime . IsPython3
76
+ ? StrMarshaler . PtrToStringUni ( p )
77
+ : Marshal . PtrToStringAnsi ( p ) ;
78
+
79
+ return result ?? "" ;
80
80
}
81
81
set { Runtime . Py_SetPythonHome ( value ) ; }
82
82
}
@@ -85,12 +85,12 @@ public static string PythonPath
85
85
{
86
86
get
87
87
{
88
- string result = Runtime . Py_GetPath ( ) ;
89
- if ( result == null )
90
- {
91
- return "" ;
92
- }
93
- return result ;
88
+ IntPtr p = Runtime . Py_GetPath ( ) ;
89
+ string result = Runtime . IsPython3
90
+ ? StrMarshaler . PtrToStringUni ( p )
91
+ : Marshal . PtrToStringAnsi ( p ) ;
92
+
93
+ return result ?? "" ;
94
94
}
95
95
set { Runtime . Py_SetPath ( value ) ; }
96
96
}
Original file line number Diff line number Diff line change @@ -683,46 +683,43 @@ public static extern int Py_Main(
683
683
684
684
#if PYTHON3
685
685
[ DllImport ( PythonDll ) ]
686
- [ return : MarshalAs ( UnmanagedType . LPWStr ) ]
687
- internal static extern string Py_GetProgramName ( ) ;
686
+ internal static extern IntPtr Py_GetProgramName( ) ;
688
687
689
688
[ DllImport ( PythonDll ) ]
690
689
internal static extern void Py_SetProgramName (
691
690
[ MarshalAs ( UnmanagedType . LPWStr ) ] string name
692
691
) ;
693
692
694
693
[ DllImport ( PythonDll ) ]
695
- [ return : MarshalAs ( UnmanagedType . LPWStr ) ]
696
- internal static extern string Py_GetPythonHome ( ) ;
694
+ internal static extern IntPtr Py_GetPythonHome( ) ;
697
695
698
696
[ DllImport ( PythonDll ) ]
699
697
internal static extern void Py_SetPythonHome (
700
698
[ MarshalAs ( UnmanagedType . LPWStr ) ] string home
701
699
) ;
702
700
703
701
[ DllImport ( PythonDll ) ]
704
- [ return : MarshalAs ( UnmanagedType . LPWStr ) ]
705
- internal static extern string Py_GetPath ( ) ;
702
+ internal static extern IntPtr Py_GetPath( ) ;
706
703
707
704
[ DllImport ( PythonDll ) ]
708
705
internal static extern void Py_SetPath (
709
706
[ MarshalAs ( UnmanagedType . LPWStr ) ] string home
710
707
) ;
711
708
#elif PYTHON2
712
709
[ DllImport ( PythonDll ) ]
713
- internal static extern string Py_GetProgramName ( ) ;
710
+ internal static extern IntPtr Py_GetProgramName( ) ;
714
711
715
712
[ DllImport ( PythonDll ) ]
716
713
internal static extern void Py_SetProgramName ( string name ) ;
717
714
718
715
[ DllImport ( PythonDll ) ]
719
- internal static extern string Py_GetPythonHome ( ) ;
716
+ internal static extern IntPtr Py_GetPythonHome( ) ;
720
717
721
718
[ DllImport ( PythonDll ) ]
722
719
internal static extern void Py_SetPythonHome ( string home ) ;
723
720
724
721
[ DllImport ( PythonDll ) ]
725
- internal static extern string Py_GetPath ( ) ;
722
+ internal static extern IntPtr Py_GetPath( ) ;
726
723
727
724
[ DllImport ( PythonDll ) ]
728
725
internal static extern void Py_SetPath ( string home ) ;
You can’t perform that action at this time.
0 commit comments