Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,12 @@ simple_stmts[asdl_stmt_seq*]:
# NOTE: assignment MUST precede expression, else parsing a simple assignment
# will throw a SyntaxError.
simple_stmt[stmt_ty] (memo):
| &("lazy" | 'import') import_stmt
| assignment
| &"type" type_alias
| e=star_expressions { _PyAST_Expr(e, EXTRA) }
| &'return' return_stmt
| &('import' | 'from') import_stmt
| &('import' | 'from' | "lazy" ) import_stmt
| &'raise' raise_stmt
| &'pass' pass_stmt
| &'del' del_stmt
Expand Down Expand Up @@ -216,21 +217,25 @@ assert_stmt[stmt_ty]:
| invalid_assert_stmt
| 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }

import_stmt[stmt_ty]:
| invalid_import
import_stmt[stmt_ty](memo):
| import_name
| import_from

# Import statements
# -----------------

import_name[stmt_ty]: 'import' a=dotted_as_names { _PyAST_Import(a, EXTRA) }
import_name[stmt_ty]:
| 'import' a=dotted_as_names { _PyAST_Import(a, 0, EXTRA) }
| "lazy" 'import' a=dotted_as_names { _PyAST_Import(a, 1, EXTRA) }

# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
import_from[stmt_ty]:
| 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets {
_PyPegen_checked_future_import(p, b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) }
| 'from' a=('.' | '...')+ 'import' b=import_from_targets {
_PyAST_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
_PyAST_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), 0, EXTRA) }
| 'from' a=('.' | '...')+ "lazy" 'import' b=import_from_targets {
_PyAST_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), 1, EXTRA) }
import_from_targets[asdl_alias_seq*]:
| '(' a=import_from_as_names [','] ')' { a }
| import_from_as_names !','
Expand Down
11 changes: 7 additions & 4 deletions Include/internal/pycore_ast.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Include/internal/pycore_ast_state.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,11 @@ PyAPI_FUNC(void) _PyEval_FormatExcCheckArg(PyThreadState *tstate, PyObject *exc,
PyAPI_FUNC(void) _PyEval_FormatExcUnbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
PyAPI_FUNC(void) _PyEval_FormatKwargsError(PyThreadState *tstate, PyObject *func, PyObject *kwargs);
PyAPI_FUNC(PyObject *) _PyEval_ImportFrom(PyThreadState *, PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) _PyEval_ImportName(PyThreadState *, _PyInterpreterFrame *, PyObject *, PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) _PyEval_LazyImportName(PyThreadState *tstate, PyObject *builtins, PyObject *globals,
PyObject *name, PyObject *fromlist, PyObject *level);
PyAPI_FUNC(PyObject *) _PyEval_LazyImportFrom(PyThreadState *tstate, PyObject *v, PyObject *name);
PyAPI_FUNC(PyObject *) _PyEval_ImportName(PyThreadState *tstate, PyObject *builtins, PyObject *globals, PyObject *locals,
PyObject *name, PyObject *fromlist, PyObject *level);
PyAPI_FUNC(PyObject *)_PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type, Py_ssize_t nargs, PyObject *kwargs);
PyAPI_FUNC(PyObject *)_PyEval_MatchKeys(PyThreadState *tstate, PyObject *map, PyObject *keys);
PyAPI_FUNC(void) _PyEval_MonitorRaise(PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_CODEUNIT *instr);
Expand Down
6 changes: 6 additions & 0 deletions Include/internal/pycore_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ extern int _PyImport_FixupBuiltin(
PyObject *modules
);

extern PyObject *
_PyImport_ResolveName(PyThreadState *tstate, PyObject *name, PyObject *globals, int level);
extern PyObject *
_PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import);


#ifdef HAVE_DLOPEN
# include <dlfcn.h> // RTLD_NOW, RTLD_LAZY
# if HAVE_DECL_RTLD_NOW
Expand Down
29 changes: 29 additions & 0 deletions Include/internal/pycore_lazyimportobject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Copyright (c) Meta, Inc. and its affiliates. All Rights Reserved */
/* File added for Lazy Imports */

/* Lazy object interface */

#ifndef Py_LAZYIMPORTOBJECT_H
#define Py_LAZYIMPORTOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif

