Skip to content

Commit abbe870

Browse files
amos402filmor
authored andcommitted
Internal implement Py_CompileString for compat with CPython 3.8.0 (#1000)
1 parent 34d8d35 commit abbe870

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/runtime/pythonengine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ public static PyObject ReloadModule(PyObject module)
503503
/// </remarks>
504504
public static PyObject ModuleFromString(string name, string code)
505505
{
506-
IntPtr c = Runtime.Py_CompileString(code, "none", (IntPtr)257);
506+
IntPtr c = Runtime.Py_CompileString(code, "none", (int)RunFlagType.File);
507507
Runtime.CheckExceptionOccurred();
508508
IntPtr m = Runtime.PyImport_ExecCodeModule(name, c);
509509
Runtime.CheckExceptionOccurred();
@@ -512,7 +512,7 @@ public static PyObject ModuleFromString(string name, string code)
512512

513513
public static PyObject Compile(string code, string filename = "", RunFlagType mode = RunFlagType.File)
514514
{
515-
var flag = (IntPtr)mode;
515+
var flag = (int)mode;
516516
IntPtr ptr = Runtime.Py_CompileString(code, filename, flag);
517517
Runtime.CheckExceptionOccurred();
518518
return new PyObject(ptr);
@@ -610,7 +610,7 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals,
610610
}
611611
}
612612

613-
public enum RunFlagType
613+
public enum RunFlagType : int
614614
{
615615
Single = 256,
616616
File = 257, /* Py_file_input */

src/runtime/runtime.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,8 +763,36 @@ public static extern int Py_Main(
763763
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
764764
internal static extern IntPtr PyEval_EvalCode(IntPtr co, IntPtr globals, IntPtr locals);
765765

766+
#if PYTHON2
767+
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
768+
internal static extern IntPtr Py_CompileString(string code, string file, int start);
769+
#else
770+
/// <summary>
771+
/// Return value: New reference.
772+
/// This is a simplified interface to Py_CompileStringFlags() below, leaving flags set to NULL.
773+
/// </summary>
774+
internal static IntPtr Py_CompileString(string str, string file, int start)
775+
{
776+
return Py_CompileStringFlags(str, file, start, IntPtr.Zero);
777+
}
778+
779+
/// <summary>
780+
/// Return value: New reference.
781+
/// This is a simplified interface to Py_CompileStringExFlags() below, with optimize set to -1.
782+
/// </summary>
783+
internal static IntPtr Py_CompileStringFlags(string str, string file, int start, IntPtr flags)
784+
{
785+
return Py_CompileStringExFlags(str, file, start, flags, -1);
786+
}
787+
788+
/// <summary>
789+
/// Return value: New reference.
790+
/// Like Py_CompileStringObject(), but filename is a byte string decoded from the filesystem encoding(os.fsdecode()).
791+
/// </summary>
792+
/// <returns></returns>
766793
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
767-
internal static extern IntPtr Py_CompileString(string code, string file, IntPtr tok);
794+
internal static extern IntPtr Py_CompileStringExFlags(string str, string file, int start, IntPtr flags, int optimize);
795+
#endif
768796

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

0 commit comments

Comments
 (0)