extern "C" Box* intNonzero(BoxedInt* v) { if (!isSubclass(v->cls, int_cls)) raiseExcHelper(TypeError, "descriptor '__nonzero__' requires a 'int' object but received a '%s'", getTypeName(v)); return boxBool(v->n != 0); }
static Box* acquire(Box* _self, Box* _waitflag) { RELEASE_ASSERT(_self->cls == thread_lock_cls, ""); BoxedThreadLock* self = static_cast<BoxedThreadLock*>(_self); RELEASE_ASSERT(isSubclass(_waitflag->cls, int_cls), ""); int waitflag = static_cast<BoxedInt*>(_waitflag)->n; // Copied + adapted from CPython: int success; auto thelock = &self->lock; int status, error = 0; { threading::GLAllowThreadsReadRegion _allow_threads; do { if (waitflag) status = fix_status(pthread_mutex_lock(thelock)); else status = fix_status(pthread_mutex_trylock(thelock)); } while (status == EINTR); /* Retry if interrupted by a signal */ } if (waitflag) { CHECK_STATUS("mutex_lock"); } else if (status != EBUSY) { CHECK_STATUS("mutex_trylock"); } success = (status == 0) ? 1 : 0; RELEASE_ASSERT(status == 0 || !waitflag, "could not lock mutex! error %d", status); return boxBool(status == 0); }
Box* isinstance(Box* obj, Box *cls) { assert(cls->cls == type_cls); BoxedClass *ccls = static_cast<BoxedClass*>(cls); // TODO need to check if it's a subclass, or if subclasshook exists return boxBool(obj->cls == cls); }
extern "C" Box* intGe(BoxedInt* lhs, Box *rhs) { assert(lhs->cls == int_cls); if (rhs->cls != int_cls) { return NotImplemented; } BoxedInt *rhs_int = static_cast<BoxedInt*>(rhs); return boxBool(lhs->n >= rhs_int->n); }
extern "C" Box* boolOr(BoxedBool* lhs, BoxedBool* rhs) { if (!PyBool_Check(lhs)) raiseExcHelper(TypeError, "descriptor '__or__' requires a 'bool' object but received a '%s'", getTypeName(lhs)); if (!PyBool_Check(rhs)) return intOr(lhs, rhs); return boxBool(lhs->n || rhs->n); }
extern "C" Box* intGe(BoxedInt* lhs, Box* rhs) { if (!isSubclass(lhs->cls, int_cls)) raiseExcHelper(TypeError, "descriptor '__ge__' requires a 'int' object but received a '%s'", getTypeName(lhs)); if (rhs->cls != int_cls) { return NotImplemented; } BoxedInt* rhs_int = static_cast<BoxedInt*>(rhs); return boxBool(lhs->n >= rhs_int->n); }
Box* _listCmp(BoxedList* lhs, BoxedList* rhs, AST_TYPE::AST_TYPE op_type) { int lsz = lhs->size; int rsz = rhs->size; bool is_order = (op_type == AST_TYPE::Lt || op_type == AST_TYPE::LtE || op_type == AST_TYPE::Gt || op_type == AST_TYPE::GtE); if (lsz != rsz) { if (op_type == AST_TYPE::Eq) return False; if (op_type == AST_TYPE::NotEq) return True; } int n = std::min(lsz, rsz); for (int i = 0; i < n; i++) { bool identity_eq = lhs->elts->elts[i] == rhs->elts->elts[i]; if (identity_eq) continue; Box* is_eq = compareInternal(lhs->elts->elts[i], rhs->elts->elts[i], AST_TYPE::Eq, NULL); bool bis_eq = nonzero(is_eq); if (bis_eq) continue; if (op_type == AST_TYPE::Eq) { return boxBool(false); } else if (op_type == AST_TYPE::NotEq) { return boxBool(true); } else { Box* r = compareInternal(lhs->elts->elts[i], rhs->elts->elts[i], op_type, NULL); return r; } } if (op_type == AST_TYPE::Lt) return boxBool(lsz < rsz); else if (op_type == AST_TYPE::LtE) return boxBool(lsz <= rsz); else if (op_type == AST_TYPE::Gt) return boxBool(lsz > rsz); else if (op_type == AST_TYPE::GtE) return boxBool(lsz >= rsz); else if (op_type == AST_TYPE::Eq) return boxBool(lsz == rsz); else if (op_type == AST_TYPE::NotEq) return boxBool(lsz != rsz); RELEASE_ASSERT(0, "%d", op_type); }
Box* setContains(BoxedSet* self, Box* key) { RELEASE_ASSERT(PyAnySet_Check(self), ""); if (PySet_Check(key)) { try { BoxAndHash k_hash(key); return boxBool(self->s.find(k_hash) != self->s.end()); } catch (ExcInfo e) { if (!e.matches(TypeError)) throw e; e.clear(); BoxedSet* tmpKey = makeNewSet(frozenset_cls, key); AUTO_DECREF(tmpKey); return boxBool(self->s.find(tmpKey) != self->s.end()); } } return boxBool(self->s.find(key) != self->s.end()); }
static Box* acquire(Box* _self, Box* _waitflag) { RELEASE_ASSERT(_self->cls == thread_lock_cls, ""); BoxedThreadLock* self = static_cast<BoxedThreadLock*>(_self); RELEASE_ASSERT(isSubclass(_waitflag->cls, int_cls), ""); int waitflag = static_cast<BoxedInt*>(_waitflag)->n; int rtn; { threading::GLAllowThreadsReadRegion _allow_threads; rtn = PyThread_acquire_lock(self->lock_lock, waitflag); } return boxBool(rtn); }
Box* generatorHasnext(Box* s) { return boxBool(generatorHasnextUnboxed(s)); }
Box* listiterHasnext(Box* s) { assert(s->cls == list_iterator_cls); BoxedListIterator* self = static_cast<BoxedListIterator*>(s); return boxBool(self->pos < self->l->size); }
extern "C" Box* intNonzero(BoxedInt* v) { assert(v->cls == int_cls); return boxBool(v->n != 0); }
extern "C" Box* intGeInt(BoxedInt* lhs, BoxedInt* rhs) { assert(lhs->cls == int_cls); assert(rhs->cls == int_cls); return boxBool(lhs->n >= rhs->n); }
Box* tupleiterHasnext(Box* s) { return boxBool(tupleiterHasnextUnboxed(s)); }
Box* tupleNonzero(BoxedTuple* self) { RELEASE_ASSERT(isSubclass(self->cls, tuple_cls), ""); return boxBool(self->size() != 0); }
extern "C" Box* listNonzero(BoxedList* self) { return boxBool(self->size != 0); }
Box* setiteratorHasnext(BoxedSetIterator* self) { assert(self->cls == set_iterator_cls); return boxBool(self->hasNext()); }
Box* setContains(BoxedSet* self, Box* v) { assert(self->cls == set_cls || self->cls == frozenset_cls); return boxBool(self->s.count(v) != 0); }
Box* JitFragmentWriter::notHelper(Box* b) { return boxBool(!b->nonzeroIC()); }
Box* JitFragmentWriter::hasnextHelper(Box* b) { return boxBool(pyston::hasnext(b)); }
Box* JitFragmentWriter::exceptionMatchesHelper(Box* obj, Box* cls) { return boxBool(exceptionMatches(obj, cls)); }
extern "C" Box* boolNew2(Box* cls, Box* val) { assert(cls == bool_cls); bool b = nonzero(val); return boxBool(b); }
Box* dictIterHasnext(Box* s) { return boxBool(dictIterHasnextUnboxed(s)); }
void setupSys() { sys_modules_dict = new BoxedDict(); PyThreadState_GET()->interp->modules = incref(sys_modules_dict); constants.push_back(sys_modules_dict); // This is ok to call here because we've already created the sys_modules_dict sys_module = createModule(autoDecref(boxString("sys"))); // sys_module is what holds on to all of the other modules: Py_INCREF(sys_module); late_constants.push_back(sys_module); sys_module->giveAttrBorrowed("modules", sys_modules_dict); BoxedList* sys_path = new BoxedList(); constants.push_back(sys_path); sys_module->giveAttrBorrowed("path", sys_path); sys_module->giveAttr("argv", new BoxedList()); sys_module->giveAttr("exc_info", new BoxedBuiltinFunctionOrMethod(BoxedCode::create((void*)sysExcInfo, BOXED_TUPLE, 0, "exc_info", exc_info_doc))); sys_module->giveAttr("exc_clear", new BoxedBuiltinFunctionOrMethod( BoxedCode::create((void*)sysExcClear, NONE, 0, "exc_clear", exc_clear_doc))); sys_module->giveAttr( "exit", new BoxedBuiltinFunctionOrMethod( BoxedCode::create((void*)sysExit, NONE, 1, false, false, "exit", exit_doc), { Py_None }, NULL)); sys_module->giveAttr("warnoptions", new BoxedList()); sys_module->giveAttrBorrowed("py3kwarning", Py_False); sys_module->giveAttr("byteorder", boxString(isLittleEndian() ? "little" : "big")); sys_module->giveAttr("platform", boxString(Py_GetPlatform())); sys_module->giveAttr("executable", boxString(Py_GetProgramFullPath())); sys_module->giveAttr( "_getframe", new BoxedFunction(BoxedCode::create((void*)sysGetFrame, UNKNOWN, 1, false, false, "_getframe"), { NULL })); sys_module->giveAttr("_current_frames", new BoxedFunction(BoxedCode::create((void*)sysCurrentFrames, UNKNOWN, 0, "_current_frames"))); sys_module->giveAttr("getdefaultencoding", new BoxedBuiltinFunctionOrMethod(BoxedCode::create( (void*)sysGetDefaultEncoding, STR, 0, "getdefaultencoding", getdefaultencoding_doc))); sys_module->giveAttr("getfilesystemencoding", new BoxedBuiltinFunctionOrMethod(BoxedCode::create( (void*)sysGetFilesystemEncoding, STR, 0, "getfilesystemencoding", getfilesystemencoding_doc))); sys_module->giveAttr("getrecursionlimit", new BoxedBuiltinFunctionOrMethod(BoxedCode::create( (void*)sysGetRecursionLimit, UNKNOWN, 0, "getrecursionlimit", getrecursionlimit_doc))); sys_module->giveAttr("dont_write_bytecode", boxBool(Py_DontWriteBytecodeFlag)); sys_module->giveAttr("prefix", boxString(Py_GetPrefix())); sys_module->giveAttr("exec_prefix", boxString(Py_GetExecPrefix())); sys_module->giveAttr("copyright", boxString("Copyright 2014-2016 Dropbox.\nAll Rights Reserved.\n\nCopyright (c) 2001-2014 " "Python Software Foundation.\nAll Rights Reserved.\n\nCopyright (c) 2000 " "BeOpen.com.\nAll Rights Reserved.\n\nCopyright (c) 1995-2001 Corporation for " "National Research Initiatives.\nAll Rights Reserved.\n\nCopyright (c) " "1991-1995 Stichting Mathematisch Centrum, Amsterdam.\nAll Rights Reserved.")); sys_module->giveAttr("version", boxString(generateVersionString())); sys_module->giveAttr("hexversion", boxInt(PY_VERSION_HEX)); sys_module->giveAttr("subversion", BoxedTuple::create({ autoDecref(boxString("Pyston")), autoDecref(boxString("")), autoDecref(boxString("")) })); sys_module->giveAttr("maxint", boxInt(PYSTON_INT_MAX)); sys_module->giveAttr("maxsize", boxInt(PY_SSIZE_T_MAX)); #define SET_SYS_FROM_STRING(key, value) sys_module->giveAttr((key), (value)) #ifdef Py_USING_UNICODE SET_SYS_FROM_STRING("maxunicode", PyInt_FromLong(PyUnicode_GetMax())); #endif /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */ #ifndef PY_NO_SHORT_FLOAT_REPR SET_SYS_FROM_STRING("float_repr_style", PyString_FromString("short")); #else SET_SYS_FROM_STRING("float_repr_style", PyString_FromString("legacy")); #endif auto sys_str = getStaticString("sys"); for (auto& md : sys_methods) { sys_module->giveAttr(md.ml_name, new BoxedCApiFunction(&md, NULL, sys_str)); } sys_module->giveAttrBorrowed("__displayhook__", sys_module->getattr(autoDecref(internStringMortal("displayhook")))); }
Box* setNe(BoxedSet* self, BoxedSet* rhs) { Box* r = setEq(self, rhs); AUTO_DECREF(r); assert(r->cls == bool_cls); return boxBool(r == Py_False); }
Box* setNonzero(BoxedSet* self) { RELEASE_ASSERT(PyAnySet_Check(self), ""); return boxBool(self->s.size()); }
extern "C" Box* intGeInt(BoxedInt* lhs, BoxedInt* rhs) { assert(isSubclass(lhs->cls, int_cls)); assert(isSubclass(rhs->cls, int_cls)); return boxBool(lhs->n >= rhs->n); }
extern "C" Box* floatGeInt(BoxedFloat* lhs, BoxedInt *rhs) { assert(lhs->cls == float_cls); assert(rhs->cls == int_cls); return boxBool(lhs->d >= rhs->n); }
extern "C" PyObject* PyBool_FromLong(long n) noexcept { return boxBool(n != 0); }
Box* floatNonzero(BoxedFloat *self) { return boxBool(floatNonzeroUnboxed(self)); }