PyAPI_DATA(PyTypeObject) PyLazyImport_Type;
#define PyLazyImport_CheckExact(op) Py_IS_TYPE((op), &PyLazyImport_Type)

typedef struct {
PyObject_HEAD
PyObject *lz_builtins;
PyObject *lz_from;
PyObject *lz_attr;
} PyLazyImportObject;


PyAPI_FUNC(PyObject *) _PyLazyImport_GetName(PyObject *lazy_import);
PyAPI_FUNC(PyObject *) _PyLazyImport_New(PyObject *builtins, PyObject *from, PyObject *attr);

#ifdef __cplusplus
}
#endif
#endif /* !Py_LAZYIMPORTOBJECT_H */
3 changes: 2 additions & 1 deletion Include/internal/pycore_magic_number.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ Known values:
Python 3.15a1 3653 (Fix handling of opcodes that may leave operands on the stack when optimizing LOAD_FAST)
Python 3.15a1 3654 (Fix missing exception handlers in logical expression)
Python 3.15a1 3655 (Fix miscompilation of some module-level annotations)
Python 3.15a1 3656 Lazy imports IMPORT_NAME opcode changes


Python 3.16 will start with 3700
Expand All @@ -299,7 +300,7 @@ PC/launcher.c must also be updated.

*/

#define PYC_MAGIC_NUMBER 3655
#define PYC_MAGIC_NUMBER 3656
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
(little-endian) and then appending b'\r\n'. */
#define PYC_MAGIC_NUMBER_TOKEN \
Expand Down
8 changes: 8 additions & 0 deletions Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
FUNCTION_ATTR_FLAGS = ('defaults', 'kwdefaults', 'annotations', 'closure', 'annotate')

