示例#1
0
        private static void AddReferenceToFileAndPath(CodeContext /*!*/ context, string file)
        {
            if (file == null)
            {
                throw PythonOps.TypeError("Expected string, got NoneType");
            }

            // update our path w/ the path of this file...
            string path = System.IO.Path.GetDirectoryName(file);
            List   list;

            PythonContext pc = PythonContext.GetContext(context);

            if (!pc.TryGetSystemPath(out list))
            {
                throw PythonOps.TypeError("cannot update path, it is not a list");
            }

            list.append(path);

            Assembly asm = pc.LoadAssemblyFromFile(file);

            if (asm == null)
            {
                throw PythonOps.IOError("file does not exist: {0}", file);
            }
            AddReference(context, asm);
        }
示例#2
0
        internal static SourceUnit TryFindSourceFile(PythonContext /*!*/ context, string /*!*/ name)
        {
            List paths;

            if (!context.TryGetSystemPath(out paths))
            {
                return(null);
            }

            foreach (object dirObj in paths)
            {
                string directory = dirObj as string;
                if (directory == null)
                {
                    continue;                     // skip invalid entries
                }
                string          candidatePath     = null;
                LanguageContext candidateLanguage = null;
                foreach (string extension in context.DomainManager.Configuration.GetFileExtensions())
                {
                    string fullPath;

                    try {
                        fullPath = Path.Combine(directory, name + extension);
                    } catch (ArgumentException) {
                        // skip invalid paths
                        continue;
                    }

                    if (context.DomainManager.Platform.FileExists(fullPath))
                    {
                        if (candidatePath != null)
                        {
                            throw PythonOps.ImportError(String.Format("Found multiple modules of the same name '{0}': '{1}' and '{2}'",
                                                                      name, candidatePath, fullPath));
                        }

                        candidatePath     = fullPath;
                        candidateLanguage = context.DomainManager.GetLanguageByExtension(extension);
                    }
                }

                if (candidatePath != null)
                {
                    return(candidateLanguage.CreateFileUnit(candidatePath));
                }
            }

            return(null);
        }
示例#3
0
        internal static SourceUnit TryFindSourceFile(PythonContext/*!*/ context, string/*!*/ name) {
            List paths;
            if (!context.TryGetSystemPath(out paths)) {
                return null;
            }

            foreach (object dirObj in paths) {
                string directory = dirObj as string;
                if (directory == null) continue;  // skip invalid entries

                string candidatePath = null;
                LanguageContext candidateLanguage = null;
                foreach (string extension in context.DomainManager.Configuration.GetFileExtensions()) {
                    string fullPath;

                    try {
                        fullPath = Path.Combine(directory, name + extension);
                    } catch (ArgumentException) {
                        // skip invalid paths
                        continue;
                    }

                    if (context.DomainManager.Platform.FileExists(fullPath)) {
                        if (candidatePath != null) {
                            throw PythonOps.ImportError(String.Format("Found multiple modules of the same name '{0}': '{1}' and '{2}'",
                                name, candidatePath, fullPath));
                        }

                        candidatePath = fullPath;
                        candidateLanguage = context.DomainManager.GetLanguageByExtension(extension);
                    }
                }

                if (candidatePath != null) {
                    return candidateLanguage.CreateFileUnit(candidatePath);
                }
            }

            return null;
        }