Commit ca858cc1 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #141 from tjhance/ospath

Had to merge in some recent changes
- compileAndRunModule -> createAndRunModule
- new "allow-warning" directive syntax in tests
parents 1b012805 2a223ebd
......@@ -35,6 +35,7 @@
#include "runtime/complex.h"
#include "runtime/float.h"
#include "runtime/generator.h"
#include "runtime/import.h"
#include "runtime/inline/boxing.h"
#include "runtime/int.h"
#include "runtime/long.h"
......
......@@ -22,6 +22,7 @@
#include "codegen/compvars.h"
#include "core/threading.h"
#include "core/types.h"
#include "runtime/import.h"
#include "runtime/objmodel.h"
#include "runtime/types.h"
......
// Copyright (c) 2014 Dropbox, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "runtime/import.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "codegen/irgen/hooks.h"
#include "codegen/parser.h"
#include "runtime/capi.h"
#include "runtime/objmodel.h"
namespace pyston {
BoxedModule* createAndRunModule(const std::string& name, const std::string& fn) {
BoxedModule* module = createModule(name, fn);
AST_Module* ast = caching_parse(fn.c_str());
compileAndRunModule(ast, module);
return module;
}
#if LLVMREV < 210072
#define LLVM_SYS_FS_EXISTS_CODE_OKAY(code) ((code) == 0)
#else
#define LLVM_SYS_FS_EXISTS_CODE_OKAY(code) (!(code))
#endif
extern "C" Box* import(const std::string* name) {
assert(name);
static StatCounter slowpath_import("slowpath_import");
slowpath_import.log();
Box* parent_module = NULL;
size_t periodIndex = name->rfind('.');
if (periodIndex != std::string::npos) {
std::string parent_name(*name, 0, periodIndex);
parent_module = import(&parent_name);
}
BoxedDict* sys_modules = getSysModulesDict();
Box* s = boxStringPtr(name);
if (sys_modules->d.find(s) != sys_modules->d.end())
return sys_modules->d[s];
BoxedList* path_list;
if (parent_module == NULL) {
path_list = getSysPath();
if (path_list == NULL || path_list->cls != list_cls) {
raiseExcHelper(RuntimeError, "sys.path must be a list of directory names");
}
} else {
path_list = static_cast<BoxedList*>(parent_module->getattr("__path__", NULL));
if (path_list == NULL || path_list->cls != list_cls) {
raiseExcHelper(ImportError, "No module named %s", name->c_str());
}
}
llvm::SmallString<128> joined_path;
for (int i = 0; i < path_list->size; i++) {
Box* _p = path_list->elts->elts[i];
if (_p->cls != str_cls)
continue;
BoxedString* p = static_cast<BoxedString*>(_p);
joined_path.clear();
llvm::sys::path::append(joined_path, p->s, *name + ".py");
std::string fn(joined_path.str());
if (VERBOSITY() >= 2)
printf("Searching for %s at %s...\n", name->c_str(), fn.c_str());
bool exists;
llvm::error_code code = llvm::sys::fs::exists(joined_path.str(), exists);
assert(LLVM_SYS_FS_EXISTS_CODE_OKAY(code));
if (!exists)
continue;
if (VERBOSITY() >= 1)
printf("Importing %s from %s\n", name->c_str(), fn.c_str());
BoxedModule* module = createAndRunModule(*name, fn);
return module;
}
if (*name == "test") {
return importTestExtension();
}
raiseExcHelper(ImportError, "No module named %s", name->c_str());
}
}
// Copyright (c) 2014 Dropbox, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef PYSTON_RUNTIME_IMPORT_H
#define PYSTON_RUNTIME_IMPORT_H
#include "runtime/types.h"
namespace pyston {
extern "C" Box* import(const std::string* name);
}
#endif
......@@ -20,6 +20,7 @@
#include "runtime/complex.h"
#include "runtime/float.h"
#include "runtime/generator.h"
#include "runtime/import.h"
#include "runtime/inline/boxing.h"
#include "runtime/int.h"
#include "runtime/list.h"
......
......@@ -21,9 +21,6 @@
#include <memory>
#include <stdint.h>
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "asm_writing/icinfo.h"
#include "asm_writing/rewriter.h"
#include "codegen/compvars.h"
......@@ -3633,70 +3630,6 @@ extern "C" Box* getGlobal(BoxedModule* m, std::string* name) {
raiseExcHelper(NameError, "global name '%s' is not defined", name->c_str());
}
BoxedModule* createAndRunModule(const std::string& name, const std::string& fn) {
BoxedModule* module = createModule(name, fn);
AST_Module* ast = caching_parse(fn.c_str());
compileAndRunModule(ast, module);
return module;
}
// TODO I feel like importing should go somewhere else; it's more closely tied to codegen
// than to the object model.
extern "C" Box* import(const std::string* name) {
assert(name);
static StatCounter slowpath_import("slowpath_import");
slowpath_import.log();
BoxedDict* sys_modules = getSysModulesDict();
Box* s = boxStringPtr(name);
if (sys_modules->d.find(s) != sys_modules->d.end())
return sys_modules->d[s];
BoxedList* sys_path = getSysPath();
if (sys_path->cls != list_cls) {
raiseExcHelper(RuntimeError, "sys.path must be a list of directory name");
}
llvm::SmallString<128> joined_path;
for (int i = 0; i < sys_path->size; i++) {
Box* _p = sys_path->elts->elts[i];
if (_p->cls != str_cls)
continue;
BoxedString* p = static_cast<BoxedString*>(_p);
joined_path.clear();
llvm::sys::path::append(joined_path, p->s, *name + ".py");
std::string fn(joined_path.str());
if (VERBOSITY() >= 2)
printf("Searching for %s at %s...\n", name->c_str(), fn.c_str());
bool exists;
llvm::error_code code = llvm::sys::fs::exists(joined_path.str(), exists);
#if LLVMREV < 210072
assert(code == 0);
#else
assert(!code);
#endif
if (!exists)
continue;
if (VERBOSITY() >= 1)
printf("Importing %s from %s\n", name->c_str(), fn.c_str());
BoxedModule* module = createAndRunModule(*name, fn);
return module;
}
if (*name == "test") {
return importTestExtension();
}
raiseExcHelper(ImportError, "No module named %s", name->c_str());
}
extern "C" Box* importFrom(Box* _m, const std::string* name) {
assert(_m->cls == module_cls);
......
......@@ -75,7 +75,6 @@ extern "C" void setitem(Box* target, Box* slice, Box* value);
extern "C" void delitem(Box* target, Box* slice);
extern "C" Box* getclsattr(Box* obj, const char* attr);
extern "C" Box* unaryop(Box* operand, int op_type);
extern "C" Box* import(const std::string* name);
extern "C" Box* importFrom(Box* obj, const std::string* attr);
extern "C" void importStar(Box* from_module, BoxedModule* to_module);
extern "C" Box** unpackIntoArray(Box* obj, int64_t expected_size);
......
# allow-warning: converting unicode literal to str
import os.path
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment