Commit 0818c46d authored by Kevin Modzelewski's avatar Kevin Modzelewski

Basic tempfile support

Had to modify tempfile to not depend on io, which is a big module (due to importing _io).
tempfile seems to barely even use it, and I think I was able to replace its usage with
a simpler os.write call.
parent 1aa2cc5e
......@@ -29,7 +29,8 @@ __all__ = [
# Imports.
import io as _io
# Pyston change: don't import io
# import io as _io
import os as _os
import errno as _errno
from random import Random as _Random
......@@ -197,8 +198,10 @@ def _get_default_tempdir():
fd = _os.open(filename, flags, 0o600)
try:
try:
with _io.open(fd, 'wb', closefd=False) as fp:
fp.write(b'blat')
# Pyston change: simplify this so that it doesn't need the _io module:
_os.write(fd, b'blat')
# with _io.open(fd, 'wb', closefd=False) as fp:
# fp.write(b'blat')
finally:
_os.close(fd)
finally:
......
......@@ -2173,7 +2173,7 @@ public:
AST_expr* enter = makeLoadAttribute(makeName(ctxmgrname_buf, AST_TYPE::Load, node->lineno), "__enter__", true);
AST_expr* exit = makeLoadAttribute(makeName(ctxmgrname_buf, AST_TYPE::Load, node->lineno), "__exit__", true);
pushAssign(exitname_buf, exit);
enter = makeCall(enter);
enter = remapExpr(makeCall(enter));
if (node->optional_vars) {
pushAssign(node->optional_vars, enter);
......
......@@ -493,7 +493,14 @@ Box* getattrFunc(Box* obj, Box* _str, Box* default_value) {
}
BoxedString* str = static_cast<BoxedString*>(_str);
Box* rtn = getattrInternal(obj, str->s, NULL);
Box* rtn = NULL;
try {
rtn = getattrInternal(obj, str->s, NULL);
} catch (ExcInfo e) {
if (!e.matches(AttributeError))
throw e;
}
if (!rtn) {
if (default_value)
......@@ -528,7 +535,7 @@ Box* hasattr(Box* obj, Box* _str) {
} catch (ExcInfo e) {
if (e.matches(Exception))
return False;
throw;
throw e;
}
Box* rtn = attr ? True : False;
......
......@@ -47,10 +47,29 @@ Box* startNewThread(Box* target, Box* args) {
static BoxedClass* thread_lock_cls;
class BoxedThreadLock : public Box {
private:
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
public:
BoxedThreadLock() {}
DEFAULT_CLASS(thread_lock_cls);
static Box* acquire(Box* _self) {
RELEASE_ASSERT(_self->cls == thread_lock_cls, "");
BoxedThreadLock* self = static_cast<BoxedThreadLock*>(_self);
pthread_mutex_lock(&self->lock);
return None;
}
static Box* release(Box* _self) {
RELEASE_ASSERT(_self->cls == thread_lock_cls, "");
BoxedThreadLock* self = static_cast<BoxedThreadLock*>(_self);
pthread_mutex_unlock(&self->lock);
return None;
}
};
Box* allocateLock() {
......@@ -71,6 +90,10 @@ void setupThread() {
thread_lock_cls = new BoxedHeapClass(object_cls, NULL, 0, sizeof(BoxedThreadLock), false);
thread_lock_cls->giveAttr("__name__", boxStrConstant("lock"));
thread_lock_cls->giveAttr("__module__", boxStrConstant("thread"));
thread_lock_cls->giveAttr("acquire", new BoxedFunction(boxRTFunction((void*)BoxedThreadLock::acquire, NONE, 1)));
thread_lock_cls->giveAttr("release", new BoxedFunction(boxRTFunction((void*)BoxedThreadLock::release, NONE, 1)));
thread_lock_cls->giveAttr("acquire_lock", thread_lock_cls->getattr("acquire"));
thread_lock_cls->giveAttr("release_lock", thread_lock_cls->getattr("release"));
thread_lock_cls->freeze();
BoxedClass* ThreadError
......
......@@ -266,7 +266,40 @@ extern "C" int _PyFile_SanitizeMode(char* mode) noexcept {
}
extern "C" int PyObject_AsFileDescriptor(PyObject* o) noexcept {
Py_FatalError("unimplemented");
int fd;
PyObject* meth;
if (PyInt_Check(o)) {
fd = _PyInt_AsInt(o);
} else if (PyLong_Check(o)) {
fd = _PyLong_AsInt(o);
} else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL) {
PyObject* fno = PyEval_CallObject(meth, NULL);
Py_DECREF(meth);
if (fno == NULL)
return -1;
if (PyInt_Check(fno)) {
fd = _PyInt_AsInt(fno);
Py_DECREF(fno);
} else if (PyLong_Check(fno)) {
fd = _PyLong_AsInt(fno);
Py_DECREF(fno);
} else {
PyErr_SetString(PyExc_TypeError, "fileno() returned a non-integer");
Py_DECREF(fno);
return -1;
}
} else {
PyErr_SetString(PyExc_TypeError, "argument must be an int, or have a fileno() method.");
return -1;
}
if (fd < 0) {
PyErr_Format(PyExc_ValueError, "file descriptor cannot be a negative integer (%i)", fd);
return -1;
}
return fd;
}
extern "C" int PyFile_SoftSpace(PyObject* f, int newflag) noexcept {
......
......@@ -62,8 +62,15 @@ extern "C" PyAPI_FUNC(PyObject*) _PyInt_Format(PyIntObject* v, int base, int new
Py_FatalError("unimplemented");
}
extern "C" int _PyInt_AsInt(PyObject*) noexcept {
Py_FatalError("unimplemented");
extern "C" int _PyInt_AsInt(PyObject* obj) noexcept {
long result = PyInt_AsLong(obj);
if (result == -1 && PyErr_Occurred())
return -1;
if (result > INT_MAX || result < INT_MIN) {
PyErr_SetString(PyExc_OverflowError, "Python int too large to convert to C int");
return -1;
}
return (int)result;
}
BoxedInt* interned_ints[NUM_INTERNED_INTS];
......
......@@ -66,6 +66,18 @@ extern "C" unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject* vv) noexcep
return bytes;
}
extern "C" int _PyLong_AsInt(PyObject* obj) noexcept {
int overflow;
long result = PyLong_AsLongAndOverflow(obj, &overflow);
if (overflow || result > INT_MAX || result < INT_MIN) {
/* XXX: could be cute and give a different
message for overflow == -1 */
PyErr_SetString(PyExc_OverflowError, "Python int too large to convert to C int");
return -1;
}
return (int)result;
}
extern "C" unsigned long PyLong_AsUnsignedLongMask(PyObject* op) noexcept {
Py_FatalError("unimplemented");
}
......
# allow-warning: converting unicode literal to str
import os
import tempfile
fd, dirname = tempfile.mkstemp()
print type(fd), type(dirname)
print os.path.exists(dirname)
os.unlink(dirname)
print os.path.exists(dirname)
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