Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix compiler warning#1226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
lostmsu merged 8 commits intopythonnet:masterfromkoubaa:compiler-warning
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletionsCHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,6 +14,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- `clr.AddReference` may now throw errors besides `FileNotFoundException`, that provide more
details about the cause of the failure
- `clr.AddReference` no longer adds ".dll" implicitly
- `PyIter(PyObject)` constructor replaced with static `PyIter.GetIter(PyObject)` method

### Fixed

Expand Down
28 changes: 18 additions & 10 deletionssrc/runtime/pyansistring.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -17,6 +17,16 @@ public PyAnsiString(IntPtr ptr) : base(ptr)
}


private static IntPtr FromObject(PyObject o)
{
if (o == null || !IsStringType(o))
{
throw new ArgumentException("object is not a string");
}
Runtime.XIncref(o.obj);
return o.obj;
}

/// <summary>
/// PyString Constructor
/// </summary>
Expand All@@ -25,14 +35,14 @@ public PyAnsiString(IntPtr ptr) : base(ptr)
/// An ArgumentException will be thrown if the given object is not
/// a Python string object.
/// </remarks>
public PyAnsiString(PyObject o)
public PyAnsiString(PyObject o) : base(FromObject(o))
{
if (!IsStringType(o))
{
throw new ArgumentException("object is not a string");
}
Runtime.XIncref(o.obj);
obj = o.obj;
}
private static IntPtr FromString(string s)
{
IntPtr val = Runtime.PyString_FromString(s);
PythonException.ThrowIfIsNull(val);
return val;
}


Expand All@@ -42,10 +52,8 @@ public PyAnsiString(PyObject o)
/// <remarks>
/// Creates a Python string from a managed string.
/// </remarks>
public PyAnsiString(string s)
public PyAnsiString(string s) : base(FromString(s))
{
obj = Runtime.PyString_FromString(s);
PythonException.ThrowIfIsNull(obj);
}


