Skip to content

Internal implement Py_CompileString for compat with CPython 3.8.0 #1000

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 1 commit into from
Dec 2, 2019
Merged
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
6 changes: 3 additions & 3 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ public static PyObject ReloadModule(PyObject module)
/// </remarks>
public static PyObject ModuleFromString(string name, string code)
{
IntPtr c = Runtime.Py_CompileString(code, "none", (IntPtr)257);
IntPtr c = Runtime.Py_CompileString(code, "none", (int)RunFlagType.File);
Runtime.CheckExceptionOccurred();
IntPtr m = Runtime.PyImport_ExecCodeModule(name, c);
Runtime.CheckExceptionOccurred();
Expand All @@ -512,7 +512,7 @@ public static PyObject ModuleFromString(string name, string code)

public static PyObject Compile(string code, string filename = "", RunFlagType mode = RunFlagType.File)
{
var flag = (IntPtr)mode;
var flag = (int)mode;
IntPtr ptr = Runtime.Py_CompileString(code, filename, flag);
Runtime.CheckExceptionOccurred();
return new PyObject(ptr);
Expand Down Expand Up @@ -610,7 +610,7 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals,
}
}

public enum RunFlagType
public enum RunFlagType : int
{
Single = 256,
File = 257, /* Py_file_input */
Expand Down
30 changes: 29 additions & 1 deletion src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -763,8 +763,36 @@ public static extern int Py_Main(
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyEval_EvalCode(IntPtr co, IntPtr globals, IntPtr locals);

#if PYTHON2
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr Py_CompileString(string code, string file, int start);
#else
/// <summary>
/// Return value: New reference.
/// This is a simplified interface to Py_CompileStringFlags() below, leaving flags set to NULL.
/// </summary>
internal static IntPtr Py_CompileString(string str, string file, int start)
{
return Py_CompileStringFlags(str, file, start, IntPtr.Zero);
}

/// <summary>
/// Return value: New reference.
/// This is a simplified interface to Py_CompileStringExFlags() below, with optimize set to -1.
/// </summary>
internal static IntPtr Py_CompileStringFlags(string str, string file, int start, IntPtr flags)
{
return Py_CompileStringExFlags(str, file, start, flags, -1);
}

/// <summary>
/// Return value: New reference.
/// Like Py_CompileStringObject(), but filename is a byte string decoded from the filesystem encoding(os.fsdecode()).
/// </summary>
/// <returns></returns>
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr Py_CompileString(string code, string file, IntPtr tok);
internal static extern IntPtr Py_CompileStringExFlags(string str, string file, int start, IntPtr flags, int optimize);
#endif

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyImport_ExecCodeModule(string name, IntPtr code);
Expand Down