From 69150669f224a1fc47de483557736e725ac5b2a1 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 26 Jun 2019 18:01:10 +0200 Subject: bpo-37414: Remove sys.callstats() (GH-14398) Remove the undocumented sys.callstats() function. Since Python 3.7, it was deprecated and always returned None. It required a special build option CALL_PROFILE which was already removed in Python 3.7. --- Doc/whatsnew/3.9.rst | 5 +++ .../2019-06-26-17-27-26.bpo-37414.o6Lnbc.rst | 3 ++ Python/clinic/sysmodule.c.h | 39 +--------------------- Python/sysmodule.c | 39 ---------------------- 4 files changed, 9 insertions(+), 77 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2019-06-26-17-27-26.bpo-37414.o6Lnbc.rst diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst index e9a31c8..780c0a9 100644 --- a/Doc/whatsnew/3.9.rst +++ b/Doc/whatsnew/3.9.rst @@ -139,6 +139,11 @@ Deprecated Removed ======= +* The undocumented ``sys.callstats()`` function has been removed. Since Python + 3.7, it was deprecated and always returned ``None``. It required a special + build option ``CALL_PROFILE`` which was already removed in Python 3.7. + (Contributed by Victor Stinner in :issue:`37414`.) + * The ``sys.getcheckinterval()`` and ``sys.setcheckinterval()`` functions have been removed. They were deprecated since Python 3.2. Use :func:`sys.getswitchinterval` and :func:`sys.setswitchinterval` instead. diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-06-26-17-27-26.bpo-37414.o6Lnbc.rst b/Misc/NEWS.d/next/Core and Builtins/2019-06-26-17-27-26.bpo-37414.o6Lnbc.rst new file mode 100644 index 0000000..a3ff57b --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-06-26-17-27-26.bpo-37414.o6Lnbc.rst @@ -0,0 +1,3 @@ +The undocumented ``sys.callstats()`` function has been removed. Since Python +3.7, it was deprecated and always returned ``None``. It required a special +build option ``CALL_PROFILE`` which was already removed in Python 3.7. diff --git a/Python/clinic/sysmodule.c.h b/Python/clinic/sysmodule.c.h index 4ddf5fe..1f09e02 100644 --- a/Python/clinic/sysmodule.c.h +++ b/Python/clinic/sysmodule.c.h @@ -879,43 +879,6 @@ exit: return return_value; } -PyDoc_STRVAR(sys_callstats__doc__, -"callstats($module, /)\n" -"--\n" -"\n" -"Return a tuple of function call statistics.\n" -"\n" -"A tuple is returned only if CALL_PROFILE was defined when Python was\n" -"built. Otherwise, this returns None.\n" -"\n" -"When enabled, this function returns detailed, implementation-specific\n" -"details about the number of function calls executed. The return value\n" -"is a 11-tuple where the entries in the tuple are counts of:\n" -"0. all function calls\n" -"1. calls to PyFunction_Type objects\n" -"2. PyFunction calls that do not create an argument tuple\n" -"3. PyFunction calls that do not create an argument tuple\n" -" and bypass PyEval_EvalCodeEx()\n" -"4. PyMethod calls\n" -"5. PyMethod calls on bound methods\n" -"6. PyType calls\n" -"7. PyCFunction calls\n" -"8. generator calls\n" -"9. All other calls\n" -"10. Number of stack pops performed by call_function()"); - -#define SYS_CALLSTATS_METHODDEF \ - {"callstats", (PyCFunction)sys_callstats, METH_NOARGS, sys_callstats__doc__}, - -static PyObject * -sys_callstats_impl(PyObject *module); - -static PyObject * -sys_callstats(PyObject *module, PyObject *Py_UNUSED(ignored)) -{ - return sys_callstats_impl(module); -} - PyDoc_STRVAR(sys__debugmallocstats__doc__, "_debugmallocstats($module, /)\n" "--\n" @@ -1026,4 +989,4 @@ sys_getandroidapilevel(PyObject *module, PyObject *Py_UNUSED(ignored)) #ifndef SYS_GETANDROIDAPILEVEL_METHODDEF #define SYS_GETANDROIDAPILEVEL_METHODDEF #endif /* !defined(SYS_GETANDROIDAPILEVEL_METHODDEF) */ -/*[clinic end generated code: output=022614f3794666ae input=a9049054013a1b77]*/ +/*[clinic end generated code: output=acef77d2bb8f6da9 input=a9049054013a1b77]*/ diff --git a/Python/sysmodule.c b/Python/sysmodule.c index c9718d9..b200318 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1779,44 +1779,6 @@ sys_call_tracing_impl(PyObject *module, PyObject *func, PyObject *funcargs) return _PyEval_CallTracing(func, funcargs); } -/*[clinic input] -sys.callstats - -Return a tuple of function call statistics. - -A tuple is returned only if CALL_PROFILE was defined when Python was -built. Otherwise, this returns None. - -When enabled, this function returns detailed, implementation-specific -details about the number of function calls executed. The return value -is a 11-tuple where the entries in the tuple are counts of: -0. all function calls -1. calls to PyFunction_Type objects -2. PyFunction calls that do not create an argument tuple -3. PyFunction calls that do not create an argument tuple - and bypass PyEval_EvalCodeEx() -4. PyMethod calls -5. PyMethod calls on bound methods -6. PyType calls -7. PyCFunction calls -8. generator calls -9. All other calls -10. Number of stack pops performed by call_function() -[clinic start generated code]*/ - -static PyObject * -sys_callstats_impl(PyObject *module) -/*[clinic end generated code: output=edc4a74957fa8def input=d447d8d224d5d175]*/ -{ - if (PyErr_WarnEx(PyExc_DeprecationWarning, - "sys.callstats() has been deprecated in Python 3.7 " - "and will be removed in the future", 1) < 0) { - return NULL; - } - - Py_RETURN_NONE; -} - #ifdef __cplusplus extern "C" { @@ -1910,7 +1872,6 @@ static PyMethodDef sys_methods[] = { {"audit", (PyCFunction)(void(*)(void))sys_audit, METH_FASTCALL, audit_doc }, {"breakpointhook", (PyCFunction)(void(*)(void))sys_breakpointhook, METH_FASTCALL | METH_KEYWORDS, breakpointhook_doc}, - SYS_CALLSTATS_METHODDEF SYS__CLEAR_TYPE_CACHE_METHODDEF SYS__CURRENT_FRAMES_METHODDEF SYS_DISPLAYHOOK_METHODDEF -- cgit v0.12 d='n83' href='#n83'>83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
#include "Python.h"
#include "errcode.h"
#include "../Parser/tokenizer.h"
#include "../Parser/pegen.h"      // _PyPegen_byte_offset_to_character_offset()
#include "../Parser/pegen.h"      // _PyPegen_byte_offset_to_character_offset()

