Skip to content

Commit 92bdd26

Browse files
author
christian.heimes
committed
readline module cleanup
fixed indention to tabs use Py_RETURN_NONE macro added more error checks to on_completion_display_matches_hook open question: Does PyList_SetItem(l, i, o) steal a reference to o in the case of an error? git-svn-id: http://svn.python.org/projects/python/trunk@58952 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 7e2aeee commit 92bdd26

1 file changed

Lines changed: 90 additions & 99 deletions

File tree

Modules/readline.c

Lines changed: 90 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ parse_and_bind(PyObject *self, PyObject *args)
5959
strcpy(copy, s);
6060
rl_parse_and_bind(copy);
6161
free(copy); /* Free the copy */
62-
Py_INCREF(Py_None);
63-
return Py_None;
62+
Py_RETURN_NONE;
6463
}
6564

6665
PyDoc_STRVAR(doc_parse_and_bind,
@@ -79,8 +78,7 @@ read_init_file(PyObject *self, PyObject *args)
7978
errno = rl_read_init_file(s);
8079
if (errno)
8180
return PyErr_SetFromErrno(PyExc_IOError);
82-
Py_INCREF(Py_None);
83-
return Py_None;
81+
Py_RETURN_NONE;
8482
}
8583

8684
PyDoc_STRVAR(doc_read_init_file,
@@ -100,8 +98,7 @@ read_history_file(PyObject *self, PyObject *args)
10098
errno = read_history(s);
10199
if (errno)
102100
return PyErr_SetFromErrno(PyExc_IOError);
103-
Py_INCREF(Py_None);
104-
return Py_None;
101+
Py_RETURN_NONE;
105102
}
106103

107104
static int _history_length = -1; /* do not truncate history by default */
@@ -124,8 +121,7 @@ write_history_file(PyObject *self, PyObject *args)
124121
history_truncate_file(s, _history_length);
125122
if (errno)
126123
return PyErr_SetFromErrno(PyExc_IOError);
127-
Py_INCREF(Py_None);
128-
return Py_None;
124+
Py_RETURN_NONE;
129125
}
130126

131127
PyDoc_STRVAR(doc_write_history_file,
@@ -143,8 +139,7 @@ set_history_length(PyObject *self, PyObject *args)
143139
if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
144140
return NULL;
145141
_history_length = length;
146-
Py_INCREF(Py_None);
147-
return Py_None;
142+
Py_RETURN_NONE;
148143
}
149144

150145
PyDoc_STRVAR(set_history_length_doc,
@@ -195,8 +190,7 @@ set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
195190
PyErr_SetString(PyExc_TypeError, buf);
196191
return NULL;
197192
}
198-
Py_INCREF(Py_None);
199-
return Py_None;
193+
Py_RETURN_NONE;
200194
}
201195

202196

@@ -218,7 +212,7 @@ set_completion_display_matches_hook(PyObject *self, PyObject *args)
218212
/* We cannot set this hook globally, since it replaces the
219213
default completion display. */
220214
rl_completion_display_matches_hook =
221-
completion_display_matches_hook ?
215+
completion_display_matches_hook ?
222216
(rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
223217
#endif
224218
return result;
@@ -325,8 +319,7 @@ set_completer_delims(PyObject *self, PyObject *args)
325319
}
326320
free((void*)rl_completer_word_break_characters);
327321
rl_completer_word_break_characters = strdup(break_chars);
328-
Py_INCREF(Py_None);
329-
return Py_None;
322+
Py_RETURN_NONE;
330323
}
331324

