Represents a Python (ansi) string object. See the documentation at http://www.python.org/doc/current/api/stringObjects.html for details.
Inheritance: PySequence
Ejemplo n.º 1
0
        /// <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();
            }
        }
Ejemplo n.º 2
0
        private PyObject GetPythonActionContext(ActionContext context)
        {
            PyDict dict = new PyDict();
            PyDict specialKeyStateDict = new PyDict();
            specialKeyStateDict["CtrlPressed"] = new PyString(context.SpecialKeyState.CtrlPressed.ToString());
            specialKeyStateDict["AltPressed"] = new PyString(context.SpecialKeyState.AltPressed.ToString());
            specialKeyStateDict["WinPressed"] = new PyString(context.SpecialKeyState.WinPressed.ToString());
            specialKeyStateDict["ShiftPressed"] = new PyString(context.SpecialKeyState.ShiftPressed.ToString());

            dict["SpecialKeyState"] = specialKeyStateDict;
            return dict;
        }
Ejemplo n.º 3
0
	// 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;
	}
Ejemplo n.º 4
0
 /// <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)
 {
     using (PyString pyKey = new PyString(key))
     {
         SetItem(pyKey, value);
     }
 }
Ejemplo n.º 5
0
 /// <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)
 {
     using (PyString pyKey = new PyString(key))
     {
         return GetItem(pyKey);
     }
 }
Ejemplo n.º 6
0
 /// <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)
 {
     using (PyString pyKey = new PyString(key))
     {
         DelItem(pyKey);
     }
 }
Ejemplo n.º 7
0
 public void TestUnicode()
 {
     PyObject s = new PyString("foo\u00e9");
     Assert.AreEqual("foo\u00e9",s.ToString());
 }
Ejemplo n.º 8
0
 /// <summary>
 /// HasKey Method
 /// </summary>
 ///
 /// <remarks>
 /// Returns true if the string key appears in the dictionary.
 /// </remarks>
 public bool HasKey(string key)
 {
     using (PyString str = new PyString(key))
         return HasKey(str);
 }
Ejemplo n.º 9
0
        private void OnBotCommandMessage(InvokeCommandMessage message)
        {
            string commandName = message.Command.Substring(1);

            try
            {
                string args = "";
                if (message.IrcEventArgs.Data.MessageArray.Length > 1)
                    args =  string.Join(" ", message.IrcEventArgs.Data.MessageArray.Skip(1));

                PyString param = new PyString(args);
                IntPtr ptr = PythonEngine.AcquireLock();
                module.SetAttr(new PyString("sys.argv"), new PyString("param"));
                PyObject result = module.InvokeMethod(commandName, param);
                PythonEngine.ReleaseLock(ptr);

                hub.Publish<IrcSendMessage>(new IrcSendMessage(this, Meebey.SmartIrc4net.SendType.Message, message.IrcEventArgs.Data.Irc.Address, message.IrcEventArgs.Data.Channel, result.ToString()));
            }
            catch (Exception ex)
            {
                log.Debug("Could not invoke Python function", ex);
            }
        }
Ejemplo n.º 10
0
        internal static IntPtr CreateSubType(IntPtr py_name, IntPtr py_base_type, IntPtr py_dict)
        {
            var dictRef = new BorrowedReference(py_dict);
            // Utility to create a subtype of a managed type with the ability for the
            // a python subtype able to override the managed implementation
            string name = Runtime.GetManagedString(py_name);

            // the derived class can have class attributes __assembly__ and __module__ which
            // control the name of the assembly and module the new type is created in.
            object assembly     = null;
            object namespaceStr = null;

            using (var assemblyKey = new PyString("__assembly__"))
            {
                var assemblyPtr = Runtime.PyDict_GetItemWithError(dictRef, assemblyKey.Reference);
                if (assemblyPtr.IsNull)
                {
                    if (Exceptions.ErrorOccurred())
                    {
                        return(IntPtr.Zero);
                    }
                }
                else if (!Converter.ToManagedValue(assemblyPtr, typeof(string), out assembly, false))
                {
                    return(Exceptions.RaiseTypeError("Couldn't convert __assembly__ value to string"));
                }

                using (var namespaceKey = new PyString("__namespace__"))
                {
                    var pyNamespace = Runtime.PyDict_GetItemWithError(dictRef, namespaceKey.Reference);
                    if (pyNamespace.IsNull)
                    {
                        if (Exceptions.ErrorOccurred())
                        {
                            return(IntPtr.Zero);
                        }
                    }
                    else if (!Converter.ToManagedValue(pyNamespace, typeof(string), out namespaceStr, false))
                    {
                        return(Exceptions.RaiseTypeError("Couldn't convert __namespace__ value to string"));
                    }
                }
            }

            // create the new managed type subclassing the base managed type
            var baseClass = ManagedType.GetManagedObject(py_base_type) as ClassBase;

            if (null == baseClass)
            {
                return(Exceptions.RaiseTypeError("invalid base class, expected CLR class type"));
            }

            try
            {
                Type subType = ClassDerivedObject.CreateDerivedType(name,
                                                                    baseClass.type,
                                                                    py_dict,
                                                                    (string)namespaceStr,
                                                                    (string)assembly);

                // create the new ManagedType and python type
                ClassBase subClass = ClassManager.GetClass(subType);
                IntPtr    py_type  = GetTypeHandle(subClass, subType);

                // by default the class dict will have all the C# methods in it, but as this is a
                // derived class we want the python overrides in there instead if they exist.
                IntPtr cls_dict = Marshal.ReadIntPtr(py_type, TypeOffset.tp_dict);
                ThrowIfIsNotZero(Runtime.PyDict_Update(cls_dict, py_dict));
                Runtime.XIncref(py_type);
                // Update the __classcell__ if it exists
                var cell = new BorrowedReference(Runtime.PyDict_GetItemString(cls_dict, "__classcell__"));
                if (!cell.IsNull)
                {
                    ThrowIfIsNotZero(Runtime.PyCell_Set(cell, py_type));
                    ThrowIfIsNotZero(Runtime.PyDict_DelItemString(cls_dict, "__classcell__"));
                }

                return(py_type);
            }
            catch (Exception e)
            {
                return(Exceptions.RaiseTypeError(e.Message));
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// HasKey Method
        /// </summary>
        ///
        /// <remarks>
        /// Returns true if the string key appears in the dictionary.
        /// </remarks>

        public bool HasKey(string key)
        {
            using (PyString str = new PyString(key))
                return(HasKey(str));
        }