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,50 +180,53 @@ int main(int argc, char** argv) {
if (repl) {
printf("Pyston v0.1, rev " STRINGIFY(GITREV) "\n");
}
while (repl) {
printf(">> ");
fflush(stdout);
char* line = NULL;
size_t size;
int read;
if ((read = getline(&line, &size, stdin)) == -1) {
repl = false;
} else {
timeval start, end;
gettimeofday(&start, NULL);
char buf[] = "pystontmp_XXXXXX";
char *tmpdir = mkdtemp(buf);
assert(tmpdir);
std::string tmp = std::string(tmpdir) + "/in.py";
if (VERBOSITY() >= 1) {
printf("writing %d bytes to %s\n", read, tmp.c_str());
}
FILE* f = fopen(tmp.c_str(), "w");
fwrite(line, 1, read, f);
fclose(f);
AST_Module* m = parse(tmp.c_str());
removeDirectoryIfExists(tmpdir);
if (m->body.size() > 0 && m->body[0]->type == AST_TYPE::Expr) {
AST_Expr *e = ast_cast<AST_Expr>(m->body[0]);
AST_Print *p = new AST_Print();
p->dest = NULL;
p->nl = true;
p->values.push_back(e->value);
m->body[0] = p;
}
compileAndRunModule(m, main);
if (VERBOSITY() >= 1) {
gettimeofday(&end, NULL);
long ms = 1000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000;
printf("%ldms\n", ms);
BoxedModule* main = createModule("__main__", "<stdin>");
while (repl) {
printf(">> ");
fflush(stdout);
char* line = NULL;
size_t size;
int read;
if ((read = getline(&line, &size, stdin)) == -1) {
repl = false;
} else {
timeval start, end;
gettimeofday(&start, NULL);
char buf[] = "pystontmp_XXXXXX";
char *tmpdir = mkdtemp(buf);
assert(tmpdir);
std::string tmp = std::string(tmpdir) + "/in.py";
if (VERBOSITY() >= 1) {
printf("writing %d bytes to %s\n", read, tmp.c_str());
}
FILE* f = fopen(tmp.c_str(), "w");
fwrite(line, 1, read, f);
fclose(f);
AST_Module* m = parse(tmp.c_str());
removeDirectoryIfExists(tmpdir);
if (m->body.size() > 0 && m->body[0]->type == AST_TYPE::Expr) {
AST_Expr *e = ast_cast<AST_Expr>(m->body[0]);
AST_Print *p = new AST_Print();
p->dest = NULL;
p->nl = true;
p->values.push_back(e->value);
m->body[0] = p;
}
compileAndRunModule(m, main);
if (VERBOSITY() >= 1) {
gettimeofday(&end, NULL);
long ms = 1000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000;
printf("%ldms\n", ms);
}
}
}
}
......
......@@ -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