@@ -32,6 +32,10 @@ object PythonObjectParser {
3232class MemoryDump (
3333 private val objects : MutableMap <String , MemoryObject >
3434) {
35+ fun contains (id : String ): Boolean {
36+ return objects.containsKey(id)
37+ }
38+
3539 fun getById (id : String ): MemoryObject {
3640 return objects[id]!!
3741 }
@@ -41,7 +45,7 @@ class MemoryDump(
4145 }
4246}
4347
44- class TypeInfo (
48+ data class TypeInfo (
4549 val module : String ,
4650 val kind : String ,
4751) {
@@ -88,82 +92,82 @@ class ReduceMemoryObject(
8892 val dictitems : String
8993) : MemoryObject(id, typeinfo, comparable)
9094
91- fun PythonTree.PythonTreeNode.toMemoryObject (memoryDump : MemoryDump ): String {
95+ fun PythonTree.PythonTreeNode.toMemoryObject (memoryDump : MemoryDump , reload : Boolean = false): String {
96+ val id = this .id.toString()
97+ if (memoryDump.contains(id) && ! reload) return id
98+
99+ val typeinfo = TypeInfo (this .type.moduleName, this .type.typeName)
92100 val obj = when (this ) {
93101 is PythonTree .PrimitiveNode -> {
94102 ReprMemoryObject (
95- this .id.toString() ,
96- TypeInfo ( this .type.moduleName, this .type.typeName) ,
103+ id ,
104+ typeinfo ,
97105 this .comparable,
98- this .repr
106+ this .repr.replace( " \n " , " \\\n " ).replace( " \r " , " \\\r " )
99107 )
100108 }
101109
102110 is PythonTree .ListNode -> {
111+ val draft = ListMemoryObject (id, typeinfo, this .comparable, emptyList())
112+ memoryDump.addObject(draft)
113+
103114 val items = this .items.entries
104- .sortedBy { it.key }
105115 .map { it.value.toMemoryObject(memoryDump) }
106- ListMemoryObject (
107- this .id.toString(),
108- TypeInfo (this .type.moduleName, this .type.typeName),
109- this .comparable,
110- items
111- )
116+ ListMemoryObject (id, typeinfo, this .comparable, items)
112117 }
113118
114119 is PythonTree .TupleNode -> {
115120 val items = this .items.entries
116- .sortedBy { it.key }
117121 .map { it.value.toMemoryObject(memoryDump) }
118- ListMemoryObject (
119- this .id.toString(),
120- TypeInfo (this .type.moduleName, this .type.typeName),
121- this .comparable,
122- items
123- )
122+ ListMemoryObject (id, typeinfo, this .comparable, items)
124123 }
125124
126125 is PythonTree .SetNode -> {
127126 val items = this .items.map { it.toMemoryObject(memoryDump) }
128- ListMemoryObject (
129- this .id.toString(),
130- TypeInfo (this .type.moduleName, this .type.typeName),
131- this .comparable,
132- items
133- )
127+ ListMemoryObject (id, typeinfo, this .comparable, items)
134128 }
135129
136130 is PythonTree .DictNode -> {
131+ val draft = DictMemoryObject (id, typeinfo, this .comparable, emptyMap())
132+ memoryDump.addObject(draft)
133+
137134 val items = this .items.entries
138135 .associate {
139136 it.key.toMemoryObject(memoryDump) to it.value.toMemoryObject(memoryDump)
140137 }
141- DictMemoryObject (
142- this .id.toString(),
143- TypeInfo (this .type.moduleName, this .type.typeName),
144- this .comparable,
145- items
146- )
138+ DictMemoryObject (id, typeinfo, this .comparable, items)
147139 }
148140
149141 is PythonTree .ReduceNode -> {
142+ val argsIds = PythonTree .ListNode (this .args.withIndex().associate { it.index to it.value }.toMutableMap())
143+ val draft = ReduceMemoryObject (
144+ id,
145+ typeinfo,
146+ this .comparable,
147+ TypeInfo (
148+ this .constructor .moduleName,
149+ this .constructor .typeName,
150+ ),
151+ argsIds.toMemoryObject(memoryDump),
152+ " " ,
153+ " " ,
154+ " " ,
155+ )
156+ memoryDump.addObject(draft)
157+
150158 val stateObjId = if (this .customState) {
151159 this .state[" state" ]!!
152160 } else {
153161 PythonTree .DictNode (this .state.entries.associate {
154162 PythonTree .fromString(it.key) to it.value
155163 }.toMutableMap())
156164 }
157- val argsIds = PythonTree .ListNode (this .args.withIndex().associate { it.index to it.value }.toMutableMap())
158165 val listItemsIds =
159166 PythonTree .ListNode (this .listitems.withIndex().associate { it.index to it.value }.toMutableMap())
160167 val dictItemsIds = PythonTree .DictNode (this .dictitems.toMutableMap())
161168 ReduceMemoryObject (
162- this .id.toString(),
163- TypeInfo (
164- this .type.moduleName,
165- this .type.typeName,
166- ),
169+ id,
170+ typeinfo,
167171 this .comparable,
168172 TypeInfo (
169173 this .constructor .moduleName,
@@ -200,33 +204,36 @@ fun MemoryObject.toPythonTree(
200204 }
201205
202206 is DictMemoryObject -> {
203- PythonTree .DictNode (
207+ val draft = PythonTree .DictNode (
204208 id,
205- items.entries.associate {
206- memoryDump.getById(it.key).toPythonTree(memoryDump, visited) to
207- memoryDump.getById(it.value).toPythonTree(memoryDump, visited)
208- }.toMutableMap()
209+ mutableMapOf ()
209210 )
211+ visited[this .id] = draft
212+ items.entries.map {
213+ draft.items[memoryDump.getById(it.key).toPythonTree(memoryDump, visited)] =
214+ memoryDump.getById(it.value).toPythonTree(memoryDump, visited)
215+ }
216+ draft
210217 }
211218
212219 is ListMemoryObject -> {
213- val elementsMap = items.withIndex().associate {
214- it.index to
215- memoryDump.getById(it.value).toPythonTree(memoryDump, visited)
216- }.toMutableMap()
217- when (this .qualname) {
218- " builtins.tuple" -> {
219- PythonTree .TupleNode (this .id.toLong(), elementsMap)
220- }
221-
222- " builtins.set" -> {
223- PythonTree .SetNode (this .id.toLong(), elementsMap.values.toMutableSet())
224- }
220+ val draft = when (this .qualname) {
221+ " builtins.tuple" -> PythonTree .TupleNode (id, mutableMapOf ())
222+ " builtins.set" -> PythonTree .SetNode (id, mutableSetOf ())
223+ else -> PythonTree .ListNode (id, mutableMapOf ())
224+ }
225+ visited[this .id] = draft
225226
226- else -> {
227- PythonTree .ListNode (this .id.toLong(), elementsMap)
227+ items.mapIndexed { index, valueId ->
228+ val value = memoryDump.getById(valueId).toPythonTree(memoryDump, visited)
229+ when (draft) {
230+ is PythonTree .TupleNode -> draft.items[index] = value
231+ is PythonTree .SetNode -> draft.items.add(value)
232+ is PythonTree .ListNode -> draft.items[index] = value
233+ else -> {}
228234 }
229235 }
236+ draft
230237 }
231238
232239 is ReduceMemoryObject -> {
0 commit comments