Commit 264540c8 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #785 from undingen/cheetah_compatibility

object.__str__ use capi slot in order to increase compatibility with cpython
parents 38e94b5f 732ddafa
...@@ -163,6 +163,17 @@ public: ...@@ -163,6 +163,17 @@ public:
PyThread_free_lock(self->lock_lock); PyThread_free_lock(self->lock_lock);
} }
} }
static Box* locked(Box* _self) {
RELEASE_ASSERT(_self->cls == thread_lock_cls, "");
BoxedThreadLock* self = static_cast<BoxedThreadLock*>(_self);
if (PyThread_acquire_lock(self->lock_lock, 0)) {
PyThread_release_lock(self->lock_lock);
return False;
}
return True;
}
}; };
...@@ -211,6 +222,9 @@ void setupThread() { ...@@ -211,6 +222,9 @@ void setupThread() {
thread_lock_cls->giveAttr("release_lock", thread_lock_cls->getattr(internStringMortal("release"))); thread_lock_cls->giveAttr("release_lock", thread_lock_cls->getattr(internStringMortal("release")));
thread_lock_cls->giveAttr("__enter__", thread_lock_cls->getattr(internStringMortal("acquire"))); thread_lock_cls->giveAttr("__enter__", thread_lock_cls->getattr(internStringMortal("acquire")));
thread_lock_cls->giveAttr("__exit__", new BoxedFunction(boxRTFunction((void*)BoxedThreadLock::exit, NONE, 4))); thread_lock_cls->giveAttr("__exit__", new BoxedFunction(boxRTFunction((void*)BoxedThreadLock::exit, NONE, 4)));
thread_lock_cls->giveAttr("locked",
new BoxedFunction(boxRTFunction((void*)BoxedThreadLock::locked, BOXED_BOOL, 1)));
thread_lock_cls->giveAttr("locked_lock", thread_lock_cls->getattr(internStringMortal("locked")));
thread_lock_cls->freeze(); thread_lock_cls->freeze();
ThreadError = BoxedHeapClass::create(type_cls, Exception, NULL, Exception->attrs_offset, ThreadError = BoxedHeapClass::create(type_cls, Exception, NULL, Exception->attrs_offset,
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "capi/typeobject.h"
#include "codegen/compvars.h" #include "codegen/compvars.h"
#include "runtime/objmodel.h" #include "runtime/objmodel.h"
#include "runtime/rewrite_args.h" #include "runtime/rewrite_args.h"
...@@ -416,26 +417,13 @@ void BoxedMethodDescriptor::gcHandler(GCVisitor* v, Box* _o) { ...@@ -416,26 +417,13 @@ void BoxedMethodDescriptor::gcHandler(GCVisitor* v, Box* _o) {
v->visit(o->type); v->visit(o->type);
} }
Box* BoxedWrapperDescriptor::__get__(BoxedWrapperDescriptor* self, Box* inst, Box* owner) {
STAT_TIMER(t0, "us_timer_boxedwrapperdescriptor_get", 20);
RELEASE_ASSERT(self->cls == wrapperdescr_cls, "");
if (inst == None)
return self;
if (!isSubclass(inst->cls, self->type))
raiseExcHelper(TypeError, "Descriptor '' for '%s' objects doesn't apply to '%s' object",
getFullNameOfClass(self->type).c_str(), getFullTypeName(inst).c_str());
return new BoxedWrapperObject(self, inst);
}
Box* BoxedWrapperDescriptor::descr_get(Box* _self, Box* inst, Box* owner) noexcept { Box* BoxedWrapperDescriptor::descr_get(Box* _self, Box* inst, Box* owner) noexcept {
STAT_TIMER(t0, "us_timer_boxedwrapperdescriptor_descr_get", 20);
RELEASE_ASSERT(_self->cls == wrapperdescr_cls, ""); RELEASE_ASSERT(_self->cls == wrapperdescr_cls, "");
BoxedWrapperDescriptor* self = static_cast<BoxedWrapperDescriptor*>(_self); BoxedWrapperDescriptor* self = static_cast<BoxedWrapperDescriptor*>(_self);
if (inst == None) if (inst == NULL)
return self; return self;
if (!isSubclass(inst->cls, self->type)) { if (!isSubclass(inst->cls, self->type)) {
...@@ -633,14 +621,14 @@ void setupDescr() { ...@@ -633,14 +621,14 @@ void setupDescr() {
method_cls->giveAttr("__doc__", new (pyston_getset_cls) BoxedGetsetDescriptor(methodGetDoc, NULL, NULL)); method_cls->giveAttr("__doc__", new (pyston_getset_cls) BoxedGetsetDescriptor(methodGetDoc, NULL, NULL));
method_cls->freeze(); method_cls->freeze();
wrapperdescr_cls->giveAttr("__get__",
new BoxedFunction(boxRTFunction((void*)BoxedWrapperDescriptor::__get__, UNKNOWN, 3)));
wrapperdescr_cls->giveAttr("__call__", new BoxedFunction(boxRTFunction((void*)BoxedWrapperDescriptor::__call__, wrapperdescr_cls->giveAttr("__call__", new BoxedFunction(boxRTFunction((void*)BoxedWrapperDescriptor::__call__,
UNKNOWN, 2, 0, true, true))); UNKNOWN, 2, 0, true, true)));
wrapperdescr_cls->giveAttr("__doc__", wrapperdescr_cls->giveAttr("__doc__",
new (pyston_getset_cls) BoxedGetsetDescriptor(wrapperdescrGetDoc, NULL, NULL)); new (pyston_getset_cls) BoxedGetsetDescriptor(wrapperdescrGetDoc, NULL, NULL));
wrapperdescr_cls->freeze();
wrapperdescr_cls->tp_descr_get = BoxedWrapperDescriptor::descr_get; wrapperdescr_cls->tp_descr_get = BoxedWrapperDescriptor::descr_get;
add_operators(wrapperdescr_cls);
wrapperdescr_cls->freeze();
assert(wrapperdescr_cls->tp_descr_get == BoxedWrapperDescriptor::descr_get);
wrapperobject_cls->giveAttr( wrapperobject_cls->giveAttr(
"__call__", new BoxedFunction(boxRTFunction((void*)BoxedWrapperObject::__call__, UNKNOWN, 1, 0, true, true))); "__call__", new BoxedFunction(boxRTFunction((void*)BoxedWrapperObject::__call__, UNKNOWN, 1, 0, true, true)));
......
...@@ -1118,12 +1118,12 @@ Box* nondataDescriptorInstanceSpecialCases(GetattrRewriteArgs* rewrite_args, Box ...@@ -1118,12 +1118,12 @@ Box* nondataDescriptorInstanceSpecialCases(GetattrRewriteArgs* rewrite_args, Box
Box* inst = obj; Box* inst = obj;
Box* owner = obj->cls; Box* owner = obj->cls;
Box* r = BoxedWrapperDescriptor::__get__(self, inst, owner); Box* r = BoxedWrapperDescriptor::descr_get(self, inst, owner);
if (rewrite_args) { if (rewrite_args) {
// TODO: inline this? // TODO: inline this?
RewriterVar* r_rtn = rewrite_args->rewriter->call( RewriterVar* r_rtn = rewrite_args->rewriter->call(
/* has_side_effects= */ false, (void*)&BoxedWrapperDescriptor::__get__, r_descr, rewrite_args->obj, /* has_side_effects= */ false, (void*)&BoxedWrapperDescriptor::descr_get, r_descr, rewrite_args->obj,
r_descr->getAttr(offsetof(Box, cls), Location::forArg(2))); r_descr->getAttr(offsetof(Box, cls), Location::forArg(2)));
rewrite_args->out_success = true; rewrite_args->out_success = true;
......
...@@ -2506,8 +2506,13 @@ Box* objectRepr(Box* obj) { ...@@ -2506,8 +2506,13 @@ Box* objectRepr(Box* obj) {
return boxString(buf); return boxString(buf);
} }
Box* objectStr(Box* obj) { static Box* object_str(Box* obj) noexcept {
return obj->reprIC(); try {
return obj->reprIC();
} catch (ExcInfo e) {
setCAPIException(e);
return NULL;
}
} }
Box* objectHash(Box* obj) { Box* objectHash(Box* obj) {
...@@ -3449,7 +3454,6 @@ void setupRuntime() { ...@@ -3449,7 +3454,6 @@ void setupRuntime() {
FROZENSET = typeFromClass(frozenset_cls); FROZENSET = typeFromClass(frozenset_cls);
object_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)objectRepr, UNKNOWN, 1, 0, false, false))); object_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)objectRepr, UNKNOWN, 1, 0, false, false)));
object_cls->giveAttr("__str__", new BoxedFunction(boxRTFunction((void*)objectStr, UNKNOWN, 1, 0, false, false)));
object_cls->giveAttr("__subclasshook__", object_cls->giveAttr("__subclasshook__",
boxInstanceMethod(object_cls, boxInstanceMethod(object_cls,
new BoxedFunction(boxRTFunction((void*)objectSubclasshook, UNKNOWN, 2)), new BoxedFunction(boxRTFunction((void*)objectSubclasshook, UNKNOWN, 2)),
...@@ -3502,9 +3506,15 @@ void setupRuntime() { ...@@ -3502,9 +3506,15 @@ void setupRuntime() {
object_cls->giveAttr(md.ml_name, new BoxedMethodDescriptor(&md, object_cls)); object_cls->giveAttr(md.ml_name, new BoxedMethodDescriptor(&md, object_cls));
} }
object_cls->giveAttr("__class__", new (pyston_getset_cls) BoxedGetsetDescriptor(objectClass, objectSetClass, NULL)); object_cls->giveAttr("__class__", new (pyston_getset_cls) BoxedGetsetDescriptor(objectClass, objectSetClass, NULL));
object_cls->tp_str = object_str;
add_operators(object_cls);
object_cls->freeze(); object_cls->freeze();
assert(object_cls->tp_init == object_init); assert(object_cls->tp_init == object_init);
assert(object_cls->tp_new == object_new); assert(object_cls->tp_new == object_new);
assert(object_cls->tp_str == object_str);
setupBool(); setupBool();
setupLong(); setupLong();
......
...@@ -945,7 +945,6 @@ public: ...@@ -945,7 +945,6 @@ public:
DEFAULT_CLASS(wrapperdescr_cls); DEFAULT_CLASS(wrapperdescr_cls);
static Box* __get__(BoxedWrapperDescriptor* self, Box* inst, Box* owner);
static Box* descr_get(Box* self, Box* inst, Box* owner) noexcept; static Box* descr_get(Box* self, Box* inst, Box* owner) noexcept;
static Box* __call__(BoxedWrapperDescriptor* descr, PyObject* self, BoxedTuple* args, Box** _args); static Box* __call__(BoxedWrapperDescriptor* descr, PyObject* self, BoxedTuple* args, Box** _args);
......
...@@ -30,9 +30,13 @@ while not done: ...@@ -30,9 +30,13 @@ while not done:
time.sleep(0) time.sleep(0)
l = allocate_lock() l = allocate_lock()
print "locked:", l.locked()
print l.acquire() print l.acquire()
print "locked:", l.locked()
print l.acquire(0) print l.acquire(0)
print "locked:", l.locked()
print l.release() print l.release()
print "locked:", l.locked()
print l.acquire(0) print l.acquire(0)
......
class C(object):
pass
print C.__str__ is object.__str__
print type(C).__str__ is object.__str__
print type(None).__str__ is object.__str__
print type(None).__str__ is None.__str__
print type(None.__str__)
print type(type(None).__str__.__get__(None, type(None)))
Subproject commit 56631b487c6bfa7ccd9525cba2a27baff9b6b491 Subproject commit e1d16ce8922700ef6bde50589e84db3a5c095c2b
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