static struct PyModuleDef _tokenizemodule;

typedef struct {
    PyTypeObject *TokenizerIter;
} tokenize_state;

static tokenize_state *
get_tokenize_state(PyObject *module) {
    return (tokenize_state *)PyModule_GetState(module);
}

#define _tokenize_get_state_by_type(type) \
    get_tokenize_state(PyType_GetModuleByDef(type, &_tokenizemodule))

#include "pycore_runtime.h"
#include "clinic/Python-tokenize.c.h"

/*[clinic input]
module _tokenizer
class _tokenizer.tokenizeriter "tokenizeriterobject *" "_tokenize_get_state_by_type(type)->TokenizerIter"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=96d98ee2fef7a8bc]*/

typedef struct
{
    PyObject_HEAD struct tok_state *tok;
    int done;
} tokenizeriterobject;

/*[clinic input]
@classmethod
_tokenizer.tokenizeriter.__new__ as tokenizeriter_new

    readline: object
    /
    *
    extra_tokens: bool
    encoding: str(c_default="NULL") = 'utf-8'
[clinic start generated code]*/

static PyObject *
tokenizeriter_new_impl(PyTypeObject *type, PyObject *readline,
                       int extra_tokens, const char *encoding)
/*[clinic end generated code: output=7501a1211683ce16 input=f7dddf8a613ae8bd]*/
{
    tokenizeriterobject *self = (tokenizeriterobject *)type->tp_alloc(type, 0);
    if (self == NULL) {
        return NULL;
    }
    PyObject *filename = PyUnicode_FromString("<string>");
    if (filename == NULL) {
        return NULL;
    }
    self->tok = _PyTokenizer_FromReadline(readline, encoding, 1, 1);
    if (self->tok == NULL) {
        Py_DECREF(filename);
        return NULL;
    }
    self->tok->filename = filename;
    if (extra_tokens) {
        self->tok->tok_extra_tokens = 1;
    }
    self->done = 0;
    return (PyObject *)self;
}

