Пример #1
0
        private void SetupStreams(ref ScriptRuntime runtime)
        {
            // set output stream
            PyObject sys = PythonEngine.ImportModule("sys");

            sys.SetAttr("stdout", PyObject.FromManagedObject(runtime.OutputStream));
        }
Пример #2
0
        public PyObject TryEncode(object value)
        {
            if (value is null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            return(PyObject.FromManagedObject(value));
        }
Пример #3
0
        private void SetupCaching(ref ScriptRuntime runtime)
        {
            // set output stream
            PyObject sys = PythonEngine.ImportModule("sys");

            // dont write bytecode (__pycache__)
            // https://docs.python.org/3.7/library/sys.html?highlight=pythondontwritebytecode#sys.dont_write_bytecode
            sys.SetAttr("dont_write_bytecode", PyObject.FromManagedObject(true));
        }
Пример #4
0
        public void RawListProxy()
        {
            var list = new List <string> {
                "hello", "world"
            };
            var listProxy = PyObject.FromManagedObject(list);
            var clrObject = (CLRObject)ManagedType.GetManagedObject(listProxy.Handle);

            Assert.AreSame(list, clrObject.inst);
        }
Пример #5
0
        public void RawPyObjectProxy()
        {
            var pyObject      = "hello world!".ToPython();
            var pyObjectProxy = PyObject.FromManagedObject(pyObject);
            var clrObject     = (CLRObject)ManagedType.GetManagedObject(pyObjectProxy.Handle);

            Assert.AreSame(pyObject, clrObject.inst);

            var proxiedHandle = pyObjectProxy.GetAttr("Handle").As <IntPtr>();

            Assert.AreEqual(pyObject.Handle, proxiedHandle);
        }
Пример #6
0
        public void RawPyObjectProxy()
        {
            var pyObject      = "hello world!".ToPython();
            var pyObjectProxy = PyObject.FromManagedObject(pyObject);
            var clrObject     = (CLRObject)ManagedType.GetManagedObject(pyObjectProxy);

            Assert.AreSame(pyObject, clrObject.inst);

#pragma warning disable CS0612 // Type or member is obsolete
            const string handlePropertyName = nameof(PyObject.Handle);
#pragma warning restore CS0612 // Type or member is obsolete
            var proxiedHandle = pyObjectProxy.GetAttr(handlePropertyName).As <IntPtr>();
            Assert.AreEqual(pyObject.DangerousGetAddressOrNull(), proxiedHandle);
        }
Пример #7
0
 public PyObject TryEncode(object value) => PyObject.FromManagedObject(this);
Пример #8
0
 public PyObject TryEncode(object value)
 {
     // This is a no-op to prevent Python.NET from encoding generic lists
     // https://github.com/pythonnet/pythonnet/pull/963#issuecomment-642938541
     return(PyObject.FromManagedObject(value));
 }
Пример #9
0
 private static void SetVariable(IntPtr?globals, string key, object value)
 {
     SetVariable(globals, key, PyObject.FromManagedObject(value).Handle);
 }