Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Python/_warnings.c

    r2 r391  
    11#include "Python.h"
    2 #include "code.h"  /* For DeprecationWarning about adding 'line'. */
    32#include "frameobject.h"
    43
    54#define MODULE_NAME "_warnings"
    6 #define DEFAULT_ACTION_NAME "default_action"
    75
    86PyDoc_STRVAR(warnings__doc__,
     
    1412static PyObject *_filters;  /* List */
    1513static PyObject *_once_registry;  /* Dict */
     14static PyObject *_default_action; /* String */
    1615
    1716
     
    8079
    8180
     81static PyObject *
     82get_default_action(void)
     83{
     84    PyObject *default_action;
     85
     86    default_action = get_warnings_attr("defaultaction");
     87    if (default_action == NULL) {
     88        if (PyErr_Occurred()) {
     89            return NULL;
     90        }
     91        return _default_action;
     92    }
     93
     94    Py_DECREF(_default_action);
     95    _default_action = default_action;
     96    return default_action;
     97}
     98
     99
    82100/* The item is a borrowed reference. */
    83101static const char *
     
    85103           PyObject *module, PyObject **item)
    86104{
    87     PyObject *action, *m, *d;
     105    PyObject *action;
    88106    Py_ssize_t i;
    89107    PyObject *warnings_filters;
     
    137155    }
    138156
    139     m = PyImport_ImportModule(MODULE_NAME);
    140     if (m == NULL)
    141         return NULL;
    142     d = PyModule_GetDict(m);
    143     Py_DECREF(m);
    144     if (d == NULL)
    145         return NULL;
    146     action = PyDict_GetItemString(d, DEFAULT_ACTION_NAME);
    147     if (action != NULL)
     157    action = get_default_action();
     158    if (action != NULL) {
    148159        return PyString_AsString(action);
     160    }
    149161
    150162    PyErr_SetString(PyExc_ValueError,
    151                     MODULE_NAME "." DEFAULT_ACTION_NAME " not found");
     163                    MODULE_NAME ".defaultaction not found");
    152164    return NULL;
    153165}
     166
    154167
    155168static int
     
    190203    mod_str = PyString_AsString(filename);
    191204    if (mod_str == NULL)
    192             return NULL;
     205        return NULL;
    193206    len = PyString_Size(filename);
    194207    if (len < 0)
    195208        return NULL;
    196209    if (len >= 3 &&
    197         strncmp(mod_str + (len - 3), ".py", 3) == 0) {
     210            strncmp(mod_str + (len - 3), ".py", 3) == 0) {
    198211        module = PyString_FromStringAndSize(mod_str, len-3);
    199212    }
     
    239252    name = PyObject_GetAttrString(category, "__name__");
    240253    if (name == NULL)  /* XXX Can an object lack a '__name__' attribute? */
    241             return;
     254        return;
    242255
    243256    f_stderr = PySys_GetObject("stderr");
     
    268281    }
    269282    else
    270         _Py_DisplaySourceLine(f_stderr, PyString_AS_STRING(filename), 
     283        _Py_DisplaySourceLine(f_stderr, PyString_AS_STRING(filename),
    271284                              lineno, 2);
    272285    PyErr_Clear();
     
    282295    const char *action;
    283296    int rc;
    284    
     297
    285298    if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
    286299        PyErr_SetString(PyExc_TypeError, "'registry' must be a dict");
     
    329342        if (rc == -1)
    330343            goto cleanup;
    331         else if (rc == 1)
     344        else if (rc == 1)
    332345            goto return_none;
    333346        /* Else this warning hasn't been generated before. */
     
    390403        }
    391404        else {
    392             const char *msg = "functions overriding warnings.showwarning() "
    393                                 "must support the 'line' argument";
    394             const char *text_char = PyString_AS_STRING(text);
    395 
    396             if (strcmp(msg, text_char) == 0) {
    397                 /* Prevent infinite recursion by using built-in implementation
    398                    of showwarning(). */
    399                 show_warning(filename, lineno, text, category, sourceline);
    400             }
    401             else {
    402                 PyObject *check_fxn;
    403                 PyObject *defaults;
    404                 PyObject *res;
    405 
    406                 if (PyMethod_Check(show_fxn))
    407                     check_fxn = PyMethod_Function(show_fxn);
    408                 else if (PyFunction_Check(show_fxn))
    409                     check_fxn = show_fxn;
    410                 else {
    411                     PyErr_SetString(PyExc_TypeError,
    412                                     "warnings.showwarning() must be set to a "
    413                                     "function or method");
    414                     Py_DECREF(show_fxn);
    415                     goto cleanup;
    416                 }
    417 
    418                 defaults = PyFunction_GetDefaults(check_fxn);
    419                 /* A proper implementation of warnings.showwarning() should
    420                     have at least two default arguments. */
    421                 if ((defaults == NULL) || (PyTuple_Size(defaults) < 2)) {
    422                     PyCodeObject *code = (PyCodeObject *)
    423                                                 PyFunction_GetCode(check_fxn);
    424                     if (!(code->co_flags & CO_VARARGS)) {
    425                         if (PyErr_WarnEx(PyExc_DeprecationWarning, msg, 1) <
    426                                 0) {
    427                             Py_DECREF(show_fxn);
    428                             goto cleanup;
    429                         }
    430                     }
    431                 }
    432                 res = PyObject_CallFunctionObjArgs(show_fxn, message, category,
    433                                                     filename, lineno_obj,
    434                                                     NULL);
    435                 Py_DECREF(show_fxn);
    436                 Py_XDECREF(res);
    437                 if (res == NULL)
    438                     goto cleanup;
    439             }
     405              PyObject *res;
     406
     407              if (!PyMethod_Check(show_fxn) && !PyFunction_Check(show_fxn)) {
     408                  PyErr_SetString(PyExc_TypeError,
     409                                  "warnings.showwarning() must be set to a "
     410                                  "function or method");
     411                  Py_DECREF(show_fxn);
     412                  goto cleanup;
     413              }
     414
     415              res = PyObject_CallFunctionObjArgs(show_fxn, message, category,
     416                                                  filename, lineno_obj,
     417                                                  NULL);
     418              Py_DECREF(show_fxn);
     419              Py_XDECREF(res);
     420              if (res == NULL)
     421                  goto cleanup;
    440422        }
    441423    }
     
    475457    else {
    476458        globals = f->f_globals;
    477         *lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
     459        *lineno = PyFrame_GetLineNumber(f);
    478460    }
    479461
     
    510492    /* Setup filename. */
    511493    *filename = PyDict_GetItemString(globals, "__file__");
    512     if (*filename != NULL) {
    513             Py_ssize_t len = PyString_Size(*filename);
     494    if (*filename != NULL && PyString_Check(*filename)) {
     495            Py_ssize_t len = PyString_Size(*filename);
    514496        const char *file_str = PyString_AsString(*filename);
    515             if (file_str == NULL || (len < 0 && PyErr_Occurred()))
     497            if (file_str == NULL || (len < 0 && PyErr_Occurred()))
    516498            goto handle_error;
    517499
     
    525507        {
    526508            *filename = PyString_FromStringAndSize(file_str, len-1);
    527                 if (*filename == NULL)
    528                         goto handle_error;
    529             }
    530             else
     509            if (*filename == NULL)
     510                goto handle_error;
     511        }
     512        else
    531513            Py_INCREF(*filename);
    532514    }
    533515    else {
    534516        const char *module_str = PyString_AsString(*module);
     517        *filename = NULL;
    535518        if (module_str && strcmp(module_str, "__main__") == 0) {
    536519            PyObject *argv = PySys_GetObject("argv");
     
    555538                /* embedded interpreters don't have sys.argv, see bug #839151 */
    556539                *filename = PyString_FromString("__main__");
    557                     if (*filename == NULL)
    558                         goto handle_error;
     540                if (*filename == NULL)
     541                    goto handle_error;
    559542            }
    560543        }
     
    744727}
    745728
    746 /* PyErr_Warn is only for backwards compatability and will be removed.
     729/* PyErr_Warn is only for backwards compatibility and will be removed.
    747730   Use PyErr_WarnEx instead. */
    748731
     
    858841init_filters(void)
    859842{
    860     PyObject *filters = PyList_New(3);
     843    /* Don't silence DeprecationWarning if -3 or -Q were used. */
     844    PyObject *filters = PyList_New(Py_Py3kWarningFlag ||
     845                                    Py_DivisionWarningFlag ? 3 : 4);
     846    unsigned int pos = 0;  /* Post-incremented in each use. */
     847    unsigned int x;
    861848    const char *bytes_action;
     849
    862850    if (filters == NULL)
    863851        return NULL;
    864852
    865     PyList_SET_ITEM(filters, 0,
     853    /* If guard changes, make sure to update 'filters' initialization above. */
     854    if (!Py_Py3kWarningFlag && !Py_DivisionWarningFlag) {
     855        PyList_SET_ITEM(filters, pos++,
     856                        create_filter(PyExc_DeprecationWarning, "ignore"));
     857    }
     858    PyList_SET_ITEM(filters, pos++,
    866859                    create_filter(PyExc_PendingDeprecationWarning, "ignore"));
    867     PyList_SET_ITEM(filters, 1, create_filter(PyExc_ImportWarning, "ignore"));
     860    PyList_SET_ITEM(filters, pos++,
     861                    create_filter(PyExc_ImportWarning, "ignore"));
    868862    if (Py_BytesWarningFlag > 1)
    869863        bytes_action = "error";
     
    872866    else
    873867        bytes_action = "ignore";
    874     PyList_SET_ITEM(filters, 2, create_filter(PyExc_BytesWarning,
     868    PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
    875869                    bytes_action));
    876870
    877     if (PyList_GET_ITEM(filters, 0) == NULL ||
    878         PyList_GET_ITEM(filters, 1) == NULL ||
    879         PyList_GET_ITEM(filters, 2) == NULL) {
    880         Py_DECREF(filters);
    881         return NULL;
     871    for (x = 0; x < pos; x += 1) {
     872        if (PyList_GET_ITEM(filters, x) == NULL) {
     873            Py_DECREF(filters);
     874            return NULL;
     875        }
    882876    }
    883877
     
    889883_PyWarnings_Init(void)
    890884{
    891     PyObject *m, *default_action;
     885    PyObject *m;
    892886
    893887    m = Py_InitModule3(MODULE_NAME, warnings_functions, warnings__doc__);
     
    909903        return;
    910904
    911     default_action = PyString_InternFromString("default");
    912     if (default_action == NULL)
     905    _default_action = PyString_FromString("default");
     906    if (_default_action == NULL)
    913907        return;
    914     if (PyModule_AddObject(m, DEFAULT_ACTION_NAME, default_action) < 0)
     908    if (PyModule_AddObject(m, "default_action", _default_action) < 0)
    915909        return;
    916910}
Note: See TracChangeset for help on using the changeset viewer.