Commit b0aea029 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge branch 'tmp'

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