/// <summary> /// PyList Constructor /// </summary> /// <remarks> /// Creates a new empty Python list object. /// </remarks> public PyList() : base(Runtime.PyList_New(0)) { if (obj == IntPtr.Zero) { throw PythonException.ThrowLastAsClrException(); } }
/// <summary> /// PyList Constructor /// </summary> /// <remarks> /// Creates a new empty Python list object. /// </remarks> public PyList() { 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) { int count = items.Length; obj = 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); if (r < 0) { throw new PythonException(); } } }
private static IntPtr FromArray(PyObject[] items) { int count = items.Length; 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(val, i, ptr); if (r < 0) { Runtime.Py_DecRef(val); throw PythonException.ThrowLastAsClrException(); } } return(val); }
public ModuleObject(string name) { if (name == string.Empty) { throw new ArgumentException("Name must not be empty!"); } moduleName = name; cache = new Dictionary <string, ManagedType>(); _namespace = name; // Use the filename from any of the assemblies just so there's something for // anything that expects __file__ to be set. var filename = "unknown"; var docstring = "Namespace containing types from the following assemblies:\n\n"; foreach (Assembly a in AssemblyManager.GetAssemblies(name)) { if (!a.IsDynamic && a.Location != null) { filename = a.Location; } docstring += "- " + a.FullName + "\n"; } var dictRef = Runtime.PyObject_GenericGetDict(ObjectReference); PythonException.ThrowIfIsNull(dictRef); dict = dictRef.DangerousMoveToPointer(); __all__ = Runtime.PyList_New(0); using var pyname = NewReference.DangerousFromPointer(Runtime.PyString_FromString(moduleName)); using var pyfilename = NewReference.DangerousFromPointer(Runtime.PyString_FromString(filename)); using var pydocstring = NewReference.DangerousFromPointer(Runtime.PyString_FromString(docstring)); BorrowedReference pycls = TypeManager.GetTypeReference(GetType()); Runtime.PyDict_SetItem(DictRef, PyIdentifier.__name__, pyname); Runtime.PyDict_SetItem(DictRef, PyIdentifier.__file__, pyfilename); Runtime.PyDict_SetItem(DictRef, PyIdentifier.__doc__, pydocstring); Runtime.PyDict_SetItem(DictRef, PyIdentifier.__class__, pycls); InitializeModuleMembers(); }