332325
PyDoc_STRVAR(doc_set_completer_delims,
@@ -336,32 +329,31 @@ set the readline word delimiters for tab-completion");
336329
static PyObject *
337330
py_remove_history(PyObject *self, PyObject *args)
338331
{
339-
int entry_number;
340-
HIST_ENTRY *entry;
341-
342-
if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
343-
return NULL;
344-
if (entry_number < 0) {
345-
PyErr_SetString(PyExc_ValueError,
346-
"History index cannot be negative");
347-
return NULL;
348-
}
349-
entry = remove_history(entry_number);
350-
if (!entry) {
351-
PyErr_Format(PyExc_ValueError,
352-
"No history item at position %d",
353-
entry_number);
354-
return NULL;
355-
}
356-
/* free memory allocated for the history entry */
357-
if (entry->line)
358-
free(entry->line);
359-
if (entry->data)
360-
free(entry->data);
361-
free(entry);
362-
363-
Py_INCREF(Py_None);
364-
return Py_None;
332+
int entry_number;
333+
HIST_ENTRY *entry;
334+
335+
if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
336+
return NULL;
337+
if (entry_number < 0) {
338+
PyErr_SetString(PyExc_ValueError,
339+
"History index cannot be negative");
340+
return NULL;
341+
}
342+
entry = remove_history(entry_number);
343+
if (!entry) {
344+
PyErr_Format(PyExc_ValueError,
345+
"No history item at position %d",
346+
entry_number);
347+
return NULL;
348+
}
349+
/* free memory allocated for the history entry */
350+
if (entry->line)
351+
free(entry->line);
352+
if (entry->data)
353+
free(entry->data);
354+
free(entry);
355+
356+
Py_RETURN_NONE;
365357
}
366358

367359
PyDoc_STRVAR(doc_remove_history,
@@ -371,34 +363,34 @@ remove history item given by its position");
371363
static PyObject *
372364
py_replace_history(PyObject *self, PyObject *args)
373365
{
374-
int entry_number;
375-
char *line;
376-
HIST_ENTRY *old_entry;
377-
378-
if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number, &line)) {
379-
return NULL;
380-
}
381-
if (entry_number < 0) {
382-
PyErr_SetString(PyExc_ValueError,
383-
"History index cannot be negative");
384-
return NULL;
385-
}
386-
old_entry = replace_history_entry(entry_number, line, (void *)NULL);
387-
if (!old_entry) {
388-
PyErr_Format(PyExc_ValueError,
389-
"No history item at position %d",
390-
entry_number);
391-
return NULL;
392-
}
393-
/* free memory allocated for the old history entry */
394-
if (old_entry->line)
395-
free(old_entry->line);
396-
if (old_entry->data)
397-
free(old_entry->data);
398-
free(old_entry);
399-
400-
Py_INCREF(Py_None);
401-
return Py_None;
366+
int entry_number;
367+
char *line;
368+
HIST_ENTRY *old_entry;
369+
370+
if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
371+
&line)) {
372+
return NULL;
373+
}
374+
if (entry_number < 0) {
375+
PyErr_SetString(PyExc_ValueError,
376+
"History index cannot be negative");
377+
return NULL;
378+
}
379+
old_entry = replace_history_entry(entry_number, line, (void *)NULL);
380+
if (!old_entry) {
381+
PyErr_Format(PyExc_ValueError,
382+
"No history item at position %d",
383+
entry_number);
384+
return NULL;
385+
}
386+
/* free memory allocated for the old history entry */
387+
if (old_entry->line)
388+
free(old_entry->line);
389+
if (old_entry->data)
390+
free(old_entry->data);
391+
free(old_entry);
392+
393+
Py_RETURN_NONE;
402394
}
403395

404396
PyDoc_STRVAR(doc_replace_history,
@@ -416,8 +408,7 @@ py_add_history(PyObject *self, PyObject *args)
416408
return NULL;
417409
}
418410
add_history(line);
419-
Py_INCREF(Py_None);
420-
return Py_None;
411+
Py_RETURN_NONE;
421412
}
422413

