[Zope-CVS] CVS: PythonNet/src/runtime - PyDict.cs:1.1
PyFloat.cs:1.1 PyInt.cs:1.1 PyList.cs:1.1 PyLong.cs:1.1
PyNumber.cs:1.1 PyObject.cs:1.1 PySequence.cs:1.1
PyString.cs:1.1 PyTuple.cs:1.1 Converter.cs:1.3 Runtime.cs:1.7
Brian Lloyd
brian at zope.com
Fri Sep 12 00:49:35 EDT 2003
Update of /cvs-repository/PythonNet/src/runtime
In directory cvs.zope.org:/tmp/cvs-serv25881
Modified Files:
Converter.cs Runtime.cs
Added Files:
PyDict.cs PyFloat.cs PyInt.cs PyList.cs PyLong.cs PyNumber.cs
PyObject.cs PySequence.cs PyString.cs PyTuple.cs
Log Message:
Initial hack at embedding api.
=== Added File PythonNet/src/runtime/PyDict.cs ===
// Copyright (c) 2001, 2002 Zope Corporation and Contributors.
//
// All Rights Reserved.
//
// This software is subject to the provisions of the Zope Public License,
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
// FOR A PARTICULAR PURPOSE.
using System;
using System.Runtime.InteropServices;
namespace Python.Runtime {
/// <summary>
/// Represents a Python dictionary object. See the documentation at
/// http://www.python.org/doc/current/api/dictObjects.html for details.
/// </summary>
public class PyDict : PyObject {
/// <summary>
/// PyDict Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyDict from an existing object reference. Note
/// that the instance assumes ownership of the object reference.
/// The object reference is not checked for type-correctness.
/// </remarks>
public PyDict(IntPtr ptr) : base(ptr) {}
/// <summary>
/// PyDict Constructor
/// </summary>
///
/// <remarks>
/// Creates a new Python dictionary object.
/// </remarks>
public PyDict() : base() {
obj = Runtime.PyDict_New();
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// PyDict Constructor
/// </summary>
///
/// <remarks>
/// Copy constructor - obtain a PyDict from a generic PyObject. An
/// ArgumentException will be thrown if the given object is not a
/// Python dictionary object.
/// </remarks>
public PyDict(PyObject o) : base() {
if (!IsDictType(o)) {
throw new ArgumentException("object is not a dict");
}
Runtime.Incref(o.obj);
obj = o.obj;
}
/// <summary>
/// IsDictType Method
/// </summary>
///
/// <remarks>
/// Returns true if the given object is a Python dictionary.
/// </remarks>
public static bool IsDictType(PyObject value) {
return Runtime.PyDict_Check(value.obj);
}
/// <summary>
/// HasKey Method
/// </summary>
///
/// <remarks>
/// Returns true if the object key appears in the dictionary.
/// </remarks>
public bool HasKey(PyObject key) {
return (Runtime.PyMapping_HasKey(obj, key.obj) != 0);
}
/// <summary>
/// HasKey Method
/// </summary>
///
/// <remarks>
/// Returns true if the string key appears in the dictionary.
/// </remarks>
public bool HasKey(string key) {
return HasKey(new PyString(key));
}
/// <summary>
/// Keys Method
/// </summary>
///
/// <remarks>
/// Returns a sequence containing the keys of the dictionary.
/// </remarks>
public PyObject Keys() {
IntPtr items = Runtime.PyDict_Keys(obj);
if (items == IntPtr.Zero) {
throw new PythonException();
}
return new PyObject(items);
}
/// <summary>
/// Values Method
/// </summary>
///
/// <remarks>
/// Returns a sequence containing the values of the dictionary.
/// </remarks>
public PyObject Values() {
IntPtr items = Runtime.PyDict_Values(obj);
if (items == IntPtr.Zero) {
throw new PythonException();
}
return new PyObject(items);
}
/// <summary>
/// Items Method
/// </summary>
///
/// <remarks>
/// Returns a sequence containing the items of the dictionary.
/// </remarks>
public PyObject Items() {
IntPtr items = Runtime.PyDict_Items(obj);
if (items == IntPtr.Zero) {
throw new PythonException();
}
return new PyObject(items);
}
/// <summary>
/// Copy Method
/// </summary>
///
/// <remarks>
/// Returns a copy of the dictionary.
/// </remarks>
public PyDict Copy() {
IntPtr op = Runtime.PyDict_Copy(obj);
if (op == IntPtr.Zero) {
throw new PythonException();
}
return new PyDict(op);
}
/// <summary>
/// Update Method
/// </summary>
///
/// <remarks>
/// Update the dictionary from another dictionary.
/// </remarks>
public void Update(PyObject other) {
int result = Runtime.PyDict_Update(obj, other.obj);
if (result < 0) {
throw new PythonException();
}
}
/// <summary>
/// Clear Method
/// </summary>
///
/// <remarks>
/// Clears the dictionary.
/// </remarks>
public void Clear() {
Runtime.PyDict_Clear(obj);
}
}
}
=== Added File PythonNet/src/runtime/PyFloat.cs ===
// Copyright (c) 2001, 2002 Zope Corporation and Contributors.
//
// All Rights Reserved.
//
// This software is subject to the provisions of the Zope Public License,
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
// FOR A PARTICULAR PURPOSE.
using System;
using System.Runtime.InteropServices;
namespace Python.Runtime {
/// <summary>
/// Represents a Python float object. See the documentation at
/// http://www.python.org/doc/current/api/floatObjects.html
/// </summary>
public class PyFloat : PyNumber {
/// <summary>
/// PyFloat Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyFloat from an existing object reference. Note
/// that the instance assumes ownership of the object reference.
/// The object reference is not checked for type-correctness.
/// </remarks>
public PyFloat(IntPtr ptr) : base(ptr) {}
/// <summary>
/// PyFloat Constructor
/// </summary>
///
/// <remarks>
/// Copy constructor - obtain a PyFloat from a generic PyObject. An
/// ArgumentException will be thrown if the given object is not a
/// Python float object.
/// </remarks>
public PyFloat(PyObject o) : base() {
if (!IsFloatType(o)) {
throw new ArgumentException("object is not a float");
}
Runtime.Incref(o.obj);
obj = o.obj;
}
/// <summary>
/// PyFloat Constructor
/// </summary>
///
/// <remarks>
/// Creates a new Python float from a double value.
/// </remarks>
public PyFloat(double value) : base() {
obj = Runtime.PyFloat_FromDouble(value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// PyFloat Constructor
/// </summary>
///
/// <remarks>
/// Creates a new Python float from a string value.
/// </remarks>
public PyFloat(string value) : base() {
PyString s = new PyString(value);
obj = Runtime.PyFloat_FromString(s.obj, IntPtr.Zero);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// IsFloatType Method
/// </summary>
///
/// <remarks>
/// Returns true if the given object is a Python float.
/// </remarks>
public static bool IsFloatType(PyObject value) {
return Runtime.PyFloat_Check(value.obj);
}
/// <summary>
/// AsFloat Method
/// </summary>
///
/// <remarks>
/// <remarks>
/// Convert a Python object to a Python float if possible, raising
/// a PythonException if the conversion is not possible. This is
/// equivalent to the Python expression "float(object)".
/// </remarks>
public static PyFloat AsFloat(PyObject value) {
IntPtr op = Runtime.PyNumber_Float(value.obj);
if (op == IntPtr.Zero) {
throw new PythonException();
}
return new PyFloat(op);
}
}
}
=== Added File PythonNet/src/runtime/PyInt.cs ===
// Copyright (c) 2001, 2002 Zope Corporation and Contributors.
//
// All Rights Reserved.
//
// This software is subject to the provisions of the Zope Public License,
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
// FOR A PARTICULAR PURPOSE.
using System;
using System.Runtime.InteropServices;
namespace Python.Runtime {
/// <summary>
/// Represents a Python integer object. See the documentation at
/// http://www.python.org/doc/current/api/intObjects.html for details.
/// </summary>
public class PyInt : PyNumber {
/// <summary>
/// PyInt Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyInt from an existing object reference. Note
/// that the instance assumes ownership of the object reference.
/// The object reference is not checked for type-correctness.
/// </remarks>
public PyInt(IntPtr ptr) : base(ptr) {}
/// <summary>
/// PyInt Constructor
/// </summary>
///
/// <remarks>
/// Copy constructor - obtain a PyInt from a generic PyObject. An
/// ArgumentException will be thrown if the given object is not a
/// Python int object.
/// </remarks>
public PyInt(PyObject o) : base() {
if (!IsIntType(o)) {
throw new ArgumentException("object is not an int");
}
Runtime.Incref(o.obj);
obj = o.obj;
}
/// <summary>
/// PyInt Constructor
/// </summary>
///
/// <remarks>
/// Creates a new Python int from an int32 value.
/// </remarks>
public PyInt(int value) : base() {
obj = Runtime.PyInt_FromInt32(value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// PyInt Constructor
/// </summary>
///
/// <remarks>
/// Creates a new Python int from a uint32 value.
/// </remarks>
public PyInt(uint value) : base(IntPtr.Zero) {
obj = Runtime.PyInt_FromInt64((long)value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// PyInt Constructor
/// </summary>
///
/// <remarks>
/// Creates a new Python int from an int64 value.
/// </remarks>
public PyInt(long value) : base(IntPtr.Zero) {
obj = Runtime.PyInt_FromInt64(value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// PyInt Constructor
/// </summary>
///
/// <remarks>
/// Creates a new Python int from a uint64 value.
/// </remarks>
public PyInt(ulong value) : base(IntPtr.Zero) {
obj = Runtime.PyInt_FromInt64((long)value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// PyInt Constructor
/// </summary>
///
/// <remarks>
/// Creates a new Python int from an int16 value.
/// </remarks>
public PyInt(short value) : this((int)value) {}
/// <summary>
/// PyInt Constructor
/// </summary>
///
/// <remarks>
/// Creates a new Python int from a uint16 value.
/// </remarks>
public PyInt(ushort value) : this((int)value) {}
/// <summary>
/// PyInt Constructor
/// </summary>
///
/// <remarks>
/// Creates a new Python int from a byte value.
/// </remarks>
public PyInt(byte value) : this((int)value) {}
/// <summary>
/// PyInt Constructor
/// </summary>
///
/// <remarks>
/// Creates a new Python int from an sbyte value.
/// </remarks>
public PyInt(sbyte value) : this((int)value) {}
/// <summary>
/// PyInt Constructor
/// </summary>
///
/// <remarks>
/// Creates a new Python int from a string value.
/// </remarks>
public PyInt(string value) : base() {
obj = Runtime.PyInt_FromString(value, IntPtr.Zero, 0);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// IsIntType Method
/// </summary>
///
/// <remarks>
/// Returns true if the given object is a Python int.
/// </remarks>
public static bool IsIntType(PyObject value) {
return Runtime.PyInt_Check(value.obj);
}
/// <summary>
/// AsInt Method
/// </summary>
///
/// <remarks>
/// <remarks>
/// Convert a Python object to a Python int if possible, raising
/// a PythonException if the conversion is not possible. This is
/// equivalent to the Python expression "int(object)".
/// </remarks>
public static PyInt AsInt(PyObject value) {
IntPtr op = Runtime.PyNumber_Int(value.obj);
if (op == IntPtr.Zero) {
throw new PythonException();
}
return new PyInt(op);
}
/// <summary>
/// ToInt16 Method
/// </summary>
///
/// <remarks>
/// Return the value of the Python int object as an int16.
/// </remarks>
public short ToInt16() {
return System.Convert.ToInt16(this.ToInt32());
}
/// <summary>
/// ToInt32 Method
/// </summary>
///
/// <remarks>
/// Return the value of the Python int object as an int32.
/// </remarks>
public int ToInt32() {
return Runtime.PyInt_AsLong(obj);
}
/// <summary>
/// ToInt64 Method
/// </summary>
///
/// <remarks>
/// Return the value of the Python int object as an int64.
/// </remarks>
public long ToInt64() {
return System.Convert.ToInt64(this.ToInt32());
}
}
}
=== Added File PythonNet/src/runtime/PyList.cs ===
// Copyright (c) 2001, 2002 Zope Corporation and Contributors.
//
// All Rights Reserved.
//
// This software is subject to the provisions of the Zope Public License,
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
// FOR A PARTICULAR PURPOSE.
using System;
namespace Python.Runtime {
/// <summary>
/// Represents a standard Python list object. See the documentation at
/// http://www.python.org/doc/current/api/listObjects.html for details.
/// </summary>
public class PyList : PySequence {
/// <summary>
/// PyList Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyList from an existing object reference. Note
/// that the instance assumes ownership of the object reference.
/// The object reference is not checked for type-correctness.
/// </remarks>
public PyList(IntPtr ptr) : base(ptr) {}
/// <summary>
/// PyList Constructor
/// </summary>
///
/// <remarks>
/// Copy constructor - obtain a PyList from a generic PyObject. An
/// ArgumentException will be thrown if the given object is not a
/// Python list object.
/// </remarks>
public PyList(PyObject o) : base() {
if (!IsListType(o)) {
throw new ArgumentException("object is not a list");
}
Runtime.Incref(o.obj);
obj = o.obj;
}
/// <summary>
/// PyList Constructor
/// </summary>
///
/// <remarks>
/// Creates a new empty Python list object.
/// </remarks>
public PyList() : base() {
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) : base() {
int count = items.Length;
obj = Runtime.PyList_New(count);
for (int i = 0; i < count; i++) {
IntPtr ptr = items[i].obj;
Runtime.Incref(ptr);
int r = Runtime.PyList_SetItem(obj, i, ptr);
if (r < 0) {
throw new PythonException();
}
}
}
/// <summary>
/// IsListType Method
/// </summary>
///
/// <remarks>
/// Returns true if the given object is a Python list.
/// </remarks>
public static bool IsListType(PyObject value) {
return Runtime.PyList_Check(value.obj);
}
/// <summary>
/// AsList Method
/// </summary>
///
/// <remarks>
/// Converts a Python object to a Python list if possible, raising
/// a PythonException if the conversion is not possible. This is
/// equivalent to the Python expression "list(object)".
/// </remarks>
public static PyList AsList(PyObject value) {
IntPtr op = Runtime.PySequence_List(value.obj);
if (op == IntPtr.Zero) {
throw new PythonException();
}
return new PyList(op);
}
/// <summary>
/// Append Method
/// </summary>
///
/// <remarks>
/// Append an item to the list object.
/// </remarks>
public void Append(PyObject item) {
int r = Runtime.PyList_Append(obj, item.obj);
if (r < 0) {
throw new PythonException();
}
}
/// <summary>
/// Insert Method
/// </summary>
///
/// <remarks>
/// Insert an item in the list object at the given index.
/// </remarks>
public void Insert(int index, PyObject item) {
int r = Runtime.PyList_Insert(obj, index, item.obj);
if (r < 0) {
throw new PythonException();
}
}
/// <summary>
/// Reverse Method
/// </summary>
///
/// <remarks>
/// Reverse the order of the list object in place.
/// </remarks>
public void Reverse() {
int r = Runtime.PyList_Reverse(obj);
if (r < 0) {
throw new PythonException();
}
}
/// <summary>
/// Sort Method
/// </summary>
///
/// <remarks>
/// Sort the list in place.
/// </remarks>
public void Sort() {
int r = Runtime.PyList_Sort(obj);
if (r < 0) {
throw new PythonException();
}
}
}
}
=== Added File PythonNet/src/runtime/PyLong.cs ===
// Copyright (c) 2001, 2002 Zope Corporation and Contributors.
//
// All Rights Reserved.
//
// This software is subject to the provisions of the Zope Public License,
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
// FOR A PARTICULAR PURPOSE.
using System;
namespace Python.Runtime {
/// <summary>
/// Represents a Python long int object. See the documentation at
/// http://www.python.org/doc/current/api/longObjects.html
/// </summary>
public class PyLong : PyNumber {
/// <summary>
/// PyLong Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyLong from an existing object reference. Note
/// that the instance assumes ownership of the object reference.
/// The object reference is not checked for type-correctness.
/// </remarks>
public PyLong(IntPtr ptr) : base(ptr) {}
/// <summary>
/// PyLong Constructor
/// </summary>
///
/// <remarks>
/// Copy constructor - obtain a PyLong from a generic PyObject. An
/// ArgumentException will be thrown if the given object is not a
/// Python long object.
/// </remarks>
public PyLong(PyObject o) : base() {
if (!IsLongType(o)) {
throw new ArgumentException("object is not a long");
}
Runtime.Incref(o.obj);
obj = o.obj;
}
/// <summary>
/// PyLong Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyLong from an int32 value.
/// </remarks>
public PyLong(int value) : base() {
obj = Runtime.PyLong_FromLong((long)value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// PyLong Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyLong from a uint32 value.
/// </remarks>
public PyLong(uint value) : base() {
obj = Runtime.PyLong_FromLong((long)value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// PyLong Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyLong from an int64 value.
/// </remarks>
public PyLong(long value) : base() {
obj = Runtime.PyLong_FromLong(value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// PyLong Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyLong from a uint64 value.
/// </remarks>
public PyLong(ulong value) : base() {
obj = Runtime.PyLong_FromUnsignedLongLong(value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// PyLong Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyLong from an int16 value.
/// </remarks>
public PyLong(short value) : base() {
obj = Runtime.PyLong_FromLong((long)value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// PyLong Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyLong from an uint16 value.
/// </remarks>
public PyLong(ushort value) : base() {
obj = Runtime.PyLong_FromLong((long)value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// PyLong Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyLong from a byte value.
/// </remarks>
public PyLong(byte value) : base() {
obj = Runtime.PyLong_FromLong((long)value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// PyLong Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyLong from an sbyte value.
/// </remarks>
public PyLong(sbyte value) : base() {
obj = Runtime.PyLong_FromLong((long)value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// PyLong Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyLong from an double value.
/// </remarks>
public PyLong(double value) : base() {
obj = Runtime.PyLong_FromDouble(value);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// PyLong Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyLong from a string value.
/// </remarks>
public PyLong(string value) : base() {
obj = Runtime.PyLong_FromString(value, IntPtr.Zero, 0);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// IsLongType Method
/// </summary>
///
/// <remarks>
/// Returns true if the given object is a Python long.
/// </remarks>
public static bool IsLongType(PyObject value) {
return Runtime.PyLong_Check(value.obj);
}
/// <summary>
/// AsLong Method
/// </summary>
///
/// <remarks>
/// <remarks>
/// Convert a Python object to a Python long if possible, raising
/// a PythonException if the conversion is not possible. This is
/// equivalent to the Python expression "long(object)".
/// </remarks>
public static PyLong AsLong(PyObject value) {
IntPtr op = Runtime.PyNumber_Long(value.obj);
if (op == IntPtr.Zero) {
throw new PythonException();
}
return new PyLong(op);
}
}
}
=== Added File PythonNet/src/runtime/PyNumber.cs ===
// Copyright (c) 2001, 2002 Zope Corporation and Contributors.
//
// All Rights Reserved.
//
// This software is subject to the provisions of the Zope Public License,
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
// FOR A PARTICULAR PURPOSE.
using System;
namespace Python.Runtime {
/// <summary>
/// Represents a generic Python number. The methods of this class are
/// equivalent to the Python "abstract number API". See
/// http://www.python.org/doc/current/api/number.html for details.
/// </summary>
public class PyNumber : PyObject {
protected PyNumber(IntPtr ptr) : base(ptr) {}
protected PyNumber() : base() {}
/// <summary>
/// IsNumberType Method
/// </summary>
///
/// <remarks>
/// Returns true if the given object is a Python numeric type.
/// </remarks>
public static bool IsNumberType(PyObject value) {
return Runtime.PyNumber_Check(value.obj);
}
// TODO: add all of the PyNumber_XXX methods.
}
}
=== Added File PythonNet/src/runtime/PyObject.cs ===
// Copyright (c) 2001, 2002 Zope Corporation and Contributors.
//
// All Rights Reserved.
//
// This software is subject to the provisions of the Zope Public License,
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
// FOR A PARTICULAR PURPOSE.
using System;
namespace Python.Runtime {
/// <summary>
/// Represents a generic Python object. The methods of this class are
/// generally equivalent to the Python "abstract object API". See
/// http://www.python.org/doc/current/api/object.html for details.
/// </summary>
public class PyObject : IDisposable {
protected internal IntPtr obj = IntPtr.Zero;
/// <summary>
/// PyObject Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyObject from an IntPtr object reference. Note that
/// the PyObject instance assumes ownership of the object reference
/// and the reference will be DECREFed when the PyObject is garbage
/// collected or explicitly disposed.
/// </remarks>
public PyObject(IntPtr ptr) {
obj = ptr;
}
// Protected default constructor to allow subclasses to manage
// initialization in different ways as appropriate.
protected PyObject() {}
/// <summary>
/// Handle Property
/// </summary>
///
/// <remarks>
/// Gets the native handle of the underlying Python object. This
/// value is generally for internal use by the PythonNet runtime.
/// </remarks>
public IntPtr Handle {
get { return obj; }
}
/// <summary>
/// FromManagedObject Method
/// </summary>
///
/// <remarks>
/// Given an arbitrary managed object, return a Python instance that
/// reflects the managed object.
/// </remarks>
public static PyObject FromManagedObject(object ob) {
IntPtr op = CLRObject.GetInstHandle(ob);
return new PyObject(op);
}
/// <summary>
/// Dispose Method
/// </summary>
///
/// <remarks>
/// The Dispose method provides a way to explicitly release the
/// Python object represented by a PyObject instance. It is a good
/// idea to call Dispose on PyObjects that wrap resources that are
/// limited or need strict lifetime control. Otherwise, references
/// to Python objects will not be released until a managed garbage
/// collection occurs.
/// </remarks>
public void Dispose() {
Console.WriteLine("dispose called: {0}", this.ToString());
GC.SuppressFinalize(this);
Runtime.Decref(obj);
obj = IntPtr.Zero;
}
/// <summary>
/// Finalize Method
/// </summary>
///
/// <remarks>
/// Provides support for automatic reference management for PyObject
/// instances. This method is called by the runtime and should not be
/// called by application code.
/// </remarks>
public void Finalize() {
Dispose();
}
/// <summary>
/// GetPythonType Method
/// </summary>
///
/// <remarks>
/// Returns the Python type of the object. This method is equivalent
/// to the Python expression: type(object).
/// </remarks>
public PyObject GetPythonType() {
IntPtr tp = Runtime.PyObject_Type(obj);
return new PyObject(tp);
}
/// <summary>
/// TypeCheck Method
/// </summary>
///
/// <remarks>
/// Returns true if the object o is of type typeOrClass or a subtype
/// of typeOrClass.
/// </remarks>
public bool TypeCheck(PyObject typeOrClass) {
return Runtime.PyObject_TypeCheck(obj, typeOrClass.obj);
}
/// <summary>
/// HasAttr Method
/// </summary>
///
/// <remarks>
/// Returns true if the object has an attribute with the given name.
/// </remarks>
public bool HasAttr(string name) {
return (Runtime.PyObject_HasAttrString(obj, name) != 0);
}
/// <summary>
/// HasAttr Method
/// </summary>
///
/// <remarks>
/// Returns true if the object has an attribute with the given name,
/// where name is a PyObject wrapping a string or unicode object.
/// </remarks>
public bool HasAttr(PyObject name) {
return (Runtime.PyObject_HasAttr(obj, name.obj) != 0);
}
/// <summary>
/// GetAttr Method
/// </summary>
///
/// <remarks>
/// Returns the named attribute of the Python object, or raises a
/// PythonException if the attribute access fails.
/// </remarks>
public PyObject GetAttr(string name) {
IntPtr op = Runtime.PyObject_GetAttrString(obj, name);
if (op == IntPtr.Zero) {
throw new PythonException();
}
return new PyObject(op);
}
/// <summary>
/// GetAttr Method
/// </summary>
///
/// <remarks>
/// Returns the named attribute of the Python object, or the given
/// default object if the attribute access fails.
/// </remarks>
public PyObject GetAttr(string name, PyObject _default) {
IntPtr op = Runtime.PyObject_GetAttrString(obj, name);
if (op == IntPtr.Zero) {
Runtime.PyErr_Clear();
return _default;
}
return new PyObject(op);
}
/// <summary>
/// GetAttr Method
/// </summary>
///
/// <remarks>
/// Returns the named attribute of the Python object or raises a
/// PythonException if the attribute access fails. The name argument
/// is a PyObject wrapping a Python string or unicode object.
/// </remarks>
public PyObject GetAttr(PyObject name) {
IntPtr op = Runtime.PyObject_GetAttr(obj, name.obj);
if (op == IntPtr.Zero) {
throw new PythonException();
}
return new PyObject(op);
}
/// <summary>
/// GetAttr Method
/// </summary>
///
/// <remarks>
/// Returns the named attribute of the Python object, or the given
/// default object if the attribute access fails. The name argument
/// is a PyObject wrapping a Python string or unicode object.
/// </remarks>
public PyObject GetAttr(PyObject name, PyObject _default) {
IntPtr op = Runtime.PyObject_GetAttr(obj, name.obj);
if (op == IntPtr.Zero) {
Runtime.PyErr_Clear();
return _default;
}
return new PyObject(op);
}
/// <summary>
/// SetAttr Method
/// </summary>
///
/// <remarks>
/// Set an attribute of the object with the given name and value. This
/// method throws a PythonException if the attribute set fails.
/// </remarks>
public void SetAttr(string name, PyObject value) {
int r = Runtime.PyObject_SetAttrString(obj, name, value.obj);
if (r < 0) {
throw new PythonException();
}
}
/// <summary>
/// SetAttr Method
/// </summary>
///
/// <remarks>
/// Set an attribute of the object with the given name and value,
/// where the name is a Python string or unicode object. This method
/// throws a PythonException if the attribute set fails.
/// </remarks>
public void SetAttr(PyObject name, PyObject value) {
int r = Runtime.PyObject_SetAttr(obj, name.obj, value.obj);
if (r < 0) {
throw new PythonException();
}
}
/// <summary>
/// DelAttr Method
/// </summary>
///
/// <remarks>
/// Delete the named attribute of the Python object. This method
/// throws a PythonException if the attribute set fails.
/// </remarks>
public void DelAttr(string name) {
int r = Runtime.PyObject_SetAttrString(obj, name, IntPtr.Zero);
if (r < 0) {
throw new PythonException();
}
}
/// <summary>
/// DelAttr Method
/// </summary>
///
/// <remarks>
/// Delete the named attribute of the Python object, where name is a
/// PyObject wrapping a Python string or unicode object. This method
/// throws a PythonException if the attribute set fails.
/// </remarks>
public void DelAttr(PyObject name) {
int r = Runtime.PyObject_SetAttr(obj, name.obj, IntPtr.Zero);
if (r < 0) {
throw new PythonException();
}
}
/// <summary>
/// GetItem Method
/// </summary>
///
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// return the item at the given object index. This method raises a
/// PythonException if the indexing operation fails.
/// </remarks>
public virtual PyObject GetItem(PyObject key) {
IntPtr op = Runtime.PyObject_GetItem(obj, key.obj);
if (op == IntPtr.Zero) {
throw new PythonException();
}
return new PyObject(op);
}
/// <summary>
/// GetItem Method
/// </summary>
///
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// return the item at the given string index. This method raises a
/// PythonException if the indexing operation fails.
/// </remarks>
public virtual PyObject GetItem(string key) {
return GetItem(new PyString(key));
}
/// <summary>
/// GetItem Method
/// </summary>
///
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// return the item at the given numeric index. This method raises a
/// PythonException if the indexing operation fails.
/// </remarks>
public virtual PyObject GetItem(int index) {
PyInt key = new PyInt(index);
return GetItem((PyObject)key);
}
/// <summary>
/// SetItem Method
/// </summary>
///
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// set the item at the given object index to the given value. This
/// method raises a PythonException if the set operation fails.
/// </remarks>
public virtual void SetItem(PyObject key, PyObject value) {
int r = Runtime.PyObject_SetItem(obj, key.obj, value.obj);
if (r < 0) {
throw new PythonException();
}
}
/// <summary>
/// SetItem Method
/// </summary>
///
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// set the item at the given string index to the given value. This
/// method raises a PythonException if the set operation fails.
/// </remarks>
public virtual void SetItem(string key, PyObject value) {
SetItem(new PyString(key), value);
}
/// <summary>
/// SetItem Method
/// </summary>
///
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// set the item at the given numeric index to the given value. This
/// method raises a PythonException if the set operation fails.
/// </remarks>
public virtual void SetItem(int index, PyObject value) {
SetItem(new PyInt(index), value);
}
/// <summary>
/// DelItem Method
/// </summary>
///
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// delete the item at the given object index. This method raises a
/// PythonException if the delete operation fails.
/// </remarks>
public virtual void DelItem(PyObject key) {
int r = Runtime.PyObject_DelItem(obj, key.obj);
if (r < 0) {
throw new PythonException();
}
}
/// <summary>
/// DelItem Method
/// </summary>
///
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// delete the item at the given string index. This method raises a
/// PythonException if the delete operation fails.
/// </remarks>
public virtual void DelItem(string key) {
DelItem(new PyString(key));
}
/// <summary>
/// DelItem Method
/// </summary>
///
/// <remarks>
/// For objects that support the Python sequence or mapping protocols,
/// delete the item at the given numeric index. This method raises a
/// PythonException if the delete operation fails.
/// </remarks>
public virtual void DelItem(int index) {
DelItem(new PyInt(index));
}
/// <summary>
/// Length Method
/// </summary>
///
/// <remarks>
/// Returns the length for objects that support the Python sequence
/// protocol, or 0 if the object does not support the protocol.
/// </remarks>
public virtual int Length() {
int s = Runtime.PyObject_Size(obj);
if (s < 0) {
Runtime.PyErr_Clear();
return 0;
}
return s;
}
/// <summary>
/// String Indexer
/// </summary>
///
/// <remarks>
/// Provides a shorthand for the string versions of the GetItem and
/// SetItem methods.
/// </remarks>
public virtual PyObject this[string key] {
get { return GetItem(key); }
set { SetItem(key, value); }
}
/// <summary>
/// PyObject Indexer
/// </summary>
///
/// <remarks>
/// Provides a shorthand for the object versions of the GetItem and
/// SetItem methods.
/// </remarks>
public virtual PyObject this[PyObject key] {
get { return GetItem(key); }
set { SetItem(key, value); }
}
/// <summary>
/// Numeric Indexer
/// </summary>
///
/// <remarks>
/// Provides a shorthand for the numeric versions of the GetItem and
/// SetItem methods.
/// </remarks>
public virtual PyObject this[int index] {
get { return GetItem(index); }
set { SetItem(index, value); }
}
/// <summary>
/// GetIterator Method
/// </summary>
///
/// <remarks>
/// Return a new (Python) iterator for the object. This is equivalent
/// to the Python expression "iter(object)". A PythonException will be
/// raised if the object cannot be iterated.
/// </remarks>
public PyObject GetIterator() {
IntPtr r = Runtime.PyObject_GetIter(obj);
if (r == IntPtr.Zero) {
throw new PythonException();
}
return new PyObject(r);
}
/// <summary>
/// Invoke Method
/// </summary>
///
/// <remarks>
/// Invoke the callable object with the given arguments, passed as a
/// PyObject[]. A PythonException is raised if the invokation fails.
/// </remarks>
public PyObject Invoke(params PyObject[] args) {
PyTuple t = new PyTuple(args);
IntPtr r = Runtime.PyObject_Call(obj, t.obj, IntPtr.Zero);
t.Dispose();
if (r == IntPtr.Zero) {
throw new PythonException();
}
return new PyObject(r);
}
/// <summary>
/// Invoke Method
/// </summary>
///
/// <remarks>
/// Invoke the callable object with the given arguments, passed as a
/// Python tuple. A PythonException is raised if the invokation fails.
/// </remarks>
public PyObject Invoke(PyTuple args) {
IntPtr r = Runtime.PyObject_Call(obj, args.obj, IntPtr.Zero);
if (r == IntPtr.Zero) {
throw new PythonException();
}
return new PyObject(r);
}
/// <summary>
/// Invoke Method
/// </summary>
///
/// <remarks>
/// Invoke the callable object with the given positional and keyword
/// arguments. A PythonException is raised if the invokation fails.
/// </remarks>
public PyObject Invoke(PyObject[] args, PyDict kw) {
PyTuple t = new PyTuple(args);
IntPtr r = Runtime.PyObject_Call(obj, t.obj, kw.obj);
t.Dispose();
if (r == IntPtr.Zero) {
throw new PythonException();
}
return new PyObject(r);
}
/// <summary>
/// Invoke Method
/// </summary>
///
/// <remarks>
/// Invoke the callable object with the given positional and keyword
/// arguments. A PythonException is raised if the invokation fails.
/// </remarks>
public PyObject Invoke(PyTuple args, PyDict kw) {
IntPtr r = Runtime.PyObject_Call(obj, args.obj, kw.obj);
if (r == IntPtr.Zero) {
throw new PythonException();
}
return new PyObject(r);
}
/// <summary>
/// InvokeMethod Method
/// </summary>
///
/// <remarks>
/// Invoke the named method of the object with the given arguments.
/// A PythonException is raised if the invokation is unsuccessful.
/// </remarks>
public PyObject InvokeMethod(string name, params PyObject[] args) {
PyObject method = GetAttr(name);
PyObject result = method.Invoke(args);
method.Dispose();
return result;
}
/// <summary>
/// InvokeMethod Method
/// </summary>
///
/// <remarks>
/// Invoke the named method of the object with the given arguments.
/// A PythonException is raised if the invokation is unsuccessful.
/// </remarks>
public PyObject InvokeMethod(string name, PyTuple args) {
PyObject method = GetAttr(name);
PyObject result = method.Invoke(args);
method.Dispose();
return result;
}
/// <summary>
/// InvokeMethod Method
/// </summary>
///
/// <remarks>
/// Invoke the named method of the object with the given arguments
/// and keyword arguments. Keyword args are passed as a PyDict object.
/// A PythonException is raised if the invokation is unsuccessful.
/// </remarks>
public PyObject InvokeMethod(string name, PyObject[] args, PyDict kw) {
PyObject method = GetAttr(name);
PyObject result = method.Invoke(args, kw);
method.Dispose();
return result;
}
/// <summary>
/// InvokeMethod Method
/// </summary>
///
/// <remarks>
/// Invoke the named method of the object with the given arguments
/// and keyword arguments. Keyword args are passed as a PyDict object.
/// A PythonException is raised if the invokation is unsuccessful.
/// </remarks>
public PyObject InvokeMethod(string name, PyTuple args, PyDict kw) {
PyObject method = GetAttr(name);
PyObject result = method.Invoke(args, kw);
method.Dispose();
return result;
}
/// <summary>
/// IsInstance Method
/// </summary>
///
/// <remarks>
/// Return true if the object is an instance of the given Python type
/// or class. This method always succeeds.
/// </remarks>
public bool IsInstance(PyObject typeOrClass) {
int r = Runtime.PyObject_IsInstance(obj, typeOrClass.obj);
if (r < 0) {
Runtime.PyErr_Clear();
return false;
}
return (r != 0);
}
/// <summary>
/// IsSubclass Method
/// </summary>
///
/// <remarks>
/// Return true if the object is identical to or derived from the
/// given Python type or class. This method always succeeds.
/// </remarks>
public bool IsSubclass(PyObject typeOrClass) {
int r = Runtime.PyObject_IsSubclass(obj, typeOrClass.obj);
if (r < 0) {
Runtime.PyErr_Clear();
return false;
}
return (r != 0);
}
/// <summary>
/// IsCallable Method
/// </summary>
///
/// <remarks>
/// Returns true if the object is a callable object. This method
/// always succeeds.
/// </remarks>
public bool IsCallable() {
return (Runtime.PyCallable_Check(obj) != 0);
}
/// <summary>
/// IsTrue Method
/// </summary>
///
/// <remarks>
/// Return true if the object is true according to Python semantics.
/// This method always succeeds.
/// </remarks>
public bool IsTrue() {
return (Runtime.PyObject_IsTrue(obj) != 0);
}
/// <summary>
/// Dir Method
/// </summary>
///
/// <remarks>
/// Return a list of the names of the attributes of the object. This
/// is equivalent to the Python expression "dir(object)".
/// </remarks>
public PyList Dir() {
IntPtr r = Runtime.PyObject_Dir(obj);
if (r == IntPtr.Zero) {
throw new PythonException();
}
return new PyList(r);
}
/// <summary>
/// Repr Method
/// </summary>
///
/// <remarks>
/// Return a string representation of the object. This method is
/// the managed equivalent of the Python expression "repr(object)".
/// </remarks>
public string Repr() {
IntPtr strval = Runtime.PyObject_Repr(obj);
string result = Runtime.PyString_AsString(strval);
Runtime.Decref(strval);
return result;
}
/// <summary>
/// ToString Method
/// </summary>
///
/// <remarks>
/// Return the string representation of the object. This method is
/// the managed equivalent of the Python expression "str(object)".
/// </remarks>
public override string ToString() {
IntPtr strval = Runtime.PyObject_Str(obj);
string result = Runtime.PyString_AsString(strval);
Runtime.Decref(strval);
return result;
}
/// <summary>
/// Equals Method
/// </summary>
///
/// <remarks>
/// Return true if this object is equal to the given object. This
/// method is based on Python equality semantics.
/// </remarks>
public override bool Equals(object o) {
if (!(o is PyObject)) {
return false;
}
if (obj == ((PyObject) o).obj) {
return true;
}
int r = Runtime.PyObject_Compare(obj, ((PyObject) o).obj);
if (Exceptions.ErrorOccurred()) {
throw new PythonException();
}
return (r == 0);
}
/// <summary>
/// GetHashCode Method
/// </summary>
///
/// <remarks>
/// Return a hashcode based on the Python object. This returns the
/// hash as computed by Python, equivalent to the Python expression
/// "hash(obj)".
/// </remarks>
public override int GetHashCode() {
return Runtime.PyObject_Hash(obj).ToInt32();
}
}
}
=== Added File PythonNet/src/runtime/PySequence.cs ===
// Copyright (c) 2001, 2002 Zope Corporation and Contributors.
//
// All Rights Reserved.
//
// This software is subject to the provisions of the Zope Public License,
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
// FOR A PARTICULAR PURPOSE.
using System;
namespace Python.Runtime {
/// <summary>
/// Represents a generic Python sequence. The methods of this class are
/// equivalent to the Python "abstract sequence API". See
/// http://www.python.org/doc/current/api/sequence.html for details.
/// </summary>
public class PySequence : PyObject {
protected PySequence(IntPtr ptr) : base(ptr) {}
protected PySequence() : base() {}
/// <summary>
/// IsSequenceType Method
/// </summary>
///
/// <remarks>
/// Returns true if the given object implements the sequence protocol.
/// </remarks>
public static bool IsSequenceType(PyObject value) {
return Runtime.PySequence_Check(value.obj);
}
/// <summary>
/// GetSlice Method
/// </summary>
///
/// <remarks>
/// Return the slice of the sequence with the given indices.
/// </remarks>
public PyObject GetSlice(int i1, int i2) {
IntPtr op = Runtime.PySequence_GetSlice(obj, i1, i2);
if (op == IntPtr.Zero) {
throw new PythonException();
}
return new PyObject(op);
}
/// <summary>
/// SetSlice Method
/// </summary>
///
/// <remarks>
/// Sets the slice of the sequence with the given indices.
/// </remarks>
public void SetSlice(int i1, int i2, PyObject v) {
int r = Runtime.PySequence_SetSlice(obj, i1, i2, v.obj);
if (r < 0) {
throw new PythonException();
}
}
/// <summary>
/// DelSlice Method
/// </summary>
///
/// <remarks>
/// Deletes the slice of the sequence with the given indices.
/// </remarks>
public void DelSlice(int i1, int i2) {
int r = Runtime.PySequence_DelSlice(obj, i1, i2);
if (r < 0) {
throw new PythonException();
}
}
/// <summary>
/// Index Method
/// </summary>
///
/// <remarks>
/// Return the index of the given item in the sequence, or -1 if
/// the item does not appear in the sequence.
/// </remarks>
public int Index(PyObject item) {
int r = Runtime.PySequence_Index(obj, item.obj);
if (r < 0) {
Runtime.PyErr_Clear();
return -1;
}
return r;
}
/// <summary>
/// Contains Method
/// </summary>
///
/// <remarks>
/// Return true if the sequence contains the given item. This method
/// throws a PythonException if an error occurs during the check.
/// </remarks>
public bool Contains(PyObject item) {
int r = Runtime.PySequence_Contains(obj, item.obj);
if (r < 0) {
throw new PythonException();
}
return (r != 0);
}
/// <summary>
/// Concat Method
/// </summary>
///
/// <remarks>
/// Return the concatenation of the sequence object with the passed in
/// sequence object.
/// </remarks>
public PyObject Concat(PyObject other) {
IntPtr op = Runtime.PySequence_Concat(obj, other.obj);
if (op == IntPtr.Zero) {
throw new PythonException();
}
return new PyObject(op);
}
/// <summary>
/// Repeat Method
/// </summary>
///
/// <remarks>
/// Return the sequence object repeated N times. This is equivalent
/// to the Python expression "object * count".
/// </remarks>
public PyObject Repeat(int count) {
IntPtr op = Runtime.PySequence_Repeat(obj, count);
if (op == IntPtr.Zero) {
throw new PythonException();
}
return new PyObject(op);
}
}
}
=== Added File PythonNet/src/runtime/PyString.cs ===
// Copyright (c) 2001, 2002 Zope Corporation and Contributors.
//
// All Rights Reserved.
//
// This software is subject to the provisions of the Zope Public License,
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
// FOR A PARTICULAR PURPOSE.
using System;
namespace Python.Runtime {
/// <summary>
/// Represents a Python (ansi) string object. See the documentation at
/// http://www.python.org/doc/current/api/stringObjects.html for details.
/// </summary>
public class PyString : PySequence {
/// <summary>
/// PyString Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyString from an existing object reference. Note
/// that the instance assumes ownership of the object reference.
/// The object reference is not checked for type-correctness.
/// </remarks>
public PyString(IntPtr ptr) : base(ptr) {}
/// <summary>
/// PyString Constructor
/// </summary>
///
/// <remarks>
/// Copy constructor - obtain a PyString from a generic PyObject.
/// An ArgumentException will be thrown if the given object is not
/// a Python string object.
/// </remarks>
public PyString(PyObject o) : base() {
if (!IsStringType(o)) {
throw new ArgumentException("object is not a string");
}
Runtime.Incref(o.obj);
obj = o.obj;
}
/// <summary>
/// PyString Constructor
/// </summary>
///
/// <remarks>
/// Creates a Python string from a managed string.
/// </remarks>
public PyString(string s) : base() {
obj = Runtime.PyString_FromStringAndSize(s, s.Length);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// IsStringType Method
/// </summary>
///
/// <remarks>
/// Returns true if the given object is a Python string.
/// </remarks>
public static bool IsStringType(PyObject value) {
return Runtime.PyString_Check(value.obj);
}
}
}
=== Added File PythonNet/src/runtime/PyTuple.cs ===
// Copyright (c) 2001, 2002 Zope Corporation and Contributors.
//
// All Rights Reserved.
//
// This software is subject to the provisions of the Zope Public License,
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
// FOR A PARTICULAR PURPOSE.
using System;
namespace Python.Runtime {
/// <summary>
/// Represents a Python tuple object. See the documentation at
/// http://www.python.org/doc/current/api/tupleObjects.html for details.
/// </summary>
public class PyTuple : PySequence {
/// <summary>
/// PyTuple Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyTuple from an existing object reference. Note
/// that the instance assumes ownership of the object reference.
/// The object reference is not checked for type-correctness.
/// </remarks>
public PyTuple(IntPtr ptr) : base(ptr) {}
/// <summary>
/// PyTuple Constructor
/// </summary>
///
/// <remarks>
/// Copy constructor - obtain a PyTuple from a generic PyObject. An
/// ArgumentException will be thrown if the given object is not a
/// Python tuple object.
/// </remarks>
public PyTuple(PyObject o) : base() {
if (!IsTupleType(o)) {
throw new ArgumentException("object is not a tuple");
}
Runtime.Incref(o.obj);
obj = o.obj;
}
/// <summary>
/// PyTuple Constructor
/// </summary>
///
/// <remarks>
/// Creates a new empty PyTuple.
/// </remarks>
public PyTuple() : base() {
obj = Runtime.PyTuple_New(0);
if (obj == IntPtr.Zero) {
throw new PythonException();
}
}
/// <summary>
/// PyTuple Constructor
/// </summary>
///
/// <remarks>
/// Creates a new PyTuple from an array of PyObject instances.
/// </remarks>
public PyTuple(PyObject[] items) : base() {
int count = items.Length;
obj = Runtime.PyTuple_New(count);
for (int i = 0; i < count; i++) {
IntPtr ptr = items[i].obj;
Runtime.Incref(ptr);
int r = Runtime.PyTuple_SetItem(obj, i, ptr);
if (r < 0) {
throw new PythonException();
}
}
}
/// <summary>
/// IsTupleType Method
/// </summary>
///
/// <remarks>
/// Returns true if the given object is a Python tuple.
/// </remarks>
public static bool IsTupleType(PyObject value) {
return Runtime.PyTuple_Check(value.obj);
}
/// <summary>
/// AsTuple Method
/// </summary>
///
/// <remarks>
/// Convert a Python object to a Python tuple if possible, raising
/// a PythonException if the conversion is not possible. This is
/// equivalent to the Python expression "tuple(object)".
/// </remarks>
public static PyTuple AsTuple(PyObject value) {
IntPtr op = Runtime.PySequence_Tuple(value.obj);
if (op == IntPtr.Zero) {
throw new PythonException();
}
return new PyTuple(op);
}
}
}
=== PythonNet/src/runtime/Converter.cs 1.2 => 1.3 ===
--- PythonNet/src/runtime/Converter.cs:1.2 Mon Jul 28 22:28:15 2003
+++ PythonNet/src/runtime/Converter.cs Thu Sep 11 23:49:34 2003
@@ -79,20 +79,20 @@
return Runtime.PyUnicode_FromString((string)value);
case TypeCode.Int32:
- return Runtime.PyInt_FromLong((int)value);
+ return Runtime.PyInt_FromInt32((int)value);
case TypeCode.Boolean:
int i = (bool) value ? 1 : 0;
- return Runtime.PyInt_FromLong(i);
+ return Runtime.PyInt_FromInt32(i);
case TypeCode.Byte:
- return Runtime.PyInt_FromLong((int)((byte)value));
+ return Runtime.PyInt_FromInt32((int)((byte)value));
case TypeCode.Char:
return Runtime.PyUnicode_FromOrdinal((int)((char)value));
case TypeCode.Int16:
- return Runtime.PyInt_FromLong((int)((short)value));
+ return Runtime.PyInt_FromInt32((int)((short)value));
case TypeCode.Int64:
return Runtime.PyLong_FromLongLong((long)value);
@@ -108,10 +108,10 @@
return Runtime.PyFloat_FromDouble((double)value);
case TypeCode.SByte:
- return Runtime.PyInt_FromLong((int)((sbyte)value));
+ return Runtime.PyInt_FromInt32((int)((sbyte)value));
case TypeCode.UInt16:
- return Runtime.PyInt_FromLong((int)((ushort)value));
+ return Runtime.PyInt_FromInt32((int)((ushort)value));
case TypeCode.UInt32:
return Runtime.PyLong_FromUnsignedLong((uint)value);
=== PythonNet/src/runtime/Runtime.cs 1.6 => 1.7 ===
--- PythonNet/src/runtime/Runtime.cs:1.6 Fri Aug 8 15:49:21 2003
+++ PythonNet/src/runtime/Runtime.cs Thu Sep 11 23:49:34 2003
@@ -67,7 +67,7 @@
PyDictType = Runtime.PyObject_Type(op);
Runtime.Decref(op);
- op = Runtime.PyInt_FromLong(0);
+ op = Runtime.PyInt_FromInt32(0);
PyIntType = Runtime.PyObject_Type(op);
Runtime.Decref(op);
@@ -431,6 +431,11 @@
[DllImport("python22", CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
+ PyObject_GetIter(IntPtr op);
+
+ [DllImport("python22", CallingConvention=CallingConvention.Cdecl,
+ ExactSpelling=true, CharSet=CharSet.Ansi)]
+ internal unsafe static extern IntPtr
PyObject_Call(IntPtr pointer, IntPtr args, IntPtr kw);
[DllImport("python22", CallingConvention=CallingConvention.Cdecl,
@@ -484,34 +489,59 @@
PyObject_Str(IntPtr pointer);
+ [DllImport("python22", CallingConvention=CallingConvention.Cdecl,
+ ExactSpelling=true, CharSet=CharSet.Ansi)]
+ internal unsafe static extern IntPtr
+ PyObject_Dir(IntPtr pointer);
+
+
//====================================================================
// Python number API
//====================================================================
[DllImport("python22", CallingConvention=CallingConvention.Cdecl,
- ExactSpelling=true, CharSet=CharSet.Ansi)]
+ ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyNumber_Int(IntPtr ob);
[DllImport("python22", CallingConvention=CallingConvention.Cdecl,
- ExactSpelling=true, CharSet=CharSet.Ansi)]
+ ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyNumber_Long(IntPtr ob);
[DllImport("python22", CallingConvention=CallingConvention.Cdecl,
- ExactSpelling=true, CharSet=CharSet.Ansi)]
+ ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
PyNumber_Float(IntPtr ob);
+ [DllImport("python22", CallingConvention=CallingConvention.Cdecl,
+ ExactSpelling=true, CharSet=CharSet.Ansi)]
+ internal unsafe static extern bool
+ PyNumber_Check(IntPtr ob);
+
+
internal static bool PyInt_Check(IntPtr ob) {
return PyObject_TYPE(ob) == Runtime.PyIntType;
}
+
+
[DllImport("python22", CallingConvention=CallingConvention.Cdecl,
- ExactSpelling=true, CharSet=CharSet.Ansi)]
- internal unsafe static extern IntPtr
- PyInt_FromLong(int value);
+ ExactSpelling=true, CharSet=CharSet.Ansi)]
+ private unsafe static extern IntPtr
+ PyInt_FromLong(IntPtr value);
+
+ internal static IntPtr PyInt_FromInt32(int value) {
+ IntPtr v = new IntPtr(value);
+ return PyInt_FromLong(v);
+ }
+
+ internal static IntPtr PyInt_FromInt64(long value) {
+ IntPtr v = new IntPtr(value);
+ return PyInt_FromLong(v);
+ }
+
[DllImport("python22", CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
@@ -583,6 +613,11 @@
internal unsafe static extern ulong
PyLong_AsUnsignedLongLong(IntPtr value);
+
+ internal static bool PyFloat_Check(IntPtr ob) {
+ return PyObject_TYPE(ob) == Runtime.PyFloatType;
+ }
+
[DllImport("python22", CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
internal unsafe static extern IntPtr
@@ -605,7 +640,7 @@
[DllImport("python22", CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
- internal unsafe static extern int
+ internal unsafe static extern bool
PySequence_Check(IntPtr pointer);
[DllImport("python22", CallingConvention=CallingConvention.Cdecl,
@@ -623,6 +658,20 @@
internal unsafe static extern int
PySequence_DelItem(IntPtr pointer, int index);
+ [DllImport("python22", CallingConvention=CallingConvention.Cdecl,
+ ExactSpelling=true, CharSet=CharSet.Ansi)]
+ internal unsafe static extern IntPtr
+ PySequence_GetSlice(IntPtr pointer, int i1, int i2);
+
+ [DllImport("python22", CallingConvention=CallingConvention.Cdecl,
+ ExactSpelling=true, CharSet=CharSet.Ansi)]
+ internal unsafe static extern int
+ PySequence_SetSlice(IntPtr pointer, int i1, int i2, IntPtr v);
+
+ [DllImport("python22", CallingConvention=CallingConvention.Cdecl,
+ ExactSpelling=true, CharSet=CharSet.Ansi)]
+ internal unsafe static extern int
+ PySequence_DelSlice(IntPtr pointer, int i1, int i2);
[DllImport("python22", CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, CharSet=CharSet.Ansi)]
More information about the Zope-CVS
mailing list