Skip to content

Commit c939e74

Browse files
committed
Getting the engine to work with both cp2 and cp3
1 parent df4f2e4 commit c939e74

File tree

1 file changed

+77
-79
lines changed

1 file changed

+77
-79
lines changed

Source/PythonEngine.pas

Lines changed: 77 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,7 +1901,6 @@ TPythonInterface=class(TDynamicDll)
19011901
_PyString_Resize:function (var ob:PPyObject;i:NativeInt):integer; cdecl;
19021902
Py_GetExecPrefixA : function : PAnsiChar; cdecl;
19031903
Py_GetPathA : function : PAnsiChar; cdecl;
1904-
Py_SetPathA : procedure (path : PAnsiChar); cdecl;
19051904
Py_SetPythonHomeA : procedure (home : PAnsiChar); cdecl;
19061905
Py_GetPythonHomeA : function : PAnsiChar; cdecl;
19071906
Py_GetPrefixA : function : PAnsiChar; cdecl;
@@ -4265,16 +4264,17 @@ procedure TPythonInterface.MapDll;
42654264
PyRange_Type := Import('PyRange_Type');
42664265
PySlice_Type := Import('PySlice_Type');
42674266
if not IsPython3000 then
4268-
PyString_Type := Import('PyString_Type')
4269-
else
4270-
PyString_Type := Import('PyBytes_Type');
4271-
PyBytes_Type := Import('PyBytes_Type');
4272-
PyByteArray_Type := Import('PyByteArray_Type');
4267+
PyString_Type := Import('PyString_Type');
4268+
4269+
if IsPython3000 then begin
4270+
PyBytes_Type := Import('PyBytes_Type');
4271+
PyByteArray_Type := Import('PyByteArray_Type');
4272+
end;
42734273
PyTuple_Type := Import('PyTuple_Type');
42744274
PyUnicode_Type := Import('PyUnicode_Type');
42754275
PyBaseObject_Type := Import('PyBaseObject_Type');
42764276
if not IsPython3000 then
4277-
PyBuffer_Type := Import('PyBuffer_Type');
4277+
PyBuffer_Type := Import('PyBuffer_Type');
42784278
PyCallIter_Type := Import('PyCallIter_Type');
42794279
PyCell_Type := Import('PyCell_Type');
42804280
PyClassMethod_Type := Import('PyClassMethod_Type');
@@ -4342,9 +4342,7 @@ procedure TPythonInterface.MapDll;
43424342
PyErr_WarnExplicit := Import('PyErr_WarnExplicit');
43434343
PyEval_GetBuiltins := Import('PyEval_GetBuiltins');
43444344
PyImport_GetModuleDict := Import('PyImport_GetModuleDict');
4345-
if IsPython3000 then
4346-
PyInt_FromLong := Import('PyLong_FromLong')
4347-
else
4345+
if not IsPython3000 then
43484346
PyInt_FromLong := Import('PyInt_FromLong');
43494347
PyArg_Parse := Import('PyArg_Parse');
43504348
PyArg_ParseTuple := Import('PyArg_ParseTuple');
@@ -4356,23 +4354,24 @@ procedure TPythonInterface.MapDll;
43564354
PyRun_String := Import('PyRun_String');
43574355
PyRun_SimpleString := Import('PyRun_SimpleString');
43584356
PyDict_GetItemString := Import('PyDict_GetItemString');
4359-
PySys_SetArgv := Import('PySys_SetArgv');
4357+
4358+
if IsPython3000 then
4359+
PySys_SetArgv := Import('PySys_SetArgv')
4360+
else
4361+
PySys_SetArgvA := Import('PySys_SetArgv');
43604362

43614363
if not IsPython3000 then begin
43624364
PyString_AsString := Import('PyString_AsString');
43634365
PyString_AsStringAndSize := Import('PyString_AsStringAndSize')
4364-
end else begin
4365-
PyString_AsString := Import('PyBytes_AsString');
4366-
PyString_AsStringAndSize := Import('PyBytes_AsStringAndSize');
43674366
end;
43684367

43694368
if not IsPython3000 then
43704369
DLL_PyString_FromString := Import('PyString_FromString');
43714370

