mirror of
https://github.com/python/cpython
synced 2024-11-02 08:37:57 +00:00
89d996e5c2
svn+ssh://pythondev@svn.python.org/python/trunk ........ r57820 | georg.brandl | 2007-08-31 08:59:27 +0200 (Fri, 31 Aug 2007) | 2 lines Document new shorthand notation for index entries. ........ r57827 | georg.brandl | 2007-08-31 10:47:51 +0200 (Fri, 31 Aug 2007) | 2 lines Fix subitem markup. ........ r57833 | martin.v.loewis | 2007-08-31 12:01:07 +0200 (Fri, 31 Aug 2007) | 1 line Mark registry components as 64-bit on Win64. ........ r57854 | bill.janssen | 2007-08-31 21:02:23 +0200 (Fri, 31 Aug 2007) | 1 line deprecate use of FakeSocket ........ r57855 | bill.janssen | 2007-08-31 21:02:46 +0200 (Fri, 31 Aug 2007) | 1 line remove mentions of socket.ssl in comments ........ r57856 | bill.janssen | 2007-08-31 21:03:31 +0200 (Fri, 31 Aug 2007) | 1 line remove use of non-existent SSLFakeSocket in apparently untested code ........ r57859 | martin.v.loewis | 2007-09-01 08:36:03 +0200 (Sat, 01 Sep 2007) | 3 lines Bug #1737210: Change Manufacturer of Windows installer to PSF. Will backport to 2.5. ........ r57865 | georg.brandl | 2007-09-01 09:51:24 +0200 (Sat, 01 Sep 2007) | 2 lines Fix RST link (backport from Py3k). ........ r57876 | georg.brandl | 2007-09-01 17:49:49 +0200 (Sat, 01 Sep 2007) | 2 lines Document sets' ">" and "<" operations (backport from py3k). ........ r57878 | skip.montanaro | 2007-09-01 19:40:03 +0200 (Sat, 01 Sep 2007) | 4 lines Added a note and examples to explain that re.split does not split on an empty pattern match. (issue 852532). ........ r57879 | walter.doerwald | 2007-09-01 20:18:09 +0200 (Sat, 01 Sep 2007) | 2 lines Fix wrong function names. ........ r57880 | walter.doerwald | 2007-09-01 20:34:05 +0200 (Sat, 01 Sep 2007) | 2 lines Fix typo. ........ r57889 | andrew.kuchling | 2007-09-01 22:31:59 +0200 (Sat, 01 Sep 2007) | 1 line Markup fix ........ r57892 | andrew.kuchling | 2007-09-01 22:43:36 +0200 (Sat, 01 Sep 2007) | 1 line Add various items ........ r57895 | andrew.kuchling | 2007-09-01 23:17:58 +0200 (Sat, 01 Sep 2007) | 1 line Wording change ........ r57896 | andrew.kuchling | 2007-09-01 23:18:31 +0200 (Sat, 01 Sep 2007) | 1 line Add more items ........ r57904 | ronald.oussoren | 2007-09-02 11:46:07 +0200 (Sun, 02 Sep 2007) | 3 lines Macosx: this patch ensures that the value of MACOSX_DEPLOYMENT_TARGET used by the Makefile is also used at configure-time. ........ r57925 | georg.brandl | 2007-09-03 09:16:46 +0200 (Mon, 03 Sep 2007) | 2 lines Fix #883466: don't allow Unicode as arguments to quopri and uu codecs. ........ r57936 | matthias.klose | 2007-09-04 01:33:04 +0200 (Tue, 04 Sep 2007) | 2 lines - Added support for linking the bsddb module against BerkeleyDB 4.6.x. ........ r57954 | mark.summerfield | 2007-09-04 10:16:15 +0200 (Tue, 04 Sep 2007) | 3 lines Added cross-references plus a note about dict & list shallow copying. ........ r57958 | martin.v.loewis | 2007-09-04 11:51:57 +0200 (Tue, 04 Sep 2007) | 3 lines Document that we rely on the OS to release the crypto context. Fixes #1626801. ........ r57960 | martin.v.loewis | 2007-09-04 15:13:14 +0200 (Tue, 04 Sep 2007) | 3 lines Patch #1388440: Add set_completion_display_matches_hook and get_completion_type to readline. ........ r57961 | martin.v.loewis | 2007-09-04 16:19:28 +0200 (Tue, 04 Sep 2007) | 3 lines Patch #1031213: Decode source line in SyntaxErrors back to its original source encoding. Will backport to 2.5. ........ r57972 | matthias.klose | 2007-09-04 20:17:36 +0200 (Tue, 04 Sep 2007) | 3 lines - Makefile.pre.in(buildbottest): Run an optional script pybuildbot.identify to include some information about the build environment. ........ r57973 | matthias.klose | 2007-09-04 21:05:38 +0200 (Tue, 04 Sep 2007) | 2 lines - Makefile.pre.in(buildbottest): Remove whitespace at eol. ........ r57975 | matthias.klose | 2007-09-04 22:46:02 +0200 (Tue, 04 Sep 2007) | 2 lines - Fix libffi configure for hppa*-*-linux* | parisc*-*-linux*. ........ r57980 | bill.janssen | 2007-09-05 02:46:27 +0200 (Wed, 05 Sep 2007) | 1 line SSL certificate distinguished names should be represented by tuples ........ r57985 | martin.v.loewis | 2007-09-05 08:39:17 +0200 (Wed, 05 Sep 2007) | 3 lines Patch #1105: Explain that one needs to build the solution to get dependencies right. ........ r57987 | armin.rigo | 2007-09-05 09:51:21 +0200 (Wed, 05 Sep 2007) | 4 lines PyDict_GetItem() returns a borrowed reference. There are probably a number of places that are open to attacks such as the following one, in bltinmodule.c:min_max(). ........ r57991 | martin.v.loewis | 2007-09-05 13:47:34 +0200 (Wed, 05 Sep 2007) | 3 lines Patch #786737: Allow building in a tree of symlinks pointing to a readonly source. ........ r57993 | georg.brandl | 2007-09-05 15:36:44 +0200 (Wed, 05 Sep 2007) | 2 lines Backport from Py3k: Bug #1684991: explain lookup semantics for __special__ methods (new-style classes only). ........ r58004 | armin.rigo | 2007-09-06 10:30:51 +0200 (Thu, 06 Sep 2007) | 4 lines Patch #1733973 by peaker: ptrace_enter_call() assumes no exception is currently set. This assumption is broken when throwing into a generator. ........ r58006 | armin.rigo | 2007-09-06 11:30:38 +0200 (Thu, 06 Sep 2007) | 4 lines PyDict_GetItem() returns a borrowed reference. This attack is against ceval.c:IMPORT_NAME, which calls an object (__builtin__.__import__) without holding a reference to it. ........ r58013 | georg.brandl | 2007-09-06 16:49:56 +0200 (Thu, 06 Sep 2007) | 2 lines Backport from 3k: #1116: fix reference to old filename. ........ r58021 | thomas.heller | 2007-09-06 22:26:20 +0200 (Thu, 06 Sep 2007) | 1 line Fix typo: c_float represents to C float type. ........ r58022 | skip.montanaro | 2007-09-07 00:29:06 +0200 (Fri, 07 Sep 2007) | 3 lines If this is correct for py3k branch and it's already in the release25-maint branch, seems like it ought to be on the trunk as well. ........ r58023 | gregory.p.smith | 2007-09-07 00:59:59 +0200 (Fri, 07 Sep 2007) | 4 lines Apply the fix from Issue1112 to make this test more robust and keep windows happy. ........ r58031 | brett.cannon | 2007-09-07 05:17:50 +0200 (Fri, 07 Sep 2007) | 4 lines Make uuid1 and uuid4 tests conditional on whether ctypes can be imported; implementation of either function depends on ctypes but uuid as a whole does not. ........ r58032 | brett.cannon | 2007-09-07 06:18:30 +0200 (Fri, 07 Sep 2007) | 6 lines Fix a crasher where Python code managed to infinitely recurse in C code without ever going back out to Python code in PyObject_Call(). Required introducing a static RuntimeError instance so that normalizing an exception there is no reliance on a recursive call that would put the exception system over the recursion check itself. ........ r58034 | thomas.heller | 2007-09-07 08:32:17 +0200 (Fri, 07 Sep 2007) | 1 line Add a 'c_longdouble' type to the ctypes module. ........ r58035 | thomas.heller | 2007-09-07 11:30:40 +0200 (Fri, 07 Sep 2007) | 1 line Remove unneeded #include. ........ r58036 | thomas.heller | 2007-09-07 11:33:24 +0200 (Fri, 07 Sep 2007) | 6 lines Backport from py3k branch: Add a workaround for a strange bug on win64, when _ctypes is compiled with the SDK compiler. This should fix the failing Lib\ctypes\test\test_as_parameter.py test. ........ r58037 | georg.brandl | 2007-09-07 16:14:40 +0200 (Fri, 07 Sep 2007) | 2 lines Fix a wrong indentation for sublists. ........ r58043 | georg.brandl | 2007-09-07 22:10:49 +0200 (Fri, 07 Sep 2007) | 2 lines #1095: ln -f doesn't work portably, fix in Makefile. ........ r58049 | skip.montanaro | 2007-09-08 02:34:17 +0200 (Sat, 08 Sep 2007) | 1 line be explicit about the actual location of the missing file ........
1020 lines
24 KiB
C
1020 lines
24 KiB
C
/* This module makes GNU readline available to Python. It has ideas
|
|
* contributed by Lee Busby, LLNL, and William Magro, Cornell Theory
|
|
* Center. The completer interface was inspired by Lele Gaifax. More
|
|
* recently, it was largely rewritten by Guido van Rossum.
|
|
*/
|
|
|
|
/* Standard definitions */
|
|
#include "Python.h"
|
|
#include <setjmp.h>
|
|
#include <signal.h>
|
|
#include <errno.h>
|
|
#include <sys/time.h>
|
|
|
|
#if defined(HAVE_SETLOCALE)
|
|
/* GNU readline() mistakenly sets the LC_CTYPE locale.
|
|
* This is evil. Only the user or the app's main() should do this!
|
|
* We must save and restore the locale around the rl_initialize() call.
|
|
*/
|
|
#define SAVE_LOCALE
|
|
#include <locale.h>
|
|
#endif
|
|
|
|
#ifdef SAVE_LOCALE
|
|
# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
|
|
#else
|
|
# define RESTORE_LOCALE(sl)
|
|
#endif
|
|
|
|
/* GNU readline definitions */
|
|
#undef HAVE_CONFIG_H /* Else readline/chardefs.h includes strings.h */
|
|
#include <readline/readline.h>
|
|
#include <readline/history.h>
|
|
|
|
#ifdef HAVE_RL_COMPLETION_MATCHES
|
|
#define completion_matches(x, y) \
|
|
rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
|
|
#else
|
|
extern char **completion_matches(char *, rl_compentry_func_t *);
|
|
#endif
|
|
|
|
|
|
/* Exported function to send one line to readline's init file parser */
|
|
|
|
static PyObject *
|
|
parse_and_bind(PyObject *self, PyObject *args)
|
|
{
|
|
char *s, *copy;
|
|
if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
|
|
return NULL;
|
|
/* Make a copy -- rl_parse_and_bind() modifies its argument */
|
|
/* Bernard Herzog */
|
|
copy = malloc(1 + strlen(s));
|
|
if (copy == NULL)
|
|
return PyErr_NoMemory();
|
|
strcpy(copy, s);
|
|
rl_parse_and_bind(copy);
|
|
free(copy); /* Free the copy */
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_parse_and_bind,
|
|
"parse_and_bind(string) -> None\n\
|
|
Parse and execute single line of a readline init file.");
|
|
|
|
|
|
/* Exported function to parse a readline init file */
|
|
|
|
static PyObject *
|
|
read_init_file(PyObject *self, PyObject *args)
|
|
{
|
|
char *s = NULL;
|
|
if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
|
|
return NULL;
|
|
errno = rl_read_init_file(s);
|
|
if (errno)
|
|
return PyErr_SetFromErrno(PyExc_IOError);
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_read_init_file,
|
|
"read_init_file([filename]) -> None\n\
|
|
Parse a readline initialization file.\n\
|
|
The default filename is the last filename used.");
|
|
|
|
|
|
/* Exported function to load a readline history file */
|
|
|
|
static PyObject *
|
|
read_history_file(PyObject *self, PyObject *args)
|
|
{
|
|
char *s = NULL;
|
|
if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
|
|
return NULL;
|
|
errno = read_history(s);
|
|
if (errno)
|
|
return PyErr_SetFromErrno(PyExc_IOError);
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
static int _history_length = -1; /* do not truncate history by default */
|
|
PyDoc_STRVAR(doc_read_history_file,
|
|
"read_history_file([filename]) -> None\n\
|
|
Load a readline history file.\n\
|
|
The default filename is ~/.history.");
|
|
|
|
|
|
/* Exported function to save a readline history file */
|
|
|
|
static PyObject *
|
|
write_history_file(PyObject *self, PyObject *args)
|
|
{
|
|
char *s = NULL;
|
|
if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
|
|
return NULL;
|
|
errno = write_history(s);
|
|
if (!errno && _history_length >= 0)
|
|
history_truncate_file(s, _history_length);
|
|
if (errno)
|
|
return PyErr_SetFromErrno(PyExc_IOError);
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_write_history_file,
|
|
"write_history_file([filename]) -> None\n\
|
|
Save a readline history file.\n\
|
|
The default filename is ~/.history.");
|
|
|
|
|
|
/* Set history length */
|
|
|
|
static PyObject*
|
|
set_history_length(PyObject *self, PyObject *args)
|
|
{
|
|
int length = _history_length;
|
|
if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
|
|
return NULL;
|
|
_history_length = length;
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
PyDoc_STRVAR(set_history_length_doc,
|
|
"set_history_length(length) -> None\n\
|
|
set the maximal number of items which will be written to\n\
|
|
the history file. A negative length is used to inhibit\n\
|
|
history truncation.");
|
|
|
|
|
|
/* Get history length */
|
|
|
|
static PyObject*
|
|
get_history_length(PyObject *self, PyObject *noarg)
|
|
{
|
|
return PyInt_FromLong(_history_length);
|
|
}
|
|
|
|
PyDoc_STRVAR(get_history_length_doc,
|
|
"get_history_length() -> int\n\
|
|
return the maximum number of items that will be written to\n\
|
|
the history file.");
|
|
|
|
|
|
/* Generic hook function setter */
|
|
|
|
static PyObject *
|
|
set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
|
|
{
|
|
PyObject *function = Py_None;
|
|
char buf[80];
|
|
PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
|
|
if (!PyArg_ParseTuple(args, buf, &function))
|
|
return NULL;
|
|
if (function == Py_None) {
|
|
Py_XDECREF(*hook_var);
|
|
*hook_var = NULL;
|
|
}
|
|
else if (PyCallable_Check(function)) {
|
|
PyObject *tmp = *hook_var;
|
|
Py_INCREF(function);
|
|
*hook_var = function;
|
|
Py_XDECREF(tmp);
|
|
}
|
|
else {
|
|
PyOS_snprintf(buf, sizeof(buf),
|
|
"set_%.50s(func): argument not callable",
|
|
funcname);
|
|
PyErr_SetString(PyExc_TypeError, buf);
|
|
return NULL;
|
|
}
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
|
|
/* Exported functions to specify hook functions in Python */
|
|
|
|
static PyObject *completion_display_matches_hook = NULL;
|
|
static PyObject *startup_hook = NULL;
|
|
|
|
#ifdef HAVE_RL_PRE_INPUT_HOOK
|
|
static PyObject *pre_input_hook = NULL;
|
|
#endif
|
|
|
|
static PyObject *
|
|
set_completion_display_matches_hook(PyObject *self, PyObject *args)
|
|
{
|
|
return set_hook("completion_display_matches_hook",
|
|
&completion_display_matches_hook, args);
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_set_completion_display_matches_hook,
|
|
"set_completion_display_matches_hook([function]) -> None\n\
|
|
Set or remove the completion display function.\n\
|
|
The function is called as\n\
|
|
function(substitution, [matches], longest_match_length)\n\
|
|
once each time matches need to be displayed.");
|
|
|
|
static PyObject *
|
|
set_startup_hook(PyObject *self, PyObject *args)
|
|
{
|
|
return set_hook("startup_hook", &startup_hook, args);
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_set_startup_hook,
|
|
"set_startup_hook([function]) -> None\n\
|
|
Set or remove the startup_hook function.\n\
|
|
The function is called with no arguments just\n\
|
|
before readline prints the first prompt.");
|
|
|
|
|
|
#ifdef HAVE_RL_PRE_INPUT_HOOK
|
|
|
|
/* Set pre-input hook */
|
|
|
|
static PyObject *
|
|
set_pre_input_hook(PyObject *self, PyObject *args)
|
|
{
|
|
return set_hook("pre_input_hook", &pre_input_hook, args);
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_set_pre_input_hook,
|
|
"set_pre_input_hook([function]) -> None\n\
|
|
Set or remove the pre_input_hook function.\n\
|
|
The function is called with no arguments after the first prompt\n\
|
|
has been printed and just before readline starts reading input\n\
|
|
characters.");
|
|
|
|
#endif
|
|
|
|
|
|
/* Exported function to specify a word completer in Python */
|
|
|
|
static PyObject *completer = NULL;
|
|
|
|
static PyObject *begidx = NULL;
|
|
static PyObject *endidx = NULL;
|
|
|
|
|
|
/* Get the completion type for the scope of the tab-completion */
|
|
static PyObject *
|
|
get_completion_type(PyObject *self, PyObject *noarg)
|
|
{
|
|
return PyInt_FromLong(rl_completion_type);
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_get_completion_type,
|
|
"get_completion_type() -> int\n\
|
|
Get the type of completion being attempted.");
|
|
|
|
|
|
/* Get the beginning index for the scope of the tab-completion */
|
|
|
|
static PyObject *
|
|
get_begidx(PyObject *self, PyObject *noarg)
|
|
{
|
|
Py_INCREF(begidx);
|
|
return begidx;
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_get_begidx,
|
|
"get_begidx() -> int\n\
|
|
get the beginning index of the readline tab-completion scope");
|
|
|
|
|
|
/* Get the ending index for the scope of the tab-completion */
|
|
|
|
static PyObject *
|
|
get_endidx(PyObject *self, PyObject *noarg)
|
|
{
|
|
Py_INCREF(endidx);
|
|
return endidx;
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_get_endidx,
|
|
"get_endidx() -> int\n\
|
|
get the ending index of the readline tab-completion scope");
|
|
|
|
|
|
/* Set the tab-completion word-delimiters that readline uses */
|
|
|
|
static PyObject *
|
|
set_completer_delims(PyObject *self, PyObject *args)
|
|
{
|
|
char *break_chars;
|
|
|
|
if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
|
|
return NULL;
|
|
}
|
|
free((void*)rl_completer_word_break_characters);
|
|
rl_completer_word_break_characters = strdup(break_chars);
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_set_completer_delims,
|
|
"set_completer_delims(string) -> None\n\
|
|
set the readline word delimiters for tab-completion");
|
|
|
|
static PyObject *
|
|
py_remove_history(PyObject *self, PyObject *args)
|
|
{
|
|
int entry_number;
|
|
HIST_ENTRY *entry;
|
|
|
|
if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
|
|
return NULL;
|
|
if (entry_number < 0) {
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"History index cannot be negative");
|
|
return NULL;
|
|
}
|
|
entry = remove_history(entry_number);
|
|
if (!entry) {
|
|
PyErr_Format(PyExc_ValueError,
|
|
"No history item at position %d",
|
|
entry_number);
|
|
return NULL;
|
|
}
|
|
/* free memory allocated for the history entry */
|
|
if (entry->line)
|
|
free(entry->line);
|
|
if (entry->data)
|
|
free(entry->data);
|
|
free(entry);
|
|
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_remove_history,
|
|
"remove_history_item(pos) -> None\n\
|
|
remove history item given by its position");
|
|
|
|
static PyObject *
|
|
py_replace_history(PyObject *self, PyObject *args)
|
|
{
|
|
int entry_number;
|
|
char *line;
|
|
HIST_ENTRY *old_entry;
|
|
|
|
if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number, &line)) {
|
|
return NULL;
|
|
}
|
|
if (entry_number < 0) {
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"History index cannot be negative");
|
|
return NULL;
|
|
}
|
|
old_entry = replace_history_entry(entry_number, line, (void *)NULL);
|
|
if (!old_entry) {
|
|
PyErr_Format(PyExc_ValueError,
|
|
"No history item at position %d",
|
|
entry_number);
|
|
return NULL;
|
|
}
|
|
/* free memory allocated for the old history entry */
|
|
if (old_entry->line)
|
|
free(old_entry->line);
|
|
if (old_entry->data)
|
|
free(old_entry->data);
|
|
free(old_entry);
|
|
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_replace_history,
|
|
"replace_history_item(pos, line) -> None\n\
|
|
replaces history item given by its position with contents of line");
|
|
|
|
/* Add a line to the history buffer */
|
|
|
|
static PyObject *
|
|
py_add_history(PyObject *self, PyObject *args)
|
|
{
|
|
char *line;
|
|
|
|
if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
|
|
return NULL;
|
|
}
|
|
add_history(line);
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_add_history,
|
|
"add_history(string) -> None\n\
|
|
add a line to the history buffer");
|
|
|
|
|
|
/* Get the tab-completion word-delimiters that readline uses */
|
|
|
|
static PyObject *
|
|
get_completer_delims(PyObject *self, PyObject *noarg)
|
|
{
|
|
return PyString_FromString(rl_completer_word_break_characters);
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_get_completer_delims,
|
|
"get_completer_delims() -> string\n\
|
|
get the readline word delimiters for tab-completion");
|
|
|
|
|
|
/* Set the completer function */
|
|
|
|
static PyObject *
|
|
set_completer(PyObject *self, PyObject *args)
|
|
{
|
|
return set_hook("completer", &completer, args);
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_set_completer,
|
|
"set_completer([function]) -> None\n\
|
|
Set or remove the completer function.\n\
|
|
The function is called as function(text, state),\n\
|
|
for state in 0, 1, 2, ..., until it returns a non-string.\n\
|
|
It should return the next possible completion starting with 'text'.");
|
|
|
|
|
|
static PyObject *
|
|
get_completer(PyObject *self, PyObject *noargs)
|
|
{
|
|
if (completer == NULL) {
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
Py_INCREF(completer);
|
|
return completer;
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_get_completer,
|
|
"get_completer() -> function\n\
|
|
\n\
|
|
Returns current completer function.");
|
|
|
|
/* Exported function to get any element of history */
|
|
|
|
static PyObject *
|
|
get_history_item(PyObject *self, PyObject *args)
|
|
{
|
|
int idx = 0;
|
|
HIST_ENTRY *hist_ent;
|
|
|
|
if (!PyArg_ParseTuple(args, "i:index", &idx))
|
|
return NULL;
|
|
if ((hist_ent = history_get(idx)))
|
|
return PyString_FromString(hist_ent->line);
|
|
else {
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_get_history_item,
|
|
"get_history_item() -> string\n\
|
|
return the current contents of history item at index.");
|
|
|
|
|
|
/* Exported function to get current length of history */
|
|
|
|
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);
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_get_current_history_length,
|
|
"get_current_history_length() -> integer\n\
|
|
return the current (not the maximum) length of history.");
|
|
|
|
|
|
/* Exported function to read the current line buffer */
|
|
|
|
static PyObject *
|
|
get_line_buffer(PyObject *self, PyObject *noarg)
|
|
{
|
|
return PyString_FromString(rl_line_buffer);
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_get_line_buffer,
|
|
"get_line_buffer() -> string\n\
|
|
return the current contents of the line buffer.");
|
|
|
|
|
|
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
|
|
|
/* Exported function to clear the current history */
|
|
|
|
static PyObject *
|
|
py_clear_history(PyObject *self, PyObject *noarg)
|
|
{
|
|
clear_history();
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_clear_history,
|
|
"clear_history() -> None\n\
|
|
Clear the current readline history.");
|
|
#endif
|
|
|
|
|
|
/* Exported function to insert text into the line buffer */
|
|
|
|
static PyObject *
|
|
insert_text(PyObject *self, PyObject *args)
|
|
{
|
|
char *s;
|
|
if (!PyArg_ParseTuple(args, "s:insert_text", &s))
|
|
return NULL;
|
|
rl_insert_text(s);
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_insert_text,
|
|
"insert_text(string) -> None\n\
|
|
Insert text into the command line.");
|
|
|
|
|
|
/* Redisplay the line buffer */
|
|
|
|
static PyObject *
|
|
redisplay(PyObject *self, PyObject *noarg)
|
|
{
|
|
rl_redisplay();
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
PyDoc_STRVAR(doc_redisplay,
|
|
"redisplay() -> None\n\
|
|
Change what's displayed on the screen to reflect the current\n\
|
|
contents of the line buffer.");
|
|
|
|
|
|
/* Table of functions exported by the module */
|
|
|
|
static struct PyMethodDef readline_methods[] =
|
|
{
|
|
{"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
|
|
{"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
|
|
{"insert_text", insert_text, METH_VARARGS, doc_insert_text},
|
|
{"redisplay", redisplay, METH_NOARGS, doc_redisplay},
|
|
{"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
|
|
{"read_history_file", read_history_file,
|
|
METH_VARARGS, doc_read_history_file},
|
|
{"write_history_file", write_history_file,
|
|
METH_VARARGS, doc_write_history_file},
|
|
{"get_history_item", get_history_item,
|
|
METH_VARARGS, doc_get_history_item},
|
|
{"get_current_history_length", (PyCFunction)get_current_history_length,
|
|
METH_NOARGS, doc_get_current_history_length},
|
|
{"set_history_length", set_history_length,
|
|
METH_VARARGS, set_history_length_doc},
|
|
{"get_history_length", get_history_length,
|
|
METH_NOARGS, get_history_length_doc},
|
|
{"set_completer", set_completer, METH_VARARGS, doc_set_completer},
|
|
{"get_completer", get_completer, METH_NOARGS, doc_get_completer},
|
|
{"get_completion_type", get_completion_type,
|
|
METH_NOARGS, doc_get_completion_type},
|
|
{"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
|
|
{"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
|
|
|
|
{"set_completer_delims", set_completer_delims,
|
|
METH_VARARGS, doc_set_completer_delims},
|
|
{"add_history", py_add_history, METH_VARARGS, doc_add_history},
|
|
{"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
|
|
{"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
|
|
{"get_completer_delims", get_completer_delims,
|
|
METH_NOARGS, doc_get_completer_delims},
|
|
|
|
{"set_completion_display_matches_hook", set_completion_display_matches_hook,
|
|
METH_VARARGS, doc_set_completion_display_matches_hook},
|
|
{"set_startup_hook", set_startup_hook,
|
|
METH_VARARGS, doc_set_startup_hook},
|
|
#ifdef HAVE_RL_PRE_INPUT_HOOK
|
|
{"set_pre_input_hook", set_pre_input_hook,
|
|
METH_VARARGS, doc_set_pre_input_hook},
|
|
#endif
|
|
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
|
{"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
|
|
#endif
|
|
{0, 0}
|
|
};
|
|
|
|
|
|
/* C function to call the Python hooks. */
|
|
|
|
static int
|
|
on_hook(PyObject *func)
|
|
{
|
|
int result = 0;
|
|
if (func != NULL) {
|
|
PyObject *r;
|
|
#ifdef WITH_THREAD
|
|
PyGILState_STATE gilstate = PyGILState_Ensure();
|
|
#endif
|
|
r = PyObject_CallFunction(func, NULL);
|
|
if (r == NULL)
|
|
goto error;
|
|
if (r == Py_None)
|
|
result = 0;
|
|
else {
|
|
result = PyInt_AsLong(r);
|
|
if (result == -1 && PyErr_Occurred())
|
|
goto error;
|
|
}
|
|
Py_DECREF(r);
|
|
goto done;
|
|
error:
|
|
PyErr_Clear();
|
|
Py_XDECREF(r);
|
|
done:
|
|
#ifdef WITH_THREAD
|
|
PyGILState_Release(gilstate);
|
|
#endif
|
|
return result;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static int
|
|
on_startup_hook(void)
|
|
{
|
|
return on_hook(startup_hook);
|
|
}
|
|
|
|
#ifdef HAVE_RL_PRE_INPUT_HOOK
|
|
static int
|
|
on_pre_input_hook(void)
|
|
{
|
|
return on_hook(pre_input_hook);
|
|
}
|
|
#endif
|
|
|
|
|
|
/* C function to call the Python completion_display_matches */
|
|
|
|
static void
|
|
on_completion_display_matches_hook(char **matches,
|
|
int num_matches, int max_length)
|
|
{
|
|
if (completion_display_matches_hook != NULL) {
|
|
int i;
|
|
PyObject *m, *s;
|
|
PyObject *r;
|
|
#ifdef WITH_THREAD
|
|
PyGILState_STATE gilstate = PyGILState_Ensure();
|
|
#endif
|
|
m = PyList_New(num_matches);
|
|
for (i = 0; i < num_matches; i++) {
|
|
s = PyString_FromString(matches[i+1]);
|
|
PyList_SetItem(m, i, s);
|
|
}
|
|
|
|
r = PyObject_CallFunction(completion_display_matches_hook,
|
|
"sOi", matches[0], m, max_length);
|
|
|
|
Py_DECREF(m);
|
|
|
|
if (r == NULL ||
|
|
(r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
|
|
goto error;
|
|
}
|
|
|
|
Py_DECREF(r);
|
|
goto done;
|
|
error:
|
|
PyErr_Clear();
|
|
Py_XDECREF(r);
|
|
done:
|
|
#ifdef WITH_THREAD
|
|
PyGILState_Release(gilstate);
|
|
#endif
|
|
}
|
|
}
|
|
|
|
|
|
/* C function to call the Python completer. */
|
|
|
|
static char *
|
|
on_completion(const char *text, int state)
|
|
{
|
|
char *result = NULL;
|
|
if (completer != NULL) {
|
|
PyObject *r;
|
|
#ifdef WITH_THREAD
|
|
PyGILState_STATE gilstate = PyGILState_Ensure();
|
|
#endif
|
|
rl_attempted_completion_over = 1;
|
|
r = PyObject_CallFunction(completer, "si", text, state);
|
|
if (r == NULL)
|
|
goto error;
|
|
if (r == Py_None) {
|
|
result = NULL;
|
|
}
|
|
else {
|
|
char *s = PyString_AsString(r);
|
|
if (s == NULL)
|
|
goto error;
|
|
result = strdup(s);
|
|
}
|
|
Py_DECREF(r);
|
|
goto done;
|
|
error:
|
|
PyErr_Clear();
|
|
Py_XDECREF(r);
|
|
done:
|
|
#ifdef WITH_THREAD
|
|
PyGILState_Release(gilstate);
|
|
#endif
|
|
return result;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
/* A more flexible constructor that saves the "begidx" and "endidx"
|
|
* before calling the normal completer */
|
|
|
|
static char **
|
|
flex_complete(char *text, int start, int end)
|
|
{
|
|
Py_XDECREF(begidx);
|
|
Py_XDECREF(endidx);
|
|
begidx = PyInt_FromLong((long) start);
|
|
endidx = PyInt_FromLong((long) end);
|
|
return completion_matches(text, *on_completion);
|
|
}
|
|
|
|
|
|
/* Helper to initialize GNU readline properly. */
|
|
|
|
static void
|
|
setup_readline(void)
|
|
{
|
|
#ifdef SAVE_LOCALE
|
|
char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
|
|
if (!saved_locale)
|
|
Py_FatalError("not enough memory to save locale");
|
|
#endif
|
|
|
|
using_history();
|
|
|
|
rl_readline_name = "python";
|
|
#if defined(PYOS_OS2) && defined(PYCC_GCC)
|
|
/* Allow $if term= in .inputrc to work */
|
|
rl_terminal_name = getenv("TERM");
|
|
#endif
|
|
/* Force rebind of TAB to insert-tab */
|
|
rl_bind_key('\t', rl_insert);
|
|
/* Bind both ESC-TAB and ESC-ESC to the completion function */
|
|
rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
|
|
rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
|
|
/* Set our hook functions */
|
|
#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
|
|
rl_completion_display_matches_hook =
|
|
(rl_compdisp_func_t *)on_completion_display_matches_hook;
|
|
#endif
|
|
rl_startup_hook = (Function *)on_startup_hook;
|
|
#ifdef HAVE_RL_PRE_INPUT_HOOK
|
|
rl_pre_input_hook = (Function *)on_pre_input_hook;
|
|
#endif
|
|
/* Set our completion function */
|
|
rl_attempted_completion_function = (CPPFunction *)flex_complete;
|
|
/* Set Python word break characters */
|
|
rl_completer_word_break_characters =
|
|
strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
|
|
/* All nonalphanums except '.' */
|
|
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
|
rl_completion_append_character ='\0';
|
|
#endif
|
|
|
|
begidx = PyInt_FromLong(0L);
|
|
endidx = PyInt_FromLong(0L);
|
|
/* Initialize (allows .inputrc to override)
|
|
*
|
|
* XXX: A bug in the readline-2.2 library causes a memory leak
|
|
* inside this function. Nothing we can do about it.
|
|
*/
|
|
rl_initialize();
|
|
|
|
RESTORE_LOCALE(saved_locale)
|
|
}
|
|
|
|
/* Wrapper around GNU readline that handles signals differently. */
|
|
|
|
|
|
#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
|
|
|
|
static char *completed_input_string;
|
|
static void
|
|
rlhandler(char *text)
|
|
{
|
|
completed_input_string = text;
|
|
rl_callback_handler_remove();
|
|
}
|
|
|
|
extern PyThreadState* _PyOS_ReadlineTState;
|
|
|
|
static char *
|
|
readline_until_enter_or_signal(char *prompt, int *signal)
|
|
{
|
|
char * not_done_reading = "";
|
|
fd_set selectset;
|
|
|
|
*signal = 0;
|
|
#ifdef HAVE_RL_CATCH_SIGNAL
|
|
rl_catch_signals = 0;
|
|
#endif
|
|
|
|
rl_callback_handler_install (prompt, rlhandler);
|
|
FD_ZERO(&selectset);
|
|
|
|
completed_input_string = not_done_reading;
|
|
|
|
while (completed_input_string == not_done_reading) {
|
|
int has_input = 0;
|
|
|
|
while (!has_input)
|
|
{ struct timeval timeout = {0, 100000}; /* 0.1 seconds */
|
|
|
|
/* [Bug #1552726] Only limit the pause if an input hook has been
|
|
defined. */
|
|
struct timeval *timeoutp = NULL;
|
|
if (PyOS_InputHook)
|
|
timeoutp = &timeout;
|
|
FD_SET(fileno(rl_instream), &selectset);
|
|
/* select resets selectset if no input was available */
|
|
has_input = select(fileno(rl_instream) + 1, &selectset,
|
|
NULL, NULL, timeoutp);
|
|
if(PyOS_InputHook) PyOS_InputHook();
|
|
}
|
|
|
|
if(has_input > 0) {
|
|
rl_callback_read_char();
|
|
}
|
|
else if (errno == EINTR) {
|
|
int s;
|
|
#ifdef WITH_THREAD
|
|
PyEval_RestoreThread(_PyOS_ReadlineTState);
|
|
#endif
|
|
s = PyErr_CheckSignals();
|
|
#ifdef WITH_THREAD
|
|
PyEval_SaveThread();
|
|
#endif
|
|
if (s < 0) {
|
|
rl_free_line_state();
|
|
rl_cleanup_after_signal();
|
|
rl_callback_handler_remove();
|
|
*signal = 1;
|
|
completed_input_string = NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
return completed_input_string;
|
|
}
|
|
|
|
|
|
#else
|
|
|
|
/* Interrupt handler */
|
|
|
|
static jmp_buf jbuf;
|
|
|
|
/* ARGSUSED */
|
|
static void
|
|
onintr(int sig)
|
|
{
|
|
longjmp(jbuf, 1);
|
|
}
|
|
|
|
|
|
static char *
|
|
readline_until_enter_or_signal(char *prompt, int *signal)
|
|
{
|
|
PyOS_sighandler_t old_inthandler;
|
|
char *p;
|
|
|
|
*signal = 0;
|
|
|
|
old_inthandler = PyOS_setsig(SIGINT, onintr);
|
|
if (setjmp(jbuf)) {
|
|
#ifdef HAVE_SIGRELSE
|
|
/* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
|
|
sigrelse(SIGINT);
|
|
#endif
|
|
PyOS_setsig(SIGINT, old_inthandler);
|
|
*signal = 1;
|
|
return NULL;
|
|
}
|
|
rl_event_hook = PyOS_InputHook;
|
|
p = readline(prompt);
|
|
PyOS_setsig(SIGINT, old_inthandler);
|
|
|
|
return p;
|
|
}
|
|
#endif /*defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT) */
|
|
|
|
|
|
static char *
|
|
call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
|
|
{
|
|
size_t n;
|
|
char *p, *q;
|
|
int signal;
|
|
|
|
#ifdef SAVE_LOCALE
|
|
char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
|
|
if (!saved_locale)
|
|
Py_FatalError("not enough memory to save locale");
|
|
setlocale(LC_CTYPE, "");
|
|
#endif
|
|
|
|
if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
|
|
rl_instream = sys_stdin;
|
|
rl_outstream = sys_stdout;
|
|
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
|
rl_prep_terminal (1);
|
|
#endif
|
|
}
|
|
|
|
p = readline_until_enter_or_signal(prompt, &signal);
|
|
|
|
/* we got an interrupt signal */
|
|
if (signal) {
|
|
RESTORE_LOCALE(saved_locale)
|
|
return NULL;
|
|
}
|
|
|
|
/* We got an EOF, return a empty string. */
|
|
if (p == NULL) {
|
|
p = PyMem_Malloc(1);
|
|
if (p != NULL)
|
|
*p = '\0';
|
|
RESTORE_LOCALE(saved_locale)
|
|
return p;
|
|
}
|
|
|
|
/* we have a valid line */
|
|
n = strlen(p);
|
|
if (n > 0) {
|
|
char *line;
|
|
HISTORY_STATE *state = history_get_history_state();
|
|
if (state->length > 0)
|
|
line = history_get(state->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. */
|
|
q = p;
|
|
p = PyMem_Malloc(n+2);
|
|
if (p != NULL) {
|
|
strncpy(p, q, n);
|
|
p[n] = '\n';
|
|
p[n+1] = '\0';
|
|
}
|
|
free(q);
|
|
RESTORE_LOCALE(saved_locale)
|
|
return p;
|
|
}
|
|
|
|
|
|
/* Initialize the module */
|
|
|
|
PyDoc_STRVAR(doc_module,
|
|
"Importing this module enables command line editing using GNU readline.");
|
|
|
|
PyMODINIT_FUNC
|
|
initreadline(void)
|
|
{
|
|
PyObject *m;
|
|
|
|
m = Py_InitModule4("readline", readline_methods, doc_module,
|
|
(PyObject *)NULL, PYTHON_API_VERSION);
|
|
if (m == NULL)
|
|
return;
|
|
|
|
PyOS_ReadlineFunctionPointer = call_readline;
|
|
setup_readline();
|
|
}
|