static int
_tokenizer_error(struct tok_state *tok)
{
    if (PyErr_Occurred()) {
        return -1;
    }

    const char *msg = NULL;
    PyObject* errtype = PyExc_SyntaxError;
    switch (tok->done) {
        case E_TOKEN:
            msg = "invalid token";
            break;
        case E_EOF:
            PyErr_SetString(PyExc_SyntaxError, "unexpected EOF in multi-line statement");
            PyErr_SyntaxLocationObject(tok->filename, tok->lineno,
                                       tok->inp - tok->buf < 0 ? 0 : (int)(tok->inp - tok->buf));
            return -1;
        case E_DEDENT:
            msg = "unindent does not match any outer indentation level";
            errtype = PyExc_IndentationError;
            break;
        case E_INTR:
            if (!PyErr_Occurred()) {
                PyErr_SetNone(PyExc_KeyboardInterrupt);
            }
            return -1;
        case E_NOMEM:
            PyErr_NoMemory();
            return -1;
        case E_TABSPACE:
            errtype = PyExc_TabError;
            msg = "inconsistent use of tabs and spaces in indentation";
            break;
        case E_TOODEEP:
            errtype = PyExc_IndentationError;
            msg = "too many levels of indentation";
            break;
        case E_LINECONT: {
            msg = "unexpected character after line continuation character";
            break;
        }
        default:
            msg = "unknown tokenization error";
    }

    PyObject* errstr = NULL;
    PyObject* error_line = NULL;
    PyObject* tmp = NULL;
    PyObject* value = NULL;
    int result = 0;

    Py_ssize_t size = tok->inp - tok->buf;
    assert(tok->buf[size-1] == '\n');
    size -= 1; // Remove the newline character from the end of the line
    error_line = PyUnicode_DecodeUTF8(tok->buf, size, "replace");
    if (!error_line) {
        result = -1;
        goto exit;
    }

    Py_ssize_t offset = _PyPegen_byte_offset_to_character_offset(error_line, tok->inp - tok->buf);
    if (offset == -1) {
        result = -1;
        goto exit;
    }
    tmp = Py_BuildValue("(OnnOOO)", tok->filename, tok->lineno, offset, error_line, Py_None, Py_None);
    if (!tmp) {
        result = -1;
        goto exit;
    }

    errstr = PyUnicode_FromString(msg);
    if (!errstr) {
        result = -1;
        goto exit;
    }

    value = PyTuple_Pack(2, errstr, tmp);
    if (!value) {
        result = -1;
        goto exit;
    }

    PyErr_SetObject(errtype, value);

exit:
    Py_XDECREF(errstr);
    Py_XDECREF(error_line);
    Py_XDECREF(tmp);
    Py_XDECREF(value);
    return result;
}

