Commit 56242493 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Add some support for sys.argv

Doing it "right" is more complicated than I thought,
so it's a little bit hacky right now.
parent ce0618d4
......@@ -370,6 +370,7 @@ std::string getPythonFuncAt(void* ip, void* sp);
// TODO where to put this
void addToSysPath(const std::string &path);
void addToSysArgv(const char* str);
}
......
......@@ -100,7 +100,7 @@ void removeDirectoryIfExists(const std::string& path) {
if (llvm::sys::fs::is_directory(status)) {
removeDirectoryIfExists(it->path());
} else {
llvm::errs() << "Removing file " << it->path() << '\n';
if (VERBOSITY()) llvm::errs() << "Removing file " << it->path() << '\n';
code = llvm::sys::fs::remove(it->path(), false);
assert(!code);
}
......@@ -109,7 +109,7 @@ void removeDirectoryIfExists(const std::string& path) {
assert(!code);
}
llvm::errs() << "Removing directory " << path << '\n';
if (VERBOSITY()) llvm::errs() << "Removing directory " << path << '\n';
code = llvm::sys::fs::remove(path, false);
assert(!code);
}
......
......@@ -87,27 +87,32 @@ int main(int argc, char** argv) {
}
const char* fn = NULL;
if (optind != argc-1 && optind != argc) {
fprintf(stderr, "Error: python-level arguments not supported yet (first given was %s)\n", argv[optind+1]);
exit(1);
{
Timer _t("for initCodegen");
initCodegen();
}
if (optind != argc) {
fn = argv[optind];
if (!force_repl)
if (strcmp("-", fn) == 0)
fn = NULL;
else if (!force_repl)
repl = false;
}
// end of argument parsing
{
Timer _t("for initCodegen");
initCodegen();
for (int i = optind; i < argc; i++) {
addToSysArgv(argv[i]);
}
} else {
addToSysArgv("");
}
BoxedModule* main = createModule("__main__", fn);
// end of argument parsing
_t.split("to run");
if (fn != NULL) {
BoxedModule* main = createModule("__main__", fn);
llvm::SmallString<128> path;
if (!llvm::sys::path::is_absolute(fn)) {
......@@ -143,6 +148,8 @@ int main(int argc, char** argv) {
}
if (repl && BENCH) {
BoxedModule* main = createModule("__main__", "<bench>");
timeval start, end;
gettimeofday(&start, NULL);
const int MAX_RUNS = 1000;
......@@ -173,7 +180,9 @@ int main(int argc, char** argv) {
if (repl) {
printf("Pyston v0.1, rev " STRINGIFY(GITREV) "\n");
}
BoxedModule* main = createModule("__main__", "<stdin>");
while (repl) {
printf(">> ");
fflush(stdout);
......@@ -220,6 +229,7 @@ int main(int argc, char** argv) {
}
}
}
}
_t.split("joinRuntime");
int rtncode = joinRuntime();
......
......@@ -53,6 +53,13 @@ BoxedList* getSysPath() {
return static_cast<BoxedList*>(_sys_path);
}
void addToSysArgv(const char* str) {
Box *sys_argv = sys_module->peekattr("argv");
assert(sys_argv);
assert(sys_argv->cls == list_cls);
listAppendInternal(sys_argv, boxStrConstant(str));
}
void addToSysPath(const std::string &path) {
BoxedList *sys_path = getSysPath();
listAppendInternal(sys_path, boxStringPtr(&path));
......@@ -70,6 +77,8 @@ void setupSys() {
BoxedList* sys_path = new BoxedList();
sys_module->giveAttr("path", sys_path);
sys_module->giveAttr("argv", new BoxedList());
sys_module->giveAttr("stdout", new BoxedFile(stdout));
sys_module->giveAttr("stdin", new BoxedFile(stdin));
sys_module->giveAttr("stderr", new BoxedFile(stderr));
......
import sys
print sys.argv
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