4372-
Py_Exit := Import('Py_Exit');
4371+
Py_Exit := Import('Py_Exit');
43734372

43744373
if IsPython3000 then
4375-
PyCFunction_NewEx :=Import('PyCFunction_NewEx')
4374+
PyCFunction_NewEx :=Import('PyCFunction_NewEx')
43764375
else
43774376
PyCFunction_New :=Import('PyCFunction_New');
43784377

@@ -4382,19 +4381,19 @@ procedure TPythonInterface.MapDll;
43824381
PyEval_GetLocals := Import('PyEval_GetLocals');
43834382
//@PyEval_GetOwner :=Import('PyEval_GetOwner');
43844383
if not IsPython3000 then
4385-
PyEval_GetRestricted :=Import('PyEval_GetRestricted');
4384+
PyEval_GetRestricted := Import('PyEval_GetRestricted');
43864385
PyEval_InitThreads := Import('PyEval_InitThreads');
43874386
PyEval_RestoreThread := Import('PyEval_RestoreThread');
43884387
PyEval_SaveThread := Import('PyEval_SaveThread');
43894388
if not IsPython3000 then
4390-
PyFile_FromString :=Import('PyFile_FromString');
4389+
PyFile_FromString := Import('PyFile_FromString');
43914390
PyFile_GetLine := Import('PyFile_GetLine');
43924391
if not IsPython3000 then
4393-
PyFile_Name :=Import('PyFile_Name');
4392+
PyFile_Name := Import('PyFile_Name');
43944393
if not IsPython3000 then
4395-
PyFile_SetBufSize :=Import('PyFile_SetBufSize');
4394+
PyFile_SetBufSize := Import('PyFile_SetBufSize');
43964395
if not IsPython3000 then
4397-
PyFile_SoftSpace :=Import('PyFile_SoftSpace');
4396+
PyFile_SoftSpace := Import('PyFile_SoftSpace');
43984397
PyFile_WriteObject := Import('PyFile_WriteObject');
43994398
PyFile_WriteString := Import('PyFile_WriteString');
44004399
PyFloat_AsDouble := Import('PyFloat_AsDouble');
@@ -4436,8 +4435,10 @@ procedure TPythonInterface.MapDll;
44364435
PyLong_FromString := Import('PyLong_FromString');
44374436
PyLong_FromUnsignedLong := Import('PyLong_FromUnsignedLong');
44384437
PyLong_AsUnsignedLong := Import('PyLong_AsUnsignedLong');
4439-
PyLong_FromUnicode :=Import('PyLong_FromUnicode');
4440-
PyLong_FromUnicodeObject := Import('PyLong_FromUnicodeObject');
4438+
if not IsPython3000 then
4439+
PyLong_FromUnicode := Import('PyLong_FromUnicode');
4440+
if IsPython3000 then
4441+
PyLong_FromUnicodeObject := Import('PyLong_FromUnicodeObject');
44414442
PyLong_FromLongLong := Import('PyLong_FromLongLong');
44424443
PyLong_FromUnsignedLongLong := Import('PyLong_FromUnsignedLongLong');
44434444
PyLong_AsLongLong := Import('PyLong_AsLongLong');
@@ -4448,8 +4449,10 @@ procedure TPythonInterface.MapDll;
44484449
PyMapping_HasKeyString := Import('PyMapping_HasKeyString');
44494450
PyMapping_Length := Import('PyMapping_Length');
44504451
PyMapping_SetItemString := Import('PyMapping_SetItemString');
4451-
PyMapping_Keys := Import('PyMapping_Keys');
4452-
PyMapping_Values := Import('PyMapping_Values');
4452+
if IsPython3000 then begin
4453+
PyMapping_Keys := Import('PyMapping_Keys');
4454+
PyMapping_Values := Import('PyMapping_Values');
4455+
end;
44534456
if not IsPython3000 then
44544457
PyMethod_Class :=Import('PyMethod_Class');
44554458
PyMethod_Function := Import('PyMethod_Function');
@@ -4485,12 +4488,13 @@ procedure TPythonInterface.MapDll;
44854488
PyNumber_Rshift := Import('PyNumber_Rshift');
44864489
PyNumber_Subtract := Import('PyNumber_Subtract');
44874490
PyNumber_Xor := Import('PyNumber_Xor');
4488-
PyOS_InitInterrupts :=Import('PyOS_InitInterrupts');
4491+
if not IsPython3000 then
4492+
PyOS_InitInterrupts :=Import('PyOS_InitInterrupts');
44894493
PyOS_InterruptOccurred := Import('PyOS_InterruptOccurred');
44904494
PyObject_CallObject := Import('PyObject_CallObject');
44914495
PyObject_CallMethod := Import('PyObject_CallMethod');
44924496
if not IsPython3000 then
4493-
PyObject_Compare :=Import('PyObject_Compare');
4497+
PyObject_Compare :=Import('PyObject_Compare');
44944498
PyObject_RichCompare := Import('PyObject_RichCompare');
44954499
PyObject_RichCompareBool := Import('PyObject_RichCompareBool');
44964500
PyObject_GetAttr := Import('PyObject_GetAttr');
@@ -4518,7 +4522,8 @@ procedure TPythonInterface.MapDll;
45184522
PyObject_Call := Import('PyObject_Call');
45194523
PyObject_GenericGetAttr := Import('PyObject_GenericGetAttr');
45204524
PyObject_GenericSetAttr := Import('PyObject_GenericSetAttr');
4521-
PyObject_GC_Malloc :=Import('_PyObject_GC_Malloc');
4525+
if not IsPython3000 then
4526+
PyObject_GC_Malloc :=Import('_PyObject_GC_Malloc');
45224527
PyObject_Malloc := Import('PyObject_Malloc');
45234528
PyObject_GC_New := Import('_PyObject_GC_New');
45244529
PyObject_GC_NewVar := Import('_PyObject_GC_NewVar');
@@ -4554,16 +4559,19 @@ procedure TPythonInterface.MapDll;
45544559
PyString_DecodeEscape :=Import('PyString_DecodeEscape');
45554560
PyString_Repr :=Import('PyString_Repr');
45564561
end;
4557-
PyBytes_AsString := Import('PyBytes_AsString');
4558-
PyBytes_AsStringAndSize := Import('PyBytes_AsStringAndSize');
4559-
PyBytes_Concat := Import('PyBytes_Concat');
4560-
PyBytes_ConcatAndDel := Import('PyBytes_ConcatAndDel');
4561-
PyBytes_FromString := Import('PyBytes_FromString');
4562-
PyBytes_FromStringAndSize := Import('PyBytes_FromStringAndSize');
4563-
PyBytes_Size := Import('PyBytes_Size');
4564-
PyBytes_DecodeEscape := Import('PyBytes_DecodeEscape');
4565-
PyBytes_Repr := Import('PyBytes_Repr');
4566-
_PyBytes_Resize := Import('_PyBytes_Resize');
4562+
if IsPython3000 then
4563+
begin
4564+
PyBytes_AsString := Import('PyBytes_AsString');
4565+
PyBytes_AsStringAndSize := Import('PyBytes_AsStringAndSize');
4566+
PyBytes_Concat := Import('PyBytes_Concat');
4567+
PyBytes_ConcatAndDel := Import('PyBytes_ConcatAndDel');
4568+
PyBytes_FromString := Import('PyBytes_FromString');
4569+
PyBytes_FromStringAndSize := Import('PyBytes_FromStringAndSize');
4570+
PyBytes_Size := Import('PyBytes_Size');
4571+
PyBytes_DecodeEscape := Import('PyBytes_DecodeEscape');
4572+
PyBytes_Repr := Import('PyBytes_Repr');
4573+
_PyBytes_Resize := Import('_PyBytes_Resize');
4574+
end;
45674575
PyByteArray_AsString := Import('PyByteArray_AsString');
45684576
PyByteArray_Concat := Import('PyByteArray_Concat');
45694577
PyByteArray_Resize := Import('PyByteArray_Resize');
@@ -4584,56 +4592,49 @@ procedure TPythonInterface.MapDll;
45844592
PyType_GenericAlloc := Import('PyType_GenericAlloc');
45854593
PyType_GenericNew := Import('PyType_GenericNew');
45864594
PyType_Ready := Import('PyType_Ready');
4587-
if not IsPython3000 then begin
4588-
PyUnicode_FromWideChar := Import(AnsiString(Format('PyUnicode%s_FromWideChar',[UnicodeSuffix])));
4589-
PyUnicode_FromString := Import(AnsiString(Format('PyUnicode%s_FromString',[UnicodeSuffix])));
4590-
PyUnicode_FromStringAndSize := Import(AnsiString(Format('PyUnicode%s_FromStringAndSize',[UnicodeSuffix])));
4591-
PyUnicode_AsWideChar := Import(AnsiString(Format('PyUnicode%s_AsWideChar',[UnicodeSuffix])));
4592-
PyUnicode_Decode := Import(AnsiString(Format('PyUnicode%s_Decode',[UnicodeSuffix])));
4593-
PyUnicode_AsEncodedString := Import(AnsiString(Format('PyUnicode%s_AsEncodedString',[UnicodeSuffix])));
4594-
PyUnicode_FromOrdinal := Import(AnsiString(Format('PyUnicode%s_FromOrdinal',[UnicodeSuffix])));
4595-
PyUnicode_GetSize := Import(AnsiString(Format('PyUnicode%s_GetSize',[UnicodeSuffix])));
4596-
end else begin
4597-
PyUnicode_FromWideChar := Import('PyUnicode_FromWideChar');
4598-
PyUnicode_FromString := Import('PyUnicode_FromString');
4599-
PyUnicode_FromStringAndSize := Import('PyUnicode_FromStringAndSize');
4600-
PyUnicode_AsWideChar := Import('PyUnicode_AsWideChar');
4601-
PyUnicode_Decode := Import('PyUnicode_Decode');
4602-
PyUnicode_AsEncodedString := Import('PyUnicode_AsEncodedString');
4603-
PyUnicode_FromOrdinal := Import('PyUnicode_FromOrdinal');
4604-
end;
4605-
PyUnicode_FromKindAndData := Import('PyUnicode_FromKindAndData');
4606-
PyUnicode_AsUTF8 := Import('PyUnicode_AsUTF8');
4607-
PyUnicode_AsUTF8AndSize := Import('PyUnicode_AsUTF8AndSize');
4608-
PyUnicode_DecodeUTF16 := Import('PyUnicode_DecodeUTF16');
4609-
PyUnicode_GetLength := Import('PyUnicode_GetLength');
4595+
if not IsPython3000 then
4596+
PyUnicode_GetSize := Import(AnsiString(Format('PyUnicode%s_GetSize',[UnicodeSuffix])));
4597+
4598+
PyUnicode_FromWideChar := Import(AnsiString(Format('PyUnicode%s_FromWideChar',[UnicodeSuffix])));
4599+
PyUnicode_FromString := Import(AnsiString(Format('PyUnicode%s_FromString',[UnicodeSuffix])));
4600+
PyUnicode_FromStringAndSize := Import(AnsiString(Format('PyUnicode%s_FromStringAndSize',[UnicodeSuffix])));
4601+
PyUnicode_AsWideChar := Import(AnsiString(Format('PyUnicode%s_AsWideChar',[UnicodeSuffix])));
4602+
PyUnicode_Decode := Import(AnsiString(Format('PyUnicode%s_Decode',[UnicodeSuffix])));
4603+
PyUnicode_AsEncodedString := Import(AnsiString(Format('PyUnicode%s_AsEncodedString',[UnicodeSuffix])));
4604+
PyUnicode_FromOrdinal := Import(AnsiString(Format('PyUnicode%s_FromOrdinal',[UnicodeSuffix])));
4605+
4606+
if IsPython3000 then begin
4607+
PyUnicode_FromKindAndData := Import('PyUnicode_FromKindAndData');
4608+
PyUnicode_AsUTF8 := Import('PyUnicode_AsUTF8');
4609+
PyUnicode_AsUTF8AndSize := Import('PyUnicode_AsUTF8AndSize');
4610+
PyUnicode_DecodeUTF16 := Import('PyUnicode_DecodeUTF16');
4611+
PyUnicode_GetLength := Import('PyUnicode_GetLength');
4612+
end;
4613+
46104614
PyWeakref_GetObject := Import('PyWeakref_GetObject');
46114615
PyWeakref_NewProxy := Import('PyWeakref_NewProxy');
46124616
PyWeakref_NewRef := Import('PyWeakref_NewRef');
46134617
PyWrapper_New := Import('PyWrapper_New');
46144618
PyBool_FromLong := Import('PyBool_FromLong');
46154619
PyThreadState_SetAsyncExc := Import('PyThreadState_SetAsyncExc');
46164620
Py_AtExit := Import('Py_AtExit');
4617-
//Py_Cleanup :=Import('Py_Cleanup');
4621+
//Py_Cleanup := Import('Py_Cleanup');
46184622
Py_FatalError := Import('Py_FatalError');
46194623
if not IsPython3000 then begin
4620-
Py_FindMethod :=Import('Py_FindMethod');
4621-
Py_FindMethodInChain :=Import('Py_FindMethodInChain');
4622-
DLL_Py_FlushLine :=Import('Py_FlushLine');
4623-
_PyString_Resize :=Import('_PyString_Resize');
4624-
Py_CompileStringFlags :=Import('Py_CompileStringFlags');
4624+
Py_FindMethod := Import('Py_FindMethod');
4625+
Py_FindMethodInChain := Import('Py_FindMethodInChain');
4626+
DLL_Py_FlushLine := Import('Py_FlushLine');
4627+
_PyString_Resize := Import('_PyString_Resize');
4628+
Py_CompileStringFlags := Import('Py_CompileStringFlags');
46254629
end else begin
4626-
_PyString_Resize :=Import('_PyBytes_Resize');
4627-
Py_CompileStringExFlags :=Import('Py_CompileStringExFlags');
4630+
_PyString_Resize := Import('_PyBytes_Resize');
4631+
Py_CompileStringExFlags := Import('Py_CompileStringExFlags');
46284632
end;
46294633
_PyObject_New := Import('_PyObject_New');
46304634
Py_Finalize := Import('Py_Finalize');
46314635

