return 1;
}
-int
-_PyFrame_FastToLocalsWithError(_PyInterpreterFrame *frame)
+
+PyObject *
+_PyFrame_GetLocals(_PyInterpreterFrame *frame, int include_hidden)
{
/* Merge fast locals into f->f_locals */
PyObject *locals = frame->f_locals;
if (locals == NULL) {
locals = frame->f_locals = PyDict_New();
if (locals == NULL) {
- return -1;
+ return NULL;
+ }
+ }
+ PyObject *hidden = NULL;
+
+ /* If include_hidden, "hidden" fast locals (from inlined comprehensions in
+ module/class scopes) will be included in the returned dict, but not in
+ frame->f_locals; the returned dict will be a modified copy. Non-hidden
+ locals will still be updated in frame->f_locals. */
+ if (include_hidden) {
+ hidden = PyDict_New();
+ if (hidden == NULL) {
+ return NULL;
}
}
PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
_PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
if (kind & CO_FAST_HIDDEN) {
+ if (include_hidden && value != NULL) {
+ if (PyObject_SetItem(hidden, name, value) != 0) {
+ goto error;
+ }
+ }
continue;
}
if (value == NULL) {
PyErr_Clear();
}
else {
- return -1;
+ goto error;
}
}
}
else {
if (PyObject_SetItem(locals, name, value) != 0) {
- return -1;
+ goto error;
}
}
}
+
+ if (include_hidden && PyDict_Size(hidden)) {
+ PyObject *innerlocals = PyDict_New();
+ if (innerlocals == NULL) {
+ goto error;
+ }
+ if (PyDict_Merge(innerlocals, locals, 1) != 0) {
+ Py_DECREF(innerlocals);
+ goto error;
+ }
+ if (PyDict_Merge(innerlocals, hidden, 1) != 0) {
+ Py_DECREF(innerlocals);
+ goto error;
+ }
+ locals = innerlocals;
+ }
+ else {
+ Py_INCREF(locals);
+ }
+ Py_CLEAR(hidden);
+
+ return locals;
+
+ error:
+ Py_XDECREF(hidden);
+ return NULL;
+}
+
+
+int
+_PyFrame_FastToLocalsWithError(_PyInterpreterFrame *frame)
+{
+ PyObject *locals = _PyFrame_GetLocals(frame, 0);
+ if (locals == NULL) {
+ return -1;
+ }
+ Py_DECREF(locals);
return 0;
}
PyObject *locals)
/*[clinic end generated code: output=0a0824aa70093116 input=11ee718a8640e527]*/
{
- PyObject *result, *source_copy;
+ PyObject *result = NULL, *source_copy;
const char *str;
if (locals != Py_None && !PyMapping_Check(locals)) {
if (globals == Py_None) {
globals = PyEval_GetGlobals();
if (locals == Py_None) {
- locals = PyEval_GetLocals();
+ locals = _PyEval_GetFrameLocals();
if (locals == NULL)
return NULL;
}
+ else {
+ Py_INCREF(locals);
+ }
}
else if (locals == Py_None)
- locals = globals;
+ locals = Py_NewRef(globals);
+ else {
+ Py_INCREF(locals);
+ }
if (globals == NULL || locals == NULL) {
PyErr_SetString(PyExc_TypeError,
"eval must be given globals and locals "
"when called without a frame");
- return NULL;
+ goto error;
}
int r = PyDict_Contains(globals, &_Py_ID(__builtins__));
r = PyDict_SetItem(globals, &_Py_ID(__builtins__), PyEval_GetBuiltins());
}
if (r < 0) {
- return NULL;
+ goto error;
}
if (PyCode_Check(source)) {
if (PySys_Audit("exec", "O", source) < 0) {
- return NULL;
+ goto error;
}
if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
PyErr_SetString(PyExc_TypeError,
"code object passed to eval() may not contain free variables");
- return NULL;
+ goto error;
}
- return PyEval_EvalCode(source, globals, locals);
+ result = PyEval_EvalCode(source, globals, locals);
}
+ else {
+ PyCompilerFlags cf = _PyCompilerFlags_INIT;
+ cf.cf_flags = PyCF_SOURCE_IS_UTF8;
+ str = _Py_SourceAsString(source, "eval", "string, bytes or code", &cf, &source_copy);
+ if (str == NULL)
+ goto error;
- PyCompilerFlags cf = _PyCompilerFlags_INIT;
- cf.cf_flags = PyCF_SOURCE_IS_UTF8;
- str = _Py_SourceAsString(source, "eval", "string, bytes or code", &cf, &source_copy);
- if (str == NULL)
- return NULL;
+ while (*str == ' ' || *str == '\t')
+ str++;
- while (*str == ' ' || *str == '\t')
- str++;
+ (void)PyEval_MergeCompilerFlags(&cf);
+ result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
+ Py_XDECREF(source_copy);
+ }
- (void)PyEval_MergeCompilerFlags(&cf);
- result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
- Py_XDECREF(source_copy);
+ error:
+ Py_XDECREF(locals);
return result;
}
if (globals == Py_None) {
globals = PyEval_GetGlobals();
if (locals == Py_None) {
- locals = PyEval_GetLocals();
+ locals = _PyEval_GetFrameLocals();
if (locals == NULL)
return NULL;
}
+ else {
+ Py_INCREF(locals);
+ }
if (!globals || !locals) {
PyErr_SetString(PyExc_SystemError,
"globals and locals cannot be NULL");
return NULL;
}
}
- else if (locals == Py_None)
- locals = globals;
+ else if (locals == Py_None) {
+ locals = Py_NewRef(globals);
+ }
+ else {
+ Py_INCREF(locals);
+ }
if (!PyDict_Check(globals)) {
PyErr_Format(PyExc_TypeError, "exec() globals must be a dict, not %.100s",
Py_TYPE(globals)->tp_name);
- return NULL;
+ goto error;
}
if (!PyMapping_Check(locals)) {
PyErr_Format(PyExc_TypeError,
"locals must be a mapping or None, not %.100s",
Py_TYPE(locals)->tp_name);
- return NULL;
+ goto error;
}
int r = PyDict_Contains(globals, &_Py_ID(__builtins__));
if (r == 0) {
r = PyDict_SetItem(globals, &_Py_ID(__builtins__), PyEval_GetBuiltins());
}
if (r < 0) {
- return NULL;
+ goto error;
}
if (closure == Py_None) {
if (closure) {
PyErr_SetString(PyExc_TypeError,
"cannot use a closure with this code object");
- return NULL;
+ goto error;
}
} else {
int closure_is_ok =
PyErr_Format(PyExc_TypeError,
"code object requires a closure of exactly length %zd",
num_free);
- return NULL;
+ goto error;
}
}
if (PySys_Audit("exec", "O", source) < 0) {
- return NULL;
+ goto error;
}
if (!closure) {
"string, bytes or code", &cf,
&source_copy);
if (str == NULL)
- return NULL;
+ goto error;
if (PyEval_MergeCompilerFlags(&cf))
v = PyRun_StringFlags(str, Py_file_input, globals,
locals, &cf);
Py_XDECREF(source_copy);
}
if (v == NULL)
- return NULL;
+ goto error;
+ Py_DECREF(locals);
Py_DECREF(v);
Py_RETURN_NONE;
+
+ error:
+ Py_XDECREF(locals);
+ return NULL;
}
builtin_locals_impl(PyObject *module)
/*[clinic end generated code: output=b46c94015ce11448 input=7874018d478d5c4b]*/
{
- PyObject *d;
-
- d = PyEval_GetLocals();
- return Py_XNewRef(d);
+ return _PyEval_GetFrameLocals();
}
PyObject *d;
if (object == NULL) {
- d = Py_XNewRef(PyEval_GetLocals());
+ d = _PyEval_GetFrameLocals();
}
else {
if (_PyObject_LookupAttr(object, &_Py_ID(__dict__), &d) == 0) {