| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 331524f commit a0849a9
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,42 @@ namespace Python.EmbeddingTest | |||
| 9 | 9 | { | |
| 10 | 10 | public class InitializeTest | |
| 11 | 11 | { | |
| 12 | + [Test] | ||
| 13 | + public static void LoadSpecificArgs() | ||
| 14 | + { | ||
| 15 | + var args = new[] { "test1", "test2" }; | ||
| 16 | + PythonEngine.Initialize(args); | ||
| 17 | + try | ||
| 18 | + { | ||
| 19 | + using (var argv = new PyList(Runtime.Runtime.PySys_GetObject("argv"))) | ||
| 20 | + { | ||
| 21 | + Assert.That(argv[0].ToString() == args[0]); | ||
| 22 | + Assert.That(argv[1].ToString() == args[1]); | ||
| 23 | + } | ||
| 24 | + } | ||
| 25 | + finally | ||
| 26 | + { | ||
| 27 | + PythonEngine.Shutdown(); | ||
| 28 | + } | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + [Test] | ||
| 32 | + public static void LoadDefaultArgs() | ||
| 33 | + { | ||
| 34 | + PythonEngine.Initialize(); | ||
| 35 | + try | ||
| 36 | + { | ||
| 37 | + using (var argv = new PyList(Runtime.Runtime.PySys_GetObject("argv"))) | ||
| 38 | + { | ||
| 39 | + Assert.That(argv.Length() != 0); | ||
| 40 | + } | ||
| 41 | + } | ||
| 42 | + finally | ||
| 43 | + { | ||
| 44 | + PythonEngine.Shutdown(); | ||
| 45 | + } | ||
| 46 | + } | ||
| 47 | + | ||
| 12 | 48 | [Test] | |
| 13 | 49 | public static void Test() | |
| 14 | 50 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,8 @@ | |||
| 2 | 2 | using System.IO; | |
| 3 | 3 | using System.Threading; | |
| 4 | 4 | using System.Reflection; | |
| 5 | + using System.Collections.Generic; | ||
| 6 | + using System.Linq; | ||
| 5 | 7 | ||
| 6 | 8 | namespace Python.Runtime | |
| 7 | 9 | { | |
@@ -102,6 +104,11 @@ public static int RunSimpleString(string code) | |||
| 102 | 104 | ||
| 103 | 105 | #endregion | |
| 104 | 106 | ||
| 107 | + public static void Initialize() | ||
| 108 | + { | ||
| 109 | + Initialize(Enumerable.Empty<string>()); | ||
| 110 | + } | ||
| 111 | + | ||
| 105 | 112 | /// <summary> | |
| 106 | 113 | /// Initialize Method | |
| 107 | 114 | /// </summary> | |
@@ -112,7 +119,7 @@ public static int RunSimpleString(string code) | |||
| 112 | 119 | /// first call. It is *not* necessary to hold the Python global | |
| 113 | 120 | /// interpreter lock (GIL) to call this method. | |
| 114 | 121 | /// </remarks> | |
| 115 | - public static void Initialize() | ||
| 122 | + public static void Initialize(IEnumerable<string> args) | ||
| 116 | 123 | { | |
| 117 | 124 | if (!initialized) | |
| 118 | 125 | { | |
@@ -126,6 +133,9 @@ public static void Initialize() | |||
| 126 | 133 | initialized = true; | |
| 127 | 134 | Exceptions.Clear(); | |
| 128 | 135 | ||
| 136 | + Py.SetArgv(args); | ||
| 137 | + Py.Throw(); | ||
| 138 | + | ||
| 129 | 139 | // register the atexit callback (this doesn't use Py_AtExit as the C atexit | |
| 130 | 140 | // callbacks are called after python is fully finalized but the python ones | |
| 131 | 141 | // are called while the python engine is still running). | |
@@ -552,9 +562,12 @@ public static void SetArgv(IEnumerable<string> argv) | |||
| 552 | 562 | ||
| 553 | 563 | internal static void Throw() | |
| 554 | 564 | { | |
| 555 | - if (Runtime.PyErr_Occurred() != 0) | ||
| 565 | + using (GIL()) | ||
| 556 | 566 | { | |
| 557 | - throw new PythonException(); | ||
| 567 | + if (Runtime.PyErr_Occurred() != 0) | ||
| 568 | + { | ||
| 569 | + throw new PythonException(); | ||
| 570 | + } | ||
| 558 | 571 | } | |
| 559 | 572 | } | |
| 560 | 573 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments