- Notifications
You must be signed in to change notification settings - Fork752
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, IntPtr? globals = null, IntPtr? locals = null | ||
) | ||
{ | ||
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; | ||
} | ||
} | ||
bool borrowedLocals = true; | ||
if (locals == null) | ||
{ | ||
locals = Runtime.PyDict_New(); | ||
borrowedLocals = false; | ||
} | ||
IntPtr flag = (IntPtr)257; /* Py_file_input */ | ||
try | ||
{ | ||
IntPtr result = Runtime.PyRun_String( | ||
code, flag, globals.Value, locals.Value | ||
); | ||
if (Runtime.PyErr_Occurred() != 0) | ||
{ | ||
throw new PythonException(); | ||
} | ||
return new PyObject(result); | ||
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. The return within the 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. I don't really see the issue here. 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. To elaborate, the link that you posted warns against relying on 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. It was more of a caveat, return statements within | ||
} | ||
finally | ||
{ | ||
if (!borrowedLocals) | ||
Runtime.XDecref(locals.Value); | ||
if (!borrowedGlobals) | ||
Runtime.XDecref(globals.Value); | ||
} | ||
} | ||
} | ||