ENTER_EXECUTOR = opmap['ENTER_EXECUTOR']
IMPORT_NAME = opmap['IMPORT_NAME']
LOAD_GLOBAL = opmap['LOAD_GLOBAL']
LOAD_SMALL_INT = opmap['LOAD_SMALL_INT']
BINARY_OP = opmap['BINARY_OP']
Expand Down Expand Up @@ -601,7 +602,14 @@ def get_argval_argrepr(self, op, arg, offset):
argval, argrepr = _get_name_info(arg//4, get_name)
if (arg & 1) and argrepr:
argrepr = f"{argrepr} + NULL|self"
elif deop == IMPORT_NAME:
argval, argrepr = _get_name_info(arg//4, get_name)
if (arg & 1) and argrepr:
argrepr = f"{argrepr} + lazy"
elif (arg & 2) and argrepr:
argrepr = f"{argrepr} + eager"
else:
print(deop)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Debug Print in Opcode Argument Handling

A debug print(deop) statement was accidentally left in the code. It prints to stdout during normal disassembly whenever an opcode's argument processing falls into the general else branch.

Fix in Cursor Fix in Web

argval, argrepr = _get_name_info(arg, get_name)
elif deop in hasjump or deop in hasexc:
argval = self.offset_from_jump_arg(op, arg, offset)
Expand Down
1 change: 1 addition & 0 deletions Lib/keyword.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ OBJECT_OBJS= \
Objects/funcobject.o \
Objects/interpolationobject.o \
Objects/iterobject.o \
Objects/lazyimportobject.o \
Objects/listobject.o \
Objects/longobject.o \
Objects/dictobject.o \
Expand Down Expand Up @@ -1368,6 +1369,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/internal/pycore_interpolation.h \
$(srcdir)/Include/internal/pycore_intrinsics.h \
$(srcdir)/Include/internal/pycore_jit.h \
$(srcdir)/Include/internal/pycore_lazyimportobject.h \
$(srcdir)/Include/internal/pycore_list.h \
$(srcdir)/Include/internal/pycore_llist.h \
$(srcdir)/Include/internal/pycore_lock.h \
Expand Down
137 changes: 137 additions & 0 deletions Objects/lazyimportobject.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/* Copyright (c) Meta, Inc. and its affiliates. All Rights Reserved */
/* File added for Lazy Imports */

/* Lazy object implementation */

#include "Python.h"
#include "pycore_lazyimportobject.h"

PyObject *
_PyLazyImport_New(PyObject *builtins, PyObject *from, PyObject *attr)
{
PyLazyImportObject *m;
if (!from || !PyUnicode_Check(from)) {
PyErr_BadArgument();
return NULL;
}
if (attr == Py_None) {
attr = NULL;
}
assert(!attr || PyObject_IsTrue(attr));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Lazy Import Assertion Masks Errors

The assertion !attr || PyObject_IsTrue(attr) in _PyLazyImport_New doesn't correctly handle PyObject_IsTrue() returning -1 on error. Because -1 is truthy in C, the assertion passes, silently masking any exception set by PyObject_IsTrue().

Fix in Cursor Fix in Web

m = PyObject_GC_New(PyLazyImportObject, &PyLazyImport_Type);
if (m == NULL) {
return NULL;
}
Py_XINCREF(builtins);
m->lz_builtins = builtins;
Py_INCREF(from);
m->lz_from = from;
Py_XINCREF(attr);
m->lz_attr = attr;
PyObject_GC_Track(m);
return (PyObject *)m;
}

static void
lazy_import_dealloc(PyLazyImportObject *m)
{
PyObject_GC_UnTrack(m);
Py_XDECREF(m->lz_builtins);
Py_XDECREF(m->lz_from);
Py_XDECREF(m->lz_attr);
Py_TYPE(m)->tp_free((PyObject *)m);
}

static PyObject *
lazy_import_name(PyLazyImportObject *m)
{
if (m->lz_attr != NULL) {
if (PyUnicode_Check(m->lz_attr)) {
return PyUnicode_FromFormat("%U.%U", m->lz_from, m->lz_attr);
} else {
return PyUnicode_FromFormat("%U...", m->lz_from);
}
}
Py_INCREF(m->lz_from);
return m->lz_from;
}

static PyObject *
lazy_import_repr(PyLazyImportObject *m)
{
PyObject *name = lazy_import_name(m);
if (name == NULL) {
return NULL;
}
PyObject *res = PyUnicode_FromFormat("<lazy_import '%U'>", name);
Py_DECREF(name);
return res;
}

static int
lazy_import_traverse(PyLazyImportObject *m, visitproc visit, void *arg)
{
Py_VISIT(m->lz_builtins);
Py_VISIT(m->lz_from);
Py_VISIT(m->lz_attr);
return 0;
}

static int
lazy_import_clear(PyLazyImportObject *m)
{
Py_CLEAR(m->lz_builtins);
Py_CLEAR(m->lz_from);
Py_CLEAR(m->lz_attr);
return 0;
}

PyObject *
_PyLazyImport_GetName(PyObject *lazy_import)
{
assert(PyLazyImport_CheckExact(lazy_import));
return lazy_import_name((PyLazyImportObject *)lazy_import);
}

PyTypeObject PyLazyImport_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"lazy_import", /* tp_name */
sizeof(PyLazyImportObject), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)lazy_import_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
(reprfunc)lazy_import_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASETYPE, /* tp_flags */
0, /* tp_doc */
(traverseproc)lazy_import_traverse, /* tp_traverse */
(inquiry)lazy_import_clear, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
PyType_GenericNew, /* tp_new */
PyObject_GC_Del, /* tp_free */
};
4 changes: 2 additions & 2 deletions Parser/Python.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ module Python
| TryStar(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody)
| Assert(expr test, expr? msg)

| Import(alias* names)
| ImportFrom(identifier? module, alias* names, int? level)
| Import(alias* names, int? is_lazy)
| ImportFrom(identifier? module, alias* names, int? level, int? is_lazy)

| Global(identifier* names)
| Nonlocal(identifier* names)
Expand Down
2 changes: 1 addition & 1 deletion Parser/action_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -1926,7 +1926,7 @@ _PyPegen_checked_future_import(Parser *p, identifier module, asdl_alias_seq * na
}
}
}
return _PyAST_ImportFrom(module, names, level, lineno, col_offset, end_lineno, end_col_offset, arena);
return _PyAST_ImportFrom(module, names, level, 0, lineno, col_offset, end_lineno, end_col_offset, arena);
}

asdl_stmt_seq*
Expand Down
Loading
Loading