Пример #1
0
        /// <summary>
        /// 尝试重新加载脚本
        /// </summary>
        /// <param name="moduleName"></param>
        /// <param name="scope"></param>
        /// <returns></returns>
        public static bool TryReLoadScript(string moduleName, out dynamic scope)
        {
            moduleName = (moduleName ?? "").Replace(@"\", "/");
            scope      = null;
            string fileName = Path.Combine(RuntimePath, moduleName);

            try
            {
                lock (syncRoot)
                {
                    if (_pyRuntime != null)
                    {
                        //使用UseFile加载相同的文件时,Scope不会改变;
                        //scope = _pyRuntime.UseFile(fileName);
                        scope = _pyRuntime.ExecuteFile(fileName);
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("Python script import \"{0}\" error\r\n{1}", fileName, ex);
            }
            return(false);
        }
Пример #2
0
        private static void ReloadChangedScriptFiles()
        {
            // Nothing to do if no files changed
            if (!s_appScriptDirChanged)
            {
                return;
            }

            // TODO: a new ScriptRuntime should be created instead
            foreach (string path in Directory.GetFiles(s_scriptFolder))
            {
                string ext = Path.GetExtension(path);
                if (IsDLRLanguageExtension(ext))
                {
                    try {
                        s_scriptEnvironment.ExecuteFile(path);
                    } catch (SyntaxErrorException e) {
                        EngineHelper.ThrowSyntaxErrorException(e);
                    }
                }
            }

            // Clear out the flag to mark that all the changes were processed
            s_appScriptDirChanged = false;
        }
Пример #3
0
        [Test]//regression test for DDB 488971
        public void ExecuteFile_RemoteADAndSyntaxError()
        {
            AppDomain     ad      = TestHelpers.CreateAppDomain("alternate 1");
            ScriptRuntime runtime = CreateRemoteRuntime(ad);

            string tmpFile = TestHelpers.CreateTempSourceFile("if", "py");

            runtime.ExecuteFile(tmpFile); //throws 'SourceUnit not serializable exception'
        }
Пример #4
0
        [Ignore] //bug ID : 446714
        public void ExecuteFile_InvalidPath()
        {
            ScriptRuntime runTime = CreateRuntime();

            // BUG : An invalid path throws an 'Microsoft.Scripting.SyntaxErrorException'
            string[] paths = StandardTestPaths.CreateBadPathCombinations(Path.GetTempPath());
            foreach (string p in paths)
            {
                runTime.ExecuteFile(p + "foo.py");
            }
        }
Пример #5
0
        public void RunFromFile()
        {
            ScriptRuntime env = ScriptRuntime.CreateFromConfiguration();

            var scope = env.Globals;

            scope.SetVariable("b", 7);

            scope = env.ExecuteFile("IronPython.py");

            var a = scope.GetVariable <int>("a");

            Console.WriteLine(a);
        }
Пример #6
0
        //[Ignore] // Bug see ResolveSourceFileName bug
        public void ResolveSourceFile_PassValidName()
        {
            // Get source
            string testSrc = _codeSnippets[CodeType.OneLineAssignmentStatement];

            // Setup env for search.
            string tmpFileName = TestHelpers.CreateTempSourceFile(testSrc, ".py");

            // This could possble enable the host to be aware of other search path
            ScriptRuntime newRuntimeEnv = CreateHostRuntime(typeof(ScriptHostBasicSubTest),
                                                            Path.GetDirectoryName(tmpFileName));
            ScriptHostBasicSubTest host  = (ScriptHostBasicSubTest)newRuntimeEnv.Host;
            ScriptScope            scope = newRuntimeEnv.ExecuteFile(tmpFileName);

            // Using abs path tmpFileName
            ValidateResolveSourceFileSearchResult(host, tmpFileName, Path.GetDirectoryName(tmpFileName));
        }