Commit b0aea029 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge branch 'tmp'

parents daa43b67 101c6e94
......@@ -141,7 +141,8 @@ add_subdirectory(test/unittests)
add_subdirectory(tools)
add_executable(pyston $<TARGET_OBJECTS:PYSTON_MAIN_OBJECT> $<TARGET_OBJECTS:PYSTON_OBJECTS> $<TARGET_OBJECTS:FROM_CPYTHON>)
target_link_libraries(pyston stdlib pthread m readline gmp unwind pypa double-conversion ${LLVM_LIBS} ${LIBLZMA_LIBRARIES})
# Wrap the stdlib in --whole-archive to force all the symbols to be included and eventually exported
target_link_libraries(pyston -Wl,--whole-archive stdlib -Wl,--no-whole-archive pthread m readline gmp unwind pypa double-conversion ${LLVM_LIBS} ${LIBLZMA_LIBRARIES})
# copy the python standard library (lib_python/2.7) and src/codegen/parse_ast.py to the build directory
add_custom_command(TARGET pyston POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/lib_python/2.7 ${CMAKE_BINARY_DIR}/lib_python/2.7)
......
......@@ -80,8 +80,9 @@ class IterableUserDict(UserDict):
def __iter__(self):
return iter(self.data)
import _abcoll
_abcoll.MutableMapping.register(IterableUserDict)
# Pyston change: disable using the _abcoll module for now.
# import _abcoll
# _abcoll.MutableMapping.register(IterableUserDict)
class DictMixin:
......
# Copyright 2007 Google, Inc. All Rights Reserved.
# Licensed to PSF under a Contributor Agreement.
# Pyston change: disable using the _abcoll module for now.
raise NotImplementedError()
"""Abstract Base Classes (ABCs) for collections, according to PEP 3119.
DON'T USE THIS MODULE DIRECTLY! The classes here should be imported
......
__all__ = ['Counter', 'deque', 'defaultdict', 'namedtuple', 'OrderedDict']
# For bootstrapping reasons, the collection ABCs are defined in _abcoll.py.
# They should however be considered an integral part of collections.py.
from _abcoll import *
import _abcoll
__all__ += _abcoll.__all__
# Pyston change: disable using the _abcoll module for now.
# from _abcoll import *
# import _abcoll
# __all__ += _abcoll.__all__
from _collections import deque, defaultdict
from operator import itemgetter as _itemgetter, eq as _eq
......
......@@ -877,6 +877,7 @@ extern "C" int PyType_Ready(PyTypeObject* cls) {
}
cls->gc_visit = &conservativeGCHandler;
cls->is_user_defined = true;
// TODO not sure how we can handle extension types that manually
// specify a dict...
......
......@@ -117,7 +117,7 @@ void popGenerator() {
}
static int signals_waiting(0);
static std::vector<ThreadState> thread_states;
static std::vector<ThreadGCState> thread_states;
static void pushThreadState(pthread_t tid, ucontext_t* context) {
#if STACK_GROWS_DOWN
......@@ -128,10 +128,10 @@ static void pushThreadState(pthread_t tid, ucontext_t* context) {
void* stack_end = (void*)(context->uc_mcontext.gregs[REG_RSP] + sizeof(void*));
#endif
assert(stack_start < stack_end);
thread_states.push_back(ThreadState(tid, context, stack_start, stack_end));
thread_states.push_back(ThreadGCState(tid, context, stack_start, stack_end));
}
std::vector<ThreadState> getAllThreadStates() {
std::vector<ThreadGCState> getAllThreadStates() {
// TODO need to prevent new threads from starting,
// though I suppose that will have been taken care of
// by the caller of this function.
......
......@@ -36,7 +36,7 @@ intptr_t start_thread(void* (*start_func)(Box*, Box*, Box*), Box* arg1, Box* arg
void registerMainThread();
void finishMainThread();
struct ThreadState {
struct ThreadGCState {
pthread_t tid; // useful mostly for debugging
ucontext_t* ucontext;
......@@ -45,13 +45,13 @@ struct ThreadState {
// in a generator, but those generators will be tracked separately.
void* stack_start, *stack_end;
ThreadState(pthread_t tid, ucontext_t* ucontext, void* stack_start, void* stack_end)
ThreadGCState(pthread_t tid, ucontext_t* ucontext, void* stack_start, void* stack_end)
: tid(tid), ucontext(ucontext), stack_start(stack_start), stack_end(stack_end) {}
};
// Gets a ThreadState per thread, not including the thread calling this function.
// Gets a ThreadGCState per thread, not including the thread calling this function.
// For this call to make sense, the threads all should be blocked;
// as a corollary, this thread is very much not thread safe.
std::vector<ThreadState> getAllThreadStates();
std::vector<ThreadGCState> getAllThreadStates();
// Get the stack "bottom" (ie first pushed data. For stacks that grow down, this
// will be the highest address).
......
......@@ -47,9 +47,9 @@ void collectRoots(void* start, void* end, TraceStack* stack) {
void collectOtherThreadsStacks(TraceStack* stack) {
std::vector<threading::ThreadState> threads = threading::getAllThreadStates();
std::vector<threading::ThreadGCState> threads = threading::getAllThreadStates();
for (threading::ThreadState& tstate : threads) {
for (threading::ThreadGCState& tstate : threads) {
collectRoots(tstate.stack_start, tstate.stack_end, stack);
collectRoots(tstate.ucontext, tstate.ucontext + 1, stack);
}
......
......@@ -482,7 +482,7 @@ BoxedClass* BaseException, *Exception, *StandardError, *AssertionError, *Attribu
}
Box* exceptionNew1(BoxedClass* cls) {
return exceptionNew2(cls, boxStrConstant(""));
return exceptionNew(cls, EmptyTuple);
}
class BoxedException : public Box {
......@@ -492,11 +492,29 @@ public:
};
Box* exceptionNew2(BoxedClass* cls, Box* message) {
assert(cls->tp_basicsize == sizeof(BoxedException));
Box* r = new BoxedException(cls);
// TODO: maybe this should be a MemberDescriptor?
r->giveAttr("message", message);
return r;
return exceptionNew(cls, new BoxedTuple({ message }));
}
Box* exceptionNew(BoxedClass* cls, BoxedTuple* args) {
if (!isSubclass(cls->cls, type_cls))
raiseExcHelper(TypeError, "exceptions.__new__(X): X is not a type object (%s)", getTypeName(cls)->c_str());
if (!isSubclass(cls, BaseException))
raiseExcHelper(TypeError, "BaseException.__new__(%s): %s is not a subtype of BaseException",
getNameOfClass(cls)->c_str(), getNameOfClass(cls)->c_str());
assert(cls->tp_basicsize >= sizeof(BoxedException));
void* mem = gc_alloc(cls->tp_basicsize, gc::GCKind::PYTHON);
memset((char*)mem + sizeof(BoxedException), 0, cls->tp_basicsize - sizeof(BoxedException));
BoxedException* rtn = ::new (mem) BoxedException(cls);
initUserAttrs(rtn, cls);
// TODO: this should be a MemberDescriptor and set during init
if (args->elts.size() == 1)
rtn->giveAttr("message", args->elts[0]);
else
rtn->giveAttr("message", boxStrConstant(""));
return rtn;
}
Box* exceptionStr(Box* b) {
......@@ -520,15 +538,16 @@ Box* exceptionRepr(Box* b) {
return boxString(*getTypeName(b) + "(" + message_s->s + ",)");
}
static BoxedClass* makeBuiltinException(BoxedClass* base, const char* name) {
BoxedClass* cls
= new BoxedHeapClass(type_cls, base, NULL, offsetof(BoxedException, attrs), sizeof(BoxedException), false);
static BoxedClass* makeBuiltinException(BoxedClass* base, const char* name, int size = 0) {
if (size == 0)
size = base->tp_basicsize;
BoxedClass* cls = new BoxedHeapClass(type_cls, base, NULL, offsetof(BoxedException, attrs), size, false);
cls->giveAttr("__name__", boxStrConstant(name));
cls->giveAttr("__module__", boxStrConstant("exceptions"));
if (base == object_cls) {
cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)exceptionNew2, UNKNOWN, 2, 1, false, false),
{ boxStrConstant("") }));
cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)exceptionNew, UNKNOWN, 1, 0, true, true)));
cls->giveAttr("__str__", new BoxedFunction(boxRTFunction((void*)exceptionStr, STR, 1)));
cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)exceptionRepr, STR, 1)));
}
......@@ -720,6 +739,23 @@ Box* pydump(void* p) {
return None;
}
class BoxedEnvironmentError : public BoxedException {
public:
// Box* args, *message, *myerrno, *strerror, *filename;
Box* myerrno, *strerror, *filename;
static Box* __init__(BoxedEnvironmentError* self, Box* errno_, Box* strerror, Box** _args) {
Box* filename = _args[0];
RELEASE_ASSERT(isSubclass(self->cls, EnvironmentError), "");
self->myerrno = errno_;
self->strerror = strerror;
self->filename = filename;
return None;
}
};
void setupBuiltins() {
builtins_module = createModule("__builtin__", "__builtin__");
......@@ -740,7 +776,7 @@ void setupBuiltins() {
builtins_module->giveAttr("all", new BoxedFunction(boxRTFunction((void*)all, BOXED_BOOL, 1)));
builtins_module->giveAttr("any", new BoxedFunction(boxRTFunction((void*)any, BOXED_BOOL, 1)));
BaseException = makeBuiltinException(object_cls, "BaseException");
BaseException = makeBuiltinException(object_cls, "BaseException", sizeof(BoxedException));
Exception = makeBuiltinException(BaseException, "Exception");
StandardError = makeBuiltinException(Exception, "StandardError");
AssertionError = makeBuiltinException(StandardError, "AssertionError");
......@@ -751,7 +787,7 @@ void setupBuiltins() {
LookupError = makeBuiltinException(StandardError, "LookupError");
KeyError = makeBuiltinException(LookupError, "KeyError");
IndexError = makeBuiltinException(LookupError, "IndexError");
EnvironmentError = makeBuiltinException(StandardError, "EnvironmentError");
EnvironmentError = makeBuiltinException(StandardError, "EnvironmentError", sizeof(BoxedEnvironmentError));
IOError = makeBuiltinException(EnvironmentError, "IOError");
OSError = makeBuiltinException(EnvironmentError, "OSError");
ArithmeticError = makeBuiltinException(StandardError, "ArithmeticError");
......@@ -775,6 +811,10 @@ void setupBuiltins() {
SystemError = makeBuiltinException(StandardError, "SystemError");
NotImplementedError = makeBuiltinException(RuntimeError, "NotImplementedError");
EnvironmentError->giveAttr(
"__init__",
new BoxedFunction(boxRTFunction((void*)BoxedEnvironmentError::__init__, NONE, 4, 1, false, false), { None }));
repr_obj = new BoxedFunction(boxRTFunction((void*)repr, UNKNOWN, 1));
builtins_module->giveAttr("repr", repr_obj);
len_obj = new BoxedFunction(boxRTFunction((void*)len, UNKNOWN, 1));
......
......@@ -19,6 +19,7 @@
#include <cstdlib>
#include <cstring>
#include <memory>
#include <sstream>
#include <stdint.h>
#include "asm_writing/icinfo.h"
......@@ -351,6 +352,8 @@ BoxedClass::BoxedClass(BoxedClass* metaclass, BoxedClass* base, gcvisit_func gc_
memset(&tp_name, 0, (char*)(&tp_version_tag + 1) - (char*)(&tp_name));
tp_basicsize = instance_size;
tp_flags |= Py_TPFLAGS_HEAPTYPE;
if (metaclass == NULL) {
assert(type_cls == NULL);
} else {
......@@ -2313,7 +2316,16 @@ Box* callFunc(BoxedFunction* func, CallRewriteArgs* rewrite_args, ArgPassSpec ar
Box* ovarargs = new BoxedTuple(unused_positional);
getArg(varargs_idx, oarg1, oarg2, oarg3, oargs) = ovarargs;
} else if (unused_positional.size()) {
raiseExcHelper(TypeError, "<function>() takes at most %d argument%s (%d given)", f->num_args,
std::string name = "<unknown function>";
if (f->source)
name = f->source->getName();
else if (f->versions.size()) {
std::ostringstream oss;
oss << "<function at " << f->versions[0]->code << ">";
name = oss.str();
}
raiseExcHelper(TypeError, "%s() takes at most %d argument%s (%d given)", name.c_str(), f->num_args,
(f->num_args == 1 ? "" : "s"), argspec.num_args + argspec.num_keywords + varargs.size());
}
......
......@@ -607,31 +607,27 @@ extern "C" int PySlice_GetIndicesEx(PySliceObject* r, Py_ssize_t length, Py_ssiz
}
Box* typeRepr(BoxedClass* self) {
if (isUserDefined(self)) {
std::ostringstream os;
std::ostringstream os;
if ((self->tp_flags & Py_TPFLAGS_HEAPTYPE) && isUserDefined(self))
os << "<class '";
else
os << "<type '";
Box* m = self->getattr("__module__");
RELEASE_ASSERT(m, "");
if (m->cls == str_cls) {
BoxedString* sm = static_cast<BoxedString*>(m);
os << sm->s << '.';
}
Box* m = self->getattr("__module__");
if (m && m->cls == str_cls) {
BoxedString* sm = static_cast<BoxedString*>(m);
os << sm->s << '.';
}
Box* n = self->getattr("__name__");
RELEASE_ASSERT(n, "");
RELEASE_ASSERT(n->cls == str_cls, "should have prevented you from setting __name__ to non-string");
BoxedString* sn = static_cast<BoxedString*>(n);
os << sn->s;
Box* n = self->getattr("__name__");
RELEASE_ASSERT(n, "");
RELEASE_ASSERT(n->cls == str_cls, "should have prevented you from setting __name__ to non-string");
BoxedString* sn = static_cast<BoxedString*>(n);
os << sn->s;
os << "'>";
os << "'>";
return boxString(os.str());
} else {
char buf[80];
snprintf(buf, 80, "<type '%s'>", getNameOfClass(self)->c_str());
return boxStrConstant(buf);
}
return boxString(os.str());
}
Box* typeHash(BoxedClass* self) {
......
......@@ -217,7 +217,7 @@ public:
// Whether this class was defined by the user or is a builtin type.
// this is used mostly for debugging.
const bool is_user_defined;
bool is_user_defined;
// will need to update this once we support tp_getattr-style overriding:
bool hasGenericGetattr() { return true; }
......@@ -539,6 +539,7 @@ extern "C" void boxGCHandler(GCVisitor* v, Box* b);
Box* exceptionNew1(BoxedClass* cls);
Box* exceptionNew2(BoxedClass* cls, Box* message);
Box* exceptionNew(BoxedClass* cls, BoxedTuple* args);
extern "C" BoxedClass* Exception, *AssertionError, *AttributeError, *TypeError, *NameError, *KeyError, *IndexError,
*IOError, *OSError, *ZeroDivisionError, *ValueError, *UnboundLocalError, *RuntimeError, *ImportError,
......
......@@ -12,3 +12,5 @@ print len(r1), len(r2), type(r1), type(r2), r1 == r2
print type(os.stat("/dev/null"))
print os.path.expanduser("~") == os.environ["HOME"]
OSError(1, 2, 3)
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