Commit a3dbaa95 authored by Kevin Modzelewski's avatar Kevin Modzelewski

passes initialization

parent f1a3d284
......@@ -2387,6 +2387,7 @@ extern "C" PyObject* PyNumber_Index(PyObject* o) noexcept {
if (o == NULL)
return null_error();
if (PyInt_Check(o) || PyLong_Check(o)) {
Py_INCREF(o);
return o;
}
......
......@@ -750,6 +750,7 @@ extern "C" PyObject* PystonType_GenericAlloc(BoxedClass* cls, Py_ssize_t nitems)
Box* rtn = static_cast<Box*>(mem); \
\
rtn->cls = default_cls; \
_Py_NewReference(rtn); \
return rtn; \
/* TODO: there should be a way to not have to do this nested inlining by hand */ \
}
......@@ -809,6 +810,7 @@ static_assert(offsetof(BoxVar, ob_size) == offsetof(struct _varobject, ob_size),
\
BoxVar* rtn = static_cast<BoxVar*>(mem); \
rtn->cls = default_cls; \
_Py_NewReference(rtn); \
rtn->ob_size = nitems; \
return rtn; \
}
......
......@@ -617,7 +617,7 @@ Box* setattrFunc(Box* obj, Box* _str, Box* value) {
static Box* hasattrFuncHelper(Box* return_val) noexcept {
if (return_val)
return True;
Py_RETURN_TRUE;
if (PyErr_Occurred()) {
if (!PyErr_ExceptionMatches(PyExc_Exception))
......@@ -625,7 +625,7 @@ static Box* hasattrFuncHelper(Box* return_val) noexcept {
PyErr_Clear();
}
return False;
Py_RETURN_FALSE;
}
template <ExceptionStyle S>
......@@ -1636,12 +1636,12 @@ Return the absolute value of the argument.");
PyDoc_STRVAR(all_doc, "all(iterable) -> bool\n\
\n\
Return True if bool(x) is True for all values x in the iterable.\n\
If the iterable is empty, return True.");
If the iterable is empty, Py_RETURN_TRUE.");
PyDoc_STRVAR(any_doc, "any(iterable) -> bool\n\
\n\
Return True if bool(x) is True for any x in the iterable.\n\
If the iterable is empty, return False.");
If the iterable is empty, Py_RETURN_FALSE.");
PyDoc_STRVAR(apply_doc, "apply(object[, args[, kwargs]]) -> value\n\
\n\
......
......@@ -176,9 +176,9 @@ public:
if (PyThread_acquire_lock(self->lock_lock, 0)) {
PyThread_release_lock(self->lock_lock);
return False;
Py_RETURN_FALSE;
}
return True;
Py_RETURN_TRUE;
}
};
......
......@@ -666,7 +666,7 @@ Box* instanceNonzero(Box* _inst) {
if (nonzero_func) {
return runtimeCall(nonzero_func, ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL);
} else {
return True;
Py_RETURN_TRUE;
}
}
......
......@@ -588,17 +588,17 @@ Box* dictEq(BoxedDict* self, Box* _rhs) {
BoxedDict* rhs = static_cast<BoxedDict*>(_rhs);
if (self->d.size() != rhs->d.size())
return False;
Py_RETURN_FALSE;
for (const auto& p : self->d) {
auto it = rhs->d.find(p.first);
if (it == rhs->d.end())
return False;
Py_RETURN_FALSE;
if (!PyEq()(p.second, it->second))
return False;
Py_RETURN_FALSE;
}
return True;
Py_RETURN_TRUE;
}
Box* dictNe(BoxedDict* self, Box* _rhs) {
......@@ -606,8 +606,8 @@ Box* dictNe(BoxedDict* self, Box* _rhs) {
if (eq == NotImplemented)
return eq;
if (eq == True)
return False;
return True;
Py_RETURN_FALSE;
Py_RETURN_TRUE;
}
......
......@@ -287,6 +287,16 @@ BoxedFile::BoxedFile(FILE* f, std::string fname, const char* fmode, int (*close)
f_bufptr(0),
f_setbuf(0),
unlocked_count(0) {
static BoxedString* not_yet_string = internStringImmortal("<uninitialized file>");
Py_INCREF(not_yet_string);
this->f_name = not_yet_string;
Py_INCREF(not_yet_string);
this->f_mode = not_yet_string;
Py_INCREF(None);
this->f_encoding = None;
Py_INCREF(None);
this->f_errors = None;
Box* r = fill_file_fields(this, f, boxString(fname), fmode, close);
checkAndThrowCAPIException();
assert(r == this);
......@@ -1062,6 +1072,7 @@ Box* fileNew(BoxedClass* cls, Box* s, Box* m, Box** args) {
auto file = new BoxedFile(f, fn->s(), PyString_AsString(m));
PyFile_SetBufSize(file, buffering->n);
return file;
}
......
......@@ -960,7 +960,7 @@ Box* impIsBuiltin(Box* _name) {
Box* impIsFrozen(Box* name) {
if (!PyString_Check(name))
raiseExcHelper(TypeError, "must be string, not %s", getTypeName(name));
return False;
Py_RETURN_FALSE;
}
PyDoc_STRVAR(find_module_doc, "find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
......
......@@ -40,7 +40,7 @@ Box* listiterHasnext(Box* s) {
BoxedListIterator* self = static_cast<BoxedListIterator*>(s);
if (!self->l) {
return False;
Py_RETURN_FALSE;
}
bool ans = (self->pos < self->l->size);
......
......@@ -52,6 +52,7 @@ extern "C" inline void listAppendInternal(Box* s, Box* v) {
self->ensure(1);
assert(self->size < self->capacity);
Py_INCREF(v);
self->elts->elts[self->size] = v;
self->size++;
}
......
......@@ -45,7 +45,7 @@ static Box* seqiterHasnext_capi(Box* s) noexcept {
BoxedSeqIter* self = static_cast<BoxedSeqIter*>(s);
if (!self->b) {
return False;
Py_RETURN_FALSE;
}
Box* next = PySequence_GetItem(self->b, self->idx);
......@@ -53,14 +53,14 @@ static Box* seqiterHasnext_capi(Box* s) noexcept {
if (PyErr_ExceptionMatches(IndexError) || PyErr_ExceptionMatches(StopIteration)) {
PyErr_Clear();
self->b = NULL;
return False;
Py_RETURN_FALSE;
}
return NULL;
}
self->idx++;
self->next = next;
return True;
Py_RETURN_TRUE;
}
Box* seqiterHasnext(Box* s) {
......@@ -79,20 +79,20 @@ Box* seqreviterHasnext_capi(Box* s) noexcept {
BoxedSeqIter* self = static_cast<BoxedSeqIter*>(s);
if (self->idx == -1 || !self->b)
return False;
Py_RETURN_FALSE;
Box* next = PySequence_GetItem(self->b, self->idx);
if (!next) {
if (PyErr_ExceptionMatches(IndexError) || PyErr_ExceptionMatches(StopIteration)) {
PyErr_Clear();
self->b = NULL;
return False;
Py_RETURN_FALSE;
}
return NULL;
}
self->idx--;
self->next = next;
return True;
Py_RETURN_TRUE;
}
Box* seqreviterHasnext(Box* s) {
......
......@@ -756,6 +756,8 @@ Box* listIAdd(BoxedList* self, Box* _rhs) {
memcpy(self->elts->elts + s1, rhs->elts->elts, sizeof(rhs->elts->elts[0]) * s2);
self->size = s1 + s2;
Py_INCREF(self);
return self;
}
......@@ -772,6 +774,8 @@ Box* listIAdd(BoxedList* self, Box* _rhs) {
memcpy(self->elts->elts + s1, rhs->elts, sizeof(self->elts->elts[0]) * s2);
self->size = s1 + s2;
Py_INCREF(self);
return self;
}
......@@ -780,6 +784,7 @@ Box* listIAdd(BoxedList* self, Box* _rhs) {
for (auto* b : _rhs->pyElements())
listAppendInternal(self, b);
Py_INCREF(self);
return self;
}
......@@ -1120,9 +1125,9 @@ Box* _listCmp(BoxedList* lhs, BoxedList* rhs, AST_TYPE::AST_TYPE op_type) {
if (lsz != rsz) {
if (op_type == AST_TYPE::Eq)
return False;
Py_RETURN_FALSE;
if (op_type == AST_TYPE::NotEq)
return True;
Py_RETURN_TRUE;
}
int n = std::min(lsz, rsz);
......
......@@ -1376,8 +1376,8 @@ Box* longNonzero(BoxedLong* self) {
getTypeName(self));
if (mpz_sgn(self->n) == 0)
return False;
return True;
Py_RETURN_FALSE;
Py_RETURN_TRUE;
}
bool longNonzeroUnboxed(BoxedLong* self) {
......
......@@ -773,6 +773,9 @@ template Box* Box::getattr<REWRITABLE>(BoxedString*, GetattrRewriteArgs*);
template Box* Box::getattr<NOT_REWRITABLE>(BoxedString*, GetattrRewriteArgs*);
void Box::appendNewHCAttr(Box* new_attr, SetattrRewriteArgs* rewrite_args) {
assert(!rewrite_args); // need to emit incref
Py_INCREF(new_attr);
assert(cls->instancesHaveHCAttrs());
HCAttrs* attrs = getHCAttrsPtr();
HiddenClass* hcls = attrs->hcls;
......
......@@ -508,13 +508,13 @@ static Box* setIssubset(BoxedSet* self, Box* container) {
BoxedSet* rhs = static_cast<BoxedSet*>(container);
if (self->s.size() > rhs->s.size())
return False;
Py_RETURN_FALSE;
for (auto e : self->s) {
if (rhs->s.find(e) == rhs->s.end())
return False;
Py_RETURN_FALSE;
}
return True;
Py_RETURN_TRUE;
}
static Box* setIssuperset(BoxedSet* self, Box* container) {
......@@ -532,9 +532,9 @@ static Box* setIsdisjoint(BoxedSet* self, Box* container) {
for (auto e : container->pyElements()) {
if (self->s.find(e) != self->s.end())
return False;
Py_RETURN_FALSE;
}
return True;
Py_RETURN_TRUE;
}
static Box* setIntersection(BoxedSet* self, BoxedTuple* args) {
......@@ -590,10 +590,10 @@ Box* setPop(BoxedSet* self) {
Box* setEq(BoxedSet* self, BoxedSet* rhs) {
RELEASE_ASSERT(PyAnySet_Check(self), "");
if (!PyAnySet_Check(rhs))
return False;
Py_RETURN_FALSE;
if (self->s.size() != rhs->s.size())
return False;
Py_RETURN_FALSE;
return setIssubset(self, rhs);
}
......@@ -618,7 +618,7 @@ Box* setLt(BoxedSet* self, BoxedSet* rhs) {
raiseExcHelper(TypeError, "can only compare to a set");
if (self->s.size() >= rhs->s.size())
return False;
Py_RETURN_FALSE;
return setIssubset(self, rhs);
}
......@@ -637,7 +637,7 @@ Box* setGt(BoxedSet* self, BoxedSet* rhs) {
raiseExcHelper(TypeError, "can only compare to a set");
if (self->s.size() <= rhs->s.size())
return False;
Py_RETURN_FALSE;
return setIssuperset(self, rhs);
}
......
......@@ -1704,14 +1704,14 @@ Box* strIsAlpha(BoxedString* self) {
llvm::StringRef str(self->s());
if (str.empty())
return False;
Py_RETURN_FALSE;
for (const auto& c : str) {
if (!std::isalpha(c))
return False;
Py_RETURN_FALSE;
}
return True;
Py_RETURN_TRUE;
}
Box* strIsDigit(BoxedString* self) {
......@@ -1719,14 +1719,14 @@ Box* strIsDigit(BoxedString* self) {
llvm::StringRef str(self->s());
if (str.empty())
return False;
Py_RETURN_FALSE;
for (const auto& c : str) {
if (!std::isdigit(c))
return False;
Py_RETURN_FALSE;
}
return True;
Py_RETURN_TRUE;
}
Box* strIsAlnum(BoxedString* self) {
......@@ -1734,14 +1734,14 @@ Box* strIsAlnum(BoxedString* self) {
llvm::StringRef str(self->s());
if (str.empty())
return False;
Py_RETURN_FALSE;
for (const auto& c : str) {
if (!std::isalnum(c))
return False;
Py_RETURN_FALSE;
}
return True;
Py_RETURN_TRUE;
}
Box* strIsLower(BoxedString* self) {
......@@ -1751,13 +1751,13 @@ Box* strIsLower(BoxedString* self) {
bool lowered = false;
if (str.empty())
return False;
Py_RETURN_FALSE;
for (const auto& c : str) {
if (std::isspace(c) || std::isdigit(c)) {
continue;
} else if (!std::islower(c)) {
return False;
Py_RETURN_FALSE;
} else {
lowered = true;
}
......@@ -1772,12 +1772,12 @@ Box* strIsUpper(BoxedString* self) {
llvm::StringRef str(self->s());
if (str.empty())
return False;
Py_RETURN_FALSE;
bool cased = false;
for (const auto& c : str) {
if (std::islower(c))
return False;
Py_RETURN_FALSE;
else if (!cased && isupper(c))
cased = true;
}
......@@ -1790,14 +1790,14 @@ Box* strIsSpace(BoxedString* self) {
llvm::StringRef str(self->s());
if (str.empty())
return False;
Py_RETURN_FALSE;
for (const auto& c : str) {
if (!std::isspace(c))
return False;
Py_RETURN_FALSE;
}
return True;
Py_RETURN_TRUE;
}
Box* strIsTitle(BoxedString* self) {
......@@ -1806,7 +1806,7 @@ Box* strIsTitle(BoxedString* self) {
llvm::StringRef str(self->s());
if (str.empty())
return False;
Py_RETURN_FALSE;
if (str.size() == 1)
return boxBool(std::isupper(str[0]));
......@@ -1815,14 +1815,14 @@ Box* strIsTitle(BoxedString* self) {
for (const auto& c : str) {
if (std::isupper(c)) {
if (!start_of_word) {
return False;
Py_RETURN_FALSE;
}
start_of_word = false;
cased = true;
} else if (std::islower(c)) {
if (start_of_word) {
return False;
Py_RETURN_FALSE;
}
start_of_word = false;
......@@ -2131,9 +2131,9 @@ Box* strStartswith(BoxedString* self, Box* elt, Box* start, Box** _args) {
auto b = strStartswith(self, e, start, _args);
assert(b->cls == bool_cls);
if (b == True)
return True;
Py_RETURN_TRUE;
}
return False;
Py_RETURN_FALSE;
}
if (isSubclass(elt->cls, unicode_cls)) {
......@@ -2163,9 +2163,9 @@ Box* strStartswith(BoxedString* self, Box* elt, Box* start, Box** _args) {
Py_ssize_t compare_len = iend - istart;
if (compare_len < 0)
return False;
Py_RETURN_FALSE;
if (sub->size() > compare_len)
return False;
Py_RETURN_FALSE;
return boxBool(compareStringRefs(self->s(), istart, sub->size(), sub->s()) == 0);
}
......@@ -2202,9 +2202,9 @@ Box* strEndswith(BoxedString* self, Box* elt, Box* start, Box** _args) {
auto b = strEndswith(self, e, start, _args);
assert(b->cls == bool_cls);
if (b == True)
return True;
Py_RETURN_TRUE;
}
return False;
Py_RETURN_FALSE;
}
if (!PyString_Check(elt))
......@@ -2226,9 +2226,9 @@ Box* strEndswith(BoxedString* self, Box* elt, Box* start, Box** _args) {
Py_ssize_t compare_len = iend - istart;
if (compare_len < 0)
return False;
Py_RETURN_FALSE;
if (sub->size() > compare_len)
return False;
Py_RETURN_FALSE;
// XXX: this line is the only difference between startswith and endswith:
istart += compare_len - sub->size();
return boxBool(compareStringRefs(self->s(), istart, sub->size(), sub->s()) == 0);
......
......@@ -61,6 +61,7 @@ Box* tupleGetitemUnboxed(BoxedTuple* self, i64 n) {
raiseExcHelper(IndexError, "tuple index out of range");
Box* rtn = self->elts[n];
Py_INCREF(rtn);
return rtn;
}
......@@ -269,9 +270,9 @@ Box* tupleContains(BoxedTuple* self, Box* elt) {
throwCAPIException();
if (r)
return True;
Py_RETURN_TRUE;
}
return False;
Py_RETURN_FALSE;
}
Box* tupleIndex(BoxedTuple* self, Box* elt, Box* startBox, Box** args) {
......@@ -409,8 +410,10 @@ extern "C" PyObject* PyTuple_Pack(Py_ssize_t n, ...) noexcept {
extern "C" PyObject* PyTuple_New(Py_ssize_t size) noexcept {
RELEASE_ASSERT(size >= 0, "");
if (size == 0)
if (size == 0) {
Py_INCREF(EmptyTuple);
return EmptyTuple;
}
return BoxedTuple::create(size);
}
......
......@@ -1632,7 +1632,7 @@ extern "C" Box* none_repr(Box* v) noexcept {
}
extern "C" Box* noneNonzero(Box* v) {
return False;
Py_RETURN_FALSE;
}
extern "C" BoxedString* builtinFunctionOrMethodRepr(BoxedBuiltinFunctionOrMethod* v) {
......@@ -1794,7 +1794,7 @@ static void functionSetDefaults(Box* b, Box* v, void*) {
}
static Box* functionNonzero(BoxedFunction* self) {
return True;
Py_RETURN_TRUE;
}
extern "C" {
......@@ -3313,6 +3313,7 @@ extern "C" PyVarObject* PyObject_InitVar(PyVarObject* op, PyTypeObject* tp, Py_s
Py_TYPE(op) = tp;
op->ob_size = size;
_Py_NewReference(op);
gc::registerPythonObject(op);
......@@ -3329,6 +3330,7 @@ extern "C" PyObject* PyObject_Init(PyObject* op, PyTypeObject* tp) noexcept {
assert(gc::isValidGCObject(tp));
Py_TYPE(op) = tp;
_Py_NewReference(op);
gc::registerPythonObject(op);
......
......@@ -129,11 +129,14 @@ extern BoxedModule* sys_module, *builtins_module, *math_module, *time_module, *t
extern "C" inline Box* boxBool(bool b) __attribute__((visibility("default")));
extern "C" inline Box* boxBool(bool b) {
return b ? True : False;
if (b)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
extern "C" inline Box* boxBoolNegated(bool b) __attribute__((visibility("default")));
extern "C" inline Box* boxBoolNegated(bool b) {
return b ? False : True;
return boxBool(!b);
}
extern "C" Box* boxInt(i64) __attribute__((visibility("default")));
extern "C" i64 unboxInt(Box*);
......@@ -424,6 +427,7 @@ public:
BoxVar* rtn = static_cast<BoxVar*>(mem);
rtn->cls = str_cls;
rtn->ob_size = nitems;
_Py_NewReference(rtn);
return rtn;
}
......@@ -516,15 +520,19 @@ static_assert(offsetof(BoxedList, capacity) == offsetof(PyListObject, allocated)
class BoxedTuple : public BoxVar {
public:
static BoxedTuple* create(int64_t size) {
if (size == 0)
if (size == 0) {
Py_INCREF(EmptyTuple);
return EmptyTuple;
}
BoxedTuple* rtn = new (size) BoxedTuple();
memset(rtn->elts, 0, size * sizeof(Box*)); // TODO not all callers want this (but some do)
return rtn;
}
static BoxedTuple* create(int64_t nelts, Box** elts) {
if (nelts == 0)
if (nelts == 0) {
Py_INCREF(EmptyTuple);
return EmptyTuple;
}
BoxedTuple* rtn = new (nelts) BoxedTuple();
for (int i = 0; i < nelts; i++)
assert(gc::isValidGCObject(elts[i]));
......@@ -652,6 +660,7 @@ public:
BoxVar* rtn = static_cast<BoxVar*>(mem);
rtn->cls = tuple_cls;
rtn->ob_size = nitems;
_Py_NewReference(rtn);
return rtn;
}
......@@ -996,6 +1005,7 @@ public:
= static_cast<BoxedClosure*>(gc_alloc(sizeof(BoxedClosure) + nelts * sizeof(Box*), gc::GCKind::PYTHON));
rtn->nelts = nelts;
rtn->cls = closure_cls;
_Py_NewReference(rtn);
memset((void*)rtn->elts, 0, sizeof(Box*) * nelts);
return rtn;
}
......@@ -1128,9 +1138,13 @@ Box* getFrame(int depth);
inline BoxedString* boxString(llvm::StringRef s) {
if (s.size() <= 1) {
BoxedString* r;
if (s.size() == 0)
return EmptyString;
return characters[s.data()[0] & UCHAR_MAX];
r = EmptyString;
else
r = characters[s.data()[0] & UCHAR_MAX];
Py_INCREF(r);
return r;
}
return new (s.size()) BoxedString(s);
}
......@@ -1139,7 +1153,9 @@ inline BoxedString* boxString(llvm::StringRef s) {
extern BoxedInt* interned_ints[NUM_INTERNED_INTS];
extern "C" inline Box* boxInt(int64_t n) {
if (0 <= n && n < NUM_INTERNED_INTS) {
return interned_ints[n];
auto r = interned_ints[n];
Py_INCREF(r);
return r;
}
return new BoxedInt(n);
}
......
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