| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -96,9 +96,6 @@ public static void GetProgramNameDefault() | |||
| 96 | 96 | /// Test default behavior of PYTHONHOME. If ENVVAR is set it will | |
| 97 | 97 | /// return the same value. If not, returns EmptyString. | |
| 98 | 98 | /// </summary> | |
| 99 | - /// <remarks> | ||
| 100 | - /// AppVeyor.yml has been update to tests with ENVVAR set. | ||
| 101 | - /// </remarks> | ||
| 102 | 99 | [Test] | |
| 103 | 100 | public static void GetPythonHomeDefault() | |
| 104 | 101 | { | |
@@ -114,22 +111,19 @@ public static void GetPythonHomeDefault() | |||
| 114 | 111 | [Test] | |
| 115 | 112 | public void SetPythonHome() | |
| 116 | 113 | { | |
| 117 | - // We needs to ensure that engine was started and shutdown at least once before setting dummy home. | ||
| 118 | - // Otherwise engine will not run with dummy path with random problem. | ||
| 119 | - if (!PythonEngine.IsInitialized) | ||
| 120 | - { | ||
| 121 | - PythonEngine.Initialize(); | ||
| 122 | - } | ||
| 123 | - | ||
| 114 | + PythonEngine.Initialize(); | ||
| 115 | + var pythonHomeBackup = PythonEngine.PythonHome; | ||
| 124 | 116 | PythonEngine.Shutdown(); | |
| 125 | 117 | ||
| 126 | - var pythonHomeBackup = PythonEngine.PythonHome; | ||
| 118 | + if (pythonHomeBackup == "") | ||
| 119 | + Assert.Inconclusive("Can't reset PythonHome to empty string, skipping"); | ||
| 127 | 120 | ||
| 128 | 121 | var pythonHome = "/dummypath/"; | |
| 129 | 122 | ||
| 130 | 123 | PythonEngine.PythonHome = pythonHome; | |
| 131 | 124 | PythonEngine.Initialize(); | |
| 132 | 125 | ||
| 126 | + Assert.AreEqual(pythonHome, PythonEngine.PythonHome); | ||
| 133 | 127 | PythonEngine.Shutdown(); | |
| 134 | 128 | ||
| 135 | 129 | // Restoring valid pythonhome. | |
@@ -139,15 +133,12 @@ public void SetPythonHome() | |||
| 139 | 133 | [Test] | |
| 140 | 134 | public void SetPythonHomeTwice() | |
| 141 | 135 | { | |
| 142 | - // We needs to ensure that engine was started and shutdown at least once before setting dummy home. | ||
| 143 | - // Otherwise engine will not run with dummy path with random problem. | ||
| 144 | - if (!PythonEngine.IsInitialized) | ||
| 145 | - { | ||
| 146 | - PythonEngine.Initialize(); | ||
| 147 | - } | ||
| 136 | + PythonEngine.Initialize(); | ||
| 137 | + var pythonHomeBackup = PythonEngine.PythonHome; | ||
| 148 | 138 | PythonEngine.Shutdown(); | |
| 149 | 139 | ||
| 150 | - var pythonHomeBackup = PythonEngine.PythonHome; | ||
| 140 | + if (pythonHomeBackup == "") | ||
| 141 | + Assert.Inconclusive("Can't reset PythonHome to empty string, skipping"); | ||
| 151 | 142 | ||
| 152 | 143 | var pythonHome = "/dummypath/"; | |
| 153 | 144 | ||
@@ -161,6 +152,26 @@ public void SetPythonHomeTwice() | |||
| 161 | 152 | PythonEngine.PythonHome = pythonHomeBackup; | |
| 162 | 153 | } | |
| 163 | 154 | ||
| 155 | + [Test] | ||
| 156 | + [Ignore("Currently buggy in Python")] | ||
| 157 | + public void SetPythonHomeEmptyString() | ||
| 158 | + { | ||
| 159 | + PythonEngine.Initialize(); | ||
| 160 | + | ||
| 161 | + var backup = PythonEngine.PythonHome; | ||
| 162 | + if (backup == "") | ||
| 163 | + { | ||
| 164 | + PythonEngine.Shutdown(); | ||
| 165 | + Assert.Inconclusive("Can't reset PythonHome to empty string, skipping"); | ||
| 166 | + } | ||
| 167 | + PythonEngine.PythonHome = ""; | ||
| 168 | + | ||
| 169 | + Assert.AreEqual("", PythonEngine.PythonHome); | ||
| 170 | + | ||
| 171 | + PythonEngine.PythonHome = backup; | ||
| 172 | + PythonEngine.Shutdown(); | ||
| 173 | + } | ||
| 174 | + | ||
| 164 | 175 | [Test] | |
| 165 | 176 | public void SetProgramName() | |
| 166 | 177 | { | |
@@ -207,7 +218,7 @@ public void SetPythonPath() | |||
| 207 | 218 | // The list sys.path is initialized with this value on interpreter startup; | |
| 208 | 219 | // it can be (and usually is) modified later to change the search path for loading modules. | |
| 209 | 220 | // See https://docs.python.org/3/c-api/init.html#c.Py_GetPath | |
| 210 | - // After PythonPath is set, then PythonEngine.PythonPath will correctly return the full search path. | ||
| 221 | + // After PythonPath is set, then PythonEngine.PythonPath will correctly return the full search path. | ||
| 211 | 222 | ||
| 212 | 223 | PythonEngine.Shutdown(); | |
| 213 | 224 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -47,6 +47,14 @@ public static bool IsInitialized | |||
| 47 | 47 | get { return initialized; } | |
| 48 | 48 | } | |
| 49 | 49 | ||
| 50 | + private static void EnsureInitialized() | ||
| 51 | + { | ||
| 52 | + if (!IsInitialized) | ||
| 53 | + throw new InvalidOperationException( | ||
| 54 | + "Python must be initialized for this operation" | ||
| 55 | + ); | ||
| 56 | + } | ||
| 57 | + | ||
| 50 | 58 | /// <summary>Set to <c>true</c> to enable GIL debugging assistance.</summary> | |
| 51 | 59 | public static bool DebugGIL { get; set; } = false; | |
| 52 | 60 | ||
@@ -96,17 +104,16 @@ public static string PythonHome | |||
| 96 | 104 | { | |
| 97 | 105 | get | |
| 98 | 106 | { | |
| 107 | + EnsureInitialized(); | ||
| 99 | 108 | IntPtr p = Runtime.TryUsingDll(() => Runtime.Py_GetPythonHome()); | |
| 100 | 109 | return UcsMarshaler.PtrToPy3UnicodePy2String(p) ?? ""; | |
| 101 | 110 | } | |
| 102 | 111 | set | |
| 103 | 112 | { | |
| 104 | 113 | // this value is null in the beginning | |
| 105 | 114 | Marshal.FreeHGlobal(_pythonHome); | |
| 106 | - _pythonHome = Runtime.TryUsingDll( | ||
| 107 | - () => UcsMarshaler.Py3UnicodePy2StringtoPtr(value) | ||
| 108 | - ); | ||
| 109 | - Runtime.Py_SetPythonHome(_pythonHome); | ||
| 115 | + _pythonHome = UcsMarshaler.Py3UnicodePy2StringtoPtr(value); | ||
| 116 | + Runtime.TryUsingDll(() => Runtime.Py_SetPythonHome(_pythonHome)); | ||
| 110 | 117 | } | |
| 111 | 118 | } | |
| 112 | 119 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments