Commit 1da4cefb authored by Kevin Modzelewski's avatar Kevin Modzelewski

sys.copyright, better old-style __nonzero__

parent 0d2c9b6e
...@@ -143,6 +143,13 @@ void setupSys() { ...@@ -143,6 +143,13 @@ void setupSys() {
sys_module->giveAttr("prefix", boxStrConstant("/usr")); sys_module->giveAttr("prefix", boxStrConstant("/usr"));
sys_module->giveAttr("exec_prefix", boxStrConstant("/usr")); sys_module->giveAttr("exec_prefix", boxStrConstant("/usr"));
sys_module->giveAttr("copyright",
boxStrConstant("Copyright 2014 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("version", boxString(generateVersionString()));
sys_module->giveAttr("hexversion", boxInt(PY_VERSION_HEX)); sys_module->giveAttr("hexversion", boxInt(PY_VERSION_HEX));
......
...@@ -215,10 +215,22 @@ Box* instanceNonzero(Box* _inst) { ...@@ -215,10 +215,22 @@ Box* instanceNonzero(Box* _inst) {
RELEASE_ASSERT(_inst->cls == instance_cls, ""); RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst); BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
Box* nonzero_func = _instanceGetattribute(inst, boxStrConstant("__nonzero__"), false); Box* nonzero_func = NULL;
try {
nonzero_func = _instanceGetattribute(inst, boxStrConstant("__nonzero__"), false);
} catch (Box* b) {
if (!isInstance(b, AttributeError))
throw;
}
if (nonzero_func == NULL) if (nonzero_func == NULL) {
nonzero_func = _instanceGetattribute(inst, boxStrConstant("__len__"), false); try {
nonzero_func = _instanceGetattribute(inst, boxStrConstant("__len__"), false);
} catch (Box* b) {
if (!isInstance(b, AttributeError))
throw;
}
}
if (nonzero_func) { if (nonzero_func) {
return runtimeCall(nonzero_func, ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL); return runtimeCall(nonzero_func, ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL);
......
...@@ -248,6 +248,10 @@ extern "C" void my_assert(bool b) { ...@@ -248,6 +248,10 @@ extern "C" void my_assert(bool b) {
assert(b); assert(b);
} }
bool isInstance(Box* obj, BoxedClass* cls) {
return isSubclass(obj->cls, cls);
}
extern "C" bool isSubclass(BoxedClass* child, BoxedClass* parent) { extern "C" bool isSubclass(BoxedClass* child, BoxedClass* parent) {
// TODO the class is allowed to override this using __subclasscheck__ // TODO the class is allowed to override this using __subclasscheck__
while (child) { while (child) {
......
...@@ -81,6 +81,7 @@ extern "C" Box* importStar(Box* from_module, BoxedModule* to_module); ...@@ -81,6 +81,7 @@ extern "C" Box* importStar(Box* from_module, BoxedModule* to_module);
extern "C" Box** unpackIntoArray(Box* obj, int64_t expected_size); extern "C" Box** unpackIntoArray(Box* obj, int64_t expected_size);
extern "C" void assertNameDefined(bool b, const char* name, BoxedClass* exc_cls, bool local_var_msg); extern "C" void assertNameDefined(bool b, const char* name, BoxedClass* exc_cls, bool local_var_msg);
extern "C" void assertFail(BoxedModule* inModule, Box* msg); extern "C" void assertFail(BoxedModule* inModule, Box* msg);
extern "C" bool isInstance(Box* obj, BoxedClass* parent);
extern "C" bool isSubclass(BoxedClass* child, BoxedClass* parent); extern "C" bool isSubclass(BoxedClass* child, BoxedClass* parent);
extern "C" BoxedClosure* createClosure(BoxedClosure* parent_closure); extern "C" BoxedClosure* createClosure(BoxedClosure* parent_closure);
extern "C" Box* getiter(Box* o); extern "C" Box* getiter(Box* o);
......
...@@ -115,6 +115,8 @@ print issubclass(OldStyleClass, OldStyleClass) ...@@ -115,6 +115,8 @@ print issubclass(OldStyleClass, OldStyleClass)
class GetattrTest: class GetattrTest:
def __getattr__(self, attr): def __getattr__(self, attr):
print "getattr", attr print "getattr", attr
if attr.startswith("__"):
raise AttributeError(attr)
return 1 return 1
g = GetattrTest() g = GetattrTest()
...@@ -123,3 +125,4 @@ print g.a ...@@ -123,3 +125,4 @@ print g.a
print g.b print g.b
print g.__class__ print g.__class__
print g.__dict__.items() print g.__dict__.items()
print bool(g)
...@@ -6,3 +6,4 @@ import os.path ...@@ -6,3 +6,4 @@ import os.path
print sys.version[:3] print sys.version[:3]
print os.path.exists(sys.executable) print os.path.exists(sys.executable)
print sys.prefix, sys.exec_prefix print sys.prefix, sys.exec_prefix
print sys.copyright[-200:]
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