本文整理汇总了C#中Python.Runtime.PyObject类的典型用法代码示例。如果您正苦于以下问题:C# PyObject类的具体用法?C# PyObject怎么用?C# PyObject使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PyObject类属于Python.Runtime命名空间,在下文中一共展示了PyObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PythonException
public PythonException() : base()
{
IntPtr gs = PythonEngine.AcquireLock();
Runtime.PyErr_Fetch(ref _pyType, ref _pyValue, ref _pyTB);
Runtime.Incref(_pyType);
Runtime.Incref(_pyValue);
Runtime.Incref(_pyTB);
if ((_pyType != IntPtr.Zero) && (_pyValue != IntPtr.Zero))
{
string type;
string message;
Runtime.Incref(_pyType);
using (PyObject pyType = new PyObject(_pyType))
using (PyObject pyTypeName = pyType.GetAttr("__name__"))
{
type = pyTypeName.ToString();
}
Runtime.Incref(_pyValue);
using (PyObject pyValue = new PyObject(_pyValue))
{
message = pyValue.ToString();
}
_message = type + " : " + message;
}
if (_pyTB != IntPtr.Zero)
{
PyObject tb_module = PythonEngine.ImportModule("traceback");
Runtime.Incref(_pyTB);
using (PyObject pyTB = new PyObject(_pyTB)) {
_tb = tb_module.InvokeMethod("format_tb", pyTB).ToString();
}
}
PythonEngine.ReleaseLock(gs);
}
示例2: PyLong
/// <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;
}
示例3: PyDict
/// <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;
}
示例4: PyTuple
/// <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;
}
示例5: SetSlice
/// <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();
}
}
示例6: Concat
/// <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);
}
示例7: Contains
/// <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);
}
示例8: PyInt
/// <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.XIncref(o.obj);
obj = o.obj;
}
示例9: PyAnsiString
/// <summary>
/// PyString Constructor
/// </summary>
///
/// <remarks>
/// Copy constructor - obtain a PyAnsiString from a generic PyObject.
/// An ArgumentException will be thrown if the given object is not
/// a Python string object.
/// </remarks>
public PyAnsiString(PyObject o)
: base()
{
if (!IsStringType(o))
{
throw new ArgumentException("object is not a string");
}
Runtime.XIncref(o.obj);
obj = o.obj;
}
示例10: GetCLRModule
//===================================================================
// Return the clr python module (new reference)
//===================================================================
public static IntPtr GetCLRModule(IntPtr? fromList = null)
{
root.InitializePreload();
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35)
// update the module dictionary with the contents of the root dictionary
root.LoadNames();
IntPtr py_mod_dict = Runtime.PyModule_GetDict(py_clr_module);
IntPtr clr_dict = Runtime._PyObject_GetDictPtr(root.pyHandle); // PyObject**
clr_dict = (IntPtr)Marshal.PtrToStructure(clr_dict, typeof(IntPtr));
Runtime.PyDict_Update(py_mod_dict, clr_dict);
// find any items from the fromlist and get them from the root if they're not
// aleady in the module dictionary
if (fromList != null && fromList != IntPtr.Zero) {
if (Runtime.PyTuple_Check(fromList.GetValueOrDefault()))
{
Runtime.XIncref(py_mod_dict);
using(PyDict mod_dict = new PyDict(py_mod_dict)) {
Runtime.XIncref(fromList.GetValueOrDefault());
using (PyTuple from = new PyTuple(fromList.GetValueOrDefault())) {
foreach (PyObject item in from) {
if (mod_dict.HasKey(item))
continue;
string s = item.AsManagedObject(typeof(string)) as string;
if (null == s)
continue;
ManagedType attr = root.GetAttribute(s, true);
if (null == attr)
continue;
Runtime.XIncref(attr.pyHandle);
using (PyObject obj = new PyObject(attr.pyHandle)) {
mod_dict.SetItem(s, obj);
}
}
}
}
}
}
Runtime.XIncref(py_clr_module);
return py_clr_module;
#else
Runtime.XIncref(root.pyHandle);
return root.pyHandle;
#endif
}
示例11: CallEchoString
// This method calls back into the CPython runtime - tests
// call this from Python to check that we don't hang on
// nested transitions from managed to Python code and back.
public static string CallEchoString(string arg) {
IntPtr gs = PythonEngine.AcquireLock();
if (module == null) {
module = PythonEngine.ModuleFromString("tt", testmod);
}
PyObject func = module.GetAttr("echostring");
PyString parg = new PyString(arg);
PyObject temp = func.Invoke(parg);
string result = (string)temp.AsManagedObject(typeof(String));
func.Dispose();
parg.Dispose();
temp.Dispose();
PythonEngine.ReleaseLock(gs);
return result;
}
示例12: InvokeMethod
/// <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;
}
示例13: IsSubclass
/// <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);
}
示例14: HasAttr
/// <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);
}
示例15: Invoke
/// <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 != null ? kw.obj : IntPtr.Zero);
t.Dispose();
if (r == IntPtr.Zero) {
throw new PythonException();
}
return new PyObject(r);
}