python / python/cpython

PyImport_Import does not fall back to default builtins

Đang mở
#130,272 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

topic-C-API type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug report

Bug description:

When importing a module with PyImport_Import() inside an already running python which does have limited globals, the import fails due to missing __builtins__: https://github.com/python/cpython/blob/01ba7df49966eaf14f44962a77898840c70dda96/Python/import.c#L3914-L3921

It feels similar to this issue: https://github.com/python/cpython/pull/24564 but with a slightly different setup.

Originally reported for pyo3 here: https://github.com/PyO3/pyo3/issues/4913

Minimal reproducer below, fuller reproducer can be found here: https://gitlab.com/nertpinx/cpython_repr

#include <Python.h>

PyObject *
func(PyObject *self __attribute__((unused)),
     PyObject *arg)
{
    return PyImport_Import(arg);
}

static PyMethodDef method_def = {
    .ml_name = "func",
    .ml_meth = func,
    .ml_flags = METH_NOARGS,
    .ml_doc = "Reproducer function",
};

int
main(int argc __attribute__((unused)),
     char **argv __attribute__((unused)))
{
    Py_InitializeEx(0);

    PyObject *program = NULL;
    PyObject *globals = NULL;
    PyObject *supermodule = NULL;
    PyObject *py_func = NULL;

    supermodule = PyUnicode_FromString("supermodule");
    py_func = PyCFunction_NewEx(&method_def, NULL, supermodule);
    globals = PyDict_New();
    PyDict_SetItemString(globals, "func", py_func);

    program = Py_CompileString("func()", "<string>", Py_file_input);
    PyEval_EvalCode(program, globals, NULL);

    if (PyErr_Occurred()) {
        PyErr_Print();
        return 1;
    }

    return 0;
}
CPython versions tested on:

3.13

Operating systems tested on:

Linux

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu từ các dòng được tham chiếu trong import.c, khoảng 3914-3921, và xây dựng trình tái hiện C tối thiểu với CPython 3.13. Kiểm tra cách PyImport_Import xử lý globals không có builtins, sau đó thêm một regression test bao quát thiết lập limited-globals. Được xem là hoàn tất khi import thành công bằng builtins mặc định thay vì thất bại do thiếu builtins.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
c, python
Lĩnh vực
api, backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.