Commit db4bc778 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Add internStringImmortal helper function

And internStringMortal, which for now just resolves to
internStringImmortal, but lets us mark strings that
could eventually be collected.  (We could use the same
approach that CPython uses and have a string destructor
that removes mortal strings from the intern table.)
parent e648044c
......@@ -1133,7 +1133,7 @@ Value ASTInterpreter::visit_assert(AST_Assert* node) {
assert(v.o->cls == int_cls && static_cast<BoxedInt*>(v.o)->n == 0);
#endif
static BoxedString* AssertionError_str = static_cast<BoxedString*>(PyString_InternFromString("AssertionError"));
static BoxedString* AssertionError_str = internStringImmortal("AssertionError");
Box* assertion_type = getGlobal(globals, AssertionError_str);
assertFail(assertion_type, node->msg ? visit_expr(node->msg).o : 0);
......
......@@ -357,7 +357,7 @@ public:
}
CompilerVariable* getPystonIter(IREmitter& emitter, const OpInfo& info, ConcreteCompilerVariable* var) override {
static BoxedString* iter_box = static_cast<BoxedString*>(PyString_InternFromString("__iter__"));
static BoxedString* iter_box = internStringImmortal("__iter__");
CallattrFlags flags = {.cls_only = true, .null_on_nonexistent = true, .argspec = ArgPassSpec(0) };
CompilerVariable* iter_call = var->callattr(emitter, info, iter_box, flags, {}, 0);
......@@ -1705,7 +1705,7 @@ public:
}
CompilerVariable* getitem(IREmitter& emitter, const OpInfo& info, VAR* var, CompilerVariable* slice) override {
static BoxedString* attr = static_cast<BoxedString*>(PyString_InternFromString("__getitem__"));
static BoxedString* attr = internStringImmortal("__getitem__");
bool no_attribute = false;
ConcreteCompilerVariable* called_constant = tryCallattrConstant(
emitter, info, var, attr, true, ArgPassSpec(1, 0, 0, 0), { slice }, NULL, &no_attribute);
......@@ -1731,7 +1731,7 @@ public:
}
ConcreteCompilerVariable* len(IREmitter& emitter, const OpInfo& info, VAR* var) override {
static BoxedString* attr = static_cast<BoxedString*>(PyString_InternFromString("__len__"));
static BoxedString* attr = internStringImmortal("__len__");
ConcreteCompilerVariable* called_constant
= tryCallattrConstant(emitter, info, var, attr, true, ArgPassSpec(0, 0, 0, 0), {}, NULL);
if (called_constant)
......@@ -1741,7 +1741,7 @@ public:
}
ConcreteCompilerVariable* nonzero(IREmitter& emitter, const OpInfo& info, ConcreteCompilerVariable* var) override {
static BoxedString* attr = static_cast<BoxedString*>(PyString_InternFromString("__nonzero__"));
static BoxedString* attr = internStringImmortal("__nonzero__");
bool no_attribute = false;
ConcreteCompilerVariable* called_constant
......@@ -1765,7 +1765,7 @@ public:
}
ConcreteCompilerVariable* hasnext(IREmitter& emitter, const OpInfo& info, ConcreteCompilerVariable* var) override {
static BoxedString* attr = static_cast<BoxedString*>(PyString_InternFromString("__hasnext__"));
static BoxedString* attr = internStringImmortal("__hasnext__");
ConcreteCompilerVariable* called_constant
= tryCallattrConstant(emitter, info, var, attr, true, ArgPassSpec(0, 0, 0, 0), {}, NULL, NULL);
......
......@@ -302,7 +302,7 @@ Box* evalOrExec(CLFunction* cl, Box* globals, Box* boxedLocals) {
Box* doc_string = cl->source->getDocString();
if (doc_string != None) {
static BoxedString* doc_box = static_cast<BoxedString*>(PyString_InternFromString("__doc__"));
static BoxedString* doc_box = internStringImmortal("__doc__");
setGlobal(boxedLocals, doc_box, doc_string);
}
......
......@@ -894,7 +894,7 @@ private:
llvm::Value* v = emitter.getBuilder()->CreateCall(g.funcs.createDict);
ConcreteCompilerVariable* rtn = new ConcreteCompilerVariable(DICT, v, true);
if (node->keys.size()) {
static BoxedString* setitem_str = static_cast<BoxedString*>(PyString_InternFromString("__setitem__"));
static BoxedString* setitem_str = internStringImmortal("__setitem__");
CompilerVariable* setitem = rtn->getattr(emitter, getEmptyOpInfo(unw_info), setitem_str, true);
for (int i = 0; i < node->keys.size(); i++) {
CompilerVariable* key = evalExpr(node->keys[i], unw_info);
......@@ -1136,7 +1136,7 @@ private:
llvm::Value* v = emitter.getBuilder()->CreateCall(g.funcs.createSet);
ConcreteCompilerVariable* rtn = new ConcreteCompilerVariable(SET, v, true);
static BoxedString* add_str = static_cast<BoxedString*>(PyString_InternFromString("add"));
static BoxedString* add_str = internStringImmortal("add");
for (int i = 0; i < node->elts.size(); i++) {
CompilerVariable* elt = elts[i];
......@@ -1722,7 +1722,7 @@ private:
// We could patchpoint this or try to avoid the overhead, but this should only
// happen when the assertion is actually thrown so I don't think it will be necessary.
static BoxedString* AssertionError_str = static_cast<BoxedString*>(PyString_InternFromString("AssertionError"));
static BoxedString* AssertionError_str = internStringImmortal("AssertionError");
llvm_args.push_back(emitter.createCall2(unw_info, g.funcs.getGlobal, embedParentModulePtr(),
embedRelocatablePtr(AssertionError_str, g.llvm_boxedstring_type_ptr)));
......@@ -1885,9 +1885,9 @@ private:
}
assert(dest);
static BoxedString* write_str = static_cast<BoxedString*>(PyString_InternFromString("write"));
static BoxedString* newline_str = static_cast<BoxedString*>(PyString_InternFromString("\n"));
static BoxedString* space_str = static_cast<BoxedString*>(PyString_InternFromString(" "));
static BoxedString* write_str = internStringImmortal("write");
static BoxedString* newline_str = internStringImmortal("\n");
static BoxedString* space_str = internStringImmortal(" ");
// TODO: why are we inline-generating all this code instead of just emitting a call to some runtime function?
// (=printHelper())
......
......@@ -114,31 +114,31 @@ BoxedString* getOpName(int op_type) {
static bool initialized = false;
if (!initialized) {
strAdd = static_cast<BoxedString*>(PyString_InternFromString("__add__"));
strBitAnd = static_cast<BoxedString*>(PyString_InternFromString("__and__"));
strBitOr = static_cast<BoxedString*>(PyString_InternFromString("__or__"));
strBitXor = static_cast<BoxedString*>(PyString_InternFromString("__xor__"));
strDiv = static_cast<BoxedString*>(PyString_InternFromString("__div__"));
strTrueDiv = static_cast<BoxedString*>(PyString_InternFromString("__truediv__"));
strDivMod = static_cast<BoxedString*>(PyString_InternFromString("__divmod__"));
strEq = static_cast<BoxedString*>(PyString_InternFromString("__eq__"));
strFloorDiv = static_cast<BoxedString*>(PyString_InternFromString("__floordiv__"));
strLShift = static_cast<BoxedString*>(PyString_InternFromString("__lshift__"));
strLt = static_cast<BoxedString*>(PyString_InternFromString("__lt__"));
strLtE = static_cast<BoxedString*>(PyString_InternFromString("__le__"));
strGt = static_cast<BoxedString*>(PyString_InternFromString("__gt__"));
strGtE = static_cast<BoxedString*>(PyString_InternFromString("__ge__"));
strIn = static_cast<BoxedString*>(PyString_InternFromString("__contains__"));
strInvert = static_cast<BoxedString*>(PyString_InternFromString("__invert__"));
strMod = static_cast<BoxedString*>(PyString_InternFromString("__mod__"));
strMult = static_cast<BoxedString*>(PyString_InternFromString("__mul__"));
strNot = static_cast<BoxedString*>(PyString_InternFromString("__nonzero__"));
strNotEq = static_cast<BoxedString*>(PyString_InternFromString("__ne__"));
strPow = static_cast<BoxedString*>(PyString_InternFromString("__pow__"));
strRShift = static_cast<BoxedString*>(PyString_InternFromString("__rshift__"));
strSub = static_cast<BoxedString*>(PyString_InternFromString("__sub__"));
strUAdd = static_cast<BoxedString*>(PyString_InternFromString("__pos__"));
strUSub = static_cast<BoxedString*>(PyString_InternFromString("__neg__"));
strAdd = internStringImmortal("__add__");
strBitAnd = internStringImmortal("__and__");
strBitOr = internStringImmortal("__or__");
strBitXor = internStringImmortal("__xor__");
strDiv = internStringImmortal("__div__");
strTrueDiv = internStringImmortal("__truediv__");
strDivMod = internStringImmortal("__divmod__");
strEq = internStringImmortal("__eq__");
strFloorDiv = internStringImmortal("__floordiv__");
strLShift = internStringImmortal("__lshift__");
strLt = internStringImmortal("__lt__");
strLtE = internStringImmortal("__le__");
strGt = internStringImmortal("__gt__");
strGtE = internStringImmortal("__ge__");
strIn = internStringImmortal("__contains__");
strInvert = internStringImmortal("__invert__");
strMod = internStringImmortal("__mod__");
strMult = internStringImmortal("__mul__");
strNot = internStringImmortal("__nonzero__");
strNotEq = internStringImmortal("__ne__");
strPow = internStringImmortal("__pow__");
strRShift = internStringImmortal("__rshift__");
strSub = internStringImmortal("__sub__");
strUAdd = internStringImmortal("__pos__");
strUSub = internStringImmortal("__neg__");
initialized = true;
}
......@@ -203,7 +203,7 @@ BoxedString* getOpName(int op_type) {
BoxedString* getInplaceOpName(int op_type) {
BoxedString* normal_name = getOpName(op_type);
// TODO inefficient
return static_cast<BoxedString*>(PyString_InternFromString(("__i" + normal_name->s().substr(2).str()).c_str()));
return internStringImmortal(("__i" + normal_name->s().substr(2).str()).c_str());
}
// Maybe better name is "swapped" -- it's what the runtime will try if the normal op
......@@ -234,7 +234,7 @@ BoxedString* getReverseOpName(int op_type) {
return getOpName(op_type);
BoxedString* normal_name = getOpName(op_type);
// TODO inefficient
return static_cast<BoxedString*>(PyString_InternFromString(("__r" + normal_name->s().substr(2).str()).c_str()));
return internStringImmortal(("__r" + normal_name->s().substr(2).str()).c_str());
}
template <class T> static void visitVector(const std::vector<T*>& vec, ASTVisitor* v) {
......
......@@ -25,9 +25,8 @@ InternedString InternedStringPool::get(llvm::StringRef arg) {
if (it != interned.end()) {
s = it->second;
} else {
s = boxString(arg);
// HACK: should properly track this liveness:
PyString_InternInPlace(reinterpret_cast<Box**>(&s));
s = internStringImmortal(arg);
// Note: make sure the key points to the value we just created, not the
// argument string:
......
......@@ -456,6 +456,13 @@ struct SetattrRewriteArgs;
struct GetattrRewriteArgs;
struct DelattrRewriteArgs;
// Helper function around PyString_InternFromString:
inline BoxedString* internStringImmortal(const char* s) {
BoxedString* r = (BoxedString*)PyString_InternFromString(s);
assert(r);
return r;
}
struct HCAttrs {
public:
struct AttrList {
......
......@@ -31,8 +31,8 @@ extern "C" Box* boolNonzero(BoxedBool* v) {
}
extern "C" Box* boolRepr(BoxedBool* v) {
static BoxedString* true_str = static_cast<BoxedString*>(PyString_InternFromString("True"));
static BoxedString* false_str = static_cast<BoxedString*>(PyString_InternFromString("False"));
static BoxedString* true_str = internStringImmortal("True");
static BoxedString* false_str = internStringImmortal("False");
if (v == True)
return true_str;
......
......@@ -95,14 +95,14 @@ extern "C" Box* abs_(Box* x) {
} else if (x->cls == long_cls) {
return longAbs(static_cast<BoxedLong*>(x));
} else {
static BoxedString* abs_str = static_cast<BoxedString*>(PyString_InternFromString("__abs__"));
static BoxedString* abs_str = internStringImmortal("__abs__");
CallattrFlags callattr_flags{.cls_only = true, .null_on_nonexistent = false, .argspec = ArgPassSpec(0) };
return callattr(x, abs_str, callattr_flags, NULL, NULL, NULL, NULL, NULL);
}
}
extern "C" Box* hexFunc(Box* x) {
static BoxedString* hex_str = static_cast<BoxedString*>(PyString_InternFromString("__hex__"));
static BoxedString* hex_str = internStringImmortal("__hex__");
CallattrFlags callattr_flags{.cls_only = true, .null_on_nonexistent = true, .argspec = ArgPassSpec(0) };
Box* r = callattr(x, hex_str, callattr_flags, NULL, NULL, NULL, NULL, NULL);
if (!r)
......@@ -115,7 +115,7 @@ extern "C" Box* hexFunc(Box* x) {
}
extern "C" Box* octFunc(Box* x) {
static BoxedString* oct_str = static_cast<BoxedString*>(PyString_InternFromString("__oct__"));
static BoxedString* oct_str = internStringImmortal("__oct__");
CallattrFlags callattr_flags{.cls_only = true, .null_on_nonexistent = true, .argspec = ArgPassSpec(0) };
Box* r = callattr(x, oct_str, callattr_flags, NULL, NULL, NULL, NULL, NULL);
if (!r)
......@@ -209,7 +209,7 @@ extern "C" Box* max(Box* arg0, BoxedTuple* args) {
extern "C" Box* next(Box* iterator, Box* _default) {
try {
static BoxedString* next_str = static_cast<BoxedString*>(PyString_InternFromString("next"));
static BoxedString* next_str = internStringImmortal("next");
CallattrFlags callattr_flags{.cls_only = true, .null_on_nonexistent = false, .argspec = ArgPassSpec(0) };
return callattr(iterator, next_str, callattr_flags, NULL, NULL, NULL, NULL, NULL);
} catch (ExcInfo e) {
......@@ -862,9 +862,9 @@ Box* print(BoxedTuple* args, BoxedDict* kwargs) {
Box* dest, *end;
static BoxedString* file_str = static_cast<BoxedString*>(PyString_InternFromString("file"));
static BoxedString* end_str = static_cast<BoxedString*>(PyString_InternFromString("end"));
static BoxedString* space_str = static_cast<BoxedString*>(PyString_InternFromString(" "));
static BoxedString* file_str = internStringImmortal("file");
static BoxedString* end_str = internStringImmortal("end");
static BoxedString* space_str = internStringImmortal(" ");
auto it = kwargs->d.find(file_str);
if (it != kwargs->d.end()) {
......@@ -884,7 +884,7 @@ Box* print(BoxedTuple* args, BoxedDict* kwargs) {
RELEASE_ASSERT(kwargs->d.size() == 0, "print() got unexpected keyword arguments");
static BoxedString* write_str = static_cast<BoxedString*>(PyString_InternFromString("write"));
static BoxedString* write_str = internStringImmortal("write");
CallattrFlags callattr_flags{.cls_only = false, .null_on_nonexistent = false, .argspec = ArgPassSpec(1) };
// TODO softspace handling?
......@@ -908,7 +908,7 @@ Box* print(BoxedTuple* args, BoxedDict* kwargs) {
}
Box* getreversed(Box* o) {
static BoxedString* reversed_str = static_cast<BoxedString*>(PyString_InternFromString("__reversed__"));
static BoxedString* reversed_str = internStringImmortal("__reversed__");
// TODO add rewriting to this? probably want to try to avoid this path though
CallattrFlags callattr_flags{.cls_only = true, .null_on_nonexistent = true, .argspec = ArgPassSpec(0) };
......
......@@ -205,7 +205,7 @@ void appendToSysPath(llvm::StringRef path) {
void prependToSysPath(llvm::StringRef path) {
BoxedList* sys_path = getSysPath();
static BoxedString* insert_str = static_cast<BoxedString*>(PyString_InternFromString("insert"));
static BoxedString* insert_str = internStringImmortal("insert");
CallattrFlags callattr_flags{.cls_only = false, .null_on_nonexistent = false, .argspec = ArgPassSpec(2) };
callattr(sys_path, insert_str, callattr_flags, boxInt(0), boxString(path), NULL, NULL, NULL);
}
......
......@@ -381,7 +381,7 @@ Box* instanceRepr(Box* _inst) {
RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
static BoxedString* repr_str = static_cast<BoxedString*>(PyString_InternFromString("__repr__"));
static BoxedString* repr_str = internStringImmortal("__repr__");
Box* repr_func = _instanceGetattribute(inst, repr_str, false);
if (repr_func) {
......@@ -400,7 +400,7 @@ Box* instanceStr(Box* _inst) {
RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
static BoxedString* str_str = static_cast<BoxedString*>(PyString_InternFromString("__str__"));
static BoxedString* str_str = internStringImmortal("__str__");
Box* str_func = _instanceGetattribute(inst, str_str, false);
if (str_func) {
......@@ -415,7 +415,7 @@ Box* instanceNonzero(Box* _inst) {
RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
static BoxedString* nonzero_str = static_cast<BoxedString*>(PyString_InternFromString("__nonzero__"));
static BoxedString* nonzero_str = internStringImmortal("__nonzero__");
Box* nonzero_func = NULL;
try {
......@@ -426,7 +426,7 @@ Box* instanceNonzero(Box* _inst) {
}
if (nonzero_func == NULL) {
static BoxedString* len_str = static_cast<BoxedString*>(PyString_InternFromString("__len__"));
static BoxedString* len_str = internStringImmortal("__len__");
try {
nonzero_func = _instanceGetattribute(inst, len_str, false);
} catch (ExcInfo e) {
......@@ -446,7 +446,7 @@ Box* instanceLen(Box* _inst) {
RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
static BoxedString* len_str = static_cast<BoxedString*>(PyString_InternFromString("__len__"));
static BoxedString* len_str = internStringImmortal("__len__");
Box* len_func = _instanceGetattribute(inst, len_str, true);
return runtimeCall(len_func, ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL);
}
......@@ -455,7 +455,7 @@ Box* instanceGetitem(Box* _inst, Box* key) {
RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
static BoxedString* getitem_str = static_cast<BoxedString*>(PyString_InternFromString("__getitem__"));
static BoxedString* getitem_str = internStringImmortal("__getitem__");
Box* getitem_func = _instanceGetattribute(inst, getitem_str, true);
return runtimeCall(getitem_func, ArgPassSpec(1), key, NULL, NULL, NULL, NULL);
}
......@@ -464,7 +464,7 @@ Box* instanceSetitem(Box* _inst, Box* key, Box* value) {
RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
static BoxedString* setitem_str = static_cast<BoxedString*>(PyString_InternFromString("__setitem__"));
static BoxedString* setitem_str = internStringImmortal("__setitem__");
Box* setitem_func = _instanceGetattribute(inst, setitem_str, true);
return runtimeCall(setitem_func, ArgPassSpec(2), key, value, NULL, NULL, NULL);
}
......@@ -473,7 +473,7 @@ Box* instanceDelitem(Box* _inst, Box* key) {
RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
static BoxedString* delitem_str = static_cast<BoxedString*>(PyString_InternFromString("__delitem__"));
static BoxedString* delitem_str = internStringImmortal("__delitem__");
Box* delitem_func = _instanceGetattribute(inst, delitem_str, true);
return runtimeCall(delitem_func, ArgPassSpec(1), key, NULL, NULL, NULL, NULL);
}
......@@ -494,7 +494,7 @@ static int half_cmp(PyObject* v, PyObject* w) noexcept {
assert(PyInstance_Check(v));
static BoxedString* cmp_str = static_cast<BoxedString*>(PyString_InternFromString("__cmp__"));
static BoxedString* cmp_str = internStringImmortal("__cmp__");
// Pyston change:
#if 0
if (cmp_obj == NULL) {
......@@ -617,7 +617,7 @@ Box* instanceContains(Box* _inst, Box* key) {
RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
static BoxedString* contains_str = static_cast<BoxedString*>(PyString_InternFromString("__contains__"));
static BoxedString* contains_str = internStringImmortal("__contains__");
Box* contains_func = _instanceGetattribute(inst, contains_str, false);
if (!contains_func) {
......@@ -638,9 +638,9 @@ static Box* instanceHash(BoxedInstance* inst) {
PyObject* func;
PyObject* res;
static BoxedString* hash_str = static_cast<BoxedString*>(PyString_InternFromString("__hash__"));
static BoxedString* eq_str = static_cast<BoxedString*>(PyString_InternFromString("__eq__"));
static BoxedString* cmp_str = static_cast<BoxedString*>(PyString_InternFromString("__cmp__"));
static BoxedString* hash_str = internStringImmortal("__hash__");
static BoxedString* eq_str = internStringImmortal("__eq__");
static BoxedString* cmp_str = internStringImmortal("__cmp__");
func = _instanceGetattribute(inst, hash_str, false);
if (func == NULL) {
......@@ -671,8 +671,8 @@ static Box* instanceIter(BoxedInstance* self) {
PyObject* func;
static BoxedString* iter_str = static_cast<BoxedString*>(PyString_InternFromString("__iter__"));
static BoxedString* getitem_str = static_cast<BoxedString*>(PyString_InternFromString("__getitem__"));
static BoxedString* iter_str = internStringImmortal("__iter__");
static BoxedString* getitem_str = internStringImmortal("__getitem__");
if ((func = _instanceGetattribute(self, iter_str, false)) != NULL) {
PyObject* res = PyEval_CallObject(func, (PyObject*)NULL);
if (!res)
......@@ -696,7 +696,7 @@ static Box* instanceIter(BoxedInstance* self) {
static Box* instanceNext(BoxedInstance* inst) {
assert(inst->cls == instance_cls);
static BoxedString* next_str = static_cast<BoxedString*>(PyString_InternFromString("next"));
static BoxedString* next_str = internStringImmortal("next");
Box* next_func = _instanceGetattribute(inst, next_str, false);
if (!next_func) {
......@@ -769,7 +769,7 @@ Box* instanceCall(Box* _inst, Box* _args, Box* _kwargs) {
assert(_inst->cls == instance_cls);
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
static BoxedString* call_str = static_cast<BoxedString*>(PyString_InternFromString("__call__"));
static BoxedString* call_str = internStringImmortal("__call__");
Box* call_func = _instanceGetattribute(inst, call_str, false);
if (!call_func)
raiseExcHelper(AttributeError, "%s instance has no __call__ method", inst->inst_cls->name->data());
......
......@@ -44,7 +44,7 @@ static void propertyDocCopy(BoxedProperty* prop, Box* fget) {
assert(fget);
Box* get_doc;
static BoxedString* doc_str = static_cast<BoxedString*>(PyString_InternFromString("__doc__"));
static BoxedString* doc_str = internStringImmortal("__doc__");
try {
get_doc = getattrInternal(fget, doc_str, NULL);
} catch (ExcInfo e) {
......
......@@ -192,7 +192,7 @@ Box* dictGetitem(BoxedDict* self, Box* k) {
if (it == self->d.end()) {
// Try calling __missing__ if this is a subclass
if (self->cls != dict_cls) {
static BoxedString* missing_str = static_cast<BoxedString*>(PyString_InternFromString("__missing__"));
static BoxedString* missing_str = internStringImmortal("__missing__");
CallattrFlags callattr_flags{.cls_only = true, .null_on_nonexistent = true, .argspec = ArgPassSpec(1) };
Box* r = callattr(self, missing_str, callattr_flags, k, NULL, NULL, NULL, NULL);
if (r)
......@@ -524,7 +524,7 @@ void dictMerge(BoxedDict* self, Box* other) {
return;
}
static BoxedString* keys_str = static_cast<BoxedString*>(PyString_InternFromString("keys"));
static BoxedString* keys_str = internStringImmortal("keys");
CallattrFlags callattr_flags{.cls_only = false, .null_on_nonexistent = true, .argspec = ArgPassSpec(0) };
Box* keys = callattr(other, keys_str, callattr_flags, NULL, NULL, NULL, NULL, NULL);
assert(keys);
......@@ -588,7 +588,7 @@ Box* dictUpdate(BoxedDict* self, BoxedTuple* args, BoxedDict* kwargs) {
RELEASE_ASSERT(args->size() <= 1, ""); // should throw a TypeError
if (args->size()) {
Box* arg = args->elts[0];
static BoxedString* keys_str = static_cast<BoxedString*>(PyString_InternFromString("keys"));
static BoxedString* keys_str = internStringImmortal("keys");
if (getattrInternal(arg, keys_str, NULL)) {
dictMerge(self, arg);
} else {
......
......@@ -651,7 +651,7 @@ BoxedFloat* _floatNew(Box* a) {
raiseExcHelper(ValueError, "could not convert string to float: %s", s.data());
return new BoxedFloat(r);
} else {
static BoxedString* float_str = static_cast<BoxedString*>(PyString_InternFromString("__float__"));
static BoxedString* float_str = internStringImmortal("__float__");
CallattrFlags callattr_flags{.cls_only = true, .null_on_nonexistent = true, .argspec = ArgPassSpec(0) };
Box* r = callattr(a, float_str, callattr_flags, NULL, NULL, NULL, NULL, NULL);
......
......@@ -188,7 +188,7 @@ SearchResult findModule(const std::string& name, const std::string& full_name, B
if (!meta_path || meta_path->cls != list_cls)
raiseExcHelper(RuntimeError, "sys.meta_path must be a list of import hooks");
static BoxedString* findmodule_str = static_cast<BoxedString*>(PyString_InternFromString("find_module"));
static BoxedString* findmodule_str = internStringImmortal("find_module");
for (int i = 0; i < meta_path->size; i++) {
Box* finder = meta_path->elts->elts[i];
......@@ -279,7 +279,7 @@ static Box* getParent(Box* globals, int level, std::string& buf) {
if (globals == NULL || globals == None || level == 0)
return None;
static BoxedString* package_str = static_cast<BoxedString*>(PyString_InternFromString("__package__"));
static BoxedString* package_str = internStringImmortal("__package__");
BoxedString* pkgname = static_cast<BoxedString*>(getFromGlobals(globals, package_str));
if (pkgname != NULL && pkgname != None) {
/* __package__ is set, so use it */
......@@ -298,14 +298,14 @@ static Box* getParent(Box* globals, int level, std::string& buf) {
}
buf += pkgname->s();
} else {
static BoxedString* name_str = static_cast<BoxedString*>(PyString_InternFromString("__name__"));
static BoxedString* name_str = internStringImmortal("__name__");
/* __package__ not set, so figure it out and set it */
BoxedString* modname = static_cast<BoxedString*>(getFromGlobals(globals, name_str));
if (modname == NULL || modname->cls != str_cls)
return None;
static BoxedString* path_str = static_cast<BoxedString*>(PyString_InternFromString("__path__"));
static BoxedString* path_str = internStringImmortal("__path__");
Box* modpath = getFromGlobals(globals, path_str);
if (modpath != NULL) {
......@@ -378,7 +378,7 @@ static Box* importSub(const std::string& name, const std::string& full_name, Box
if (parent_module == NULL || parent_module == None) {
path_list = NULL;
} else {
static BoxedString* path_str = static_cast<BoxedString*>(PyString_InternFromString("__path__"));
static BoxedString* path_str = internStringImmortal("__path__");
path_list = static_cast<BoxedList*>(getattrInternal(parent_module, path_str, NULL));
if (path_list == NULL || path_list->cls != list_cls) {
return None;
......@@ -398,8 +398,7 @@ static Box* importSub(const std::string& name, const std::string& full_name, Box
else if (sr.type == SearchResult::C_EXTENSION)
module = importCExtension(full_name, name, sr.path);
else if (sr.type == SearchResult::IMP_HOOK) {
static BoxedString* loadmodule_str
= static_cast<BoxedString*>(PyString_InternFromString("load_module"));
static BoxedString* loadmodule_str = internStringImmortal("load_module");
CallattrFlags callattr_flags{.cls_only = false,
.null_on_nonexistent = false,
.argspec = ArgPassSpec(1) };
......@@ -557,7 +556,7 @@ extern "C" PyObject* PyImport_ImportModuleLevel(const char* name, PyObject* glob
}
static void ensureFromlist(Box* module, Box* fromlist, std::string& buf, bool recursive) {
static BoxedString* path_str = static_cast<BoxedString*>(PyString_InternFromString("__path__"));
static BoxedString* path_str = internStringImmortal("__path__");
Box* pathlist = NULL;
try {
pathlist = getattrInternal(module, path_str, NULL);
......@@ -580,7 +579,7 @@ static void ensureFromlist(Box* module, Box* fromlist, std::string& buf, bool re
if (recursive)
continue;
static BoxedString* all_str = static_cast<BoxedString*>(PyString_InternFromString("__all__"));
static BoxedString* all_str = internStringImmortal("__all__");
Box* all = getattrInternal(module, all_str, NULL);
if (all) {
ensureFromlist(module, all, buf, true);
......
......@@ -931,7 +931,7 @@ static Box* _intNew(Box* val, Box* base) {
return PyLong_FromDouble(wholepart);
} else {
RELEASE_ASSERT(!base, "");
static BoxedString* int_str = static_cast<BoxedString*>(PyString_InternFromString("__int__"));
static BoxedString* int_str = internStringImmortal("__int__");
CallattrFlags callattr_flags{.cls_only = true, .null_on_nonexistent = true, .argspec = ArgPassSpec(0) };
Box* r = callattr(val, int_str, callattr_flags, NULL, NULL, NULL, NULL, NULL);
......
......@@ -642,7 +642,7 @@ BoxedLong* _longNew(Box* val, Box* _base) {
} else if (val->cls == float_cls) {
mpz_init_set_si(rtn->n, static_cast<BoxedFloat*>(val)->d);
} else {
static BoxedString* long_str = static_cast<BoxedString*>(PyString_InternFromString("__long__"));
static BoxedString* long_str = internStringImmortal("__long__");
CallattrFlags callattr_flags{.cls_only = true, .null_on_nonexistent = true, .argspec = ArgPassSpec(0) };
Box* r = callattr(val, long_str, callattr_flags, NULL, NULL, NULL, NULL, NULL);
......
......@@ -200,7 +200,7 @@ extern "C" bool softspace(Box* b, bool newval) {
return (bool)r;
}
static BoxedString* softspace_str = static_cast<BoxedString*>(PyString_InternFromString("softspace"));
static BoxedString* softspace_str = internStringImmortal("softspace");
bool r;
Box* gotten = NULL;
......@@ -225,9 +225,9 @@ extern "C" bool softspace(Box* b, bool newval) {
}
extern "C" void printHelper(Box* dest, Box* var, bool nl) {
static BoxedString* write_str = static_cast<BoxedString*>(PyString_InternFromString("write"));
static BoxedString* newline_str = static_cast<BoxedString*>(PyString_InternFromString("\n"));
static BoxedString* space_str = static_cast<BoxedString*>(PyString_InternFromString(" "));
static BoxedString* write_str = internStringImmortal("write");
static BoxedString* newline_str = internStringImmortal("\n");
static BoxedString* space_str = internStringImmortal(" ");
if (var) {
// begin code for handling of softspace
......@@ -2302,8 +2302,8 @@ extern "C" bool nonzero(Box* obj) {
}
// TODO: rewrite these.
static BoxedString* nonzero_str = static_cast<BoxedString*>(PyString_InternFromString("__nonzero__"));
static BoxedString* len_str = static_cast<BoxedString*>(PyString_InternFromString("__len__"));
static BoxedString* nonzero_str = internStringImmortal("__nonzero__");
static BoxedString* len_str = internStringImmortal("__len__");
// go through descriptor logic
Box* func = getclsattrInternal(obj, nonzero_str, NULL);
if (!func)
......@@ -2341,7 +2341,7 @@ extern "C" BoxedString* str(Box* obj) {
static StatCounter slowpath_str("slowpath_str");
slowpath_str.log();
static BoxedString* str_box = static_cast<BoxedString*>(PyString_InternFromString(str_str.c_str()));
static BoxedString* str_box = internStringImmortal(str_str.c_str());
if (obj->cls != str_cls) {
// TODO could do an IC optimization here (once we do rewrites here at all):
// if __str__ is objectStr, just guard on that and call repr directly.
......@@ -2373,7 +2373,7 @@ extern "C" BoxedString* repr(Box* obj) {
static StatCounter slowpath_repr("slowpath_repr");
slowpath_repr.log();
static BoxedString* repr_box = static_cast<BoxedString*>(PyString_InternFromString(repr_str.c_str()));
static BoxedString* repr_box = internStringImmortal(repr_str.c_str());
obj = callattrInternal(obj, repr_box, CLASS_ONLY, NULL, ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL);
if (isSubclass(obj->cls, unicode_cls)) {
......@@ -2455,7 +2455,7 @@ extern "C" BoxedInt* hash(Box* obj) {
}
extern "C" BoxedInt* lenInternal(Box* obj, LenRewriteArgs* rewrite_args) {
static BoxedString* len_str = static_cast<BoxedString*>(PyString_InternFromString("__len__"));
static BoxedString* len_str = internStringImmortal("__len__");
// Corresponds to the first part of PyObject_Size:
PySequenceMethods* m = obj->cls->tp_as_sequence;
......@@ -3640,7 +3640,7 @@ Box* runtimeCallInternal(Box* obj, CallRewriteArgs* rewrite_args, ArgPassSpec ar
assert((obj->cls->tp_call == NULL) == (typeLookup(obj->cls, call_str, NULL) == NULL));
}
static BoxedString* call_box = static_cast<BoxedString*>(PyString_InternFromString(call_str.c_str()));
static BoxedString* call_box = internStringImmortal(call_str.c_str());
if (rewrite_args) {
rtn = callattrInternal(obj, call_box, CLASS_ONLY, rewrite_args, argspec, arg1, arg2, arg3, args,
......@@ -4056,7 +4056,7 @@ Box* compareInternal(Box* lhs, Box* rhs, int op_type, CompareRewriteArgs* rewrit
}
if (op_type == AST_TYPE::In || op_type == AST_TYPE::NotIn) {
static BoxedString* contains_str = static_cast<BoxedString*>(PyString_InternFromString("__contains__"));
static BoxedString* contains_str = internStringImmortal("__contains__");
// The checks for this branch are taken from CPython's PySequence_Contains
if (PyType_HasFeature(rhs->cls, Py_TPFLAGS_HAVE_SEQUENCE_IN)) {
......@@ -4240,7 +4240,7 @@ Box* compareInternal(Box* lhs, Box* rhs, int op_type, CompareRewriteArgs* rewrit
if (rrtn != NULL && rrtn != NotImplemented)
return rrtn;
static BoxedString* cmp_str = static_cast<BoxedString*>(PyString_InternFromString("__cmp__"));
static BoxedString* cmp_str = internStringImmortal("__cmp__");
lrtn = callattrInternal1(lhs, cmp_str, CLASS_ONLY, NULL, ArgPassSpec(1), rhs);
if (lrtn && lrtn != NotImplemented) {
return boxBool(convert3wayCompareResultToBool(lrtn, op_type));
......@@ -4390,7 +4390,7 @@ extern "C" Box* getitem(Box* value, Box* slice) {
return r;
}
static BoxedString* getitem_str = static_cast<BoxedString*>(PyString_InternFromString("__getitem__"));
static BoxedString* getitem_str = internStringImmortal("__getitem__");
Box* rtn;
if (rewriter.get()) {
CallRewriteArgs rewrite_args(rewriter.get(), rewriter->getArg(0), rewriter->getReturnDestination());
......@@ -4432,7 +4432,7 @@ extern "C" void setitem(Box* target, Box* slice, Box* value) {
std::unique_ptr<Rewriter> rewriter(
Rewriter::createRewriter(__builtin_extract_return_addr(__builtin_return_address(0)), 3, "setitem"));
static BoxedString* setitem_str = static_cast<BoxedString*>(PyString_InternFromString("__setitem__"));
static BoxedString* setitem_str = internStringImmortal("__setitem__");
Box* rtn;
if (rewriter.get()) {
......@@ -4468,7 +4468,7 @@ extern "C" void delitem(Box* target, Box* slice) {
std::unique_ptr<Rewriter> rewriter(
Rewriter::createRewriter(__builtin_extract_return_addr(__builtin_return_address(0)), 2, "delitem"));
static BoxedString* delitem_str = static_cast<BoxedString*>(PyString_InternFromString("__delitem__"));
static BoxedString* delitem_str = internStringImmortal("__delitem__");
Box* rtn;
if (rewriter.get()) {
......@@ -4674,7 +4674,7 @@ extern "C" Box* getiterHelper(Box* o) {
Box* getiter(Box* o) {
// TODO add rewriting to this? probably want to try to avoid this path though
static BoxedString* iter_str = static_cast<BoxedString*>(PyString_InternFromString("__iter__"));
static BoxedString* iter_str = internStringImmortal("__iter__");
Box* r = callattrInternal0(o, iter_str, LookupScope::CLASS_ONLY, NULL, ArgPassSpec(0));
if (r)
return r;
......@@ -4763,7 +4763,7 @@ Box* typeNew(Box* _cls, Box* arg1, Box* arg2, Box** _args) {
"of the metaclasses of all its bases");
}
static BoxedString* new_box = static_cast<BoxedString*>(PyString_InternFromString(new_str.c_str()));
static BoxedString* new_box = internStringImmortal(new_str.c_str());
if (winner != metatype) {
if (getattr(winner, new_box) != getattr(type_cls, new_box)) {
CallattrFlags callattr_flags
......
......@@ -232,7 +232,7 @@ Box* BoxedClass::callHasnextIC(Box* obj, bool null_on_nonexistent) {
hasnext_ic.reset(ic);
}
static BoxedString* hasnext_str = static_cast<BoxedString*>(PyString_InternFromString("__hasnext__"));
static BoxedString* hasnext_str = internStringImmortal("__hasnext__");
CallattrFlags callattr_flags
= {.cls_only = true, .null_on_nonexistent = null_on_nonexistent, .argspec = ArgPassSpec(0) };
return ic->call(obj, hasnext_str, callattr_flags, nullptr, nullptr, nullptr, nullptr, nullptr);
......@@ -276,7 +276,7 @@ Box* BoxedClass::callNextIC(Box* obj) {
next_ic.reset(ic);
}
static BoxedString* next_str = static_cast<BoxedString*>(PyString_InternFromString("next"));
static BoxedString* next_str = internStringImmortal("next");
CallattrFlags callattr_flags{.cls_only = true, .null_on_nonexistent = false, .argspec = ArgPassSpec(0) };
return ic->call(obj, next_str, callattr_flags, nullptr, nullptr, nullptr, nullptr, nullptr);
}
......@@ -290,7 +290,7 @@ Box* BoxedClass::callReprIC(Box* obj) {
repr_ic.reset(ic);
}
static BoxedString* repr_str = static_cast<BoxedString*>(PyString_InternFromString("__repr__"));
static BoxedString* repr_str = internStringImmortal("__repr__");
CallattrFlags callattr_flags{.cls_only = true, .null_on_nonexistent = false, .argspec = ArgPassSpec(0) };
return ic->call(obj, repr_str, callattr_flags, nullptr, nullptr, nullptr, nullptr, nullptr);
}
......@@ -1240,7 +1240,7 @@ extern "C" Box* createUserClass(BoxedString* name, Box* _bases, Box* _attr_dict)
assert(msg);
// TODO this is an extra Pyston check and I don't think we should have to do it:
if (isSubclass(e.value->cls, BaseException)) {
static BoxedString* message_str = static_cast<BoxedString*>(PyString_InternFromString("message"));
static BoxedString* message_str = internStringImmortal("message");
msg = getattr(e.value, message_str);
}
......@@ -1396,7 +1396,7 @@ static Box* functionGlobals(Box* self, void*) {
assert(func->f->source);
assert(func->f->source->scoping->areGlobalsFromModule());
static BoxedString* dict_str = static_cast<BoxedString*>(PyString_InternFromString("__dict__"));
static BoxedString* dict_str = internStringImmortal("__dict__");
return getattr(func->f->source->parent_module, dict_str);
}
......@@ -1521,7 +1521,7 @@ static Box* instancemethodRepr(Box* b) {
Box* funcname = NULL, * klassname = NULL, * result = NULL;
const char* sfuncname = "?", * sklassname = "?";
static BoxedString* name_str = static_cast<BoxedString*>(PyString_InternFromString("__name__"));
static BoxedString* name_str = internStringImmortal("__name__");
funcname = getattrInternal(func, name_str, NULL);
if (funcname != NULL) {
......@@ -2173,7 +2173,7 @@ public:
// In order to not have to reimplement dict cmp: just create a real dict for now and us it.
BoxedDict* dict = (BoxedDict*)AttrWrapper::copy(_self);
assert(dict->cls == dict_cls);
static BoxedString* eq_str = static_cast<BoxedString*>(PyString_InternFromString("__eq__"));
static BoxedString* eq_str = internStringImmortal("__eq__");
return callattrInternal(dict, eq_str, LookupScope::CLASS_ONLY, NULL, ArgPassSpec(1), _other, NULL, NULL, NULL,
NULL);
}
......
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