Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9093361

Browse files
committed
printobject now returns an error code
1 parent dd01080 commit 9093361

10 files changed

Lines changed: 70 additions & 86 deletions

File tree

Objects/fileobject.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,20 @@ file_dealloc(f)
117117
free((char *)f);
118118
}
119119

120-
static void
120+
static int
121121
file_print(f, fp, flags)
122122
fileobject *f;
123123
FILE *fp;
124124
int flags;
125125
{
126126
fprintf(fp, "<%s file ", f->f_fp == NULL ? "closed" : "open");
127-
printobject(f->f_name, fp, flags);
127+
if (printobject(f->f_name, fp, flags) != 0)
128+
return -1;
128129
fprintf(fp, ", mode ");
129-
printobject(f->f_mode, fp, flags);
130+
if (printobject(f->f_mode, fp, flags) != 0)
131+
return -1;
130132
fprintf(fp, ">");
133+
return 0;
131134
}
132135

133136
static object *

Objects/floatobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ float_buf_repr(buf, v)
9898
}
9999
}
100100

101-
static void
101+
static int
102102
float_print(v, fp, flags)
103103
floatobject *v;
104104
FILE *fp;
@@ -107,6 +107,7 @@ float_print(v, fp, flags)
107107
char buf[100];
108108
float_buf_repr(buf, v);
109109
fputs(buf, fp);
110+
return 0;
110111
}
111112

112113
static object *

Objects/intobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,14 @@ getintvalue(op)
120120

121121
/* Methods */
122122

123-
static void
123+
static int
124124
int_print(v, fp, flags)
125125
intobject *v;
126126
FILE *fp;
127127
int flags;
128128
{
129129
fprintf(fp, "%ld", v->ob_ival);
130+
return 0;
130131
}
131132

132133
static object *

Objects/listobject.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,21 +185,22 @@ list_dealloc(op)
185185
free((ANY *)op);
186186
}
187187

188-
static void
188+
static int
189189
list_print(op, fp, flags)
190190
listobject *op;
191191
FILE *fp;
192192
int flags;
193193
{
194194
int i;
195195
fprintf(fp, "[");
196-
for (i = 0; i < op->ob_size && !StopPrint; i++) {
197-
if (i > 0) {
196+
for (i = 0; i < op->ob_size; i++) {
197+
if (i > 0)
198198
fprintf(fp, ", ");
199-
}
200-
printobject(op->ob_item[i], fp, flags);
199+
if (printobject(op->ob_item[i], fp, flags) != 0)
200+
return -1;
201201
}
202202
fprintf(fp, "]");
203+
return 0;
203204
}
204205

205206
object *
@@ -302,7 +303,7 @@ list_concat(a, bb)
302303
size = a->ob_size + b->ob_size;
303304
np = (listobject *) newlistobject(size);
304305
if (np == NULL) {
305-
return err_nomem();
306+
return NULL;
306307
}
307308
for (i = 0; i < a->ob_size; i++) {
308309
object *v = a->ob_item[i];

Objects/longobject.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -533,21 +533,18 @@ long_dealloc(v)
533533
DEL(v);
534534
}
535535

536-
static void
536+
static int
537537
long_print(v, fp, flags)
538538
longobject *v;
539539
FILE *fp;
540540
int flags;
541541
{
542542
stringobject *str = long_format(v, 10);
543-
if (str == NULL) {
544-
err_clear();
545-
fprintf(fp, "[err]");
546-
}
547-
else {
548-
fprintf(fp, "%sL", GETSTRINGVALUE(str));
549-
DECREF(str);
550-
}
543+
if (str == NULL)
544+
return -1;
545+
fprintf(fp, "%sL", GETSTRINGVALUE(str));
546+
DECREF(str);
547+
return 0;
551548
}
552549

553550
static object *

Objects/methodobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ meth_dealloc(m)
8585
free((char *)m);
8686
}
8787

88-
static void
88+
static int
8989
meth_print(m, fp, flags)
9090
methodobject *m;
9191
FILE *fp;
@@ -96,6 +96,7 @@ meth_print(m, fp, flags)
9696
else
9797
fprintf(fp, "<built-in method '%s' of some %s object>",
9898
m->m_name, m->m_self->ob_type->tp_name);
99+
return 0;
99100
}
100101

101102
static object *

Objects/moduleobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ module_dealloc(m)
8383
free((char *)m);
8484
}
8585

86-
static void
86+
static int
8787
module_print(m, fp, flags)
8888
moduleobject *m;
8989
FILE *fp;
9090
int flags;
9191
{
9292
fprintf(fp, "<module '%s'>", getstringvalue(m->md_name));
93+
return 0;
9394
}
9495

9596
static object *

Objects/object.c

Lines changed: 31 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -61,77 +61,49 @@ newvarobject(tp, size)
6161
return op;
6262
}
6363

