This repository was archived by the owner on Apr 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpython-2.6.6-readline-introduce-get-history-length.patch
96 lines (92 loc) · 3.63 KB
/
python-2.6.6-readline-introduce-get-history-length.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
diff -up Python-2.6.6/Misc/NEWS.03aa05ebd9bc Python-2.6.6/Misc/NEWS
diff -up Python-2.6.6/Modules/readline.c.03aa05ebd9bc Python-2.6.6/Modules/readline.c
--- Python-2.6.6/Modules/readline.c.03aa05ebd9bc 2011-06-30 19:18:11.050644574 -0400
+++ Python-2.6.6/Modules/readline.c 2011-06-30 19:20:10.618636903 -0400
@@ -511,6 +511,25 @@ PyDoc_STRVAR(doc_get_completer,
\n\
Returns current completer function.");
+/* Private function to get current length of history. XXX It may be
+ * possible to replace this with a direct use of history_length instead,
+ * but it's not clear whether BSD's libedit keeps history_length up to date.
+ * See issue #8065.*/
+
+static int
+_py_get_history_length(void)
+{
+ HISTORY_STATE *hist_st = history_get_history_state();
+ int length = hist_st->length;
+ /* the history docs don't say so, but the address of hist_st changes each
+ time history_get_history_state is called which makes me think it's
+ freshly malloc'd memory... on the other hand, the address of the last
+ line stays the same as long as history isn't extended, so it appears to
+ be malloc'd but managed by the history package... */
+ free(hist_st);
+ return length;
+}
+
/* Exported function to get any element of history */
static PyObject *
@@ -529,9 +548,7 @@ get_history_item(PyObject *self, PyObjec
* code doesn't have to worry about the
* difference.
*/
- HISTORY_STATE *hist_st;
- hist_st = history_get_history_state();
-
+ int length = _py_get_history_length();
idx --;
/*
@@ -539,7 +556,7 @@ get_history_item(PyObject *self, PyObjec
* the index is out of range, therefore
* test for that and fail gracefully.
*/
- if (idx < 0 || idx >= hist_st->length) {
+ if (idx < 0 || idx >= length) {
Py_RETURN_NONE;
}
}
@@ -561,10 +578,7 @@ return the current contents of history i
static PyObject *
get_current_history_length(PyObject *self, PyObject *noarg)
{
- HISTORY_STATE *hist_st;
-
- hist_st = history_get_history_state();
- return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
+ return PyInt_FromLong((long)_py_get_history_length());
}
PyDoc_STRVAR(doc_get_current_history_length,
@@ -1045,29 +1059,22 @@ call_readline(FILE *sys_stdin, FILE *sys
n = strlen(p);
if (n > 0) {
char *line;
- HISTORY_STATE *state = history_get_history_state();
- if (state->length > 0)
+ int length = _py_get_history_length();
+ if (length > 0)
#ifdef __APPLE__
if (using_libedit_emulation) {
/*
* Libedit's emulation uses 0-based indexes,
* the real readline uses 1-based indexes.
*/
- line = history_get(state->length - 1)->line;
+ line = history_get(length - 1)->line;
} else
#endif /* __APPLE__ */
- line = history_get(state->length)->line;
+ line = history_get(length)->line;
else
line = "";
if (strcmp(p, line))
add_history(p);
- /* the history docs don't say so, but the address of state
- changes each time history_get_history_state is called
- which makes me think it's freshly malloc'd memory...
- on the other hand, the address of the last line stays the
- same as long as history isn't extended, so it appears to
- be malloc'd but managed by the history package... */
- free(state);
}
/* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
release the original. */