423414
PyDoc_STRVAR(doc_add_history,
@@ -458,8 +449,7 @@ static PyObject *
458449
get_completer(PyObject *self, PyObject *noargs)
459450
{
460451
if (completer == NULL) {
461-
Py_INCREF(Py_None);
462-
return Py_None;
452+
Py_RETURN_NONE;
463453
}
464454
Py_INCREF(completer);
465455
return completer;
@@ -483,8 +473,7 @@ get_history_item(PyObject *self, PyObject *args)
483473
if ((hist_ent = history_get(idx)))
484474
return PyString_FromString(hist_ent->line);
485475
else {
486-
Py_INCREF(Py_None);
487-
return Py_None;
476+
Py_RETURN_NONE;
488477
}
489478
}
490479

@@ -530,8 +519,7 @@ static PyObject *
530519
py_clear_history(PyObject *self, PyObject *noarg)
531520
{
532521
clear_history();
533-
Py_INCREF(Py_None);
534-
return Py_None;
522+
Py_RETURN_NONE;
535523
}
536524

537525
PyDoc_STRVAR(doc_clear_history,
@@ -549,8 +537,7 @@ insert_text(PyObject *self, PyObject *args)
549537
if (!PyArg_ParseTuple(args, "s:insert_text", &s))
550538
return NULL;
551539
rl_insert_text(s);
552-
Py_INCREF(Py_None);
553-
return Py_None;
540+
Py_RETURN_NONE;
554541
}
555542

556543
PyDoc_STRVAR(doc_insert_text,
@@ -564,8 +551,7 @@ static PyObject *
564551
redisplay(PyObject *self, PyObject *noarg)
565552
{
566553
rl_redisplay();
567-
Py_INCREF(Py_None);
568-
return Py_None;
554+
Py_RETURN_NONE;
569555
}
570556

571557
PyDoc_STRVAR(doc_redisplay,
@@ -591,9 +577,9 @@ static struct PyMethodDef readline_methods[] =
591577
METH_VARARGS, doc_get_history_item},
592578
{"get_current_history_length", (PyCFunction)get_current_history_length,
593579
METH_NOARGS, doc_get_current_history_length},
594-
{"set_history_length", set_history_length,
580+
{"set_history_length", set_history_length,
595581
METH_VARARGS, set_history_length_doc},
596-
{"get_history_length", get_history_length,
582+
{"get_history_length", get_history_length,
597583
METH_NOARGS, get_history_length_doc},
598584
{"set_completer", set_completer, METH_VARARGS, doc_set_completer},
599585
{"get_completer", get_completer, METH_NOARGS, doc_get_completer},
@@ -605,8 +591,8 @@ static struct PyMethodDef readline_methods[] =
605591
{"set_completer_delims", set_completer_delims,
606592
METH_VARARGS, doc_set_completer_delims},
607593
{"add_history", py_add_history, METH_VARARGS, doc_add_history},
608-
{"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
609-
{"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
594+
{"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
595+
{"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
610596
{"get_completer_delims", get_completer_delims,
611597
METH_NOARGS, doc_get_completer_delims},
612598

@@ -633,7 +619,7 @@ on_hook(PyObject *func)
633619
int result = 0;
634620
if (func != NULL) {
635621
PyObject *r;
636-
#ifdef WITH_THREAD
622+
#ifdef WITH_THREAD
637623
PyGILState_STATE gilstate = PyGILState_Ensure();
638624
#endif
639625
r = PyObject_CallFunction(func, NULL);
@@ -652,7 +638,7 @@ on_hook(PyObject *func)
652638
PyErr_Clear();
653639
Py_XDECREF(r);
654640
done:
655-
#ifdef WITH_THREAD
641+
#ifdef WITH_THREAD
656642
PyGILState_Release(gilstate);
657643
#endif
658644
return result;
@@ -682,34 +668,39 @@ on_completion_display_matches_hook(char **matches,
682668
int num_matches, int max_length)
683669
{
684670
int i;
685-
PyObject *m, *s;
686-
PyObject *r;
687-
#ifdef WITH_THREAD
671+
PyObject *m=NULL, *s=NULL, *r=NULL;
672+
#ifdef WITH_THREAD
688673
PyGILState_STATE gilstate = PyGILState_Ensure();
689674
#endif
690675
m = PyList_New(num_matches);
676+
if (m == NULL)
677+
goto error;
691678
for (i = 0; i < num_matches; i++) {
692679
s = PyString_FromString(matches[i+1]);
693-
PyList_SetItem(m, i, s);
680+
if (s == NULL)
681+
goto error;
682+
if (PyList_SetItem(m, i, s) == -1)
683+
goto error;
694684
}
695685

696686
r = PyObject_CallFunction(completion_display_matches_hook,
697687
"sOi", matches[0], m, max_length);
698688

699-
Py_DECREF(m);
689+
Py_DECREF(m), m=NULL;
700690

701691
if (r == NULL ||
702692
(r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
703693
goto error;
704694
}
695+
Py_XDECREF(r), r=NULL;
705696

706-
Py_DECREF(r);
707-
goto done;
708-
error:
709-
PyErr_Clear();
710-
Py_XDECREF(r);
711-
done:
712-
#ifdef WITH_THREAD
697+
if (0) {
698+
error:
699+
PyErr_Clear();
700+
Py_XDECREF(m);
701+
Py_XDECREF(r);
702+
}
703+
#ifdef WITH_THREAD
713704
PyGILState_Release(gilstate);
714705
#endif
715706
}

0 commit comments

Comments
 (0)