Expand Down
8 changes: 3 additions & 5 deletionssrc/runtime/pydict.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,9 +29,8 @@ public PyDict(IntPtr ptr) : base(ptr)
/// <remarks>
/// Creates a new Python dictionary object.
/// </remarks>
public PyDict()
public PyDict() : base(Runtime.PyDict_New())
{
obj = Runtime.PyDict_New();
if (obj == IntPtr.Zero)
{
throw new PythonException();
Expand All@@ -47,14 +46,13 @@ public PyDict()
/// ArgumentException will be thrown if the given object is not a
/// Python dictionary object.
/// </remarks>
public PyDict(PyObject o)
public PyDict(PyObject o) : base(o.obj)
{
Runtime.XIncref(o.obj);
if (!IsDictType(o))
{
throw new ArgumentException("object is not a dict");
}
Runtime.XIncref(o.obj);
obj = o.obj;
}


Expand Down
46 changes: 29 additions & 17 deletionssrc/runtime/pyfloat.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,7 +4,6 @@ namespace Python.Runtime
{
/// <summary>
/// Represents a Python float object. See the documentation at
/// PY2: https://docs.python.org/2/c-api/float.html
/// PY3: https://docs.python.org/3/c-api/float.html
/// for details.
/// </summary>
Expand All@@ -31,14 +30,8 @@ public PyFloat(IntPtr ptr) : base(ptr)
/// ArgumentException will be thrown if the given object is not a
/// Python float object.
/// </remarks>
public PyFloat(PyObject o)
public PyFloat(PyObject o) : base(FromObject(o))
{
if (!IsFloatType(o))
{
throw new ArgumentException("object is not a float");
}
Runtime.XIncref(o.obj);
obj = o.obj;
}


Expand All@@ -48,26 +41,45 @@ public PyFloat(PyObject o)
/// <remarks>
/// Creates a new Python float from a double value.
/// </remarks>
public PyFloat(double value)
public PyFloat(double value) : base(FromDouble(value))
{
}

private static IntPtr FromObject(PyObject o)
{
if (o == null || !IsFloatType(o))
{
throw new ArgumentException("object is not a float");
}
Runtime.XIncref(o.obj);
return o.obj;
}

private static IntPtr FromDouble(double value)
{
obj = Runtime.PyFloat_FromDouble(value);
PythonException.ThrowIfIsNull(obj);
IntPtr val = Runtime.PyFloat_FromDouble(value);
PythonException.ThrowIfIsNull(val);
return val;
}

private static IntPtr FromString(string value)
{
using (var s = new PyString(value))
{
IntPtr val = Runtime.PyFloat_FromString(s.obj, IntPtr.Zero);
PythonException.ThrowIfIsNull(val);
return val;
}
}

/// <summary>
/// PyFloat Constructor
/// </summary>
/// <remarks>
/// Creates a new Python float from a string value.
/// </remarks>
public PyFloat(string value)
public PyFloat(string value) : base(FromString(value))
{
using (var s = new PyString(value))
{
obj = Runtime.PyFloat_FromString(s.obj, IntPtr.Zero);
PythonException.ThrowIfIsNull(obj);
}
}


Expand Down
50 changes: 32 additions & 18 deletionssrc/runtime/pyint.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,27 +31,35 @@ public PyInt(IntPtr ptr) : base(ptr)
/// ArgumentException will be thrown if the given object is not a
/// Python int object.
/// </remarks>
public PyInt(PyObject o)
public PyInt(PyObject o) : base(FromObject(o))
{
if (!IsIntType(o))
}

private static IntPtr FromObject(PyObject o)
{
if (o == null || !IsIntType(o))
{
throw new ArgumentException("object is not an int");
}
Runtime.XIncref(o.obj);
obj = o.obj;
return o.obj;
}

private static IntPtr FromInt(int value)
{
IntPtr val = Runtime.PyInt_FromInt32(value);
PythonException.ThrowIfIsNull(val);
return val;
}

/// <summary>
/// PyInt Constructor
/// </summary>
/// <remarks>
/// Creates a new Python int from an int32 value.
/// </remarks>
public PyInt(int value)
public PyInt(int value) : base(FromInt(value))
{
obj = Runtime.PyInt_FromInt32(value);
PythonException.ThrowIfIsNull(obj);
}


Expand All@@ -62,10 +70,8 @@ public PyInt(int value)
/// Creates a new Python int from a uint32 value.
/// </remarks>
[CLSCompliant(false)]
public PyInt(uint value)
public PyInt(uint value) : base(FromLong(value))
{
obj = Runtime.PyInt_FromInt64(value);
PythonException.ThrowIfIsNull(obj);
}


Expand All@@ -75,10 +81,15 @@ public PyInt(uint value)
/// <remarks>
/// Creates a new Python int from an int64 value.
/// </remarks>
public PyInt(long value)
public PyInt(long value) : base(FromLong(value))
{
obj = Runtime.PyInt_FromInt64(value);
PythonException.ThrowIfIsNull(obj);
}

private static IntPtr FromLong(long value)
{
IntPtr val = Runtime.PyInt_FromInt64(value);
PythonException.ThrowIfIsNull(val);
return val;
}


Expand All@@ -89,10 +100,8 @@ public PyInt(long value)
/// Creates a new Python int from a uint64 value.
/// </remarks>
[CLSCompliant(false)]
public PyInt(ulong value)
public PyInt(ulong value) : base(FromLong((long)value))
{
obj = Runtime.PyInt_FromInt64((long)value);
PythonException.ThrowIfIsNull(obj);
}


Expand DownExpand Up@@ -142,16 +151,21 @@ public PyInt(sbyte value) : this((int)value)
}


private static IntPtr FromString(string value)
{
IntPtr val = Runtime.PyInt_FromString(value, IntPtr.Zero, 0);
PythonException.ThrowIfIsNull(val);
return val;
}

/// <summary>
/// PyInt Constructor
/// </summary>
/// <remarks>
/// Creates a new Python int from a string value.
/// </remarks>
public PyInt(string value)
public PyInt(string value) : base(FromString(value))
{
obj = Runtime.PyInt_FromString(value, IntPtr.Zero, 0);
PythonException.ThrowIfIsNull(obj);
}


Expand Down
16 changes: 10 additions & 6 deletionssrc/runtime/pyiter.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,18 +26,22 @@ public PyIter(IntPtr ptr) : base(ptr)
}

/// <summary>
/// PyIterConstructor
/// PyIterfactory function.
/// </summary>
/// <remarks>
///Creates aPython iterator fromaniterable. Like doing "iter(iterable)" in python.
///Create anew PyIter froma giveniterable. Like doing "iter(iterable)" in python.
/// </remarks>
public PyIter(PyObject iterable)
/// <param name="iterable"></param>
/// <returns></returns>
public static PyIter GetIter(PyObject iterable)
{
obj = Runtime.PyObject_GetIter(iterable.obj);
if (obj == IntPtr.Zero)
if (iterable == null)
{
throw newPythonException();
throw newArgumentNullException();
}
IntPtr val = Runtime.PyObject_GetIter(iterable.obj);
PythonException.ThrowIfIsNull(val);
return new PyIter(val);
}

protected override void Dispose(bool disposing)
Expand Down
46 changes: 27 additions & 19 deletionssrc/runtime/pylist.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,16 @@ public PyList(IntPtr ptr) : base(ptr)
internal PyList(BorrowedReference reference) : base(reference) { }


private static IntPtr FromObject(PyObject o)
{
if (o == null || !IsListType(o))
{
throw new ArgumentException("object is not a list");
}
Runtime.XIncref(o.obj);
return o.obj;
}

/// <summary>
/// PyList Constructor
/// </summary>
Expand All@@ -36,14 +46,8 @@ internal PyList(BorrowedReference reference) : base(reference) { }
/// ArgumentException will be thrown if the given object is not a
/// Python list object.
/// </remarks>
public PyList(PyObject o)
public PyList(PyObject o) : base(FromObject(o))
{
if (!IsListType(o))
{
throw new ArgumentException("object is not a list");
}
Runtime.XIncref(o.obj);
obj = o.obj;
}


Expand All@@ -53,36 +57,40 @@ public PyList(PyObject o)
/// <remarks>
/// Creates a new empty Python list object.
/// </remarks>
public PyList()
public PyList() : base(Runtime.PyList_New(0))
{
obj = Runtime.PyList_New(0);
if (obj == IntPtr.Zero)
{
throw new PythonException();
}
}


/// <summary>
/// PyList Constructor
/// </summary>
/// <remarks>
/// Creates a new Python list object from an array of PyObjects.
/// </remarks>
public PyList(PyObject[] items)
private static IntPtr FromArray(PyObject[] items)
{
int count = items.Length;
obj = Runtime.PyList_New(count);
IntPtr val = Runtime.PyList_New(count);
for (var i = 0; i < count; i++)
{
IntPtr ptr = items[i].obj;
Runtime.XIncref(ptr);
int r = Runtime.PyList_SetItem(obj, i, ptr);
int r = Runtime.PyList_SetItem(val, i, ptr);
if (r < 0)
{
Runtime.Py_DecRef(val);
throw new PythonException();
}
}
return val;
}

/// <summary>
/// PyList Constructor
/// </summary>
/// <remarks>
/// Creates a new Python list object from an array of PyObjects.
/// </remarks>
public PyList(PyObject[] items) : base(FromArray(items))
{
}


Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp