| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b4ed645 commit 8ff338a
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,11 +33,6 @@ public static void LoadDefaultArgs() | |||
| 33 | 33 | [Test] | |
| 34 | 34 | public static void LoadSpecificArgs() | |
| 35 | 35 | { | |
| 36 | - if (Environment.GetEnvironmentVariable("TRAVIS") == "true" && | ||
| 37 | - Environment.GetEnvironmentVariable("TRAVIS_PYTHON_VERSION") != "2.7") | ||
| 38 | - { | ||
| 39 | - Assert.Ignore("FIXME: Fails on Travis/PY3+: Fatal Python error: no mem for sys.argv"); | ||
| 40 | - } | ||
| 41 | 36 | var args = new[] { "test1", "test2" }; | |
| 42 | 37 | using (new PythonEngine(args)) | |
| 43 | 38 | using (var argv = new PyList(Runtime.Runtime.PySys_GetObject("argv"))) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -667,8 +667,41 @@ internal unsafe static extern IntPtr | |||
| 667 | 667 | public unsafe static extern int | |
| 668 | 668 | Py_Main( | |
| 669 | 669 | int argc, | |
| 670 | - [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr)] string[] argv | ||
| 670 | + [MarshalAs(UnmanagedType.SysUInt)] IntPtr lplpargv | ||
| 671 | 671 | ); | |
| 672 | + | ||
| 673 | + public static int Py_Main(int argc, string[] argv) | ||
| 674 | + { | ||
| 675 | + // Totally ignoring argc. | ||
| 676 | + argc = argv.Length; | ||
| 677 | + | ||
| 678 | + var allStringsLength = 0; | ||
| 679 | + foreach (string x in argv) | ||
| 680 | + { | ||
| 681 | + allStringsLength += x.Length + 1; | ||
| 682 | + } | ||
| 683 | + int requiredSize = IntPtr.Size * argc + allStringsLength * UCS; | ||
| 684 | + IntPtr mem = Marshal.AllocHGlobal(requiredSize); | ||
| 685 | + try | ||
| 686 | + { | ||
| 687 | + // Preparing array of pointers to UTF32 strings. | ||
| 688 | + IntPtr curStrPtr = mem + argc * IntPtr.Size; | ||
| 689 | + for (var i = 0; i < argv.Length; i++) | ||
| 690 | + { | ||
| 691 | + // Unicode or UTF8 work | ||
| 692 | + Encoding enc = UCS == 2 ? Encoding.Unicode : Encoding.UTF32; | ||
| 693 | + byte[] zstr = enc.GetBytes(argv[i] + "\0"); | ||
| 694 | + Marshal.Copy(zstr, 0, curStrPtr, zstr.Length); | ||
| 695 | + Marshal.WriteIntPtr(mem + IntPtr.Size * i, curStrPtr); | ||
| 696 | + curStrPtr += zstr.Length; | ||
| 697 | + } | ||
| 698 | + return Py_Main(argc, mem); | ||
| 699 | + } | ||
| 700 | + finally | ||
| 701 | + { | ||
| 702 | + Marshal.FreeHGlobal(mem); | ||
| 703 | + } | ||
| 704 | + } | ||
| 672 | 705 | #elif PYTHON2 | |
| 673 | 706 | [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, | |
| 674 | 707 | ExactSpelling = true, CharSet = CharSet.Ansi)] | |
@@ -2087,9 +2120,41 @@ internal unsafe static extern IntPtr | |||
| 2087 | 2120 | internal unsafe static extern void | |
| 2088 | 2121 | PySys_SetArgvEx( | |
| 2089 | 2122 | int argc, | |
| 2090 | - [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr)] string[] argv, | ||
| 2123 | + [MarshalAs(UnmanagedType.SysUInt)] IntPtr lplpargv, | ||
| 2091 | 2124 | int updatepath | |
| 2092 | 2125 | ); | |
| 2126 | + | ||
| 2127 | + internal static void PySys_SetArgvEx(int argc, string[] argv, int updatepath) | ||
| 2128 | + { | ||
| 2129 | + // Totally ignoring argc. | ||
| 2130 | + argc = argv.Length; | ||
| 2131 | + | ||
| 2132 | + var allStringsLength = 0; | ||
| 2133 | + foreach (string x in argv) | ||
| 2134 | + { | ||
| 2135 | + allStringsLength += x.Length + 1; | ||
| 2136 | + } | ||
| 2137 | + int requiredSize = IntPtr.Size * argc + allStringsLength * UCS; | ||
| 2138 | + IntPtr mem = Marshal.AllocHGlobal(requiredSize); | ||
| 2139 | + try | ||
| 2140 | + { | ||
| 2141 | + // Preparing array of pointers to UTF32 strings. | ||
| 2142 | + IntPtr curStrPtr = mem + argc * IntPtr.Size; | ||
| 2143 | + for (var i = 0; i < argv.Length; i++) | ||
| 2144 | + { | ||
| 2145 | + Encoding enc = UCS == 2 ? Encoding.Unicode : Encoding.UTF32; | ||
| 2146 | + byte[] zstr = enc.GetBytes(argv[i] + "\0"); | ||
| 2147 | + Marshal.Copy(zstr, 0, curStrPtr, zstr.Length); | ||
| 2148 | + Marshal.WriteIntPtr(mem + IntPtr.Size * i, curStrPtr); | ||
| 2149 | + curStrPtr += zstr.Length; | ||
| 2150 | + } | ||
| 2151 | + PySys_SetArgvEx(argc, mem, updatepath); | ||
| 2152 | + } | ||
| 2153 | + finally | ||
| 2154 | + { | ||
| 2155 | + Marshal.FreeHGlobal(mem); | ||
| 2156 | + } | ||
| 2157 | + } | ||
| 2093 | 2158 | #elif PYTHON2 | |
| 2094 | 2159 | [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, | |
| 2095 | 2160 | ExactSpelling = true, CharSet = CharSet.Ansi)] | |
| Back | FazBrowse Home | New Git URL |
0 commit comments