Skip to content
Snippets Groups Projects
Select Git revision
  • eb0004c27163ec089201c81eb9ece470700be6c7
  • main default protected
  • 3.10
  • 3.11
  • revert-15688-bpo-38031-_io-FileIO-opener-crash
  • 3.8
  • 3.9
  • 3.7
  • enum-fix_auto
  • branch-v3.11.0
  • backport-c3648f4-3.11
  • gh-93963/remove-importlib-resources-abcs
  • refactor-wait_for
  • shared-testcase
  • v3.12.0a2
  • v3.12.0a1
  • v3.11.0
  • v3.8.15
  • v3.9.15
  • v3.10.8
  • v3.7.15
  • v3.11.0rc2
  • v3.8.14
  • v3.9.14
  • v3.7.14
  • v3.10.7
  • v3.11.0rc1
  • v3.10.6
  • v3.11.0b5
  • v3.11.0b4
  • v3.10.5
  • v3.11.0b3
  • v3.11.0b2
  • v3.9.13
34 results

topics.py

Blame
  • codeobject.c 65.64 KiB
    #include <stdbool.h>
    
    #include "Python.h"
    #include "opcode.h"
    #include "structmember.h"         // PyMemberDef
    #include "pycore_code.h"          // _PyCodeConstructor
    #include "pycore_interp.h"        // PyInterpreterState.co_extra_freefuncs
    #include "pycore_opcode.h"        // _PyOpcode_Deopt
    #include "pycore_pystate.h"       // _PyInterpreterState_GET()
    #include "pycore_tuple.h"         // _PyTuple_ITEMS()
    #include "clinic/codeobject.c.h"
    
    
    /******************
     * generic helpers
     ******************/
    
    /* all_name_chars(s): true iff s matches [a-zA-Z0-9_]* */
    static int
    all_name_chars(PyObject *o)
    {
        const unsigned char *s, *e;
    
        if (!PyUnicode_IS_ASCII(o))
            return 0;
    
        s = PyUnicode_1BYTE_DATA(o);
        e = s + PyUnicode_GET_LENGTH(o);
        for (; s != e; s++) {
            if (!Py_ISALNUM(*s) && *s != '_')
                return 0;
        }
        return 1;
    }
    
    static int
    intern_strings(PyObject *tuple)
    {
        Py_ssize_t i;
    
        for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
            PyObject *v = PyTuple_GET_ITEM(tuple, i);
            if (v == NULL || !PyUnicode_CheckExact(v)) {
                PyErr_SetString(PyExc_SystemError,
                                "non-string found in code slot");
                return -1;
            }
            PyUnicode_InternInPlace(&_PyTuple_ITEMS(tuple)[i]);
        }
        return 0;
    }
    
    /* Intern selected string constants */
    static int
    intern_string_constants(PyObject *tuple, int *modified)
    {
        for (Py_ssize_t i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
            PyObject *v = PyTuple_GET_ITEM(tuple, i);
            if (PyUnicode_CheckExact(v)) {
                if (PyUnicode_READY(v) == -1) {
                    return -1;
                }
    
                if (all_name_chars(v)) {
                    PyObject *w = v;
                    PyUnicode_InternInPlace(&v);
                    if (w != v) {
                        PyTuple_SET_ITEM(tuple, i, v);
                        if (modified) {
                            *modified = 1;