Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all 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 deletions CHANGELOG.md
Original file line number Diff line number Diff 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 deletions src/runtime/pyansistring.cs
Original file line number Diff line number Diff 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 deletions src/runtime/pydict.cs
Original file line number Diff line number Diff 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 deletions src/runtime/pyfloat.cs
Original file line number Diff line number Diff 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 deletions src/runtime/pyint.cs
Original file line number Diff line number Diff 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 Down Expand 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 deletions src/runtime/pyiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,22 @@ public PyIter(IntPtr ptr) : base(ptr)
}

/// <summary>
/// PyIter Constructor
/// PyIter factory function.
/// </summary>
/// <remarks>
/// Creates a Python iterator from an iterable. Like doing "iter(iterable)" in python.
/// Create a new PyIter from a given iterable. 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 new PythonException();
throw new ArgumentNullException();
}
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 deletions src/runtime/pylist.cs
Original file line number Diff line number Diff 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