static PyObject *
tokenizeriter_next(tokenizeriterobject *it)
{
    PyObject* result = NULL;
    struct token token;
    _PyToken_Init(&token);

    int type = _PyTokenizer_Get(it->tok, &token);
    if (type == ERRORTOKEN) {
        if(!PyErr_Occurred()) {
            _tokenizer_error(it->tok);
            assert(PyErr_Occurred());
        }
        goto exit;
    }
    if (it->done || type == ERRORTOKEN) {
        PyErr_SetString(PyExc_StopIteration, "EOF");
        it->done = 1;
        goto exit;
    }
    PyObject *str = NULL;
    if (token.start == NULL || token.end == NULL) {
        str = PyUnicode_FromString("");
    }
    else {
        str = PyUnicode_FromStringAndSize(token.start, token.end - token.start);
    }
    if (str == NULL) {
        goto exit;
    }

    int is_trailing_token = 0;
    if (type == ENDMARKER || (type == DEDENT && it->tok->done == E_EOF)) {
        is_trailing_token = 1;
    }

    const char *line_start = ISSTRINGLIT(type) ? it->tok->multi_line_start : it->tok->line_start;
    PyObject* line = NULL;
    if (it->tok->tok_extra_tokens && is_trailing_token) {
        line = PyUnicode_FromString("");
    } else {
        Py_ssize_t size = it->tok->inp - line_start;
        if (size >= 1 && it->tok->implicit_newline) {
            size -= 1;
        }
        line = PyUnicode_DecodeUTF8(line_start, size, "replace");
    }
    if (line == NULL) {
        Py_DECREF(str);
        goto exit;
    }

    Py_ssize_t lineno = ISSTRINGLIT(type) ? it->tok->first_lineno : it->tok->lineno;
    Py_ssize_t end_lineno = it->tok->lineno;
    Py_ssize_t col_offset = -1;
    Py_ssize_t end_col_offset = -1;
    if (token.start != NULL && token.start >= line_start) {
        col_offset = _PyPegen_byte_offset_to_character_offset(line, token.start - line_start);
    }
    if (token.end != NULL && token.end >= it->tok->line_start) {
        end_col_offset = _PyPegen_byte_offset_to_character_offset(line, token.end - it->tok->line_start);
    }

    if (it->tok->tok_extra_tokens) {
        if (is_trailing_token) {
            lineno = end_lineno = lineno + 1;
            col_offset = end_col_offset = 0;
        }
        // Necessary adjustments to match the original Python tokenize
        // implementation
        if (type > DEDENT && type < OP) {
            type = OP;
        }
        else if (type == NEWLINE) {
            Py_DECREF(str);
            if (!it->tok->implicit_newline) {
                if (it->tok->start[0] == '\r') {
                    str = PyUnicode_FromString("\r\n");
                } else {
                    str = PyUnicode_FromString("\n");
                }
            }
            end_col_offset++;
        }
        else if (type == NL) {
            if (it->tok->implicit_newline) {
                Py_DECREF(str);
                str = PyUnicode_FromString("");
            }
        }

        if (str == NULL) {
            Py_DECREF(line);
            goto exit;
        }
    }

    result = Py_BuildValue("(iN(nn)(nn)N)", type, str, lineno, col_offset, end_lineno, end_col_offset, line);
exit:
    _PyToken_Free(&token);
    if (type == ENDMARKER) {
        it->done = 1;
    }
    return result;
}

static void
tokenizeriter_dealloc(tokenizeriterobject *it)
{
    PyTypeObject *tp = Py_TYPE(it);
    _PyTokenizer_Free(it->tok);
    tp->tp_free(it);
    Py_DECREF(tp);
}

static PyType_Slot tokenizeriter_slots[] = {
    {Py_tp_new, tokenizeriter_new},
    {Py_tp_dealloc, tokenizeriter_dealloc},
    {Py_tp_getattro, PyObject_GenericGetAttr},
    {Py_tp_iter, PyObject_SelfIter},
    {Py_tp_iternext, tokenizeriter_next},
    {0, NULL},
};

static PyType_Spec tokenizeriter_spec = {
    .name = "_tokenize.TokenizerIter",
    .basicsize = sizeof(tokenizeriterobject),
    .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
    .slots = tokenizeriter_slots,
};

static int
tokenizemodule_exec(PyObject *m)
{
    tokenize_state *state = get_tokenize_state(m);
    if (state == NULL) {
        return -1;
    }

    state->TokenizerIter = (PyTypeObject *)PyType_FromModuleAndSpec(m, &tokenizeriter_spec, NULL);
    if (state->TokenizerIter == NULL) {
        return -1;
    }
    if (PyModule_AddType(m, state->TokenizerIter) < 0) {
        return -1;
    }

    return 0;
}

static PyMethodDef tokenize_methods[] = {
    {NULL, NULL, 0, NULL} /* Sentinel */
};

static PyModuleDef_Slot tokenizemodule_slots[] = {
    {Py_mod_exec, tokenizemodule_exec},
    {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
    {0, NULL}
};

static int
tokenizemodule_traverse(PyObject *m, visitproc visit, void *arg)
{
    tokenize_state *state = get_tokenize_state(m);
    Py_VISIT(state->TokenizerIter);
    return 0;
}

static int
tokenizemodule_clear(PyObject *m)
{
    tokenize_state *state = get_tokenize_state(m);
    Py_CLEAR(state->TokenizerIter);
    return 0;
}

static void
tokenizemodule_free(void *m)
{
    tokenizemodule_clear((PyObject *)m);
}

static struct PyModuleDef _tokenizemodule = {
    PyModuleDef_HEAD_INIT,
    .m_name = "_tokenize",
    .m_size = sizeof(tokenize_state),
    .m_slots = tokenizemodule_slots,
    .m_methods = tokenize_methods,
    .m_traverse = tokenizemodule_traverse,
    .m_clear = tokenizemodule_clear,
    .m_free = tokenizemodule_free,
};

PyMODINIT_FUNC
PyInit__tokenize(void)
{
    return PyModuleDef_Init(&_tokenizemodule);
}