python字符串清洗(Python如何清理驻留的字符串)
导读:1、说明...
1 、说明
清除函数从interneddictionary遍历所有字符串 ,调整这些对象的引用计数 ,并将它们标记为NOT_INTERNED ,这样垃圾回收就可以了 。当所有的字符串都被标记为NOT_INTERNED时 ,将清空并删除interned字典 。
2 、清理实例
这个清理函数就是_PyUnicode_ClearInterned ,在 unicodeobject.c 中定义。
void _PyUnicode_ClearInterned(PyThreadState*tstate) { ......... //Getallthekeystotheinterneddictionary PyObject*keys=PyDict_Keys(interned); ......... //InternedUnicodestringsarenotforciblydeallocated; //rather,wegivethemtheirstolenreferencesback //andthenclearandDECREFtheinterneddict. for(Py_ssize_ti=0;i<n;i++){ PyObject*s=PyList_GET_ITEM(keys,i); ......... switch(PyUnicode_CHECK_INTERNED(s)){ caseSSTATE_INTERNED_IMMORTAL: Py_SET_REFCNT(s,Py_REFCNT(s)+1); break; caseSSTATE_INTERNED_MORTAL: //Restorethetworeferences(keyandvalue)ignored //byPyUnicode_InternInPlace(). Py_SET_REFCNT(s,Py_REFCNT(s)+2); break; caseSSTATE_NOT_INTERNED: /*fallthrough*/ default: Py_UNREACHABLE(); } //markingthestringtobeNOT_INTERNED _PyUnicode_STATE(s).interned=SSTATE_NOT_INTERNED; } //decreasingthereferencetotheinitializedand //accesskeysobject. Py_DECREF(keys); //clearingthedictionary PyDict_Clear(interned); //clearingtheobjectinterned Py_CLEAR(interned); }创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!