Title: MSTests failing when importing a module with imports · Issue #262 · pythonnet/pythonnet · GitHub
Open Graph Title: MSTests failing when importing a module with imports · Issue #262 · pythonnet/pythonnet
X Title: MSTests failing when importing a module with imports · Issue #262 · pythonnet/pythonnet
Description: Very simple to reproduce: In the Python.EmbeddingTests project (I am using master branch): Add reference to MSTest (Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 10.0.0.0) Convert pyimport from NUnit to MSTest: remove NUnit I...
Open Graph Description: Very simple to reproduce: In the Python.EmbeddingTests project (I am using master branch): Add reference to MSTest (Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 10.0.0.0) Convert pyimp...
X Description: Very simple to reproduce: In the Python.EmbeddingTests project (I am using master branch): Add reference to MSTest (Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 10.0.0.0) Convert pyimp...
Opengraph URL: https://github.com/pythonnet/pythonnet/issues/262
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"MSTests failing when importing a module with imports","articleBody":"Very simple to reproduce:\n\nIn the Python.EmbeddingTests project (I am using master branch):\n- Add reference to MSTest (Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 10.0.0.0)\n- Convert pyimport from NUnit to MSTest:\n - remove NUnit Import, add using Microsoft.VisualStudio.TestTools.UnitTesting\n - replace attributes with MStest attributes (TestClass, TestInitialise, TestCleanup, TestMethod)\n- Running TestDottedName works as expected.\n- Edit one.py and add `import numpy as np` as first line\n- Run TestDottedName should work first time\n- Run TestDottedName again, now it fails with:\n\n```\n\nTest method Python.EmbeddingTest.PyImportTest.TestDottedName threw exception: \nSystem.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.\n```\n\nThis error is also being shown in my ASP.Net Web API application on restart so I hope a solution will solve both MSTest and ASP.Net.\n\nFurther Information: Full version of pyimport.cs after edits is:\n\n```\nusing System;\nusing System.Reflection;\nusing Microsoft.VisualStudio.TestTools.UnitTesting;\nusing Python.Runtime;\n\nnamespace Python.EmbeddingTest\n{\n [TestClass]\n public class PyImportTest\n {\n private IntPtr gs;\n\n [TestInitialize]\n public void SetUp()\n {\n string path = @\"c:\\dev\\libs\\Anaconda3\\envs\\python2;\" + Environment.GetEnvironmentVariable(\"PATH\", EnvironmentVariableTarget.Machine);\n Environment.SetEnvironmentVariable(\"PATH\", path, EnvironmentVariableTarget.Process);\n Environment.SetEnvironmentVariable(\"PYTHONHOME\", @\"c:\\dev\\libs\\Anaconda3\\envs\\python2\", EnvironmentVariableTarget.Process);\n Environment.SetEnvironmentVariable(\"PYTHONPATH \", @\"c:\\dev\\libs\\Anaconda3\\envs\\python2\\Lib\", EnvironmentVariableTarget.Process);\n\n PythonEngine.Initialize();\n gs = PythonEngine.AcquireLock();\n\n //string here = Environment.CurrentDirectory;\n //trunk\\pythonnet\\src\\embed_tests\\bin\\x86\\DebugWin\n\n /*\n * Append the tests directory to sys.path\n * using reflection to circumvent the private modifires placed on most Runtime methods.\n */\n const string s = @\"../../../../tests\";\n\n Type RTClass = typeof(Runtime.Runtime);\n\n /* pyStrPtr = PyString_FromString(s); */\n MethodInfo PyString_FromString = RTClass.GetMethod(\"PyString_FromString\",\n BindingFlags.NonPublic | BindingFlags.Static);\n object[] funcArgs = new object[1];\n funcArgs[0] = s;\n IntPtr pyStrPtr = (IntPtr)PyString_FromString.Invoke(null, funcArgs);\n\n /* SysDotPath = sys.path */\n MethodInfo PySys_GetObject = RTClass.GetMethod(\"PySys_GetObject\",\n BindingFlags.NonPublic | BindingFlags.Static);\n funcArgs[0] = \"path\";\n IntPtr SysDotPath = (IntPtr)PySys_GetObject.Invoke(null, funcArgs);\n\n /* SysDotPath.append(*pyStrPtr) */\n MethodInfo PyList_Append = RTClass.GetMethod(\"PyList_Append\", BindingFlags.NonPublic | BindingFlags.Static);\n funcArgs = new object[] { SysDotPath, pyStrPtr };\n int r = (int)PyList_Append.Invoke(null, funcArgs);\n }\n\n [TestCleanup]\n public void TearDown()\n {\n PythonEngine.ReleaseLock(gs);\n PythonEngine.Shutdown();\n }\n\n [TestMethod]\n public void TestDottedName()\n {\n PyObject module = PythonEngine.ImportModule(\"PyImportTest.test.one\");\n Assert.IsNotNull(module, \"\u003e\u003e\u003e import PyImportTest.test.one # FAILED\");\n }\n }\n}\n```\n","author":{"url":"https://github.com/gatapia","@type":"Person","name":"gatapia"},"datePublished":"2016-09-07T23:12:43.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":12},"url":"https://github.com/262/pythonnet/issues/262"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:da539ba2-59b5-69a5-a035-b50d33d746c1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C778:32FE0E:140C494:1C21D15:6970D117 |
| html-safe-nonce | 15a3e879df877b0600e61c3f62c6d26e38d9599ae572c376e8d6077e387f5ec9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNzc4OjMyRkUwRToxNDBDNDk0OjFDMjFEMTU6Njk3MEQxMTciLCJ2aXNpdG9yX2lkIjoiODQ5MzE1ODg1MzA1NzIzNzI3MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8cbcfa80d26d261af3b174f7270c34f5f743a6217a5b9fb7928714eb1da21108 |
| hovercard-subject-tag | issue:175628495 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/pythonnet/pythonnet/262/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a2f32c8530594686c0b83157071af1e3fdb9a4075a03a5a00962f8e85a103f6a/pythonnet/pythonnet/issues/262 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a2f32c8530594686c0b83157071af1e3fdb9a4075a03a5a00962f8e85a103f6a/pythonnet/pythonnet/issues/262 |
| og:image:alt | Very simple to reproduce: In the Python.EmbeddingTests project (I am using master branch): Add reference to MSTest (Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 10.0.0.0) Convert pyimp... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gatapia |
| hostname | github.com |
| expected-hostname | github.com |
| None | 034aaee9edccd455c18591b13122193c6e12dc773e8a203be73abe934f3e3a72 |
| turbo-cache-control | no-preview |
| go-import | github.com/pythonnet/pythonnet git https://github.com/pythonnet/pythonnet.git |
| octolytics-dimension-user_id | 6050430 |
| octolytics-dimension-user_login | pythonnet |
| octolytics-dimension-repository_id | 14748123 |
| octolytics-dimension-repository_nwo | pythonnet/pythonnet |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 14748123 |
| octolytics-dimension-repository_network_root_nwo | pythonnet/pythonnet |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 52d1d534d0b88c64972e852deb460d09b433b8b2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width