64-
int StopPrint; /* Flag to indicate printing must be stopped */
65-
66-
static int prlevel;
67-
68-
void
64+
int
6965
printobject(op, fp, flags)
7066
object *op;
7167
FILE *fp;
7268
int flags;
7369
{
74-
/* Hacks to make printing a long or recursive object interruptible */
75-
/* XXX Interrupts should leave a more permanent error */
76-
prlevel++;
77-
if (!StopPrint && intrcheck()) {
78-
fprintf(fp, "\n[print interrupted]\n");
79-
StopPrint = 1;
70+
if (intrcheck()) {
71+
err_set(KeyboardInterrupt);
72+
return -1;
8073
}
81-
if (!StopPrint) {
82-
if (op == NULL) {
83-
fprintf(fp, "<nil>");
84-
}
85-
else {
86-
if (op->ob_refcnt <= 0)
87-
fprintf(fp, "(refcnt %d):", op->ob_refcnt);
88-
if (op->ob_type->tp_print == NULL) {
89-
fprintf(fp, "<%s object at %lx>",
90-
op->ob_type->tp_name, (long)op);
91-
}
92-
else {
93-
(*op->ob_type->tp_print)(op, fp, flags);
94-
}
95-
}
74+
if (op == NULL) {
75+
fprintf(fp, "<nil>");
9676
}
97-
prlevel--;
98-
if (prlevel == 0)
99-
StopPrint = 0;
77+
else {
78+
if (op->ob_refcnt <= 0)
79+
fprintf(fp, "(refcnt %d):", op->ob_refcnt);
80+
if (op->ob_type->tp_print == NULL)
81+
fprintf(fp, "<%s object at %lx>",
82+
op->ob_type->tp_name, (long)op);
83+
else
84+
return (*op->ob_type->tp_print)(op, fp, flags);
85+
}
86+
return 0;
10087
}
10188

10289
object *
10390
reprobject(v)
10491
object *v;
10592
{
106-
object *w = NULL;
107-
/* Hacks to make converting a long or recursive object interruptible */
108-
prlevel++;
109-
if (!StopPrint && intrcheck()) {
110-
StopPrint = 1;
93+
if (intrcheck()) {
11194
err_set(KeyboardInterrupt);
95+
return NULL;
11296
}
113-
if (!StopPrint) {
114-
if (v == NULL) {
115-
w = newstringobject("<NULL>");
116-
}
117-
else if (v->ob_type->tp_repr == NULL) {
118-
char buf[100];
119-
sprintf(buf, "<%.80s object at %lx>",
120-
v->ob_type->tp_name, (long)v);
121-
w = newstringobject(buf);
122-
}
123-
else {
124-
w = (*v->ob_type->tp_repr)(v);
125-
}
126-
if (StopPrint) {
127-
XDECREF(w);
128-
w = NULL;
129-
}
97+
if (v == NULL)
98+
return newstringobject("<NULL>");
99+
else if (v->ob_type->tp_repr == NULL) {
100+
char buf[120];
101+
sprintf(buf, "<%.80s object at %lx>",
102+
v->ob_type->tp_name, (long)v);
103+
return newstringobject(buf);
130104
}
131-
prlevel--;
132-
if (prlevel == 0)
133-
StopPrint = 0;
134-
return w;
105+
else
106+
return (*v->ob_type->tp_repr)(v);
135107
}
136108

137109
int
@@ -191,13 +163,14 @@ There is (and should be!) no way to create other objects of this type,
191163
so there is exactly one (which is indestructible, by the way).
192164
*/
193165

194-
static void
166+
static int
195167
none_print(op, fp, flags)
196168
object *op;
197169
FILE *fp;
198170
int flags;
199171
{
200172
fprintf(fp, "None");
173+
return 0;
201174
}
202175

203176
static object *
@@ -278,7 +251,8 @@ printrefs(fp)
278251
fprintf(fp, "Remaining objects:\n");
279252
for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
280253
fprintf(fp, "[%d] ", op->ob_refcnt);
281-
printobject(op, fp, 0);
254+
if (printobject(op, fp, 0) != 0)
255+
err_clear();
282256
putc('\n', fp);
283257
}
284258
}

Python/bltinmodule.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,10 @@ builtin_input(self, v)
192192
int err;
193193
object *m, *d;
194194
flushline();
195-
if (v != NULL)
196-
printobject(v, out, PRINT_RAW);
195+
if (v != NULL) {
196+
if (printobject(v, out, PRINT_RAW) != 0)
197+
return NULL;
198+
}
197199
m = add_module("__main__");
198200
d = getmoduledict(m);
199201
return run_file(in, "<stdin>", expr_input, d, d);
@@ -450,8 +452,10 @@ builtin_raw_input(self, v)
450452
{
451453
FILE *out = sysgetfile("stdout", stdout);
452454
flushline();
453-
if (v != NULL)
454-
printobject(v, out, PRINT_RAW);
455+
if (v != NULL) {
456+
if (printobject(v, out, PRINT_RAW) != 0)
457+
return NULL;
458+
}
455459
return filegetline(sysget("stdin"), -1);
456460
}
457461

Python/ceval.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ eval_code(co, globals, locals, arg)
428428
if (v != None) {
429429
flushline();
430430
softspace(sysget("stdout"), 1);
431-
printobject(v, fp, 0);
431+
err = printobject(v, fp, 0);
432432
flushline();
433433
}
434434
DECREF(v);
@@ -447,7 +447,7 @@ eval_code(co, globals, locals, arg)
447447
softspace(sysget("stdout"), 0);
448448
}
449449
else {
450-
printobject(v, fp, 0);
450+
err = printobject(v, fp, 0);
451451
}
452452
DECREF(v);
453453
break;
@@ -933,7 +933,8 @@ prtrace(v, str)
933933
char *str;
934934
{
935935
printf("%s ", str);
936-
printobject(v, stdout, 0);
936+
if (printobject(v, stdout, 0) != 0)
937+
err_clear(); /* Don't know what else to do */
937938
printf("\n");
938939
}
939940
#endif

0 commit comments

Comments
 (0)