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); ...@@ -370,6 +370,7 @@ std::string getPythonFuncAt(void* ip, void* sp);
// TODO where to put this // TODO where to put this
void addToSysPath(const std::string &path); void addToSysPath(const std::string &path);
void addToSysArgv(const char* str);
} }
......
...@@ -100,7 +100,7 @@ void removeDirectoryIfExists(const std::string& path) { ...@@ -100,7 +100,7 @@ void removeDirectoryIfExists(const std::string& path) {
if (llvm::sys::fs::is_directory(status)) { if (llvm::sys::fs::is_directory(status)) {
removeDirectoryIfExists(it->path()); removeDirectoryIfExists(it->path());
} else { } 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); code = llvm::sys::fs::remove(it->path(), false);
assert(!code); assert(!code);
} }
...@@ -109,7 +109,7 @@ void removeDirectoryIfExists(const std::string& path) { ...@@ -109,7 +109,7 @@ void removeDirectoryIfExists(const std::string& path) {
assert(!code); assert(!code);
} }
llvm::errs() << "Removing directory " << path << '\n'; if (VERBOSITY()) llvm::errs() << "Removing directory " << path << '\n';
code = llvm::sys::fs::remove(path, false); code = llvm::sys::fs::remove(path, false);
assert(!code); assert(!code);
} }
......
...@@ -87,27 +87,32 @@ int main(int argc, char** argv) { ...@@ -87,27 +87,32 @@ int main(int argc, char** argv) {
} }
const char* fn = NULL; 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) { if (optind != argc) {
fn = argv[optind]; fn = argv[optind];
if (!force_repl) if (strcmp("-", fn) == 0)
fn = NULL;
else if (!force_repl)
repl = false; repl = false;
}
// end of argument parsing for (int i = optind; i < argc; i++) {
addToSysArgv(argv[i]);
{ }
Timer _t("for initCodegen"); } else {
initCodegen(); addToSysArgv("");
} }
BoxedModule* main = createModule("__main__", fn); // end of argument parsing
_t.split("to run"); _t.split("to run");
if (fn != NULL) { if (fn != NULL) {
BoxedModule* main = createModule("__main__", fn);
llvm::SmallString<128> path; llvm::SmallString<128> path;
if (!llvm::sys::path::is_absolute(fn)) { if (!llvm::sys::path::is_absolute(fn)) {
...@@ -143,6 +148,8 @@ int main(int argc, char** argv) { ...@@ -143,6 +148,8 @@ int main(int argc, char** argv) {
} }
if (repl && BENCH) { if (repl && BENCH) {
BoxedModule* main = createModule("__main__", "<bench>");
timeval start, end; timeval start, end;
gettimeofday(&start, NULL); gettimeofday(&start, NULL);
const int MAX_RUNS = 1000; const int MAX_RUNS = 1000;
...@@ -173,7 +180,9 @@ int main(int argc, char** argv) { ...@@ -173,7 +180,9 @@ int main(int argc, char** argv) {
if (repl) { if (repl) {
printf("Pyston v0.1, rev " STRINGIFY(GITREV) "\n"); printf("Pyston v0.1, rev " STRINGIFY(GITREV) "\n");
}
BoxedModule* main = createModule("__main__", "<stdin>");
while (repl) { while (repl) {
printf(">> "); printf(">> ");
fflush(stdout); fflush(stdout);
...@@ -220,6 +229,7 @@ int main(int argc, char** argv) { ...@@ -220,6 +229,7 @@ int main(int argc, char** argv) {
} }
} }
} }
}
_t.split("joinRuntime"); _t.split("joinRuntime");
int rtncode = joinRuntime(); int rtncode = joinRuntime();
......
...@@ -53,6 +53,13 @@ BoxedList* getSysPath() { ...@@ -53,6 +53,13 @@ BoxedList* getSysPath() {
return static_cast<BoxedList*>(_sys_path); 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) { void addToSysPath(const std::string &path) {
BoxedList *sys_path = getSysPath(); BoxedList *sys_path = getSysPath();
listAppendInternal(sys_path, boxStringPtr(&path)); listAppendInternal(sys_path, boxStringPtr(&path));
...@@ -70,6 +77,8 @@ void setupSys() { ...@@ -70,6 +77,8 @@ void setupSys() {
BoxedList* sys_path = new BoxedList(); BoxedList* sys_path = new BoxedList();
sys_module->giveAttr("path", sys_path); sys_module->giveAttr("path", sys_path);
sys_module->giveAttr("argv", new BoxedList());
sys_module->giveAttr("stdout", new BoxedFile(stdout)); sys_module->giveAttr("stdout", new BoxedFile(stdout));
sys_module->giveAttr("stdin", new BoxedFile(stdin)); sys_module->giveAttr("stdin", new BoxedFile(stdin));
sys_module->giveAttr("stderr", new BoxedFile(stderr)); 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