-
Notifications
You must be signed in to change notification settings - Fork 1
Lazyyy #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Lazyyy #105
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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 */ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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 */ | ||
| }; | ||
There was a problem hiding this comment.
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 generalelsebranch.