| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. | |||
| 14 | 14 | - `clr.AddReference` may now throw errors besides `FileNotFoundException`, that provide more | |
| 15 | 15 | details about the cause of the failure | |
| 16 | 16 | - `clr.AddReference` no longer adds ".dll" implicitly | |
| 17 | + - `PyIter(PyObject)` constructor replaced with static `PyIter.GetIter(PyObject)` method | ||
| 17 | 18 | ||
| 18 | 19 | ### Fixed | |
| 19 | 20 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,6 +17,16 @@ public PyAnsiString(IntPtr ptr) : base(ptr) | |||
| 17 | 17 | } | |
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | + private static IntPtr FromObject(PyObject o) | ||
| 21 | + { | ||
| 22 | + if (o == null || !IsStringType(o)) | ||
| 23 | + { | ||
| 24 | + throw new ArgumentException("object is not a string"); | ||
| 25 | + } | ||
| 26 | + Runtime.XIncref(o.obj); | ||
| 27 | + return o.obj; | ||
| 28 | + } | ||
| 29 | + | ||
| 20 | 30 | /// <summary> | |
| 21 | 31 | /// PyString Constructor | |
| 22 | 32 | /// </summary> | |
@@ -25,14 +35,14 @@ public PyAnsiString(IntPtr ptr) : base(ptr) | |||
| 25 | 35 | /// An ArgumentException will be thrown if the given object is not | |
| 26 | 36 | /// a Python string object. | |
| 27 | 37 | /// </remarks> | |
| 28 | - public PyAnsiString(PyObject o) | ||
| 38 | + public PyAnsiString(PyObject o) : base(FromObject(o)) | ||
| 29 | 39 | { | |
| 30 | - if (!IsStringType(o)) | ||
| 31 | - { | ||
| 32 | - throw new ArgumentException("object is not a string"); | ||
| 33 | - } | ||
| 34 | - Runtime.XIncref(o.obj); | ||
| 35 | - obj = o.obj; | ||
| 40 | + } | ||
| 41 | + private static IntPtr FromString(string s) | ||
| 42 | + { | ||
| 43 | + IntPtr val = Runtime.PyString_FromString(s); | ||
| 44 | + PythonException.ThrowIfIsNull(val); | ||
| 45 | + return val; | ||
| 36 | 46 | } | |
| 37 | 47 | ||
| 38 | 48 | ||
@@ -42,10 +52,8 @@ public PyAnsiString(PyObject o) | |||
| 42 | 52 | /// <remarks> | |
| 43 | 53 | /// Creates a Python string from a managed string. | |
| 44 | 54 | /// </remarks> | |
| 45 | - public PyAnsiString(string s) | ||
| 55 | + public PyAnsiString(string s) : base(FromString(s)) | ||
| 46 | 56 | { | |
| 47 | - obj = Runtime.PyString_FromString(s); | ||
| 48 | - PythonException.ThrowIfIsNull(obj); | ||
| 49 | 57 | } | |
| 50 | 58 | ||
| 51 | 59 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,9 +29,8 @@ public PyDict(IntPtr ptr) : base(ptr) | |||
| 29 | 29 | /// <remarks> | |
| 30 | 30 | /// Creates a new Python dictionary object. | |
| 31 | 31 | /// </remarks> | |
| 32 | - public PyDict() | ||
| 32 | + public PyDict() : base(Runtime.PyDict_New()) | ||
| 33 | 33 | { | |
| 34 | - obj = Runtime.PyDict_New(); | ||
| 35 | 34 | if (obj == IntPtr.Zero) | |
| 36 | 35 | { | |
| 37 | 36 | throw new PythonException(); | |
@@ -47,14 +46,13 @@ public PyDict() | |||
| 47 | 46 | /// ArgumentException will be thrown if the given object is not a | |
| 48 | 47 | /// Python dictionary object. | |
| 49 | 48 | /// </remarks> | |
| 50 | - public PyDict(PyObject o) | ||
| 49 | + public PyDict(PyObject o) : base(o.obj) | ||
| 51 | 50 | { | |
| 51 | + Runtime.XIncref(o.obj); | ||
| 52 | 52 | if (!IsDictType(o)) | |
| 53 | 53 | { | |
| 54 | 54 | throw new ArgumentException("object is not a dict"); | |
| 55 | 55 | } | |
| 56 | - Runtime.XIncref(o.obj); | ||
| 57 | - obj = o.obj; | ||
| 58 | 56 | } | |
| 59 | 57 | ||
| 60 | 58 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,6 @@ namespace Python.Runtime | |||
| 4 | 4 | { | |
| 5 | 5 | /// <summary> | |
| 6 | 6 | /// Represents a Python float object. See the documentation at | |
| 7 | - /// PY2: https://docs.python.org/2/c-api/float.html | ||
| 8 | 7 | /// PY3: https://docs.python.org/3/c-api/float.html | |
| 9 | 8 | /// for details. | |
| 10 | 9 | /// </summary> | |
@@ -31,14 +30,8 @@ public PyFloat(IntPtr ptr) : base(ptr) | |||
| 31 | 30 | /// ArgumentException will be thrown if the given object is not a | |
| 32 | 31 | /// Python float object. | |
| 33 | 32 | /// </remarks> | |
| 34 | - public PyFloat(PyObject o) | ||
| 33 | + public PyFloat(PyObject o) : base(FromObject(o)) | ||
| 35 | 34 | { | |
| 36 | - if (!IsFloatType(o)) | ||
| 37 | - { | ||
| 38 | - throw new ArgumentException("object is not a float"); | ||
| 39 | - } | ||
| 40 | - Runtime.XIncref(o.obj); | ||
| 41 | - obj = o.obj; | ||
| 42 | 35 | } | |
| 43 | 36 | ||
| 44 | 37 | ||
@@ -48,26 +41,45 @@ public PyFloat(PyObject o) | |||
| 48 | 41 | /// <remarks> | |
| 49 | 42 | /// Creates a new Python float from a double value. | |
| 50 | 43 | /// </remarks> | |
| 51 | - public PyFloat(double value) | ||
| 44 | + public PyFloat(double value) : base(FromDouble(value)) | ||
| 45 | + { | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + private static IntPtr FromObject(PyObject o) | ||
| 49 | + { | ||
| 50 | + if (o == null || !IsFloatType(o)) | ||
| 51 | + { | ||
| 52 | + throw new ArgumentException("object is not a float"); | ||
| 53 | + } | ||
| 54 | + Runtime.XIncref(o.obj); | ||
| 55 | + return o.obj; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + private static IntPtr FromDouble(double value) | ||
| 52 | 59 | { | |
| 53 | - obj = Runtime.PyFloat_FromDouble(value); | ||
| 54 | - PythonException.ThrowIfIsNull(obj); | ||
| 60 | + IntPtr val = Runtime.PyFloat_FromDouble(value); | ||
| 61 | + PythonException.ThrowIfIsNull(val); | ||
| 62 | + return val; | ||
| 55 | 63 | } | |
| 56 | 64 | ||
| 65 | + private static IntPtr FromString(string value) | ||
| 66 | + { | ||
| 67 | + using (var s = new PyString(value)) | ||
| 68 | + { | ||
| 69 | + IntPtr val = Runtime.PyFloat_FromString(s.obj, IntPtr.Zero); | ||
| 70 | + PythonException.ThrowIfIsNull(val); | ||
| 71 | + return val; | ||
| 72 | + } | ||
| 73 | + } | ||
| 57 | 74 | ||
| 58 | 75 | /// <summary> | |
| 59 | 76 | /// PyFloat Constructor | |
| 60 | 77 | /// </summary> | |
| 61 | 78 | /// <remarks> | |
| 62 | 79 | /// Creates a new Python float from a string value. | |
| 63 | 80 | /// </remarks> | |
| 64 | - public PyFloat(string value) | ||
| 81 | + public PyFloat(string value) : base(FromString(value)) | ||
| 65 | 82 | { | |
| 66 | - using (var s = new PyString(value)) | ||
| 67 | - { | ||
| 68 | - obj = Runtime.PyFloat_FromString(s.obj, IntPtr.Zero); | ||
| 69 | - PythonException.ThrowIfIsNull(obj); | ||
| 70 | - } | ||
| 71 | 83 | } | |
| 72 | 84 | ||
| 73 | 85 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,27 +31,35 @@ public PyInt(IntPtr ptr) : base(ptr) | |||
| 31 | 31 | /// ArgumentException will be thrown if the given object is not a | |
| 32 | 32 | /// Python int object. | |
| 33 | 33 | /// </remarks> | |
| 34 | - public PyInt(PyObject o) | ||
| 34 | + public PyInt(PyObject o) : base(FromObject(o)) | ||
| 35 | 35 | { | |
| 36 | - if (!IsIntType(o)) | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + private static IntPtr FromObject(PyObject o) | ||
| 39 | + { | ||
| 40 | + if (o == null || !IsIntType(o)) | ||
| 37 | 41 | { | |
| 38 | 42 | throw new ArgumentException("object is not an int"); | |
| 39 | 43 | } | |
| 40 | 44 | Runtime.XIncref(o.obj); | |
| 41 | - obj = o.obj; | ||
| 45 | + return o.obj; | ||
| 42 | 46 | } | |
| 43 | 47 | ||
| 48 | + private static IntPtr FromInt(int value) | ||
| 49 | + { | ||
| 50 | + IntPtr val = Runtime.PyInt_FromInt32(value); | ||
| 51 | + PythonException.ThrowIfIsNull(val); | ||
| 52 | + return val; | ||
| 53 | + } | ||
| 44 | 54 | ||
| 45 | 55 | /// <summary> | |
| 46 | 56 | /// PyInt Constructor | |
| 47 | 57 | /// </summary> | |
| 48 | 58 | /// <remarks> | |
| 49 | 59 | /// Creates a new Python int from an int32 value. | |
| 50 | 60 | /// </remarks> | |
| 51 | - public PyInt(int value) | ||
| 61 | + public PyInt(int value) : base(FromInt(value)) | ||
| 52 | 62 | { | |
| 53 | - obj = Runtime.PyInt_FromInt32(value); | ||
| 54 | - PythonException.ThrowIfIsNull(obj); | ||
| 55 | 63 | } | |
| 56 | 64 | ||
| 57 | 65 | ||
@@ -62,10 +70,8 @@ public PyInt(int value) | |||
| 62 | 70 | /// Creates a new Python int from a uint32 value. | |
| 63 | 71 | /// </remarks> | |
| 64 | 72 | [CLSCompliant(false)] | |
| 65 | - public PyInt(uint value) | ||
| 73 | + public PyInt(uint value) : base(FromLong(value)) | ||
| 66 | 74 | { | |
| 67 | - obj = Runtime.PyInt_FromInt64(value); | ||
| 68 | - PythonException.ThrowIfIsNull(obj); | ||
| 69 | 75 | } | |
| 70 | 76 | ||
| 71 | 77 | ||
@@ -75,10 +81,15 @@ public PyInt(uint value) | |||
| 75 | 81 | /// <remarks> | |
| 76 | 82 | /// Creates a new Python int from an int64 value. | |
| 77 | 83 | /// </remarks> | |
| 78 | - public PyInt(long value) | ||
| 84 | + public PyInt(long value) : base(FromLong(value)) | ||
| 79 | 85 | { | |
| 80 | - obj = Runtime.PyInt_FromInt64(value); | ||
| 81 | - PythonException.ThrowIfIsNull(obj); | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + private static IntPtr FromLong(long value) | ||
| 89 | + { | ||
| 90 | + IntPtr val = Runtime.PyInt_FromInt64(value); | ||
| 91 | + PythonException.ThrowIfIsNull(val); | ||
| 92 | + return val; | ||
| 82 | 93 | } | |
| 83 | 94 | ||
| 84 | 95 | ||
@@ -89,10 +100,8 @@ public PyInt(long value) | |||
| 89 | 100 | /// Creates a new Python int from a uint64 value. | |
| 90 | 101 | /// </remarks> | |
| 91 | 102 | [CLSCompliant(false)] | |
| 92 | - public PyInt(ulong value) | ||
| 103 | + public PyInt(ulong value) : base(FromLong((long)value)) | ||
| 93 | 104 | { | |
| 94 | - obj = Runtime.PyInt_FromInt64((long)value); | ||
| 95 | - PythonException.ThrowIfIsNull(obj); | ||
| 96 | 105 | } | |
| 97 | 106 | ||
| 98 | 107 | ||
@@ -142,16 +151,21 @@ public PyInt(sbyte value) : this((int)value) | |||
| 142 | 151 | } | |
| 143 | 152 | ||
| 144 | 153 | ||
| 154 | + private static IntPtr FromString(string value) | ||
| 155 | + { | ||
| 156 | + IntPtr val = Runtime.PyInt_FromString(value, IntPtr.Zero, 0); | ||
| 157 | + PythonException.ThrowIfIsNull(val); | ||
| 158 | + return val; | ||
| 159 | + } | ||
| 160 | + | ||
| 145 | 161 | /// <summary> | |
| 146 | 162 | /// PyInt Constructor | |
| 147 | 163 | /// </summary> | |
| 148 | 164 | /// <remarks> | |
| 149 | 165 | /// Creates a new Python int from a string value. | |
| 150 | 166 | /// </remarks> | |
| 151 | - public PyInt(string value) | ||
| 167 | + public PyInt(string value) : base(FromString(value)) | ||
| 152 | 168 | { | |
| 153 | - obj = Runtime.PyInt_FromString(value, IntPtr.Zero, 0); | ||
| 154 | - PythonException.ThrowIfIsNull(obj); | ||
| 155 | 169 | } | |
| 156 | 170 | ||
| 157 | 171 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,18 +26,22 @@ public PyIter(IntPtr ptr) : base(ptr) | |||
| 26 | 26 | } | |
| 27 | 27 | ||
| 28 | 28 | /// <summary> | |
| 29 | - /// PyIter Constructor | ||
| 29 | + /// PyIter factory function. | ||
| 30 | 30 | /// </summary> | |
| 31 | 31 | /// <remarks> | |
| 32 | - /// Creates a Python iterator from an iterable. Like doing "iter(iterable)" in python. | ||
| 32 | + /// Create a new PyIter from a given iterable. Like doing "iter(iterable)" in python. | ||
| 33 | 33 | /// </remarks> | |
| 34 | - public PyIter(PyObject iterable) | ||
| 34 | + /// <param name="iterable"></param> | ||
| 35 | + /// <returns></returns> | ||
| 36 | + public static PyIter GetIter(PyObject iterable) | ||
| 35 | 37 | { | |
| 36 | - obj = Runtime.PyObject_GetIter(iterable.obj); | ||
| 37 | - if (obj == IntPtr.Zero) | ||
| 38 | + if (iterable == null) | ||
| 38 | 39 | { | |
| 39 | - throw new PythonException(); | ||
| 40 | + throw new ArgumentNullException(); | ||
| 40 | 41 | } | |
| 42 | + IntPtr val = Runtime.PyObject_GetIter(iterable.obj); | ||
| 43 | + PythonException.ThrowIfIsNull(val); | ||
| 44 | + return new PyIter(val); | ||
| 41 | 45 | } | |
| 42 | 46 | ||
| 43 | 47 | protected override void Dispose(bool disposing) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,6 +28,16 @@ public PyList(IntPtr ptr) : base(ptr) | |||
| 28 | 28 | internal PyList(BorrowedReference reference) : base(reference) { } | |
| 29 | 29 | ||
| 30 | 30 | ||
| 31 | + private static IntPtr FromObject(PyObject o) | ||
| 32 | + { | ||
| 33 | + if (o == null || !IsListType(o)) | ||
| 34 | + { | ||
| 35 | + throw new ArgumentException("object is not a list"); | ||
| 36 | + } | ||
| 37 | + Runtime.XIncref(o.obj); | ||
| 38 | + return o.obj; | ||
| 39 | + } | ||
| 40 | + | ||
| 31 | 41 | /// <summary> | |
| 32 | 42 | /// PyList Constructor | |
| 33 | 43 | /// </summary> | |
@@ -36,14 +46,8 @@ internal PyList(BorrowedReference reference) : base(reference) { } | |||
| 36 | 46 | /// ArgumentException will be thrown if the given object is not a | |
| 37 | 47 | /// Python list object. | |
| 38 | 48 | /// </remarks> | |
| 39 | - public PyList(PyObject o) | ||
| 49 | + public PyList(PyObject o) : base(FromObject(o)) | ||
| 40 | 50 | { | |
| 41 | - if (!IsListType(o)) | ||
| 42 | - { | ||
| 43 | - throw new ArgumentException("object is not a list"); | ||
| 44 | - } | ||
| 45 | - Runtime.XIncref(o.obj); | ||
| 46 | - obj = o.obj; | ||
| 47 | 51 | } | |
| 48 | 52 | ||
| 49 | 53 | ||
@@ -53,36 +57,40 @@ public PyList(PyObject o) | |||
| 53 | 57 | /// <remarks> | |
| 54 | 58 | /// Creates a new empty Python list object. | |
| 55 | 59 | /// </remarks> | |
| 56 | - public PyList() | ||
| 60 | + public PyList() : base(Runtime.PyList_New(0)) | ||
| 57 | 61 | { | |
| 58 | - obj = Runtime.PyList_New(0); | ||
| 59 | 62 | if (obj == IntPtr.Zero) | |
| 60 | 63 | { | |
| 61 | 64 | throw new PythonException(); | |
| 62 | 65 | } | |
| 63 | 66 | } | |
| 64 | 67 | ||
| 65 | - | ||
| 66 | - /// <summary> | ||
| 67 | - /// PyList Constructor | ||
| 68 | - /// </summary> | ||
| 69 | - /// <remarks> | ||
| 70 | - /// Creates a new Python list object from an array of PyObjects. | ||
| 71 | - /// </remarks> | ||
| 72 | - public PyList(PyObject[] items) | ||
| 68 | + private static IntPtr FromArray(PyObject[] items) | ||
| 73 | 69 | { | |
| 74 | 70 | int count = items.Length; | |
| 75 | - obj = Runtime.PyList_New(count); | ||
| 71 | + IntPtr val = Runtime.PyList_New(count); | ||
| 76 | 72 | for (var i = 0; i < count; i++) | |
| 77 | 73 | { | |
| 78 | 74 | IntPtr ptr = items[i].obj; | |
| 79 | 75 | Runtime.XIncref(ptr); | |
| 80 | - int r = Runtime.PyList_SetItem(obj, i, ptr); | ||
| 76 | + int r = Runtime.PyList_SetItem(val, i, ptr); | ||
| 81 | 77 | if (r < 0) | |
| 82 | 78 | { | |
| 79 | + Runtime.Py_DecRef(val); | ||
| 83 | 80 | throw new PythonException(); | |
| 84 | 81 | } | |
| 85 | 82 | } | |
| 83 | + return val; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + /// <summary> | ||
| 87 | + /// PyList Constructor | ||
| 88 | + /// </summary> | ||
| 89 | + /// <remarks> | ||
| 90 | + /// Creates a new Python list object from an array of PyObjects. | ||
| 91 | + /// </remarks> | ||
| 92 | + public PyList(PyObject[] items) : base(FromArray(items)) | ||
| 93 | + { | ||
| 86 | 94 | } | |
| 87 | 95 | ||
| 88 | 96 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments