Commit c7819eea authored by Kevin Modzelewski's avatar Kevin Modzelewski

Add os.environ (via posix.environ)

parent 6d9d1c3d
...@@ -57,8 +57,33 @@ static Box* posix_getuid() { ...@@ -57,8 +57,33 @@ static Box* posix_getuid() {
return boxInt(getuid()); return boxInt(getuid());
} }
extern "C" {
extern char** environ;
}
static Box* convertEnviron() {
assert(environ);
BoxedDict* d = new BoxedDict();
// Ported from CPython:
for (char** e = environ; *e != NULL; e++) {
char* p = strchr(*e, '=');
if (p == NULL)
continue;
Box* k = PyString_FromStringAndSize(*e, (int)(p - *e));
Box* v = PyString_FromString(p + 1);
Box*& cur_v = d->d[k];
if (cur_v == NULL)
cur_v = v;
}
return d;
}
} // namespace posix } // namespace posix
using namespace posix;
void setupPosix() { void setupPosix() {
posix_module = createModule("posix", "__builtin__"); posix_module = createModule("posix", "__builtin__");
...@@ -66,6 +91,7 @@ void setupPosix() { ...@@ -66,6 +91,7 @@ void setupPosix() {
posix_module->giveAttr("getuid", new BoxedFunction(boxRTFunction((void*)posix::posix_getuid, BOXED_INT, 0))); posix_module->giveAttr("getuid", new BoxedFunction(boxRTFunction((void*)posix::posix_getuid, BOXED_INT, 0)));
posix_module->giveAttr("error", OSError); posix_module->giveAttr("error", OSError);
posix_module->giveAttr("environ", convertEnviron());
} }
} }
......
# expected: fail # allow-warning: converting unicode literal to str
# - wip
import pwd import pwd
import os import os
......
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