4632-
if getProcAddress( FDLLHandle, 'PyCode_Addr2Line' ) <> nil then
4633-
DLL_PyCode_Addr2Line := Import('PyCode_Addr2Line');
4634-
4635-
if getProcAddress( FDLLHandle, 'PyImport_ExecCodeModule' ) <> nil then
4636-
DLL_PyImport_ExecCodeModule := Import('PyImport_ExecCodeModule');
4636+
DLL_PyCode_Addr2Line := Import('PyCode_Addr2Line', false);
4637+
DLL_PyImport_ExecCodeModule := Import('PyImport_ExecCodeModule', false);
46374638
//@PyClass_IsSubclass :=Import('PyClass_IsSubclass');
46384639

46394640
PyErr_ExceptionMatches := Import('PyErr_ExceptionMatches');
@@ -4644,9 +4645,7 @@ procedure TPythonInterface.MapDll;
46444645
Py_GetExecPrefix := Import('Py_GetExecPrefix');
46454646

46464647
if IsPython3000 then
4647-
Py_SetPath := Import('Py_SetPath')
4648-
else
4649-
Py_SetPathA := Import('Py_SetPath');
4648+
Py_SetPath := Import('Py_SetPath');
46504649

46514650
if IsPython3000 then
46524651
Py_GetPath := Import('Py_GetPath')
@@ -4690,8 +4689,7 @@ procedure TPythonInterface.MapDll;
46904689
Py_IsInitialized := Import('Py_IsInitialized');
46914690
Py_GetProgramFullPath := Import('Py_GetProgramFullPath');
46924691

4693-
if getProcAddress( FDLLHandle, 'Py_GetBuildInfo' ) <> nil then
4694-
DLL_Py_GetBuildInfo := Import('Py_GetBuildInfo');
4692+
DLL_Py_GetBuildInfo := Import('Py_GetBuildInfo', false);
46954693

46964694
Py_NewInterpreter := Import('Py_NewInterpreter');
46974695
Py_EndInterpreter := Import('Py_EndInterpreter');

0 commit comments

Comments
 (0)