Skip to content

Commit 6e61685

Browse files
committed
Put reprLeave in finally blocks
1 parent 28e3c15 commit 6e61685

File tree

6 files changed

+65
-53
lines changed

6 files changed

+65
-53
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/BufferedIOMixinBuiltins.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,12 @@ Object repr(VirtualFrame frame, PBuffered self,
389389
if (!getContext().reprEnter(self)) {
390390
throw raise(RuntimeError, "reentrant call inside %s.__repr__", clazz);
391391
} else {
392-
Object name = repr.executeObject(frame, nameobj);
393-
getContext().reprLeave(self);
394-
return PythonUtils.format("<%s name=%s>", clazz, name);
392+
try {
393+
Object name = repr.executeObject(frame, nameobj);
394+
return PythonUtils.format("<%s name=%s>", clazz, name);
395+
} finally {
396+
getContext().reprLeave(self);
397+
}
395398
}
396399
}
397400
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/FileIOBuiltins.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,9 +1025,12 @@ Object doit(VirtualFrame frame, PFileIO self,
10251025
if (!getContext().reprEnter(self)) {
10261026
throw raise(RuntimeError, "reentrant call inside %p.__repr__", self);
10271027
} else {
1028-
Object name = repr.executeObject(frame, nameobj);
1029-
getContext().reprLeave(self);
1030-
return PythonUtils.format("<_io.FileIO name=%s mode='%s' closefd=%s>", name, mode, closefd);
1028+
try {
1029+
Object name = repr.executeObject(frame, nameobj);
1030+
return PythonUtils.format("<_io.FileIO name=%s mode='%s' closefd=%s>", name, mode, closefd);
1031+
} finally {
1032+
getContext().reprLeave(self);
1033+
}
10311034
}
10321035
}
10331036
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/TextIOWrapperBuiltins.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,9 +1148,9 @@ Object doit(VirtualFrame frame, PTextIO self,
11481148
if (!getContext().reprEnter(self)) {
11491149
throw raise(RuntimeError, "reentrant call inside %p.__repr__", self);
11501150
} else {
1151-
StringBuilder sb = PythonUtils.newStringBuilder();
1152-
PythonUtils.append(sb, "<_io.TextIOWrapper");
11531151
try {
1152+
StringBuilder sb = PythonUtils.newStringBuilder();
1153+
PythonUtils.append(sb, "<_io.TextIOWrapper");
11541154
Object nameobj = PNone.NO_VALUE;
11551155
try {
11561156
nameobj = libSelf.lookupAttributeStrict(self, frame, NAME);
@@ -1167,11 +1167,9 @@ Object doit(VirtualFrame frame, PTextIO self,
11671167
PythonUtils.append(sb, PythonUtils.format(" mode='%s'", toString.execute(modeobj)));
11681168
}
11691169
PythonUtils.append(sb, PythonUtils.format(" encoding='%s'>", self.getEncoding()));
1170-
getContext().reprLeave(self);
11711170
return PythonUtils.sbToString(sb);
1172-
} catch (PException e) {
1171+
} finally {
11731172
getContext().reprLeave(self);
1174-
throw e;
11751173
}
11761174
}
11771175
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/dict/DictReprBuiltin.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,15 @@ public static Object repr(PDict dict,
235235
if (!ctxt.reprEnter(dict)) {
236236
return "{...}";
237237
}
238-
StringBuilder sb = PythonUtils.newStringBuilder("{");
239-
HashingStorage dictStorage = dict.getDictStorage();
240-
lib.forEach(dictStorage, consumerNode, new ReprState(dict, dictStorage, sb));
241-
PythonUtils.append(sb, "}");
242-
ctxt.reprLeave(dict);
243-
return PythonUtils.sbToString(sb);
238+
try {
239+
StringBuilder sb = PythonUtils.newStringBuilder("{");
240+
HashingStorage dictStorage = dict.getDictStorage();
241+
lib.forEach(dictStorage, consumerNode, new ReprState(dict, dictStorage, sb));
242+
PythonUtils.append(sb, "}");
243+
return PythonUtils.sbToString(sb);
244+
} finally {
245+
ctxt.reprLeave(dict);
246+
}
244247
}
245248

246249
@Specialization// use same limit as for EachRepr nodes library

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/list/ListBuiltins.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -172,31 +172,33 @@ public Object repr(VirtualFrame frame, PList self,
172172
if (!ctxt.reprEnter(self)) {
173173
return "[...]";
174174
}
175-
176-
StringBuilder result = PythonUtils.newStringBuilder();
177-
PythonUtils.append(result, "[");
178-
boolean initial = true;
179-
Object value;
180-
Object reprString;
181-
for (int index = 0; index < length; index++) {
182-
value = getItem.execute(frame, storage, index);
183-
reprString = repr.executeObject(frame, value);
184-
if (reprString instanceof PString) {
185-
reprString = ((PString) reprString).getValue();
186-
}
187-
if (reprString instanceof String) {
188-
if (initial) {
189-
initial = false;
175+
try {
176+
StringBuilder result = PythonUtils.newStringBuilder();
177+
PythonUtils.append(result, "[");
178+
boolean initial = true;
179+
Object value;
180+
Object reprString;
181+
for (int index = 0; index < length; index++) {
182+
value = getItem.execute(frame, storage, index);
183+
reprString = repr.executeObject(frame, value);
184+
if (reprString instanceof PString) {
185+
reprString = ((PString) reprString).getValue();
186+
}
187+
if (reprString instanceof String) {
188+
if (initial) {
189+
initial = false;
190+
} else {
191+
PythonUtils.append(result, ", ");
192+
}
193+
PythonUtils.append(result, (String) reprString);
190194
} else {
191-
PythonUtils.append(result, ", ");
195+
raise(PythonErrorType.TypeError, ErrorMessages.RETURNED_NON_STRING, "__repr__", reprString);
192196
}
193-
PythonUtils.append(result, (String) reprString);
194-
} else {
195-
raise(PythonErrorType.TypeError, ErrorMessages.RETURNED_NON_STRING, "__repr__", reprString);
196197
}
198+
return PythonUtils.sbToString(PythonUtils.append(result, "]"));
199+
} finally {
200+
ctxt.reprLeave(self);
197201
}
198-
ctxt.reprLeave(self);
199-
return PythonUtils.sbToString(PythonUtils.append(result, "]"));
200202
}
201203
}
202204

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/tuple/TupleBuiltins.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -211,24 +211,27 @@ public static String repr(VirtualFrame frame, PTuple self,
211211
if (!ctxt.reprEnter(self)) {
212212
return "(...)";
213213
}
214-
StringBuilder buf = PythonUtils.newStringBuilder();
215-
PythonUtils.append(buf, "(");
216-
for (int i = 0; i < len - 1; i++) {
217-
PythonUtils.append(buf, toString(frame, getItemNode.execute(frame, tupleStore, i), reprNode));
218-
PythonUtils.append(buf, ", ");
219-
}
214+
try {
215+
StringBuilder buf = PythonUtils.newStringBuilder();
216+
PythonUtils.append(buf, "(");
217+
for (int i = 0; i < len - 1; i++) {
218+
PythonUtils.append(buf, toString(frame, getItemNode.execute(frame, tupleStore, i), reprNode));
219+
PythonUtils.append(buf, ", ");
220+
}
220221

221-
if (len > 0) {
222-
PythonUtils.append(buf, toString(frame, getItemNode.execute(frame, tupleStore, len - 1), reprNode));
223-
}
222+
if (len > 0) {
223+
PythonUtils.append(buf, toString(frame, getItemNode.execute(frame, tupleStore, len - 1), reprNode));
224+
}
224225

225-
if (len == 1) {
226-
PythonUtils.append(buf, ",");
227-
}
226+
if (len == 1) {
227+
PythonUtils.append(buf, ",");
228+
}
228229

229-
PythonUtils.append(buf, ")");
230-
ctxt.reprLeave(self);
231-
return PythonUtils.sbToString(buf);
230+
PythonUtils.append(buf, ")");
231+
return PythonUtils.sbToString(buf);
232+
} finally {
233+
ctxt.reprLeave(self);
234+
}
232235
}
233236
}
234237

0 commit comments

Comments
 (0)