我如果在函数中输出id(conn), 在报错之后是可以通过内存地址取到这个连接变量的, 问题在于函数内部的局部变量在函数报错后不释放, 就算不是数据库连接, 而是一个列表或字典也是一样的In [9]: def g():
...: conn = [1, 2, 3]
...: print(id(conn))
...: cursor = conn.cursor()
...: cursor.execute('select select')
...: conn.close()
...: return
...:
In [10]: g()
140065573745480
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-10-5fd69ddb5074> in <module>
----> 1 g()
<ipython-input-9-59a7e29f8a3c> in g()
2 conn = [1, 2, 3]
3 print(id(conn))
----> 4 cursor = conn.cursor()
5 cursor.execute('select select')
6 conn.close()
AttributeError: 'list' object has no attribute 'cursor'
In [11]: get_value = ctypes.cast(140065573745480, ctypes.py_object).value
In [12]: get_value
Out[12]: [1, 2, 3]
In [13]: def h():
...: conn = [1, 2, 3]
...: print(id(conn))
...:
In [14]: h()
140065573750024
In [15]: get_value = ctypes.cast(140065573750024, ctypes.py_object).value
In [16]: get_value
Out[16]: (0, 37)
|