BOOL Ps_LogObject(PyObject *object, int _type) { FILE *file; PyTypeObject *type; PyObject *tmp; if(object == NULL) return FALSE; type = object->ob_type; GET_LOG_LOCK(); switch(_type) { case Ps_LOG_ERROR: file = log_error; break; case Ps_LOG_WARING: file = log_waring; break; default: file = log_normal; break; } assert(file); if(!type) { fprintf(file, "Object at %p is unknowntype\n", object); return FALSE; } fputs("--------------------\n", file); /*打印object的名字*/ if(type->tp_name) { fprintf(file, "Object %s info:\n", type->tp_name); } else { fprintf(file, "Object at %p info:\n", object); } /*打印文档信息*/ if(type->tp_doc) fprintf(file, "doc: %s\n", type->tp_doc); /*打印内存分配信息*/ fprintf(file, "base size is %d\n", type->tp_basicsize); if(type->tp_itemsize) { fprintf(file, "Length changable object, Item size is %d\n", type->tp_itemsize); } else fputs("Length unchangable object.\n", file); /*repr和str的内容*/ if(type->tp_repr) { tmp = type->tp_repr(object); if(tmp && PyString_Check(tmp) && PyString_AsString(tmp)) { fprintf(file, "repr:%s\n", PyString_AsString(tmp)); } else { fprintf(file, "repr:--Waring-- __repr__ donesn't return a PyStringObject\n"); } } if(type->tp_str) { tmp = type->tp_str(object); if(tmp && PyString_Check(tmp) && PyString_AsString(tmp)) { fprintf(file, "str:%s\n", PyString_AsString(tmp)); } else { fprintf(file, "str:--Waring-- __str__ donesn't return a PyStringObject\n"); } } /*HASH值*/ if(type->tp_hash) { fprintf(file, "hash value: %ld\n", type->tp_hash(object)); } /*是否是可调用的*/ if(type->tp_call) { fprintf(file, "It's a callable object\n"); } else fputs("It's an uncallable object\n", file); /*是否可迭代*/ if(type->tp_iter && type->tp_iternext) fputs("It's a iterable object\n", file); else fputs("It's an uniterable object\n", file); fputs("----------------------\n", file); RELEASE_LOG_LOCK(); return TRUE; }