Skip to content

Fix Re-initialization #343

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

Merged
merged 2 commits into from
Jan 31, 2017
Merged
Changes from 1 commit
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
Next Next commit
Make RunString safer and more standard.
  • Loading branch information
filmor committed Jan 31, 2017
commit 9912712981867df79493832e32665127359f23dd
59 changes: 40 additions & 19 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,33 +411,54 @@ public static PyObject ModuleFromString(string name, string code)
/// executing the code string as a PyObject instance, or null if
/// an exception was raised.
/// </remarks>
public static PyObject RunString(string code)
public static PyObject RunString(
string code, IntPtr? globals = null, IntPtr? locals = null
)
{
IntPtr globals = Runtime.PyEval_GetGlobals();
IntPtr locals = Runtime.PyDict_New();

IntPtr builtins = Runtime.PyEval_GetBuiltins();
Runtime.PyDict_SetItemString(locals, "__builtins__", builtins);
bool borrowedGlobals = true;
if (globals == null)
{
globals = Runtime.PyEval_GetGlobals();
if (globals == IntPtr.Zero)
{
globals = Runtime.PyDict_New();
Runtime.PyDict_SetItemString(
globals.Value, "__builtins__",
Runtime.PyEval_GetBuiltins()
);
borrowedGlobals = false;
}
}

IntPtr flag = (IntPtr)257; /* Py_file_input */
IntPtr result = Runtime.PyRun_String(code, flag, globals, locals);
Runtime.XDecref(locals);
if (result == IntPtr.Zero)
bool borrowedLocals = true;
if (locals == null)
{
return null;
locals = Runtime.PyDict_New();
borrowedLocals = false;
}
return new PyObject(result);
}

public static PyObject RunString(string code, IntPtr globals, IntPtr locals)
{
IntPtr flag = (IntPtr)257; /* Py_file_input */
IntPtr result = Runtime.PyRun_String(code, flag, globals, locals);
if (result == IntPtr.Zero)

try
{
return null;
IntPtr result = Runtime.PyRun_String(
code, flag, globals.Value, locals.Value
);

if (Runtime.PyErr_Occurred() != 0)
{
throw new PythonException();
}

return new PyObject(result);
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't really see the issue here.

Copy link
Member Author

Choose a reason for hiding this comment

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

To elaborate, the link that you posted warns against relying on try {} finally {} as the finally may not be executed, but in such a case the problem has either exited already or is in the process of doing so, so we don't have to worry about dereferencing the Python resources anyhow.

Copy link
Contributor

Choose a reason for hiding this comment

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

It was more of a caveat, return statements within try statements confuse me at times.

}
finally
{
if (!borrowedLocals)
Runtime.XDecref(locals.Value);
if (!borrowedGlobals)
Runtime.XDecref(globals.Value);
}
return new PyObject(result);
}
}

Expand Down