| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,6 +20,7 @@ | |||
| 20 | 20 | - Alexandre Catarino([@AlexCatarino](https://github.com/AlexCatarino)) | |
| 21 | 21 | - Andrey Sant'Anna ([@andreydani](https://github.com/andreydani)) | |
| 22 | 22 | - Arvid JB ([@ArvidJB](https://github.com/ArvidJB)) | |
| 23 | + - Avinash Maddikonda ([@SFM61319](https://github.com/SFM61319)) | ||
| 23 | 24 | - Benoît Hudson ([@benoithudson](https://github.com/benoithudson)) | |
| 24 | 25 | - Bradley Friedman ([@leith-bartrich](https://github.com/leith-bartrich)) | |
| 25 | 26 | - Callum Noble ([@callumnoble](https://github.com/callumnoble)) | |
@@ -73,7 +74,7 @@ | |||
| 73 | 74 | - ([@OneBlue](https://github.com/OneBlue)) | |
| 74 | 75 | - ([@rico-chet](https://github.com/rico-chet)) | |
| 75 | 76 | - ([@rmadsen-ks](https://github.com/rmadsen-ks)) | |
| 77 | + - ([@SnGmng](https://github.com/SnGmng)) | ||
| 76 | 78 | - ([@stonebig](https://github.com/stonebig)) | |
| 77 | 79 | - ([@testrunner123](https://github.com/testrunner123)) | |
| 78 | 80 | - ([@DanBarzilian](https://github.com/DanBarzilian)) | |
| 79 | - | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,6 +11,9 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. | |||
| 11 | 11 | ||
| 12 | 12 | ### Changed | |
| 13 | 13 | - Drop support for Python 2 | |
| 14 | + - `clr.AddReference` may now throw errors besides `FileNotFoundException`, that provide more | ||
| 15 | + details about the cause of the failure | ||
| 16 | + - `clr.AddReference` no longer adds ".dll" implicitly | ||
| 14 | 17 | ||
| 15 | 18 | ### Fixed | |
| 16 | 19 | ||
@@ -35,6 +38,7 @@ This version improves performance on benchmarks significantly compared to 2.3. | |||
| 35 | 38 | - Support for Python 3.8 | |
| 36 | 39 | - Codecs as the designated way to handle automatic conversions between | |
| 37 | 40 | .NET and Python types | |
| 41 | + - Added Python 3 buffer api support and PyBuffer interface for fast byte and numpy array read/write ([#980][p980]) | ||
| 38 | 42 | ||
| 39 | 43 | ### Changed | |
| 40 | 44 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,12 +14,17 @@ provides a powerful application scripting tool for .NET developers. It | |||
| 14 | 14 | allows Python code to interact with the CLR, and may also be used to | |
| 15 | 15 | embed Python into a .NET application. | |
| 16 | 16 | ||
| 17 | + .. note:: | ||
| 18 | + The master branch of this repository tracks the ongoing development of version 3.0. | ||
| 19 | + Backports of patches to 2.5 are tracked in the | ||
| 20 | + `backports-2.5 branch <https://github.com/pythonnet/pythonnet/tree/backports-2.5>`_. | ||
| 21 | + | ||
| 17 | 22 | Calling .NET code from Python | |
| 18 | 23 | ----------------------------- | |
| 19 | 24 | ||
| 20 | 25 | Python.NET allows CLR namespaces to be treated essentially as Python packages. | |
| 21 | 26 | ||
| 22 | - .. code-block:: | ||
| 27 | + .. code-block:: python | ||
| 23 | 28 | ||
| 24 | 29 | import clr | |
| 25 | 30 | from System import String | |
@@ -28,7 +33,7 @@ Python.NET allows CLR namespaces to be treated essentially as Python packages. | |||
| 28 | 33 | To load an assembly, use the ``AddReference`` function in the ``clr`` | |
| 29 | 34 | module: | |
| 30 | 35 | ||
| 31 | - .. code-block:: | ||
| 36 | + .. code-block:: python | ||
| 32 | 37 | ||
| 33 | 38 | import clr | |
| 34 | 39 | clr.AddReference("System.Windows.Forms") | |
@@ -80,7 +85,7 @@ Example | |||
| 80 | 85 | ||
| 81 | 86 | Output: | |
| 82 | 87 | ||
| 83 | - .. code:: | ||
| 88 | + .. code:: csharp | ||
| 84 | 89 | ||
| 85 | 90 | 1.0 | |
| 86 | 91 | -0.958924274663 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -42,6 +42,7 @@ init: | |||
| 42 | 42 | install: | |
| 43 | 43 | - python -m pip install -U pip | |
| 44 | 44 | - pip install --upgrade -r requirements.txt --quiet | |
| 45 | + - pip install pycparser --quiet | ||
| 45 | 46 | ||
| 46 | 47 | # Install OpenCover. Can't put on `packages.config`, not Mono compatible | |
| 47 | 48 | - .\tools\nuget\nuget.exe install OpenCover -OutputDirectory packages -Verbosity quiet | |
@@ -53,7 +54,7 @@ build_script: | |||
| 53 | 54 | - coverage run setup.py bdist_wheel %BUILD_OPTS% | |
| 54 | 55 | ||
| 55 | 56 | test_script: | |
| 56 | - - pip install --find-links=.\dist\ pythonnet | ||
| 57 | + - pip install --no-index --find-links=.\dist\ pythonnet | ||
| 57 | 58 | - ps: .\ci\appveyor_run_tests.ps1 | |
| 58 | 59 | ||
| 59 | 60 | on_finish: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -97,6 +97,7 @@ | |||
| 97 | 97 | <Compile Include="TestFinalizer.cs" /> | |
| 98 | 98 | <Compile Include="TestInstanceWrapping.cs" /> | |
| 99 | 99 | <Compile Include="TestPyAnsiString.cs" /> | |
| 100 | + <Compile Include="TestPyBuffer.cs" /> | ||
| 100 | 101 | <Compile Include="TestPyFloat.cs" /> | |
| 101 | 102 | <Compile Include="TestPyInt.cs" /> | |
| 102 | 103 | <Compile Include="TestPyList.cs" /> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,12 +1,27 @@ | |||
| 1 | 1 | using System; | |
| 2 | 2 | using System.Collections.Generic; | |
| 3 | + using System.Linq; | ||
| 4 | + | ||
| 3 | 5 | using NUnit.Framework; | |
| 6 | + | ||
| 4 | 7 | using Python.Runtime; | |
| 5 | 8 | ||
| 9 | + using PyRuntime = Python.Runtime.Runtime; | ||
| 10 | + | ||
| 6 | 11 | namespace Python.EmbeddingTest | |
| 7 | 12 | { | |
| 8 | 13 | public class TestConverter | |
| 9 | 14 | { | |
| 15 | + static readonly Type[] _numTypes = new Type[] | ||
| 16 | + { | ||
| 17 | + typeof(short), | ||
| 18 | + typeof(ushort), | ||
| 19 | + typeof(int), | ||
| 20 | + typeof(uint), | ||
| 21 | + typeof(long), | ||
| 22 | + typeof(ulong) | ||
| 23 | + }; | ||
| 24 | + | ||
| 10 | 25 | [OneTimeSetUp] | |
| 11 | 26 | public void SetUp() | |
| 12 | 27 | { | |
@@ -47,6 +62,60 @@ public void TestConvertDoubleToManaged( | |||
| 47 | 62 | Assert.IsTrue(((double) convertedValue).Equals(testValue)); | |
| 48 | 63 | } | |
| 49 | 64 | ||
| 65 | + [Test] | ||
| 66 | + public void CovertTypeError() | ||
| 67 | + { | ||
| 68 | + Type[] floatTypes = new Type[] | ||
| 69 | + { | ||
| 70 | + typeof(float), | ||
| 71 | + typeof(double) | ||
| 72 | + }; | ||
| 73 | + using (var s = new PyString("abc")) | ||
| 74 | + { | ||
| 75 | + foreach (var type in _numTypes.Union(floatTypes)) | ||
| 76 | + { | ||
| 77 | + object value; | ||
| 78 | + try | ||
| 79 | + { | ||
| 80 | + bool res = Converter.ToManaged(s.Handle, type, out value, true); | ||
| 81 | + Assert.IsFalse(res); | ||
| 82 | + var bo = Exceptions.ExceptionMatches(Exceptions.TypeError); | ||
| 83 | + Assert.IsTrue(Exceptions.ExceptionMatches(Exceptions.TypeError) | ||
| 84 | + || Exceptions.ExceptionMatches(Exceptions.ValueError)); | ||
| 85 | + } | ||
| 86 | + finally | ||
| 87 | + { | ||
| 88 | + Exceptions.Clear(); | ||
| 89 | + } | ||
| 90 | + } | ||
| 91 | + } | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + [Test] | ||
| 95 | + public void ConvertOverflow() | ||
| 96 | + { | ||
| 97 | + using (var num = new PyLong(ulong.MaxValue)) | ||
| 98 | + { | ||
| 99 | + IntPtr largeNum = PyRuntime.PyNumber_Add(num.Handle, num.Handle); | ||
| 100 | + try | ||
| 101 | + { | ||
| 102 | + object value; | ||
| 103 | + foreach (var type in _numTypes) | ||
| 104 | + { | ||
| 105 | + bool res = Converter.ToManaged(largeNum, type, out value, true); | ||
| 106 | + Assert.IsFalse(res); | ||
| 107 | + Assert.IsTrue(Exceptions.ExceptionMatches(Exceptions.OverflowError)); | ||
| 108 | + Exceptions.Clear(); | ||
| 109 | + } | ||
| 110 | + } | ||
| 111 | + finally | ||
| 112 | + { | ||
| 113 | + Exceptions.Clear(); | ||
| 114 | + PyRuntime.XDecref(largeNum); | ||
| 115 | + } | ||
| 116 | + } | ||
| 117 | + } | ||
| 118 | + | ||
| 50 | 119 | [Test] | |
| 51 | 120 | public void RawListProxy() | |
| 52 | 121 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,68 @@ | |||
| 1 | + using System.Text; | ||
| 2 | + using NUnit.Framework; | ||
| 3 | + using Python.Runtime; | ||
| 4 | + | ||
| 5 | + namespace Python.EmbeddingTest { | ||
| 6 | + class TestPyBuffer | ||
| 7 | + { | ||
| 8 | + [OneTimeSetUp] | ||
| 9 | + public void SetUp() | ||
| 10 | + { | ||
| 11 | + PythonEngine.Initialize(); | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + [OneTimeTearDown] | ||
| 15 | + public void Dispose() | ||
| 16 | + { | ||
| 17 | + PythonEngine.Shutdown(); | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + [Test] | ||
| 21 | + public void TestBufferWrite() | ||
| 22 | + { | ||
| 23 | + string bufferTestString = "hello world! !$%&/()=?"; | ||
| 24 | + | ||
| 25 | + using (Py.GIL()) | ||
| 26 | + { | ||
| 27 | + using (var scope = Py.CreateScope()) | ||
| 28 | + { | ||
| 29 | + scope.Exec($"arr = bytearray({bufferTestString.Length})"); | ||
| 30 | + PyObject pythonArray = scope.Get("arr"); | ||
| 31 | + byte[] managedArray = new UTF8Encoding().GetBytes(bufferTestString); | ||
| 32 | + | ||
| 33 | + using (PyBuffer buf = pythonArray.GetBuffer()) | ||
| 34 | + { | ||
| 35 | + buf.Write(managedArray, 0, managedArray.Length); | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + string result = scope.Eval("arr.decode('utf-8')").ToString(); | ||
| 39 | + Assert.IsTrue(result == bufferTestString); | ||
| 40 | + } | ||
| 41 | + } | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + [Test] | ||
| 45 | + public void TestBufferRead() | ||
| 46 | + { | ||
| 47 | + string bufferTestString = "hello world! !$%&/()=?"; | ||
| 48 | + | ||
| 49 | + using (Py.GIL()) | ||
| 50 | + { | ||
| 51 | + using (var scope = Py.CreateScope()) | ||
| 52 | + { | ||
| 53 | + scope.Exec($"arr = b'{bufferTestString}'"); | ||
| 54 | + PyObject pythonArray = scope.Get("arr"); | ||
| 55 | + byte[] managedArray = new byte[bufferTestString.Length]; | ||
| 56 | + | ||
| 57 | + using (PyBuffer buf = pythonArray.GetBuffer()) | ||
| 58 | + { | ||
| 59 | + buf.Read(managedArray, 0, managedArray.Length); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + string result = new UTF8Encoding().GetString(managedArray); | ||
| 63 | + Assert.IsTrue(result == bufferTestString); | ||
| 64 | + } | ||
| 65 | + } | ||
| 66 | + } | ||
| 67 | + } | ||
| 68 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -38,8 +38,8 @@ public void SetUp() | |||
| 38 | 38 | string testPath = Path.Combine(TestContext.CurrentContext.TestDirectory, s); | |
| 39 | 39 | ||
| 40 | 40 | IntPtr str = Runtime.Runtime.PyString_FromString(testPath); | |
| 41 | - IntPtr path = Runtime.Runtime.PySys_GetObject("path"); | ||
| 42 | - Runtime.Runtime.PyList_Append(new BorrowedReference(path), str); | ||
| 41 | + BorrowedReference path = Runtime.Runtime.PySys_GetObject("path"); | ||
| 42 | + Runtime.Runtime.PyList_Append(path, str); | ||
| 43 | 43 | Runtime.Runtime.XDecref(str); | |
| 44 | 44 | } | |
| 45 | 45 | ||
@@ -84,5 +84,28 @@ public void TestCastGlobalVar() | |||
| 84 | 84 | Assert.AreEqual("2", foo.FOO.ToString()); | |
| 85 | 85 | Assert.AreEqual("2", foo.test_foo().ToString()); | |
| 86 | 86 | } | |
| 87 | + | ||
| 88 | + [Test] | ||
| 89 | + public void BadAssembly() | ||
| 90 | + { | ||
| 91 | + string path; | ||
| 92 | + if (Python.Runtime.Runtime.IsWindows) | ||
| 93 | + { | ||
| 94 | + path = @"C:\Windows\System32\kernel32.dll"; | ||
| 95 | + } | ||
| 96 | + else | ||
| 97 | + { | ||
| 98 | + Assert.Pass("TODO: add bad assembly location for other platforms"); | ||
| 99 | + return; | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + string code = $@" | ||
| 103 | + import clr | ||
| 104 | + clr.AddReference('{path}') | ||
| 105 | + "; | ||
| 106 | + | ||
| 107 | + var error = Assert.Throws<PythonException>(() => PythonEngine.Exec(code)); | ||
| 108 | + Assert.AreEqual(nameof(FileLoadException), error.PythonTypeName); | ||
| 109 | + } | ||
| 87 | 110 | } | |
| 88 | 111 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,8 +11,7 @@ readonly ref struct BorrowedReference | |||
| 11 | 11 | public bool IsNull => this.pointer == IntPtr.Zero; | |
| 12 | 12 | ||
| 13 | 13 | /// <summary>Gets a raw pointer to the Python object</summary> | |
| 14 | - public IntPtr DangerousGetAddress() | ||
| 15 | - => this.IsNull ? throw new NullReferenceException() : this.pointer; | ||
| 14 | + public IntPtr DangerousGetAddress() => this.pointer; | ||
| 16 | 15 | ||
| 17 | 16 | /// <summary> | |
| 18 | 17 | /// Creates new instance of <see cref="BorrowedReference"/> from raw pointer. Unsafe. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,14 +27,18 @@ public PyObject MoveToPyObject() | |||
| 27 | 27 | this.pointer = IntPtr.Zero; | |
| 28 | 28 | return result; | |
| 29 | 29 | } | |
| 30 | + | ||
| 30 | 31 | /// <summary> | |
| 31 | 32 | /// Removes this reference to a Python object, and sets it to <c>null</c>. | |
| 32 | 33 | /// </summary> | |
| 33 | 34 | public void Dispose() | |
| 34 | 35 | { | |
| 35 | - if (!this.IsNull()) | ||
| 36 | - Runtime.XDecref(this.pointer); | ||
| 37 | - this.pointer = IntPtr.Zero; | ||
| 36 | + if (this.IsNull()) | ||
| 37 | + { | ||
| 38 | + return; | ||
| 39 | + } | ||
| 40 | + Runtime.XDecref(pointer); | ||
| 41 | + pointer = IntPtr.Zero; | ||
| 38 | 42 | } | |
| 39 | 43 | ||
| 40 | 44 | /// <summary> | |
| Back | FazBrowse Home | New Git URL |
0 commit comments