Ignore:
Timestamp:
Mar 19, 2014, 11:11:30 AM (11 years ago)
Author:
dmik
Message:

python: Update vendor to 2.7.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/vendor/current/Modules/readline.c

    r2 r388  
    2424#  define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
    2525#else
    26 #  define RESTORE_LOCALE(sl) 
     26#  define RESTORE_LOCALE(sl)
    2727#endif
    2828
     
    3434#ifdef HAVE_RL_COMPLETION_MATCHES
    3535#define completion_matches(x, y) \
    36         rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
     36    rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
    3737#else
    3838#if defined(_RL_FUNCTION_TYPEDEF)
    3939extern char **completion_matches(char *, rl_compentry_func_t *);
    4040#else
     41
    4142#if !defined(__APPLE__)
    4243extern char **completion_matches(char *, CPFunction *);
     
    4849/*
    4950 * It is possible to link the readline module to the readline
    50  * emulation library of editline/libedit. 
    51  * 
     51 * emulation library of editline/libedit.
     52 *
    5253 * On OSX this emulation library is not 100% API compatible
    5354 * with the "real" readline and cannot be detected at compile-time,
    5455 * hence we use a runtime check to detect if we're using libedit
    5556 *
    56  * Currently there is one know API incompatibility:
     57 * Currently there is one known API incompatibility:
    5758 * - 'get_history' has a 1-based index with GNU readline, and a 0-based
    58  *   index with libedit's emulation.
     59 *   index with older versions of libedit's emulation.
    5960 * - Note that replace_history and remove_history use a 0-based index
    60  *   with both implementation.
     61 *   with both implementations.
    6162 */
    6263static int using_libedit_emulation = 0;
    6364static const char libedit_version_tag[] = "EditLine wrapper";
     65
     66static int libedit_history_start = 0;
    6467#endif /* __APPLE__ */
    6568
    6669static void
    6770on_completion_display_matches_hook(char **matches,
    68                                    int num_matches, int max_length);
    69 
     71                                   int num_matches, int max_length);
     72
     73
     74/* Memory allocated for rl_completer_word_break_characters
     75   (see issue #17289 for the motivation). */
     76static char *completer_word_break_characters;
    7077
    7178/* Exported function to send one line to readline's init file parser */
     
    7481parse_and_bind(PyObject *self, PyObject *args)
    7582{
    76         char *s, *copy;
    77         if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
    78                 return NULL;
    79         /* Make a copy -- rl_parse_and_bind() modifies its argument */
    80         /* Bernard Herzog */
    81         copy = malloc(1 + strlen(s));
    82         if (copy == NULL)
    83                 return PyErr_NoMemory();
    84         strcpy(copy, s);
    85         rl_parse_and_bind(copy);
    86         free(copy); /* Free the copy */
    87         Py_RETURN_NONE;
     83    char *s, *copy;
     84    if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
     85        return NULL;
     86    /* Make a copy -- rl_parse_and_bind() modifies its argument */
     87    /* Bernard Herzog */
     88    copy = malloc(1 + strlen(s));
     89    if (copy == NULL)
     90        return PyErr_NoMemory();
     91    strcpy(copy, s);
     92    rl_parse_and_bind(copy);
     93    free(copy); /* Free the copy */
     94    Py_RETURN_NONE;
    8895}
    8996
     
    98105read_init_file(PyObject *self, PyObject *args)
    99106{
    100         char *s = NULL;
    101         if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
    102                 return NULL;
    103         errno = rl_read_init_file(s);
    104         if (errno)
    105                 return PyErr_SetFromErrno(PyExc_IOError);
    106         Py_RETURN_NONE;
     107    char *s = NULL;
     108    if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
     109        return NULL;
     110    errno = rl_read_init_file(s);
     111    if (errno)
     112        return PyErr_SetFromErrno(PyExc_IOError);
     113    Py_RETURN_NONE;
    107114}
    108115
     
    118125read_history_file(PyObject *self, PyObject *args)
    119126{
    120         char *s = NULL;
    121         if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
    122                 return NULL;
    123         errno = read_history(s);
    124         if (errno)
    125                 return PyErr_SetFromErrno(PyExc_IOError);
    126         Py_RETURN_NONE;
     127    char *s = NULL;
     128    if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
     129        return NULL;
     130    errno = read_history(s);
     131    if (errno)
     132        return PyErr_SetFromErrno(PyExc_IOError);
     133    Py_RETURN_NONE;
    127134}
    128135
     
    139146write_history_file(PyObject *self, PyObject *args)
    140147{
    141         char *s = NULL;
    142         if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
    143                 return NULL;
    144         errno = write_history(s);
    145         if (!errno && _history_length >= 0)
    146                 history_truncate_file(s, _history_length);
    147         if (errno)
    148                 return PyErr_SetFromErrno(PyExc_IOError);
    149         Py_RETURN_NONE;
     148    char *s = NULL;
     149    if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
     150        return NULL;
     151    errno = write_history(s);
     152    if (!errno && _history_length >= 0)
     153        history_truncate_file(s, _history_length);
     154    if (errno)
     155        return PyErr_SetFromErrno(PyExc_IOError);
     156    Py_RETURN_NONE;
    150157}
    151158
     
    161168set_history_length(PyObject *self, PyObject *args)
    162169{
    163         int length = _history_length;
    164         if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
    165                 return NULL;
    166         _history_length = length;
    167         Py_RETURN_NONE;
     170    int length = _history_length;
     171    if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
     172        return NULL;
     173    _history_length = length;
     174    Py_RETURN_NONE;
    168175}
    169176
     
    180187get_history_length(PyObject *self, PyObject *noarg)
    181188{
    182         return PyInt_FromLong(_history_length);
     189    return PyInt_FromLong(_history_length);
    183190}
    184191
     
    194201set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
    195202{
    196         PyObject *function = Py_None;
    197         char buf[80];
    198         PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
    199         if (!PyArg_ParseTuple(args, buf, &function))
    200                 return NULL;
    201         if (function == Py_None) {
    202                 Py_XDECREF(*hook_var);
    203                 *hook_var = NULL;
    204         }
    205         else if (PyCallable_Check(function)) {
    206                 PyObject *tmp = *hook_var;
    207                 Py_INCREF(function);
    208                 *hook_var = function;
    209                 Py_XDECREF(tmp);
    210         }
    211         else {
    212                 PyOS_snprintf(buf, sizeof(buf),
    213                               "set_%.50s(func): argument not callable",
    214                               funcname);
    215                 PyErr_SetString(PyExc_TypeError, buf);
    216                 return NULL;
    217         }
    218         Py_RETURN_NONE;
     203    PyObject *function = Py_None;
     204    char buf[80];
     205    PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
     206    if (!PyArg_ParseTuple(args, buf, &function))
     207        return NULL;
     208    if (function == Py_None) {
     209        Py_XDECREF(*hook_var);
     210        *hook_var = NULL;
     211    }
     212    else if (PyCallable_Check(function)) {
     213        PyObject *tmp = *hook_var;
     214        Py_INCREF(function);
     215        *hook_var = function;
     216        Py_XDECREF(tmp);
     217    }
     218    else {
     219        PyOS_snprintf(buf, sizeof(buf),
     220                      "set_%.50s(func): argument not callable",
     221                      funcname);
     222        PyErr_SetString(PyExc_TypeError, buf);
     223        return NULL;
     224    }
     225    Py_RETURN_NONE;
    219226}
    220227
     
    232239set_completion_display_matches_hook(PyObject *self, PyObject *args)
    233240{
    234         PyObject *result = set_hook("completion_display_matches_hook",
    235                         &completion_display_matches_hook, args);
     241    PyObject *result = set_hook("completion_display_matches_hook",
     242                    &completion_display_matches_hook, args);
    236243#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
    237         /* We cannot set this hook globally, since it replaces the
    238            default completion display. */
    239         rl_completion_display_matches_hook =
    240                 completion_display_matches_hook ?
     244    /* We cannot set this hook globally, since it replaces the
     245       default completion display. */
     246    rl_completion_display_matches_hook =
     247        completion_display_matches_hook ?
    241248#if defined(_RL_FUNCTION_TYPEDEF)
    242                 (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
     249        (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
    243250#else
    244                 (VFunction *)on_completion_display_matches_hook : 0;
    245 #endif
    246 #endif
    247         return result;
     251        (VFunction *)on_completion_display_matches_hook : 0;
     252#endif
     253#endif
     254    return result;
    248255
    249256}
     
    259266set_startup_hook(PyObject *self, PyObject *args)
    260267{
    261         return set_hook("startup_hook", &startup_hook, args);
     268    return set_hook("startup_hook", &startup_hook, args);
    262269}
    263270
     
    276283set_pre_input_hook(PyObject *self, PyObject *args)
    277284{
    278         return set_hook("pre_input_hook", &pre_input_hook, args);
     285    return set_hook("pre_input_hook", &pre_input_hook, args);
    279286}
    280287
     
    314321get_begidx(PyObject *self, PyObject *noarg)
    315322{
    316         Py_INCREF(begidx);
    317         return begidx;
     323    Py_INCREF(begidx);
     324    return begidx;
    318325}
    319326
     
    328335get_endidx(PyObject *self, PyObject *noarg)
    329336{
    330         Py_INCREF(endidx);
    331         return endidx;
     337    Py_INCREF(endidx);
     338    return endidx;
    332339}
    333340
     
    342349set_completer_delims(PyObject *self, PyObject *args)
    343350{
    344         char *break_chars;
    345 
    346         if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
    347                 return NULL;
    348         }
    349         free((void*)rl_completer_word_break_characters);
    350         rl_completer_word_break_characters = strdup(break_chars);
    351         Py_RETURN_NONE;
     351    char *break_chars;
     352
     353    if (!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
     354        return NULL;
     355    }
     356    /* Keep a reference to the allocated memory in the module state in case
     357       some other module modifies rl_completer_word_break_characters
     358       (see issue #17289). */
     359    free(completer_word_break_characters);
     360    completer_word_break_characters = strdup(break_chars);
     361    if (completer_word_break_characters) {
     362        rl_completer_word_break_characters = completer_word_break_characters;
     363        Py_RETURN_NONE;
     364    }
     365    else
     366        return PyErr_NoMemory();
    352367}
    353368
     
    356371set the readline word delimiters for tab-completion");
    357372
     373/* _py_free_history_entry: Utility function to free a history entry. */
     374
     375#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
     376
     377/* Readline version >= 5.0 introduced a timestamp field into the history entry
     378   structure; this needs to be freed to avoid a memory leak.  This version of
     379   readline also introduced the handy 'free_history_entry' function, which
     380   takes care of the timestamp. */
     381
     382static void
     383_py_free_history_entry(HIST_ENTRY *entry)
     384{
     385    histdata_t data = free_history_entry(entry);
     386    free(data);
     387}
     388
     389#else
     390
     391/* No free_history_entry function;  free everything manually. */
     392
     393static void
     394_py_free_history_entry(HIST_ENTRY *entry)
     395{
     396    if (entry->line)
     397        free((void *)entry->line);
     398    if (entry->data)
     399        free(entry->data);
     400    free(entry);
     401}
     402
     403#endif
     404
    358405static PyObject *
    359406py_remove_history(PyObject *self, PyObject *args)
    360407{
    361         int entry_number;
    362         HIST_ENTRY *entry;
    363 
    364         if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
    365                 return NULL;
    366         if (entry_number < 0) {
    367                 PyErr_SetString(PyExc_ValueError,
    368                                 "History index cannot be negative");
    369                 return NULL;
    370         }
    371         entry = remove_history(entry_number);
    372         if (!entry) {
    373                 PyErr_Format(PyExc_ValueError,
    374                              "No history item at position %d",
    375                               entry_number);
    376                 return NULL;
    377         }
    378         /* free memory allocated for the history entry */
    379         if (entry->line)
    380                 free(entry->line);
    381         if (entry->data)
    382                 free(entry->data);
    383         free(entry);
    384 
    385         Py_RETURN_NONE;
     408    int entry_number;
     409    HIST_ENTRY *entry;
     410
     411    if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
     412        return NULL;
     413    if (entry_number < 0) {
     414        PyErr_SetString(PyExc_ValueError,
     415                        "History index cannot be negative");
     416        return NULL;
     417    }
     418    entry = remove_history(entry_number);
     419    if (!entry) {
     420        PyErr_Format(PyExc_ValueError,
     421                     "No history item at position %d",
     422                      entry_number);
     423        return NULL;
     424    }
     425    /* free memory allocated for the history entry */
     426    _py_free_history_entry(entry);
     427    Py_RETURN_NONE;
    386428}
    387429
     
    393435py_replace_history(PyObject *self, PyObject *args)
    394436{
    395         int entry_number;
    396         char *line;
    397         HIST_ENTRY *old_entry;
    398 
    399         if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
    400                               &line)) {
    401                 return NULL;
    402         }
    403         if (entry_number < 0) {
    404                 PyErr_SetString(PyExc_ValueError,
    405                                 "History index cannot be negative");
    406                 return NULL;
    407         }
    408         old_entry = replace_history_entry(entry_number, line, (void *)NULL);
    409         if (!old_entry) {
    410                 PyErr_Format(PyExc_ValueError,
    411                              "No history item at position %d",
    412                              entry_number);
    413                 return NULL;
    414         }
    415         /* free memory allocated for the old history entry */
    416         if (old_entry->line)
    417             free(old_entry->line);
    418         if (old_entry->data)
    419             free(old_entry->data);
    420         free(old_entry);
    421 
    422         Py_RETURN_NONE;
     437    int entry_number;
     438    char *line;
     439    HIST_ENTRY *old_entry;
     440
     441    if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
     442                          &line)) {
     443        return NULL;
     444    }
     445    if (entry_number < 0) {
     446        PyErr_SetString(PyExc_ValueError,
     447                        "History index cannot be negative");
     448        return NULL;
     449    }
     450    old_entry = replace_history_entry(entry_number, line, (void *)NULL);
     451    if (!old_entry) {
     452        PyErr_Format(PyExc_ValueError,
     453                     "No history item at position %d",
     454                     entry_number);
     455        return NULL;
     456    }
     457    /* free memory allocated for the old history entry */
     458    _py_free_history_entry(old_entry);
     459    Py_RETURN_NONE;
    423460}
    424461
     
    432469py_add_history(PyObject *self, PyObject *args)
    433470{
    434         char *line;
    435 
    436         if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
    437                 return NULL;
    438         }
    439         add_history(line);
    440         Py_RETURN_NONE;
     471    char *line;
     472
     473    if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
     474        return NULL;
     475    }
     476    add_history(line);
     477    Py_RETURN_NONE;
    441478}
    442479
     
    451488get_completer_delims(PyObject *self, PyObject *noarg)
    452489{
    453         return PyString_FromString(rl_completer_word_break_characters);
     490    return PyString_FromString(rl_completer_word_break_characters);
    454491}
    455492
     
    464501set_completer(PyObject *self, PyObject *args)
    465502{
    466         return set_hook("completer", &completer, args);
     503    return set_hook("completer", &completer, args);
    467504}
    468505
     
    478515get_completer(PyObject *self, PyObject *noargs)
    479516{
    480         if (completer == NULL) {
    481                 Py_RETURN_NONE;
    482         }
    483         Py_INCREF(completer);
    484         return completer;
     517    if (completer == NULL) {
     518        Py_RETURN_NONE;
     519    }
     520    Py_INCREF(completer);
     521    return completer;
    485522}
    486523
     
    490527Returns current completer function.");
    491528
     529/* Private function to get current length of history.  XXX It may be
     530 * possible to replace this with a direct use of history_length instead,
     531 * but it's not clear whether BSD's libedit keeps history_length up to date.
     532 * See issue #8065.*/
     533
     534static int
     535_py_get_history_length(void)
     536{
     537    HISTORY_STATE *hist_st = history_get_history_state();
     538    int length = hist_st->length;
     539    /* the history docs don't say so, but the address of hist_st changes each
     540       time history_get_history_state is called which makes me think it's
     541       freshly malloc'd memory...  on the other hand, the address of the last
     542       line stays the same as long as history isn't extended, so it appears to
     543       be malloc'd but managed by the history package... */
     544    free(hist_st);
     545    return length;
     546}
     547
    492548/* Exported function to get any element of history */
    493549
     
    495551get_history_item(PyObject *self, PyObject *args)
    496552{
    497         int idx = 0;
    498         HIST_ENTRY *hist_ent;
    499 
    500         if (!PyArg_ParseTuple(args, "i:index", &idx))
    501                 return NULL;
     553    int idx = 0;
     554    HIST_ENTRY *hist_ent;
     555
     556    if (!PyArg_ParseTuple(args, "i:index", &idx))
     557        return NULL;
    502558#ifdef  __APPLE__
    503         if (using_libedit_emulation) {
    504                 /* Libedit emulation uses 0-based indexes,
    505                  * the real one uses 1-based indexes,
    506                  * adjust the index to ensure that Python
    507                  * code doesn't have to worry about the
    508                  * difference.
    509                  */
    510                 HISTORY_STATE *hist_st;
    511                 hist_st = history_get_history_state();
    512 
    513                 idx --;
    514 
    515                 /*
    516                  * Apple's readline emulation crashes when
    517                  * the index is out of range, therefore
    518                  * test for that and fail gracefully.
    519                  */
    520                 if (idx < 0 || idx >= hist_st->length) {
    521                         Py_RETURN_NONE;
    522                 }
    523         }
     559    if (using_libedit_emulation) {
     560        /* Older versions of libedit's readline emulation
     561         * use 0-based indexes, while readline and newer
     562         * versions of libedit use 1-based indexes.
     563         */
     564        int length = _py_get_history_length();
     565
     566        idx = idx - 1 + libedit_history_start;
     567
     568        /*
     569         * Apple's readline emulation crashes when
     570         * the index is out of range, therefore
     571         * test for that and fail gracefully.
     572         */
     573        if (idx < (0 + libedit_history_start)
     574                || idx >= (length + libedit_history_start)) {
     575            Py_RETURN_NONE;
     576        }
     577    }
    524578#endif /* __APPLE__ */
    525         if ((hist_ent = history_get(idx)))
    526                 return PyString_FromString(hist_ent->line);
    527         else {
    528                 Py_RETURN_NONE;
    529         }
     579    if ((hist_ent = history_get(idx)))
     580        return PyString_FromString(hist_ent->line);
     581    else {
     582        Py_RETURN_NONE;
     583    }
    530584}
    531585
     
    540594get_current_history_length(PyObject *self, PyObject *noarg)
    541595{
    542         HISTORY_STATE *hist_st;
    543 
    544         hist_st = history_get_history_state();
    545         return PyInt_FromLong(hist_st ? (long) hist_st->length : (long) 0);
     596    return PyInt_FromLong((long)_py_get_history_length());
    546597}
    547598
     
    556607get_line_buffer(PyObject *self, PyObject *noarg)
    557608{
    558         return PyString_FromString(rl_line_buffer);
     609    return PyString_FromString(rl_line_buffer);
    559610}
    560611
     
    571622py_clear_history(PyObject *self, PyObject *noarg)
    572623{
    573         clear_history();
    574         Py_RETURN_NONE;
     624    clear_history();
     625    Py_RETURN_NONE;
    575626}
    576627
     
    586637insert_text(PyObject *self, PyObject *args)
    587638{
    588         char *s;
    589         if (!PyArg_ParseTuple(args, "s:insert_text", &s))
    590                 return NULL;
    591         rl_insert_text(s);
    592         Py_RETURN_NONE;
     639    char *s;
     640    if (!PyArg_ParseTuple(args, "s:insert_text", &s))
     641        return NULL;
     642    rl_insert_text(s);
     643    Py_RETURN_NONE;
    593644}
    594645
     
    603654redisplay(PyObject *self, PyObject *noarg)
    604655{
    605         rl_redisplay();
    606         Py_RETURN_NONE;
     656    rl_redisplay();
     657    Py_RETURN_NONE;
    607658}
    608659
     
    617668static struct PyMethodDef readline_methods[] =
    618669{
    619         {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
    620         {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
    621         {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
    622         {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
    623         {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
    624         {"read_history_file", read_history_file,
    625         METH_VARARGS, doc_read_history_file},
    626         {"write_history_file", write_history_file,
    627         METH_VARARGS, doc_write_history_file},
    628         {"get_history_item", get_history_item,
    629         METH_VARARGS, doc_get_history_item},
    630         {"get_current_history_length", (PyCFunction)get_current_history_length,
    631         METH_NOARGS, doc_get_current_history_length},
    632         {"set_history_length", set_history_length,
    633         METH_VARARGS, set_history_length_doc},
    634         {"get_history_length", get_history_length,
    635         METH_NOARGS, get_history_length_doc},
    636         {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
    637         {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
    638         {"get_completion_type", get_completion_type,
    639         METH_NOARGS, doc_get_completion_type},
    640         {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
    641         {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
    642 
    643         {"set_completer_delims", set_completer_delims,
    644         METH_VARARGS, doc_set_completer_delims},
    645         {"add_history", py_add_history, METH_VARARGS, doc_add_history},
    646         {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
    647         {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
    648         {"get_completer_delims", get_completer_delims,
    649         METH_NOARGS, doc_get_completer_delims},
    650 
    651         {"set_completion_display_matches_hook", set_completion_display_matches_hook,
    652         METH_VARARGS, doc_set_completion_display_matches_hook},
    653         {"set_startup_hook", set_startup_hook,
    654         METH_VARARGS, doc_set_startup_hook},
     670    {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
     671    {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
     672    {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
     673    {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
     674    {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
     675    {"read_history_file", read_history_file,
     676    METH_VARARGS, doc_read_history_file},
     677    {"write_history_file", write_history_file,
     678    METH_VARARGS, doc_write_history_file},
     679    {"get_history_item", get_history_item,
     680    METH_VARARGS, doc_get_history_item},
     681    {"get_current_history_length", (PyCFunction)get_current_history_length,
     682    METH_NOARGS, doc_get_current_history_length},
     683    {"set_history_length", set_history_length,
     684    METH_VARARGS, set_history_length_doc},
     685    {"get_history_length", get_history_length,
     686    METH_NOARGS, get_history_length_doc},
     687    {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
     688    {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
     689    {"get_completion_type", get_completion_type,
     690    METH_NOARGS, doc_get_completion_type},
     691    {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
     692    {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
     693
     694    {"set_completer_delims", set_completer_delims,
     695    METH_VARARGS, doc_set_completer_delims},
     696    {"add_history", py_add_history, METH_VARARGS, doc_add_history},
     697    {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
     698    {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
     699    {"get_completer_delims", get_completer_delims,
     700    METH_NOARGS, doc_get_completer_delims},
     701
     702    {"set_completion_display_matches_hook", set_completion_display_matches_hook,
     703    METH_VARARGS, doc_set_completion_display_matches_hook},
     704    {"set_startup_hook", set_startup_hook,
     705    METH_VARARGS, doc_set_startup_hook},
    655706#ifdef HAVE_RL_PRE_INPUT_HOOK
    656         {"set_pre_input_hook", set_pre_input_hook,
    657         METH_VARARGS, doc_set_pre_input_hook},
     707    {"set_pre_input_hook", set_pre_input_hook,
     708    METH_VARARGS, doc_set_pre_input_hook},
    658709#endif
    659710#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
    660         {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
    661 #endif
    662         {0, 0}
     711    {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
     712#endif
     713    {0, 0}
    663714};
    664715
     
    669720on_hook(PyObject *func)
    670721{
    671         int result = 0;
    672         if (func != NULL) {
    673                 PyObject *r;
     722    int result = 0;
     723    if (func != NULL) {
     724        PyObject *r;
    674725#ifdef WITH_THREAD
    675                 PyGILState_STATE gilstate = PyGILState_Ensure();
    676 #endif
    677                 r = PyObject_CallFunction(func, NULL);
    678                 if (r == NULL)
    679                         goto error;
    680                 if (r == Py_None)
    681                         result = 0;
    682                 else {
    683                         result = PyInt_AsLong(r);
    684                         if (result == -1 && PyErr_Occurred())
    685                                 goto error;
    686                 }
    687                 Py_DECREF(r);
    688                 goto done;
    689           error:
    690                 PyErr_Clear();
    691                 Py_XDECREF(r);
    692           done:
     726        PyGILState_STATE gilstate = PyGILState_Ensure();
     727#endif
     728        r = PyObject_CallFunction(func, NULL);
     729        if (r == NULL)
     730            goto error;
     731        if (r == Py_None)
     732            result = 0;
     733        else {
     734            result = PyInt_AsLong(r);
     735            if (result == -1 && PyErr_Occurred())
     736                goto error;
     737        }
     738        Py_DECREF(r);
     739        goto done;
     740      error:
     741        PyErr_Clear();
     742        Py_XDECREF(r);
     743      done:
    693744#ifdef WITH_THREAD
    694                 PyGILState_Release(gilstate);
    695 #endif
    696                 return result;
    697         }
    698         return result;
     745        PyGILState_Release(gilstate);
     746#endif
     747        return result;
     748    }
     749    return result;
    699750}
    700751
     
    702753on_startup_hook(void)
    703754{
    704         return on_hook(startup_hook);
     755    return on_hook(startup_hook);
    705756}
    706757
     
    709760on_pre_input_hook(void)
    710761{
    711         return on_hook(pre_input_hook);
     762    return on_hook(pre_input_hook);
    712763}
    713764#endif
     
    718769static void
    719770on_completion_display_matches_hook(char **matches,
    720                                    int num_matches, int max_length)
    721 {
    722         int i;
    723         PyObject *m=NULL, *s=NULL, *r=NULL;
     771                                   int num_matches, int max_length)
     772{
     773    int i;
     774    PyObject *m=NULL, *s=NULL, *r=NULL;
    724775#ifdef WITH_THREAD
    725         PyGILState_STATE gilstate = PyGILState_Ensure();
    726 #endif
    727         m = PyList_New(num_matches);
    728         if (m == NULL)
    729                 goto error;
    730         for (i = 0; i < num_matches; i++) {
    731                 s = PyString_FromString(matches[i+1]);
    732                 if (s == NULL)
    733                         goto error;
    734                 if (PyList_SetItem(m, i, s) == -1)
    735                         goto error;
    736         }
    737 
    738         r = PyObject_CallFunction(completion_display_matches_hook,
    739                                   "sOi", matches[0], m, max_length);
    740 
    741         Py_DECREF(m), m=NULL;
    742        
    743         if (r == NULL ||
    744             (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
    745                 goto error;
    746         }
    747         Py_XDECREF(r), r=NULL;
    748 
    749         if (0) {
    750         error:
    751                 PyErr_Clear();
    752                 Py_XDECREF(m);
    753                 Py_XDECREF(r);
    754         }
     776    PyGILState_STATE gilstate = PyGILState_Ensure();
     777#endif
     778    m = PyList_New(num_matches);
     779    if (m == NULL)
     780        goto error;
     781    for (i = 0; i < num_matches; i++) {
     782        s = PyString_FromString(matches[i+1]);
     783        if (s == NULL)
     784            goto error;
     785        if (PyList_SetItem(m, i, s) == -1)
     786            goto error;
     787    }
     788
     789    r = PyObject_CallFunction(completion_display_matches_hook,
     790                              "sOi", matches[0], m, max_length);
     791
     792    Py_DECREF(m); m=NULL;
     793
     794    if (r == NULL ||
     795        (r != Py_None && PyInt_AsLong(r) == -1 && PyErr_Occurred())) {
     796        goto error;
     797    }
     798    Py_XDECREF(r); r=NULL;
     799
     800    if (0) {
     801    error:
     802        PyErr_Clear();
     803        Py_XDECREF(m);
     804        Py_XDECREF(r);
     805    }
    755806#ifdef WITH_THREAD
    756         PyGILState_Release(gilstate);
     807    PyGILState_Release(gilstate);
    757808#endif
    758809}
     
    764815on_completion(const char *text, int state)
    765816{
    766         char *result = NULL;
    767         if (completer != NULL) {
    768                 PyObject *r;
    769 #ifdef WITH_THREAD           
    770                 PyGILState_STATE gilstate = PyGILState_Ensure();
    771 #endif
    772                 rl_attempted_completion_over = 1;
    773                 r = PyObject_CallFunction(completer, "si", text, state);
    774                 if (r == NULL)
    775                         goto error;
    776                 if (r == Py_None) {
    777                         result = NULL;
    778                 }
    779                 else {
    780                         char *s = PyString_AsString(r);
    781                         if (s == NULL)
    782                                 goto error;
    783                         result = strdup(s);
    784                 }
    785                 Py_DECREF(r);
    786                 goto done;
    787           error:
    788                 PyErr_Clear();
    789                 Py_XDECREF(r);
    790           done:
    791 #ifdef WITH_THREAD           
    792                 PyGILState_Release(gilstate);
    793 #endif
    794                 return result;
    795         }
    796         return result;
     817    char *result = NULL;
     818    if (completer != NULL) {
     819        PyObject *r;
     820#ifdef WITH_THREAD
     821        PyGILState_STATE gilstate = PyGILState_Ensure();
     822#endif
     823        rl_attempted_completion_over = 1;
     824        r = PyObject_CallFunction(completer, "si", text, state);
     825        if (r == NULL)
     826            goto error;
     827        if (r == Py_None) {
     828            result = NULL;
     829        }
     830        else {
     831            char *s = PyString_AsString(r);
     832            if (s == NULL)
     833                goto error;
     834            result = strdup(s);
     835        }
     836        Py_DECREF(r);
     837        goto done;
     838      error:
     839        PyErr_Clear();
     840        Py_XDECREF(r);
     841      done:
     842#ifdef WITH_THREAD
     843        PyGILState_Release(gilstate);
     844#endif
     845        return result;
     846    }
     847    return result;
    797848}
    798849
     
    805856{
    806857#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
    807         rl_completion_append_character ='\0';
     858    rl_completion_append_character ='\0';
    808859#endif
    809860#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
    810         rl_completion_suppress_append = 0;
    811 #endif
    812         Py_XDECREF(begidx);
    813         Py_XDECREF(endidx);
    814         begidx = PyInt_FromLong((long) start);
    815         endidx = PyInt_FromLong((long) end);
    816         return completion_matches(text, *on_completion);
     861    rl_completion_suppress_append = 0;
     862#endif
     863    Py_XDECREF(begidx);
     864    Py_XDECREF(endidx);
     865    begidx = PyInt_FromLong((long) start);
     866    endidx = PyInt_FromLong((long) end);
     867    return completion_matches(text, *on_completion);
    817868}
    818869
     
    824875{
    825876#ifdef SAVE_LOCALE
    826         char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
    827         if (!saved_locale)
    828                 Py_FatalError("not enough memory to save locale");
    829 #endif
    830 
    831         using_history();
    832 
    833         rl_readline_name = "python";
     877    char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
     878    if (!saved_locale)
     879        Py_FatalError("not enough memory to save locale");
     880#endif
     881
     882#ifdef __APPLE__
     883    /* the libedit readline emulation resets key bindings etc
     884     * when calling rl_initialize.  So call it upfront
     885     */
     886    if (using_libedit_emulation)
     887        rl_initialize();
     888
     889    /* Detect if libedit's readline emulation uses 0-based
     890     * indexing or 1-based indexing.
     891     */
     892    add_history("1");
     893    if (history_get(1) == NULL) {
     894        libedit_history_start = 0;
     895    } else {
     896        libedit_history_start = 1;
     897    }
     898    clear_history();
     899#endif /* __APPLE__ */
     900
     901    using_history();
     902
     903    rl_readline_name = "python";
    834904#if defined(PYOS_OS2) && defined(PYCC_GCC)
    835         /* Allow $if term= in .inputrc to work */
    836         rl_terminal_name = getenv("TERM");
    837 #endif
    838         /* Force rebind of TAB to insert-tab */
    839         rl_bind_key('\t', rl_insert);
    840         /* Bind both ESC-TAB and ESC-ESC to the completion function */
    841         rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
    842         rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
    843         /* Set our hook functions */
    844         rl_startup_hook = (Function *)on_startup_hook;
     905    /* Allow $if term= in .inputrc to work */
     906    rl_terminal_name = getenv("TERM");
     907#endif
     908    /* Force rebind of TAB to insert-tab */
     909    rl_bind_key('\t', rl_insert);
     910    /* Bind both ESC-TAB and ESC-ESC to the completion function */
     911    rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
     912    rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
     913    /* Set our hook functions */
     914    rl_startup_hook = (Function *)on_startup_hook;
    845915#ifdef HAVE_RL_PRE_INPUT_HOOK
    846         rl_pre_input_hook = (Function *)on_pre_input_hook;
    847 #endif
    848         /* Set our completion function */
    849         rl_attempted_completion_function = (CPPFunction *)flex_complete;
    850         /* Set Python word break characters */
    851         rl_completer_word_break_characters =
    852                 strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
    853                 /* All nonalphanums except '.' */
    854 
    855         begidx = PyInt_FromLong(0L);
    856         endidx = PyInt_FromLong(0L);
    857         /* Initialize (allows .inputrc to override)
    858          *
    859          * XXX: A bug in the readline-2.2 library causes a memory leak
    860          * inside this function.  Nothing we can do about it.
    861          */
    862         rl_initialize();
    863 
    864         RESTORE_LOCALE(saved_locale)
     916    rl_pre_input_hook = (Function *)on_pre_input_hook;
     917#endif
     918    /* Set our completion function */
     919    rl_attempted_completion_function = (CPPFunction *)flex_complete;
     920    /* Set Python word break characters */
     921    completer_word_break_characters =
     922        rl_completer_word_break_characters =
     923        strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
     924        /* All nonalphanums except '.' */
     925
     926    begidx = PyInt_FromLong(0L);
     927    endidx = PyInt_FromLong(0L);
     928    /* Initialize (allows .inputrc to override)
     929     *
     930     * XXX: A bug in the readline-2.2 library causes a memory leak
     931     * inside this function.  Nothing we can do about it.
     932     */
     933#ifdef __APPLE__
     934    if (using_libedit_emulation)
     935        rl_read_init_file(NULL);
     936    else
     937#endif /* __APPLE__ */
     938        rl_initialize();
     939   
     940    RESTORE_LOCALE(saved_locale)
    865941}
    866942
     
    870946#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
    871947
    872 static  char *completed_input_string;
     948static  char *completed_input_string;
    873949static void
    874950rlhandler(char *text)
    875951{
    876         completed_input_string = text;
    877         rl_callback_handler_remove();
     952    completed_input_string = text;
     953    rl_callback_handler_remove();
    878954}
    879955
     
    883959readline_until_enter_or_signal(char *prompt, int *signal)
    884960{
    885         char * not_done_reading = "";
    886         fd_set selectset;
    887 
    888         *signal = 0;
     961    char * not_done_reading = "";
     962    fd_set selectset;
     963
     964    *signal = 0;
    889965#ifdef HAVE_RL_CATCH_SIGNAL
    890         rl_catch_signals = 0;
    891 #endif
    892 
    893         rl_callback_handler_install (prompt, rlhandler);
    894         FD_ZERO(&selectset);
    895        
    896         completed_input_string = not_done_reading;
    897 
    898         while (completed_input_string == not_done_reading) {
    899                 int has_input = 0;
    900 
    901                 while (!has_input)
    902                 {       struct timeval timeout = {0, 100000}; /* 0.1 seconds */
    903 
    904                         /* [Bug #1552726] Only limit the pause if an input hook has been
    905                            defined.  */
    906                         struct timeval *timeoutp = NULL;
    907                         if (PyOS_InputHook)
    908                                 timeoutp = &timeout;
    909                         FD_SET(fileno(rl_instream), &selectset);
    910                         /* select resets selectset if no input was available */
    911                         has_input = select(fileno(rl_instream) + 1, &selectset,
    912                                            NULL, NULL, timeoutp);
    913                         if(PyOS_InputHook) PyOS_InputHook();
    914                 }
    915 
    916                 if(has_input > 0) {
    917                         rl_callback_read_char();
    918                 }
    919                 else if (errno == EINTR) {
    920                         int s;
     966    rl_catch_signals = 0;
     967#endif
     968
     969    rl_callback_handler_install (prompt, rlhandler);
     970    FD_ZERO(&selectset);
     971
     972    completed_input_string = not_done_reading;
     973
     974    while (completed_input_string == not_done_reading) {
     975        int has_input = 0;
     976
     977        while (!has_input)
     978        {               struct timeval timeout = {0, 100000}; /* 0.1 seconds */
     979
     980            /* [Bug #1552726] Only limit the pause if an input hook has been
     981               defined.  */
     982            struct timeval *timeoutp = NULL;
     983            if (PyOS_InputHook)
     984                timeoutp = &timeout;
     985            FD_SET(fileno(rl_instream), &selectset);
     986            /* select resets selectset if no input was available */
     987            has_input = select(fileno(rl_instream) + 1, &selectset,
     988                               NULL, NULL, timeoutp);
     989            if(PyOS_InputHook) PyOS_InputHook();
     990        }
     991
     992        if(has_input > 0) {
     993            rl_callback_read_char();
     994        }
     995        else if (errno == EINTR) {
     996            int s;
    921997#ifdef WITH_THREAD
    922                         PyEval_RestoreThread(_PyOS_ReadlineTState);
    923 #endif
    924                         s = PyErr_CheckSignals();
     998            PyEval_RestoreThread(_PyOS_ReadlineTState);
     999#endif
     1000            s = PyErr_CheckSignals();
    9251001#ifdef WITH_THREAD
    926                         PyEval_SaveThread();   
    927 #endif
    928                         if (s < 0) {
    929                                 rl_free_line_state();
    930                                 rl_cleanup_after_signal();
    931                                 rl_callback_handler_remove();
    932                                 *signal = 1;
    933                                 completed_input_string = NULL;
    934                         }
    935                 }
    936         }
    937 
    938         return completed_input_string;
     1002            PyEval_SaveThread();
     1003#endif
     1004            if (s < 0) {
     1005                rl_free_line_state();
     1006                rl_cleanup_after_signal();
     1007                rl_callback_handler_remove();
     1008                *signal = 1;
     1009                completed_input_string = NULL;
     1010            }
     1011        }
     1012    }
     1013
     1014    return completed_input_string;
    9391015}
    9401016
     
    9501026onintr(int sig)
    9511027{
    952         longjmp(jbuf, 1);
     1028    longjmp(jbuf, 1);
    9531029}
    9541030
     
    9571033readline_until_enter_or_signal(char *prompt, int *signal)
    9581034{
    959         PyOS_sighandler_t old_inthandler;
    960         char *p;
    961    
    962         *signal = 0;
    963 
    964         old_inthandler = PyOS_setsig(SIGINT, onintr);
    965         if (setjmp(jbuf)) {
     1035    PyOS_sighandler_t old_inthandler;
     1036    char *p;
     1037
     1038    *signal = 0;
     1039
     1040    old_inthandler = PyOS_setsig(SIGINT, onintr);
     1041    if (setjmp(jbuf)) {
    9661042#ifdef HAVE_SIGRELSE
    967                 /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
    968                 sigrelse(SIGINT);
    969 #endif
    970                 PyOS_setsig(SIGINT, old_inthandler);
    971                 *signal = 1;
    972                 return NULL;
    973         }
    974         rl_event_hook = PyOS_InputHook;
    975         p = readline(prompt);
    976         PyOS_setsig(SIGINT, old_inthandler);
     1043        /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
     1044        sigrelse(SIGINT);
     1045#endif
     1046        PyOS_setsig(SIGINT, old_inthandler);
     1047        *signal = 1;
     1048        return NULL;
     1049    }
     1050    rl_event_hook = PyOS_InputHook;
     1051    p = readline(prompt);
     1052    PyOS_setsig(SIGINT, old_inthandler);
    9771053
    9781054    return p;
     
    9841060call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
    9851061{
    986         size_t n;
    987         char *p, *q;
    988         int signal;
     1062    size_t n;
     1063    char *p, *q;
     1064    int signal;
    9891065
    9901066#ifdef SAVE_LOCALE
    991         char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
    992         if (!saved_locale)
    993                 Py_FatalError("not enough memory to save locale");
    994         setlocale(LC_CTYPE, "");
    995 #endif
    996 
    997         if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
    998                 rl_instream = sys_stdin;
    999                 rl_outstream = sys_stdout;
     1067    char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
     1068    if (!saved_locale)
     1069        Py_FatalError("not enough memory to save locale");
     1070    setlocale(LC_CTYPE, "");
     1071#endif
     1072
     1073    if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
     1074        rl_instream = sys_stdin;
     1075        rl_outstream = sys_stdout;
    10001076#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
    1001                 rl_prep_terminal (1);
    1002 #endif
    1003         }
    1004 
    1005         p = readline_until_enter_or_signal(prompt, &signal);
    1006        
    1007         /* we got an interrupt signal */
    1008         if (signal) {
    1009                 RESTORE_LOCALE(saved_locale)
    1010                 return NULL;
    1011         }
    1012 
    1013         /* We got an EOF, return a empty string. */
    1014         if (p == NULL) {
    1015                 p = PyMem_Malloc(1);
    1016                 if (p != NULL)
    1017                         *p = '\0';
    1018                 RESTORE_LOCALE(saved_locale)
    1019                 return p;
    1020         }
    1021 
    1022         /* we have a valid line */
    1023         n = strlen(p);
    1024         if (n > 0) {
    1025                 char *line;
    1026                 HISTORY_STATE *state = history_get_history_state();
    1027                 if (state->length > 0)
     1077        rl_prep_terminal (1);
     1078#endif
     1079    }
     1080
     1081    p = readline_until_enter_or_signal(prompt, &signal);
     1082
     1083    /* we got an interrupt signal */
     1084    if (signal) {
     1085        RESTORE_LOCALE(saved_locale)
     1086        return NULL;
     1087    }
     1088
     1089    /* We got an EOF, return a empty string. */
     1090    if (p == NULL) {
     1091        p = PyMem_Malloc(1);
     1092        if (p != NULL)
     1093            *p = '\0';
     1094        RESTORE_LOCALE(saved_locale)
     1095        return p;
     1096    }
     1097
     1098    /* we have a valid line */
     1099    n = strlen(p);
     1100    if (n > 0) {
     1101        const char *line;
     1102        int length = _py_get_history_length();
     1103        if (length > 0)
    10281104#ifdef __APPLE__
    1029                         if (using_libedit_emulation) {
    1030                                 /*
    1031                                  * Libedit's emulation uses 0-based indexes,
    1032                                  * the real readline uses 1-based indexes.
    1033                                  */
    1034                                 line = history_get(state->length - 1)->line;
    1035                         } else
     1105            if (using_libedit_emulation) {
     1106                /* handle older 0-based or newer 1-based indexing */
     1107                line = history_get(length + libedit_history_start - 1)->line;
     1108            } else
    10361109#endif /* __APPLE__ */
    1037                         line = history_get(state->length)->line;
    1038                 else
    1039                         line = "";
    1040                 if (strcmp(p, line))
    1041                         add_history(p);
    1042                 /* the history docs don't say so, but the address of state
    1043                    changes each time history_get_history_state is called
    1044                    which makes me think it's freshly malloc'd memory...
    1045                    on the other hand, the address of the last line stays the
    1046                    same as long as history isn't extended, so it appears to
    1047                    be malloc'd but managed by the history package... */
    1048                 free(state);
    1049         }
    1050         /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
    1051            release the original. */
    1052         q = p;
    1053         p = PyMem_Malloc(n+2);
    1054         if (p != NULL) {
    1055                 strncpy(p, q, n);
    1056                 p[n] = '\n';
    1057                 p[n+1] = '\0';
    1058         }
    1059         free(q);
    1060         RESTORE_LOCALE(saved_locale)
    1061         return p;
     1110            line = history_get(length)->line;
     1111        else
     1112            line = "";
     1113        if (strcmp(p, line))
     1114            add_history(p);
     1115    }
     1116    /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
     1117       release the original. */
     1118    q = p;
     1119    p = PyMem_Malloc(n+2);
     1120    if (p != NULL) {
     1121        strncpy(p, q, n);
     1122        p[n] = '\n';
     1123        p[n+1] = '\0';
     1124    }
     1125    free(q);
     1126    RESTORE_LOCALE(saved_locale)
     1127    return p;
    10621128}
    10631129
     
    10761142initreadline(void)
    10771143{
    1078         PyObject *m;
     1144    PyObject *m;
    10791145
    10801146#ifdef __APPLE__
    1081         if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
    1082                 using_libedit_emulation = 1;
    1083         }
    1084 
    1085         if (using_libedit_emulation)
    1086                 m = Py_InitModule4("readline", readline_methods, doc_module_le,
    1087                            (PyObject *)NULL, PYTHON_API_VERSION);
    1088         else
     1147    if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
     1148        using_libedit_emulation = 1;
     1149    }
     1150
     1151    if (using_libedit_emulation)
     1152        m = Py_InitModule4("readline", readline_methods, doc_module_le,
     1153                   (PyObject *)NULL, PYTHON_API_VERSION);
     1154    else
    10891155
    10901156#endif /* __APPLE__ */
    10911157
    1092         m = Py_InitModule4("readline", readline_methods, doc_module,
    1093                            (PyObject *)NULL, PYTHON_API_VERSION);
    1094         if (m == NULL)
    1095                 return;
    1096 
    1097 
    1098 
    1099         PyOS_ReadlineFunctionPointer = call_readline;
    1100         setup_readline();
    1101 }
     1158    m = Py_InitModule4("readline", readline_methods, doc_module,
     1159                       (PyObject *)NULL, PYTHON_API_VERSION);
     1160    if (m == NULL)
     1161        return;
     1162
     1163    PyOS_ReadlineFunctionPointer = call_readline;
     1164    setup_readline();
     1165}
Note: See TracChangeset for help on using the changeset viewer.