diff --git a/Demos/Demo33/ThSort.dfm b/Demos/Demo33/ThSort.dfm index 3b4c41e6..07127d4a 100644 --- a/Demos/Demo33/ThSort.dfm +++ b/Demos/Demo33/ThSort.dfm @@ -180,6 +180,7 @@ object ThreadSortForm: TThreadSortForm Engine = PythonEngine1 OnInitialization = SortModuleInitialization ModuleName = 'SortModule' + MultInterpretersSupport = mmiPerInterpreterGIL Errors = <> Left = 64 Top = 88 diff --git a/Modules/DelphiVCL/TestVCL.py b/Modules/DelphiVCL/TestVCL.py index 2641d390..deae197c 100644 --- a/Modules/DelphiVCL/TestVCL.py +++ b/Modules/DelphiVCL/TestVCL.py @@ -40,6 +40,7 @@ def main(): f.Show() FreeConsole() Application.Run() + f.Free() main() diff --git a/Modules/DemoModule/InterpreterExecutor.py b/Modules/DemoModule/InterpreterExecutor.py new file mode 100644 index 00000000..97cca2ab --- /dev/null +++ b/Modules/DemoModule/InterpreterExecutor.py @@ -0,0 +1,35 @@ +#------------------------------------------------------------------------------- +# Name: InterpreterExecutor.py +# Purpose: Showcases the use of extension modules created with Delphi +# with the new in Python 3.14 InterpreterPoolExecutor +# You need python 3.14 to run this demo +# It uses the support module prime_utils which imports +# the delphi created extension module. +# Note that each interpreters has its own GIL and +# they are all running in parallel. +#------------------------------------------------------------------------------- + +from concurrent.futures import InterpreterPoolExecutor +from prime_utils import count_primes_in_range +import time + +def count_primes(max_num, num_interpreters=4): + chunk_size = max_num // num_interpreters + ranges = [(i, min(i + chunk_size - 1, max_num)) for i in range(2, max_num + 1, chunk_size)] + print(ranges) + + total = 0 + with InterpreterPoolExecutor(max_workers=num_interpreters) as executor: + results = executor.map(count_primes_in_range, ranges) + total = sum(results) + + return total + +if __name__ == "__main__": + max_number = 1_000_000 + start_time = time.time() + prime_count = count_primes(max_number) + end_time = time.time() + + print(f"Count of prime numbers up to {max_number}: {prime_count}") + print(f"Time taken: {end_time - start_time:.2f} seconds") \ No newline at end of file diff --git a/Modules/DemoModule/prime_utils.py b/Modules/DemoModule/prime_utils.py new file mode 100644 index 00000000..29abdea3 --- /dev/null +++ b/Modules/DemoModule/prime_utils.py @@ -0,0 +1,5 @@ +# prime_utils.py +from DemoModule import is_prime + +def count_primes_in_range(arange): + return sum(1 for n in range(arange[0], arange[1] + 1) if is_prime(n)) diff --git a/Modules/DemoModule/uMain.pas b/Modules/DemoModule/uMain.pas index 8df2b082..922ef62e 100644 --- a/Modules/DemoModule/uMain.pas +++ b/Modules/DemoModule/uMain.pas @@ -7,13 +7,14 @@ interface function PyInit_DemoModule: PPyObject; cdecl; implementation -Uses +uses + Winapi.Windows, System.Math, WrapDelphi; var - gEngine : TPythonEngine; - gModule : TPythonModule; + gEngine : TPythonEngine = nil; + gModule : TPythonModule = nil; function IsPrime(x: Integer): Boolean; // Naive implementation. It is just a demo @@ -46,6 +47,7 @@ function delphi_is_prime(self, args : PPyObject) : PPyObject; cdecl; function PyInit_DemoModule: PPyObject; begin + if not Assigned(gEngine) then try gEngine := TPythonEngine.Create(nil); gEngine.AutoFinalize := False; @@ -56,12 +58,18 @@ function PyInit_DemoModule: PPyObject; gModule.ModuleName := 'DemoModule'; gModule.AddMethod('is_prime', delphi_is_prime, 'is_prime(n) -> bool' ); + // We need to set this so that the module is not created by Initialzize + gModule.IsExtensionModule := True; + gModule.MultInterpretersSupport := mmiPerInterpreterGIL; + gEngine.LoadDllInExtensionModule; except + Exit(nil); end; - Result := gModule.Module; -end; + // The python import machinery will create the python module from ModuleDef + Result := gEngine.PyModuleDef_Init(@gModule.ModuleDef); +end; initialization finalization diff --git a/Modules/RttiModule/uMain.pas b/Modules/RttiModule/uMain.pas index 386b522f..38b5be84 100644 --- a/Modules/RttiModule/uMain.pas +++ b/Modules/RttiModule/uMain.pas @@ -17,6 +17,7 @@ implementation TDelphiFunctions = class public class function is_prime(const N: Integer): Boolean; static; + class procedure AfterModuleInit(Sender: TObject); end; var @@ -25,32 +26,37 @@ TDelphiFunctions = class gDelphiWrapper : TPyDelphiWrapper; DelphiFunctions: TDelphiFunctions; - - function PyInit_DemoModule: PPyObject; -var - Py : PPyObject; begin + if not Assigned(gEngine) then try gEngine := TPythonEngine.Create(nil); gEngine.AutoFinalize := False; gEngine.UseLastKnownVersion := True; + gDelphiWrapper := TPyDelphiWrapper.Create(nil); + gDelphiWrapper.Engine := gEngine; + + // !!It is important that the extension module is the last + // Engine client created gModule := TPythonModule.Create(nil); gModule.Engine := gEngine; gModule.ModuleName := 'DemoModule'; - gDelphiWrapper := TPyDelphiWrapper.Create(nil); - gDelphiWrapper.Engine := gEngine; + // Set IsExtensionModule so that the module is not created by Initialzize + gModule.IsExtensionModule := True; + gModule.MultInterpretersSupport := mmiPerInterpreterGIL; + gModule.OnAfterInitialization := TDelphiFunctions.AfterModuleInit; + gDelphiWrapper.Module := gModule; gEngine.LoadDllInExtensionModule; - Py := gDelphiWrapper.Wrap(DelphiFunctions, TObjectOwnership.soReference); - gModule.SetVar('delphi_funcs', Py); - gEngine.Py_DecRef(Py); except + Exit(nil); end; - Result := gModule.Module; + + // The python import machinery will create the python module from ModuleDef + Result := gEngine.PyModuleDef_Init(@gModule.ModuleDef); end; { TTestRttiAccess } @@ -58,6 +64,15 @@ function PyInit_DemoModule: PPyObject; { TDelphiFunctions } +class procedure TDelphiFunctions.AfterModuleInit(Sender: TObject); +var + Py : PPyObject; +begin + Py := gDelphiWrapper.Wrap(DelphiFunctions, TObjectOwnership.soReference); + gModule.SetVar('delphi_funcs', Py); + gEngine.Py_DecRef(Py); +end; + class function TDelphiFunctions.is_prime(const N: Integer): Boolean; // Naive implementation. It is just a demo... begin diff --git a/Source/PythonEngine.pas b/Source/PythonEngine.pas index 27c510a3..1d3cd846 100644 --- a/Source/PythonEngine.pas +++ b/Source/PythonEngine.pas @@ -344,6 +344,20 @@ TPythonVersionProp = record PyBUF_READ = $100; PyBUF_WRITE = $200; +const + // constants used in PyModuleDef slots from moduleobject.h + Py_mod_create = 1; + Py_mod_exec = 2; + Py_mod_multiple_interpreters = 3; // Added in version 3.12 + Py_mod_gil = 4; // Added in version 3.13 + + Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED: Pointer = Pointer(0); + Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED: Pointer = Pointer(1); + Py_MOD_PER_INTERPRETER_GIL_SUPPORTED: Pointer = Pointer(2); + + Py_MOD_GIL_USED: Pointer = Pointer(0); + Py_MOD_GIL_NOT_USED: Pointer = Pointer(1); + //####################################################### //## ## //## Non-Python specific constants ## @@ -538,7 +552,8 @@ TPythonVersionProp = record {#define PyDescr_COMMON \ PyObject_HEAD \ PyTypeObject *d_type; \ - PyObject *d_name + PyObject *d_name \ + PyObject *d_qualname } PPyDescrObject = ^PyDescrObject; @@ -549,6 +564,7 @@ TPythonVersionProp = record // End of the Head of an object d_type : PPyTypeObject; d_name : PPyObject; + d_qualname : PPyObject; end; PPyMethodDescrObject = ^PyMethodDescrObject; @@ -560,6 +576,7 @@ TPythonVersionProp = record // End of the Head of an object d_type : PPyTypeObject; d_name : PPyObject; + d_qualname : PPyObject; // End of PyDescr_COMMON d_method : PPyMethodDef; end; @@ -573,6 +590,7 @@ TPythonVersionProp = record // End of the Head of an object d_type : PPyTypeObject; d_name : PPyObject; + d_qualname : PPyObject; // End of PyDescr_COMMON d_member : PPyMemberDef; end; @@ -586,6 +604,7 @@ TPythonVersionProp = record // End of the Head of an object d_type : PPyTypeObject; d_name : PPyObject; + d_qualname : PPyObject; // End of PyDescr_COMMON d_getset : PPyGetSetDef; end; @@ -599,6 +618,7 @@ TPythonVersionProp = record // End of the Head of an object d_type : PPyTypeObject; d_name : PPyObject; + d_qualname : PPyObject; // End of PyDescr_COMMON d_base : pwrapperbase; d_wrapped : Pointer; // This can be any function pointer @@ -622,6 +642,7 @@ TPythonVersionProp = record PyModuleDef_Slot = {$IFDEF CPUX86}packed{$ENDIF} record slot: integer; value: Pointer; + class function Make(slot: integer; value: Pointer): PyModuleDef_Slot; static; end; PPyModuleDef = ^PyModuleDef; @@ -637,6 +658,10 @@ TPythonVersionProp = record m_free : inquiry; end; + // signature of functions used in slots + Py_create_module_function = function(spec: PPyObject; def: PPyModuleDef):PPyObject; cdecl; + Py_exec_module_function = function(module: PPyObject): Integer; cdecl; + // pybuffer.h PPy_buffer = ^Py_Buffer; @@ -1028,7 +1053,7 @@ PyConfig = record {$IFDEF CPU64BITS} ((8, 80, 88, 144, 156, 160, 164, 172, 224, 104, 240, 248, 256, 272), (8, 80, 88, 144, 156, 160, 164, 172, 224, 104, 240, 248, 256, 272), - (8, 80, 104, 152, 168, 172, 176, 184, 232, 240, 256, 272, 280, 296), + (8, 80, 104, 152, 168, 172, 176, 184, 240, 248, 264, 280, 288, 304), (8, 96, 120, 168, 184, 188, 192, 200, 264, 272, 288, 304, 312, 336), (8, 96, 120, 168, 184, 188, 192, 200, 268, 272, 288, 304, 312, 336), (8, 96, 120, 168, 184, 188, 192, 200, 272, 280, 296, 312, 320, 344)); @@ -1513,6 +1538,7 @@ TPythonInterface=class(TDynamicDll) PySuper_Type: PPyTypeObject; PyTraceBack_Type: PPyTypeObject; PyUnicode_Type: PPyTypeObject; + PyGetSetDescr_Type: PPyTypeObject; PyWrapperDescr_Type: PPyTypeObject; _PyWeakref_RefType: PPyTypeObject; _PyWeakref_ProxyType: PPyTypeObject; @@ -1534,6 +1560,9 @@ TPythonInterface=class(TDynamicDll) PyCallable_Check: function(ob : PPyObject): integer; cdecl; PyModule_Create2: function(moduledef: PPyModuleDef; Api_Version: Integer):PPyObject; cdecl; + PyModuleDef_Init: function(moduledef: PPyModuleDef):PPyObject; cdecl; + PyModule_ExecDef: function(module: PPyObject; moduledef: PPyModuleDef):Integer; cdecl; + PyModule_FromDefAndSpec2: function(moduledef: PPyModuleDef; spec: PPyObject; Api_Version: Integer):PPyObject; cdecl; PyErr_BadArgument: function: integer; cdecl; PyErr_BadInternalCall: procedure; cdecl; PyErr_CheckSignals: function: integer; cdecl; @@ -1642,6 +1671,7 @@ TPythonInterface=class(TDynamicDll) PyLong_FromLongLong:function(val:Int64): PPyObject; cdecl; PyLong_FromUnsignedLongLong:function(val:UInt64) : PPyObject; cdecl; PyLong_AsLongLong:function(ob:PPyObject): Int64; cdecl; + PyLong_AsVoidPtr:function(ob:PPyObject): Pointer; cdecl; PyLong_FromVoidPtr:function(p: Pointer): PPyObject; cdecl; PyMapping_Check:function (ob:PPyObject):integer; cdecl; PyMapping_GetItemString:function (ob:PPyObject;key:PAnsiChar):PPyObject; cdecl; @@ -1924,7 +1954,6 @@ TPythonInterface=class(TDynamicDll) function PyWeakref_CheckProxy( obj : PPyObject ) : Boolean; function PyBool_Check( obj : PPyObject ) : Boolean; function PyEnum_Check( obj : PPyObject ) : Boolean; - function Py_InitModule( const md : PyModuleDef) : PPyObject; // The following are defined as non-exported inline functions in object.h function Py_Type(ob: PPyObject): PPyTypeObject; inline; @@ -2109,6 +2138,7 @@ TPythonEngine = class(TPythonInterface) function ArrayToPyDict( const items : array of const) : PPyObject; function StringsToPyList( strings : TStrings ) : PPyObject; function StringsToPyTuple( strings : TStrings ) : PPyObject; + function Py_InitModule( const md : PyModuleDef) : PPyObject; procedure PyListToStrings(list: PPyObject; Strings: TStrings; ClearStrings: Boolean = True); procedure PyTupleToStrings( tuple: PPyObject; strings : TStrings ); function GetSequenceItem( sequence : PPyObject; idx : Integer ) : Variant; @@ -2165,7 +2195,7 @@ TPythonEngine = class(TPythonInterface) property IO: TPythonInputOutput read FIO write SetIO; property PyFlags: TPythonFlags read FPyFlags write SetPyFlags default DEFAULT_FLAGS; property RedirectIO: Boolean read FRedirectIO write FRedirectIO default True; - property UseWindowsConsole: Boolean read FUseWindowsConsole write FUseWindowsConsole default False; + property UseWindowsConsole: Boolean read FUseWindowsConsole write SetUseWindowsConsole default False; property OnAfterInit: TNotifyEvent read FOnAfterInit write FOnAfterInit; property OnSysPathInit: TSysPathInitEvent read FOnSysPathInit write FOnSysPathInit; property OnConfigInit: TConfigInitEvent read FOnConfigInit write FOnConfigInit; @@ -2274,7 +2304,6 @@ TMethodsContainer = class(TEngineClient) FMethodCount : Integer; FAllocatedMethodCount : Integer; FMethods : PPyMethodDef; - FModuleDef : PyModuleDef; FEventDefs: TEventDefs; procedure AllocMethods; @@ -2318,7 +2347,6 @@ TMethodsContainer = class(TEngineClient) property MethodCount : Integer read FMethodCount; property Methods[ idx : Integer ] : PPyMethodDef read GetMethods; property MethodsData : PPyMethodDef read FMethods; - property ModuleDef : PyModuleDef read FModuleDef; published property Events: TEventDefs read fEventDefs write fEventDefs stored StoreEventDefs; @@ -2473,65 +2501,71 @@ TErrors = class(TCollection) property Items[Index: Integer]: TError read GetError write SetError; default; end; + TMultIntperpretersSupport = (mmiSupported, mmiNotSupported, mmiPerInterpreterGIL); + {$IF not Defined(FPC) and (CompilerVersion >= 23)} [ComponentPlatformsAttribute(pidSupportedPlatforms)] {$IFEND} TPythonModule = class(TMethodsContainer) - protected - FModuleName : AnsiString; - FModule : PPyObject; - FClients : TList; - FErrors : TErrors; - FOnAfterInitialization : TNotifyEvent; - FDocString : TStringList; - - function GetClientCount : Integer; - function GetClients( idx : Integer ) : TEngineClient; - procedure SetErrors( val : TErrors ); - procedure SetModuleName( const val : AnsiString ); - procedure SetDocString( value : TStringList ); - public - // Constructors & destructors - constructor Create( AOwner : TComponent ); override; - destructor Destroy; override; + private + FModuleDef : PyModuleDef; + FMultInterpretersSupport: TMultIntperpretersSupport; + FEncodedDocString: AnsiString; + FIsExtensionModule: Boolean; + function Exec_Module(module: PPyObject): Integer; cdecl; // used in the slot + protected + FModuleName : AnsiString; + FModule : PPyObject; + FSlots: TArray; + FClients : TList; + FErrors : TErrors; + FDocString : TStringList; + FOnAfterInitialization : TNotifyEvent; + + function GetClientCount : Integer; + function GetClients( idx : Integer ) : TEngineClient; + procedure SetErrors( val : TErrors ); + procedure SetModuleName( const val : AnsiString ); + procedure SetDocString( value : TStringList ); + public + // Constructors & destructors + constructor Create( AOwner : TComponent ); override; + destructor Destroy; override; - // Public methods - procedure MakeModule; - procedure DefineDocString; - procedure Initialize; override; - procedure InitializeForNewInterpreter; - procedure AddClient(Client : TEngineClient); - procedure RemoveClient(Client : TEngineClient); - function ErrorByName( const AName : AnsiString ) : TError; - procedure RaiseError( const error, msg : AnsiString ); - procedure RaiseErrorFmt( const error, format : AnsiString; const Args : array of const ); - procedure RaiseErrorObj( const error, msg : AnsiString; obj : PPyObject ); - procedure BuildErrors; - procedure SetVar( const varName : AnsiString; value : PPyObject ); - function GetVar( const varName : AnsiString ) : PPyObject; - procedure DeleteVar( const varName : AnsiString ); - procedure ClearVars; - procedure SetVarFromVariant( const varName : AnsiString; const value : Variant ); - function GetVarAsVariant( const varName: AnsiString ) : Variant; + // Public methods + procedure MakeModuleDef; + procedure Initialize; override; + procedure InitializeForNewInterpreter; + procedure AddClient(Client : TEngineClient); + procedure RemoveClient(Client : TEngineClient); + function ErrorByName( const AName : AnsiString ) : TError; + procedure RaiseError( const error, msg : AnsiString ); + procedure RaiseErrorFmt( const error, format : AnsiString; const Args : array of const ); + procedure RaiseErrorObj( const error, msg : AnsiString; obj : PPyObject ); + procedure BuildErrors; + procedure SetVar( const varName : AnsiString; value : PPyObject ); + function GetVar( const varName : AnsiString ) : PPyObject; + procedure DeleteVar( const varName : AnsiString ); + procedure ClearVars; + procedure SetVarFromVariant( const varName : AnsiString; const value : Variant ); + function GetVarAsVariant( const varName: AnsiString ) : Variant; - // Public properties - property Module : PPyObject read FModule; - property Clients[ idx : Integer ] : TEngineClient read GetClients; - property ClientCount : Integer read GetClientCount; - published - property DocString : TStringList read FDocString write SetDocString; - property ModuleName : AnsiString read FModuleName write SetModuleName; - property Errors : TErrors read FErrors write SetErrors; - property OnAfterInitialization : TNotifyEvent read FOnAfterInitialization write FOnAfterInitialization; + // Public properties + property Module : PPyObject read FModule; + property ModuleDef : PyModuleDef read FModuleDef; + property IsExtensionModule: Boolean read FIsExtensionModule write FIsExtensionModule; + property Clients[ idx : Integer ] : TEngineClient read GetClients; + property ClientCount : Integer read GetClientCount; + published + property DocString : TStringList read FDocString write SetDocString; + property ModuleName : AnsiString read FModuleName write SetModuleName; + property MultInterpretersSupport: TMultIntperpretersSupport + read FMultInterpretersSupport write FMultInterpretersSupport; + property Errors : TErrors read FErrors write SetErrors; + property OnAfterInitialization : TNotifyEvent read FOnAfterInitialization write FOnAfterInitialization; end; -//------------------------------------------------------- -//-- -- -//--class: TPythonType derived from TGetSetContainer -- -//-- -- -//------------------------------------------------------- - { A B C +-------------------++------------------------------------------------------+ @@ -2548,15 +2582,15 @@ TPythonModule = class(TMethodsContainer) by GetSelf - a Python object must start at A. - - a Delphi class class must start at B + - a Delphi class must start at B - TPyObject.InstanceSize will return C-B - Sizeof(TPyObject) will return C-B - The total memory allocated for a TPyObject instance will be C-A, even if its InstanceSize is C-B. - - When turning a Python object pointer into a Delphi instance pointer, PythonToDelphi - will offset the pointer from A to B. - - When turning a Delphi instance into a Python object pointer, GetSelf will offset - Self from B to A. + - When turning a Python object pointer into a Delphi instance pointer, + PythonToDelphi will offset the pointer from A to B. + - When turning a Delphi instance into a Python object pointer, GetSelf + will offset Self from B to A. - Properties ob_refcnt and ob_type will call GetSelf to access their data. Further Notes: @@ -2573,7 +2607,6 @@ TPythonModule = class(TMethodsContainer) FreeInstance. - This class is heart of the P4D library. Pure magic!! } - // The base class of all new Python types TPyObject = class private function Get_ob_refcnt: NativeUInt; @@ -2743,8 +2776,15 @@ TTypeServices = class(TPersistent) property Mapping : TMappingServices read FMapping write FMapping; end; - // The component that initializes the Python type and - // that creates instances of itself. +//------------------------------------------------------- +//-- -- +//--class: TPythonType derived from TGetSetContainer -- +//-- -- +//------------------------------------------------------- + + // The component that initializes a Python type and + // creates instances of itself. + // The base class of all new Python types {$IF not Defined(FPC) and (CompilerVersion >= 23)} [ComponentPlatformsAttribute(pidSupportedPlatforms)] {$IFEND} @@ -3072,6 +3112,59 @@ implementation SPyExcSystemError = 'Unhandled SystemExit exception. Code: %s'; SPyInitFailed = 'Python initialization failed: %s'; SPyInitFailedUnknown = 'Unknown initialization error'; +SCannotCreateMain = 'Run_CommandAsObject: can''t create __main__'; +SRaiseError = 'RaiseError: couldn''t fetch last exception'; +SMissingModuleDateTime = 'dcmToDatetime DatetimeConversionMode cannot be used with this version of python. Missing module datetime'; +SInvalidDateTimeConvMode = 'Invalid DatetimeConversionMode'; +SUnexpectedTypeInTimeObject = 'Unexpected type found in member %s of a time_struct object'; +SArguementTypeNotAllowed = 'Argument type not allowed'; +SCouldNotCreateTuple = 'Could not create a new tuple object'; +SCouldNotCreateList = 'Could not create a new list object'; +SCouldNotCreateDict = 'Could not create a new dict object'; +SArgumemntsShouldBeEven = 'You must provide an even number of arguments'; +SExpectedList = 'The python object is not a list'; +SExpectedTuple = 'The python object is not a tuple'; +SCouldNotSetVar = 'Could not set var "%s" in module "%s"'; +SCannotSetVarNoInit = 'Can''t set var "%s" in module "%s", because it is not yet initialized'; +SCannotGetDict = 'Can''t get __dict__ of module "%s"'; +SCannotDelVarNoInit = 'Can''t delete var "%s" in module "%s", because it is not yet initialized'; +SExpectedDelphiClass = 'Pytho;n object "%s" is not a Delphi class'; +SCannotCreateModule = 'CreateVar: can''t create module "%s"'; +SVarNotCreated = 'No variable was created'; +SVarExists = 'A variable "%s" already exists in the module "%s"'; +SCannotCreateThreadState = 'Could not create a new thread state'; +SCannotCreatePythonEngine = 'No Python engine was created'; +SCannotInitPythonEngine = 'The Python engine is not properly initialized'; +SThreadPythonExec = 'ThreadPythonExec should only be called from the main thread'; +SQuitMessage = 'Dll %s could not be loaded. We must quit.'; +SPythonQuitMessage = 'Python DLL %s could not be initialized. We must quit.'; +SErrCannotOpenDLL = 'Error %d: Could not open Dll "%s"'; +SPythonNoInit = 'Python is not initialized'; +SOnlyOnePythonEngine = 'You canott have more than one TPythonEngine component'; +SMoreThanOnePythonEngine = 'There is already one instance of TPythonEngine running'; +SGlobalVarsShouldBeDict = 'You must set a Python dictionary in the GlobalVars property'; +SLocalVarsShouldBeDict = 'You must set a Python dictionary in the LocalVars property'; +SCannotModifyFlags = 'You can''t modify Python flags after it has been initialized'; +SCannotFindType = 'Could not find type: %s'; +SCannotFindModule = 'Could not find module: %s'; +SCannotFindComponent = 'Could not find component: %s'; +SCannotHandleMoreThan3Dim = 'Can''t convert a variant array of more than 3 dimensions to a Python sequence'; +SNoEngineForComponent = 'No Engine defined for component "%s"'; +SIndexOutOfRange = '%s: Index %d out of range'; +SUnknownMemberType = 'Unknown member type'; +SUnknownMemberFlag = 'Unknown member flag'; +SDuplicateErrorName = 'In module "%s", there''s already an error named "%s"'; +SNoModuleWithParentClass = 'Could not find module containing the parent class of error "%s"'; +SCannotFindParentClass = 'Could not find the parent class "%s" of error "%s"'; +SObjectNotClass = 'The object "%s" in module "%s" is not a class'; +SErrorNotClass = 'Error without name in module "%s"'; +SCouldNotCreateError = 'Could not create error "%s"'; +STErrorCouldNotCreateInstance = 'TError.RaiseErrorObj: Could not create an instance of "%s"'; +STErrorCouldNotCreateTuple = 'TError.RaiseErrorObj: Could not create an empty tuple'; +STErrorNoInstance = 'TError.RaiseErrorObj: I didn''t get an instance'; +SCouldNotFindError = 'Could not find error "%s"'; +SCouldNotMapSymbol = 'Error %d: could not map symbol "%s"'; +SUndeterminedPythonVersion = 'Undetermined Python version'; (*******************************************************) (** **) @@ -3423,7 +3516,7 @@ procedure TDynamicDll.OpenDll(const aDllName : string); if not IsHandleValid then begin {$IFDEF MSWINDOWS} - s := Format('Error %d: Could not open Dll "%s"',[GetLastError, DllName]); + s := Format(SErrCannotOpenDLL, [GetLastError, DllName]); {$ELSE} s := Format('Error: Could not open Dll "%s"',[DllName]); {$ENDIF} @@ -3471,7 +3564,7 @@ function TDynamicDll.Import(const funcname: AnsiString; canFail : Boolean = True {$IFEND} if (Result = nil) and canFail then begin {$IFDEF MSWINDOWS} - E := EDllImportError.CreateFmt('Error %d: could not map symbol "%s"', [GetLastError, funcname]); + E := EDllImportError.CreateFmt(SCouldNotMapSymbol, [GetLastError, funcname]); E.ErrorCode := GetLastError; {$ELSE} E := EDllImportError.CreateFmt('Error: could not map symbol "%s"', [funcname]); @@ -3566,7 +3659,7 @@ procedure TDynamicDll.LoadPythonInfoFromModule; end; if not LFound then - raise EDLLLoadError.Create('Undetermined Python version from loaded module.'); + raise EDLLLoadError.Create(SUndeterminedPythonVersion); end; procedure TDynamicDll.LoadDll; @@ -3619,7 +3712,7 @@ procedure TDynamicDll.BeforeUnload; function TDynamicDll.GetQuitMessage : string; begin - Result := Format( 'Dll %s could not be loaded. We must quit.', [DllName]); + Result := Format(SQuitMessage, [DllName]); end; function TDynamicDll.HasPythonSymbolsInLibrary: boolean; @@ -3692,7 +3785,7 @@ procedure TPythonInterface.AfterLoad; if not FInExtensionModule then PythonVersionFromDLLName(DLLName, FMajorVersion, FMinorVersion) else if not PythonVersionFromRegVersion(RegVersion, FMajorVersion, FMinorVersion) then - raise EDLLLoadError.Create('Undetermined Python version.'); + raise EDLLLoadError.Create(SUndeterminedPythonVersion); FBuiltInModuleName := 'builtins'; @@ -3713,13 +3806,13 @@ procedure TPythonInterface.AfterLoad; function TPythonInterface.GetQuitMessage : string; begin - Result := Format( 'Python could not be properly initialized. We must quit.', [DllName]); + Result := Format(SPythonQuitMessage, [DllName]); end; procedure TPythonInterface.CheckPython; begin if not Initialized then - raise Exception.Create('Python is not properly initialized' ); + raise Exception.Create(SPythonNoInit); end; procedure TPythonInterface.MapDll; @@ -3806,6 +3899,7 @@ procedure TPythonInterface.MapDll; PyStaticMethod_Type := Import('PyStaticMethod_Type'); PySuper_Type := Import('PySuper_Type'); PyTraceBack_Type := Import('PyTraceBack_Type'); + PyGetSetDescr_Type := Import('PyGetSetDescr_Type'); PyWrapperDescr_Type := Import('PyWrapperDescr_Type'); _PyWeakref_RefType := Import('_PyWeakref_RefType'); _PyWeakref_ProxyType := Import('_PyWeakref_ProxyType'); @@ -3839,6 +3933,9 @@ procedure TPythonInterface.MapDll; PyDict_SetItemString := Import('PyDict_SetItemString'); PyDictProxy_New := Import('PyDictProxy_New'); PyModule_Create2 := Import('PyModule_Create2'); + PyModuleDef_Init := Import('PyModuleDef_Init'); + PyModule_ExecDef := Import('PyModule_ExecDef'); + PyModule_FromDefAndSpec2 := Import('PyModule_FromDefAndSpec2'); PyErr_Print := Import('PyErr_Print'); PyErr_SetNone := Import('PyErr_SetNone'); PyErr_SetObject := Import('PyErr_SetObject'); @@ -3923,6 +4020,7 @@ procedure TPythonInterface.MapDll; PyLong_FromLongLong := Import('PyLong_FromLongLong'); PyLong_FromUnsignedLongLong := Import('PyLong_FromUnsignedLongLong'); PyLong_AsLongLong := Import('PyLong_AsLongLong'); + PyLong_AsVoidPtr := Import('PyLong_AsVoidPtr'); PyLong_FromVoidPtr := Import('PyLong_FromVoidPtr'); PyMapping_Check := Import('PyMapping_Check'); PyMapping_GetItemString := Import('PyMapping_GetItemString'); @@ -4362,21 +4460,6 @@ function TPythonInterface.PyObject_TypeCheck(obj: PPyObject; t: PPyTypeObject): Result := IsType(obj, t) or (PyType_IsSubtype(obj^.ob_type, t) = 1); end; -function TPythonInterface.Py_InitModule(const md: PyModuleDef): PPyObject; -Var - modules : PPyObject; -begin - CheckPython; - Result:= PyModule_Create2(@md, APIVersion); - if not Assigned(Result) then - GetPythonEngine.CheckError; - // To emulate Py_InitModule4 we need to add the module to sys.modules - modules := PyImport_GetModuleDict; - if PyDict_SetItemString(modules, md.m_name, Result) <> 0 then - GetPythonEngine.CheckError; -end; - - (*******************************************************) (** **) (** class TPythonTraceback **) @@ -4562,7 +4645,7 @@ constructor TPythonEngine.Create(AOwner: TComponent); for i := 0 to AOwner.ComponentCount - 1 do if (AOwner.Components[i] is TPythonEngine) and (AOwner.Components[i] <> Self) then - raise Exception.Create('You can''t drop more than one TPythonEngine component'); + raise Exception.Create(SOnlyOnePythonEngine); end; end; @@ -4669,7 +4752,7 @@ procedure TPythonEngine.DoOpenDll(const aDllName : string); procedure TPythonEngine.Initialize; - procedure ConfgigPEP587(var ErrMsg: string); + procedure ConfigPEP587(var ErrMsg: string); // Initialize according to PEP587 available since python 3.8 procedure AssignPyFlags(var Config: PyConfig); @@ -4794,7 +4877,7 @@ procedure TPythonEngine.Initialize; end; end; - procedure ConfgigPEP741(var ErrMsg: string); + procedure ConfigPEP741(var ErrMsg: string); // Initialize according to PEP587 available since python 3.8 procedure AssignPyFlags(Config: PPyInitConfig); @@ -4968,7 +5051,7 @@ procedure TPythonEngine.Initialize; ErrMsg: string; begin if Assigned(gPythonEngine) then - raise Exception.Create('There is already one instance of TPythonEngine running' ); + raise Exception.Create(SMoreThanOnePythonEngine); gPythonEngine := Self; @@ -4978,9 +5061,9 @@ procedure TPythonEngine.Initialize; else begin if (MajorVersion > 3) or (MinorVersion >= 14) then - ConfgigPEP741(ErrMsg) + ConfigPEP741(ErrMsg) else - ConfgigPEP587(ErrMsg); + ConfigPEP587(ErrMsg); if not FInitialized then begin @@ -5009,6 +5092,19 @@ procedure TPythonEngine.Initialize; if not Initialized then Initialize; + {$IFDEF MSWINDOWS} + // fix #504 + if not FRedirectIO and UseWindowsConsole then + PyRun_SimpleString( + 'import sys, io'#10 + + 'sys.stdout = io.TextIOWrapper(open("CONOUT$", "wb", buffering=0), ' + + 'encoding="utf-8", errors="replace", line_buffering=True)'#10 + + 'sys.stderr = io.TextIOWrapper(open("CONOUT$", "wb", buffering=0), ' + + 'encoding="utf-8", errors="replace", line_buffering=False)'#10 + + 'sys.stdin = io.TextIOWrapper(open("CONIN$", "rb", buffering=0), ' + + 'encoding="utf-8", errors="replace", line_buffering=True)'#10); + {$ENDIF} + if InitScript.Count > 0 then ExecStrings(InitScript); if Assigned(FOnAfterInit) then @@ -5064,10 +5160,12 @@ procedure TPythonEngine.InitWinConsole; FreeConsole; AllocConsole; SetConsoleTitle( 'Python console' ); + SetConsoleOutputCP(CP_UTF8); + SetConsoleCP(CP_UTF8); {$ENDIF} end; -procedure TPythonEngine.SetUseWindowsConsole( const Value : Boolean ); +procedure TPythonEngine.SetUseWindowsConsole(const Value: Boolean); begin FUseWindowsConsole := Value; if (csDesigning in ComponentState) then @@ -5085,7 +5183,7 @@ procedure TPythonEngine.SetGlobalVars(const Value: PPyObject); else begin FGlobalVars := nil; - raise Exception.Create('You must set a Python dictionary in the GlobalVars property'); + raise Exception.Create(SGlobalVarsShouldBeDict); end else FGlobalVars := nil; @@ -5103,7 +5201,7 @@ procedure TPythonEngine.SetLocalVars(const Value: PPyObject); else begin FLocalVars := nil; - raise Exception.Create('You must set a Python dictionary in the LocalVars property'); + raise Exception.Create(SLocalVarsShouldBeDict); end else FLocalVars := nil; @@ -5115,7 +5213,7 @@ procedure TPythonEngine.SetPyFlags(const Value: TPythonFlags); if FPyFlags <> Value then begin if Initialized then - raise Exception.Create('You can''t modify Python flags after it has been initialized'); + raise Exception.Create(SCannotModifyFlags); FPyFlags := Value; end; // of if end; @@ -5256,7 +5354,7 @@ function TPythonEngine.Run_CommandAsObjectWithDict(const command: AnsiString; m := GetMainModule; if m = nil then - raise EPythonError.Create('Run_CommandAsObject: can''t create __main__'); + raise EPythonError.Create(SCannotCreateMain); if Assigned(locals) then _locals := locals @@ -5507,9 +5605,7 @@ procedure TPythonEngine.RaiseError; s_type := GetTypeAsString(err_type); s_value := PyObjectAsString(err_value); - if (PyErr_GivenExceptionMatches(err_type, PyExc_SystemExit^) <> 0) then - raise Define( EPySystemExit.Create(''), s_type, s_value ) - else if (PyErr_GivenExceptionMatches(err_type, PyExc_StopIteration^) <> 0) then + if (PyErr_GivenExceptionMatches(err_type, PyExc_StopIteration^) <> 0) then raise Define( EPyStopIteration.Create(''), s_type, s_value ) else if (PyErr_GivenExceptionMatches(err_type, PyExc_KeyboardInterrupt^) <> 0) then raise Define( EPyKeyboardInterrupt.Create(''), s_type, s_value ) @@ -5600,7 +5696,7 @@ procedure TPythonEngine.RaiseError; raise Define( EPyExecError.Create(''), s_type, s_value ); end else - raise EPythonError.Create('RaiseError: couldn''t fetch last exception'); + raise EPythonError.Create(SRaiseError); end; function TPythonEngine.PyObjectAsString( obj : PPyObject ) : string; @@ -5734,22 +5830,22 @@ function TPythonEngine.TypeByName( const aTypeName : AnsiString ) : PPyTypeObjec Result := TheTypePtr; Exit; end; - raise Exception.CreateFmt('Could not find type: %s', [aTypeName]); + raise Exception.CreateFmt(SCannotFindType, [aTypeName]); end; -function TPythonEngine.ModuleByName( const aModuleName : AnsiString ) : PPyObject; +function TPythonEngine.ModuleByName( const aModuleName : AnsiString ) : PPyObject; var i : Integer; begin for i := 0 to ClientCount - 1 do if Clients[i] is TPythonModule then - with TPythonModule( Clients[i] ) do + with TPythonModule(Clients[i]) do if ModuleName = aModuleName then begin Result := Module; Exit; end; - raise Exception.CreateFmt('Could not find module: %s', [aModuleName]); + raise Exception.CreateFmt(SCannotFindModule, [aModuleName]); end; function TPythonEngine.MethodsByName( const aMethodsContainer: string ) : PPyMethodDef; @@ -5764,7 +5860,7 @@ function TPythonEngine.MethodsByName( const aMethodsContainer: string ) : PPyMet Result := MethodsData; Exit; end; - raise Exception.CreateFmt('Could not find component: %s', [aMethodsContainer]); + raise Exception.CreateFmt(SCannotFindComponent, [aMethodsContainer]); end; function TPythonEngine.VariantAsPyObject( const V : Variant ) : PPyObject; @@ -5884,7 +5980,7 @@ function TPythonEngine.VariantAsPyObject( const V : Variant ) : PPyObject; else if (DatetimeConversionMode = dcmToDatetime) then begin if not Assigned(FPyDateTime_DateTimeType) then - raise EPythonError.Create('dcmToDatetime DatetimeConversionMode cannot be used with this version of python. Missing module datetime'); + raise EPythonError.Create(SMissingModuleDateTime); args := ArrayToPyTuple([y, m, d, h, mi, sec, ms*1000]); try Result := PyObject_Call(FPyDateTime_DateTimeType, args, nil); @@ -5894,7 +5990,7 @@ function TPythonEngine.VariantAsPyObject( const V : Variant ) : PPyObject; end; end else - raise EPythonError.Create('Invalid DatetimeConversionMode'); + raise EPythonError.Create(SInvalidDateTimeConvMode); end; varOleStr: begin @@ -5922,7 +6018,7 @@ function TPythonEngine.VariantAsPyObject( const V : Variant ) : PPyObject; 2: Result := ArrayVarDim2; 3: Result := ArrayVarDim3; else - raise Exception.Create('Can''t convert a variant array of more than 3 dimensions to a Python sequence'); + raise Exception.Create(SCannotHandleMoreThan3Dim); end; end else if VarIsNull(DeRefV) or VarIsEmpty(DeRefV) then @@ -5948,7 +6044,7 @@ function TPythonEngine.PyObjectAsVariant( obj : PPyObject ) : Variant; if PyLong_Check(member) then Result := PyLong_AsLong(member) else - raise EPythonError.CreateFmt('Unexpected type found in member %s of a time_struct object', [AMember]); + raise EPythonError.CreateFmt(SUnexpectedTypeInTimeObject, [AMember]); Py_XDecRef(member); end; @@ -6138,7 +6234,7 @@ function TPythonEngine.VarRecAsPyObject( const v : TVarRec ) : PPyObject; Result := PyUnicodeFromString(''); end; else - Raise Exception.Create('Argument type not allowed'); + Raise Exception.Create(SArguementTypeNotAllowed); end; end; @@ -6151,7 +6247,7 @@ function TPythonEngine.MakePyTuple( const objects : array of PPyObject ) : PPyOb begin Result := PyTuple_New( High(objects)+1 ); if not Assigned(Result) then - raise EPythonError.Create('Could not create a new tuple object'); + raise EPythonError.Create(SCouldNotCreateTuple); for i := Low(objects) to High(objects) do begin Py_XINCREF( objects[i] ); @@ -6168,7 +6264,7 @@ function TPythonEngine.MakePyList( const objects : array of PPyObject ) : PPyObj begin Result := PyList_New( High(objects)+1 ); if not Assigned(Result) then - raise EPythonError.Create('Could not create a new list object'); + raise EPythonError.Create(SCouldNotCreateList); for i := Low(objects) to High(objects) do begin Py_XIncRef( objects[i] ); @@ -6182,7 +6278,7 @@ function TPythonEngine.ArrayToPyTuple( const items : array of const) : PPyObject begin Result := PyTuple_New( High(items)+1 ); if not Assigned(Result) then - raise EPythonError.Create('Could not create a new tuple object'); + raise EPythonError.Create(SCouldNotCreateTuple); for i := Low(items) to High(items) do PyTuple_SetItem( Result, i, VarRecAsPyObject( items[i] ) ); end; @@ -6193,7 +6289,7 @@ function TPythonEngine.ArrayToPyList( const items : array of const) : PPyObject; begin Result := PyList_New( High(items)+1 ); if not Assigned(Result) then - raise EPythonError.Create('Could not create a new list object'); + raise EPythonError.Create(SCouldNotCreateList); for i := Low(items) to High(items) do PyList_SetItem( Result, i, VarRecAsPyObject( items[i] ) ); end; @@ -6249,7 +6345,7 @@ function TPythonEngine.ArrayToPyDict( const items : array of const) : PPyObject; Result := ''; end; else - Raise Exception.Create('Argument type not allowed'); + Raise Exception.Create(SArguementTypeNotAllowed); end; end; @@ -6259,10 +6355,10 @@ function TPythonEngine.ArrayToPyDict( const items : array of const) : PPyObject; obj : PPyObject; begin if ((High(items)+1) mod 2) <> 0 then - raise Exception.Create('You must provide an even number of arguments'); + raise Exception.Create(SArgumemntsShouldBeEven); Result := PyDict_New; if not Assigned(Result) then - raise EPythonError.Create('Could not create a new dict object'); + raise EPythonError.Create(SCouldNotCreateDict); i := Low(items); try while i <= High(items) do @@ -6287,7 +6383,7 @@ function TPythonEngine.StringsToPyList( strings : TStrings ) : PPyObject; begin Result := PyList_New( strings.Count ); if not Assigned(Result) then - raise EPythonError.Create('Could not create a new list object'); + raise EPythonError.Create(SCouldNotCreateList); for i := 0 to strings.Count - 1 do PyList_SetItem( Result, i, PyUnicodeFromString(strings.Strings[i])); @@ -6299,7 +6395,7 @@ function TPythonEngine.StringsToPyTuple( strings : TStrings ) : PPyObject; begin Result := PyTuple_New( strings.Count ); if not Assigned(Result) then - raise EPythonError.Create('Could not create a new tuple object'); + raise EPythonError.Create(SCouldNotCreateTuple); for i := 0 to strings.Count - 1 do PyTuple_SetItem( Result, i, PyUnicodeFromString(strings.Strings[i])); @@ -6311,7 +6407,7 @@ procedure TPythonEngine.PyListToStrings(list: PPyObject; Strings: TStrings; i : Integer; begin if not PyList_Check(list) then - raise EPythonError.Create('the python object is not a list'); + raise EPythonError.Create(SExpectedList); if ClearStrings then Strings.Clear; for i := 0 to PyList_Size( list ) - 1 do @@ -6323,7 +6419,7 @@ procedure TPythonEngine.PyTupleToStrings( tuple: PPyObject; strings : TStrings ) i : Integer; begin if not PyTuple_Check(tuple) then - raise EPythonError.Create('the python object is not a tuple'); + raise EPythonError.Create(SExpectedTuple); strings.Clear; for i := 0 to PyTuple_Size( tuple ) - 1 do strings.Add( PyObjectAsString( PyTuple_GetItem( tuple, i ) ) ); @@ -6543,6 +6639,7 @@ procedure TPythonEngine.CheckError(ACatchStopEx : Boolean = False); var errtype, errvalue, errtraceback: PPyObject; SErrValue: string; + SystemExit: EPySystemExit; begin // PyErr_Fetch clears the error. The returned python objects are new references PyErr_Fetch(errtype, errvalue, errtraceback); @@ -6551,7 +6648,11 @@ procedure TPythonEngine.CheckError(ACatchStopEx : Boolean = False); Py_XDECREF(errtype); Py_XDECREF(errvalue); Py_XDECREF(errtraceback); - raise EPySystemExit.CreateResFmt(@SPyExcSystemError, [SErrValue]); + + SystemExit := EPySystemExit.CreateResFmt(@SPyExcSystemError, [SErrValue]); + SystemExit.EValue := SErrValue; + SystemExit.EName := 'SystemExit'; + raise SystemExit; end; var @@ -6643,6 +6744,64 @@ function TPythonEngine.PyUnicodeFromString(const AString: AnsiString): PPyObject end; +function TPythonEngine.Py_InitModule(const md: PyModuleDef): PPyObject; +// Implements multi-phase module intialization +var + modules, importlib, spec_func, module_name, args, spec: PPyObject; +begin + CheckPython; + + importlib := nil; + spec_func := nil; + module_name := nil; + args := nil; + spec := nil; + + try + // We need a spec and for that we need importlib; + importlib := PyImport_ImportModule('importlib.util'); + if not Assigned(importlib) then CheckError; + + // Get spec_from_loader function + spec_func := PyObject_GetAttrString(importlib, 'spec_from_loader'); + if not Assigned(spec_func) then CheckError; + + // Create module name + module_name := PyUnicode_FromString(md.m_name); + if not Assigned(module_name) then CheckError; + + // Create arguments tuple for spec_from_loader(name, loader) + args := MakePyTuple([module_name, Py_None]); + + // Create the module specification + spec := PyObject_CallObject(spec_func, args); + if not Assigned(spec) then CheckError; + + // Create the module from the definition and spec + Result := PyModule_FromDefAndSpec2(@md, spec, APIVersion); + if not Assigned(spec) then CheckError; + + // Execute the module (triggers Py_mod_exec slot) + if (PyModule_ExecDef(Result, @md) < 0) then + begin + Py_DECREF(Result); + CheckError; + end; + + finally + Py_XDECREF(importlib); + Py_XDECREF(spec_func); + Py_XDECREF(module_name); + Py_XDECREF(args); + Py_XDECREF(spec); + end; + + // We need to add the module to sys.modules + modules := PyImport_GetModuleDict; + if PyDict_SetItemString(modules, md.m_name, Result) <> 0 then + GetPythonEngine.CheckError; +end; + (*******************************************************) (** **) (** class TEngineClient **) @@ -6725,7 +6884,7 @@ procedure TEngineClient.ClearEngine; procedure TEngineClient.CheckEngine; begin if not Assigned(FEngine) then - raise Exception.CreateFmt('No Engine defined for component "%s"', [Name]); + raise Exception.CreateFmt(SNoEngineForComponent, [Name]); end; @@ -6871,7 +7030,7 @@ procedure TMethodsContainer.ReallocMethods; function TMethodsContainer.GetMethods( idx : Integer ) : PPyMethodDef; begin if (idx < 0) or (idx > MethodCount) then - raise Exception.CreateFmt('%s: Index %d out of range', [ClassName, idx]); + raise Exception.CreateFmt(SIndexOutOfRange, [ClassName, idx]); Result := @( FMethods[idx] ); end; @@ -7004,7 +7163,7 @@ procedure TMembersContainer.AddMember(MemberName: PAnsiChar; MemberType : TPyMe mtStringInplace: _type := T_STRING_INPLACE; mtObjectEx: _type := T_OBJECT_EX; else - raise Exception.Create('Unknown member type'); + raise Exception.Create(SUnknownMemberType); end; offset := MemberOffset + GetMembersStartOffset; case MemberFlags of @@ -7014,9 +7173,9 @@ procedure TMembersContainer.AddMember(MemberName: PAnsiChar; MemberType : TPyMe mfWriteRestricted: flags := PY_WRITE_RESTRICTED; mfRestricted: flags := RESTRICTED; else - raise Exception.Create('Unknown member flag'); + raise Exception.Create(SUnknownMemberFlag); end; - doc := MemberDoc; + doc := MemberDoc; end; Inc( FMemberCount ); end; @@ -7067,7 +7226,7 @@ procedure TMembersContainer.FreeMembers; function TMembersContainer.GetMembers(idx: Integer): PPyMemberDef; begin if (idx < 0) or (idx > MemberCount) then - raise Exception.CreateFmt('%s: Index %d out of range', [ClassName, idx]); + raise Exception.CreateFmt(SIndexOutOfRange, [ClassName, idx]); Result := @( FMembers[idx] ); end; @@ -7144,7 +7303,7 @@ procedure TGetSetContainer.FreeGetSets; function TGetSetContainer.GetGetSet(idx: Integer): PPyGetSetDef; begin if (idx < 0) or (idx > GetSetCount) then - raise Exception.CreateFmt('%s: Index %d out of range', [ClassName, idx]); + raise Exception.CreateFmt(SIndexOutOfRange, [ClassName, idx]); Result := @( FGetSets[idx] ); end; @@ -7181,7 +7340,8 @@ procedure TParentClassError.AssignTo( Dest: TPersistent ); function TError.GetDisplayName: string; begin Result := string(Name); - if Result = '' then Result := inherited GetDisplayName; + if Result = '' then + Result := inherited GetDisplayName; end; procedure TError.SetName( const Value : AnsiString ); @@ -7199,8 +7359,8 @@ procedure TError.SetName( const Value : AnsiString ); for i := 0 to Count - 1 do with Items[i] do if Name = Value then - raise Exception.CreateFmt( 'In module "%s", there''s already an error named "%s"', - [m.ModuleName, Value]); + raise Exception.CreateFmt(SDuplicateErrorName, + [m.ModuleName, Value]); end; end; @@ -7310,13 +7470,13 @@ procedure TError.BuildError( const ModuleName : AnsiString ); else m := FindModule( ModuleName ); if not Assigned(m) then - raise Exception.CreateFmt('Could not find module containing the parent class of error "%s"', [Self.Name]); + raise Exception.CreateFmt(SNoModuleWithParentClass, [Self.Name]); d := PyModule_GetDict(m); Result := PyDict_GetItemString( d, PAnsiChar(ParentClass.Name) ); if not Assigned(Result) then - raise Exception.CreateFmt('Could not find the parent class "%s" of error "%s"', [ParentClass.Name, Self.Name]); + raise Exception.CreateFmt(SCannotFindParentClass, [ParentClass.Name, Self.Name]); if not PyClass_Check( Result ) and not PyType_CheckExact( Result ) then - raise Exception.CreateFmt('The object "%s" in module "%s" is not a class', [ParentClass.Name, ParentClass.Module] ); + raise Exception.CreateFmt(SObjectNotClass, [ParentClass.Name, ParentClass.Module] ); end; end; @@ -7327,7 +7487,7 @@ procedure TError.BuildError( const ModuleName : AnsiString ); Exit; if Name = '' then with GetOwner as TPythonModule do - raise Exception.CreateFmt( 'Error without name in module "%s"', [ModuleName] ); + raise Exception.CreateFmt(SErrorNotClass, [ModuleName] ); if Text = '' then Text := Name; Owner.Owner.CheckEngine; @@ -7347,7 +7507,7 @@ procedure TError.BuildError( const ModuleName : AnsiString ); end; end; if not Assigned(Error) then - raise Exception.CreateFmt( 'Could not create error "%s"', [Name] ); + raise Exception.CreateFmt(SCouldNotCreateError, [Name]); end; procedure TError.RaiseError(const msg : AnsiString); @@ -7373,18 +7533,18 @@ procedure TError.RaiseErrorObj(const msg : AnsiString; obj : PPyObject); begin res := PyObject_CallObject(Error, nil); if not Assigned(res) then - raise Exception.CreateFmt('TError.RaiseErrorObj: Could not create an instance of "%s"', [Self.Name]); + raise Exception.CreateFmt(STErrorCouldNotCreateInstance, [Self.Name]); if PyObject_TypeCheck(res, PPyTypeObject(PyExc_Exception^)) then begin args := PyTuple_New(1); if not Assigned(args) then - raise Exception.Create('TError.RaiseErrorObj: Could not create an empty tuple'); + raise Exception.Create(STErrorCouldNotCreateTuple); str := PyUnicodeFromString(msg); PyTuple_SetItem(args, 0, str); res := PyObject_Call(Error, args, nil); Py_DECREF(args); if not Assigned(res) then - raise Exception.CreateFmt('TError.RaiseErrorObj: Could not create an instance of "%s"', [Self.Name]); + raise Exception.CreateFmt(STErrorCouldNotCreateInstance, [Self.Name]); keys := PyDict_Keys(obj); for i := 0 to PySequence_Length(keys)-1 do begin @@ -7400,7 +7560,7 @@ procedure TError.RaiseErrorObj(const msg : AnsiString; obj : PPyObject); Py_XDECREF(keys); end else - raise Exception.Create('TError.RaiseErrorObj: I didn''t get an instance' ); + raise Exception.Create(STErrorNoInstance); PyErr_SetObject(Error, res); Py_XDECREF(res); end @@ -7504,6 +7664,7 @@ constructor TPythonModule.Create( AOwner : TComponent ); FClients := TList.Create; FErrors := TErrors.Create(Self); FDocString := TStringList.Create; + FDocString.TrailingLineBreak := False; end; destructor TPythonModule.Destroy; @@ -7519,52 +7680,59 @@ procedure TPythonModule.SetDocString( value : TStringList ); FDocString.Assign( value ); end; -procedure TPythonModule.DefineDocString; +procedure TPythonModule.MakeModuleDef; var - doc : PPyObject; + P: Pointer; begin - with Engine do - begin - if DocString.Text <> '' then - begin - doc := - PyUnicodeFromString(CleanString(FDocString.Text, False)); - PyObject_SetAttrString( FModule, '__doc__', doc ); - Py_XDecRef(doc); - CheckError(False); - end; - end; -end; + FillChar(FModuleDef, SizeOf(FModuleDef), 0); + FModuleDef.m_base.ob_refcnt := 1; + FModuleDef.m_name := PAnsiChar(ModuleName); + FModuleDef.m_methods := MethodsData; + FModuleDef.m_size := 0; -procedure TPythonModule.MakeModule; -begin - CheckEngine; - if Assigned(FModule) then - Exit; - with Engine do - begin - FillChar(FModuleDef, SizeOf(FModuleDef), 0); - FModuleDef.m_base.ob_refcnt := 1; - FModuleDef.m_name := PAnsiChar(ModuleName); - FModuleDef.m_methods := MethodsData; - FModuleDef.m_size := -1; - FModule := Py_InitModule( ModuleDef ); - DefineDocString; + // Doc string + if FDocString.Count > 0 then + begin + FEncodedDocString := UTF8Encode(CleanString(FDocString.Text, False)); + FModuleDef.m_doc := PAnsiChar(FEncodedDocString); + end; + + // Fill the m_slots for multi-phase initialization + FSlots := [PyModuleDef_Slot.Make(Py_mod_exec, + GetCallBack(Self, @TPythonModule.Exec_Module, 1, DEFAULT_CALLBACK_TYPE))]; + + if (Engine.MajorVersion > 3) or (Engine.MinorVersion >= 12) then + begin + case FMultInterpretersSupport of + mmiNotSupported: P := Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED; + mmiPerInterpreterGIL: P := Py_MOD_PER_INTERPRETER_GIL_SUPPORTED; + else + P := Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED; end; + FSlots := FSlots + [PyModuleDef_Slot.Make(Py_mod_multiple_interpreters, P)]; + end; + FSlots := FSlots + [PyModuleDef_Slot.Make(0, nil)]; + + FModuleDef.m_slots := @FSlots[0]; end; procedure TPythonModule.Initialize; -var - i : Integer; begin inherited; - FModule := nil; - MakeModule; - for i := 0 to ClientCount - 1 do - Clients[i].ModuleReady(Self); - BuildErrors; - if Assigned(FOnAfterInitialization) then - FOnAfterInitialization( Self ); + + if Assigned(FModule) then Exit; + + MakeModuleDef; + // Py_InitModule will call Exec_Module which will + // - Set FModule + // - initialize clients + // - Call OnInitialized + CheckEngine; + + // Extension modules are intilized directly from ModuleDef + if FIsExtensionModule then Exit; + + FModule := Engine.Py_InitModule(FModuleDef); end; procedure TPythonModule.InitializeForNewInterpreter; @@ -7600,7 +7768,22 @@ function TPythonModule.ErrorByName( const AName : AnsiString ) : TError; Result := Errors.Items[i]; Exit; end; - raise Exception.CreateFmt( 'Could not find error "%s"', [AName] ); + raise Exception.CreateFmt(SCouldNotFindError, [AName] ); +end; + +function TPythonModule.Exec_Module(module: PPyObject): Integer; +// Executed via the m_slots of PyModuleDef as part of the +// multi-phase module initialization +var + I : Integer; +begin + FModule := module; + for I := 0 to ClientCount - 1 do + Clients[I].ModuleReady(Self); + BuildErrors; + if Assigned(FOnAfterInitialization) then + FOnAfterInitialization(Self); + Result := 0; end; procedure TPythonModule.RaiseError( const error, msg : AnsiString ); @@ -7632,7 +7815,7 @@ procedure TPythonModule.BuildErrors; CheckEngine; with Engine do begin - d := PyModule_GetDict( Module ); + d := PyModule_GetDict(FModule); if not Assigned(d) then Exit; for i := 0 to Errors.Count - 1 do @@ -7651,11 +7834,11 @@ procedure TPythonModule.SetVar( const varName : AnsiString; value : PPyObject ); begin if Assigned(FEngine) and Assigned( FModule ) then begin - if Engine.PyObject_SetAttrString(Module, PAnsiChar(varName), value ) <> 0 then - raise EPythonError.CreateFmt( 'Could not set var "%s" in module "%s"', [varName, ModuleName] ); + if Engine.PyObject_SetAttrString(FModule, PAnsiChar(varName), value ) <> 0 then + raise EPythonError.CreateFmt(SCouldNotSetVar, [varName, ModuleName]); end else - raise EPythonError.CreateFmt( 'Can''t set var "%s" in module "%s", because it is not yet initialized', [varName, ModuleName] ); + raise EPythonError.CreateFmt(SCannotSetVarNoInit, [varName, ModuleName]); end; // warning, this function will increase the refcount of value, @@ -7665,26 +7848,27 @@ function TPythonModule.GetVar( const varName : AnsiString ) : PPyObject; begin if Assigned(FEngine) and Assigned( FModule ) then begin - Result := Engine.PyObject_GetAttrString(Module, PAnsiChar(varName) ); + Result := Engine.PyObject_GetAttrString(FModule, PAnsiChar(varName) ); Engine.PyErr_Clear; end else - raise EPythonError.CreateFmt( 'Can''t get var "%s" in module "%s", because it is not yet initialized', [varName, ModuleName] ); + raise EPythonError.CreateFmt(SCannotSetVarNoInit, [varName, ModuleName]); end; -procedure TPythonModule.DeleteVar( const varName : AnsiString ); +procedure TPythonModule.DeleteVar(const varName : AnsiString); var dict : PPyObject; begin if Assigned(FEngine) and Assigned( FModule ) then with Engine do begin - dict := PyModule_GetDict( Module ); - if not Assigned(dict) then raise EPythonError.CreateFmt( 'Can''t get __dict__ of module "%s"', [ModuleName] ); + dict := PyModule_GetDict(FModule); + if not Assigned(dict) then + raise EPythonError.CreateFmt(SCannotGetDict, [ModuleName] ); PyDict_DelItemString( dict, PAnsiChar(varName) ); end else - raise EPythonError.CreateFmt( 'Can''t delete var "%s" in module "%s", because it is not yet initialized', [varName, ModuleName] ); + raise EPythonError.CreateFmt(SCannotDelVarNoInit, [varName, ModuleName]); end; procedure TPythonModule.ClearVars; @@ -7693,7 +7877,7 @@ procedure TPythonModule.ClearVars; begin if Assigned(FEngine) and Assigned( FModule ) then with Engine do begin - dict := PyModule_GetDict( Module ); + dict := PyModule_GetDict(FModule); PyDict_Clear(dict); end; end; @@ -8252,7 +8436,7 @@ function PythonToDelphi( obj : PPyObject ) : TPyObject; if IsDelphiObject( obj ) then Result := TPyObject(PAnsiChar(obj)+Sizeof(PyObject)) else - raise EPythonError.CreateFmt( 'Python object "%s" is not a Delphi class', [GetPythonEngine.PyObjectAsString(obj)] ); + raise EPythonError.CreateFmt(SExpectedDelphiClass, [GetPythonEngine.PyObjectAsString(obj)]); end; procedure PyObjectDestructor( pSelf : PPyObject); cdecl; @@ -8825,7 +9009,7 @@ procedure TPythonType.InitServices; begin tp_init := TPythonType_InitSubtype; tp_alloc := FEngine.PyType_GenericAlloc; - tp_new := GetCallBack( Self, @TPythonType.NewSubtypeInst, 3, DEFAULT_CALLBACK_TYPE); + tp_new := GetCallBack(Self, @TPythonType.NewSubtypeInst, 3, DEFAULT_CALLBACK_TYPE); tp_free := FEngine.PyObject_Free; tp_methods := MethodsData; tp_members := MembersData; @@ -9103,7 +9287,7 @@ procedure TPythonDelphiVar.CreateVar; // Add a reference to this var in the module m := PyImport_AddModule(PAnsiChar(Module)); if m = nil then - raise EPythonError.CreateFmt('CreateVar: can''t create module "%s"', [Module]); + raise EPythonError.CreateFmt(SCannotCreateModule, [Module]); d := PyModule_GetDict(m); if @PyDict_SetItemString = nil then raise Exception.Create('nil'); @@ -9117,7 +9301,7 @@ function TPythonDelphiVar.GetValue : Variant; with TPyVar(PythonToDelphi(FVarObject)) do Result := GetValueAsVariant else - raise Exception.Create('No variable was created' ); + raise Exception.Create(SVarNotCreated); end; procedure TPythonDelphiVar.SetValue( const val : Variant ); @@ -9126,7 +9310,7 @@ procedure TPythonDelphiVar.SetValue( const val : Variant ); with TPyVar(PythonToDelphi(FVarObject)) do SetValueFromVariant(val) else - raise Exception.Create('No variable was created' ); + raise Exception.Create(SVarNotCreated); end; // Warning: GetValueAsPyObject returns a preincremented object ! @@ -9136,7 +9320,7 @@ function TPythonDelphiVar.GetValueAsPyObject : PPyObject; with TPyVar(PythonToDelphi(FVarObject)) do Result := GetValue else - raise Exception.Create('No variable was created' ); + raise Exception.Create(SVarNotCreated); end; procedure TPythonDelphiVar.SetValueFromPyObject( val : PPyObject ); @@ -9145,7 +9329,7 @@ procedure TPythonDelphiVar.SetValueFromPyObject( val : PPyObject ); with TPyVar(PythonToDelphi(FVarObject)) do SetValue(val) else - raise Exception.Create('No variable was created' ); + raise Exception.Create(SVarNotCreated); end; function TPythonDelphiVar.IsVariantOk( const v : Variant ) : Boolean; @@ -9198,7 +9382,7 @@ procedure TPythonDelphiVar.SetVarName( const val : AnsiString ); if Owner.Components[i] is TPythonDelphiVar then with TPythonDelphiVar(Owner.Components[i]) do if (VarName = val) and (Module = Self.Module) then - raise Exception.CreateFmt('A variable "%s" already exists in the module "%s"',[val, Module]); + raise Exception.CreateFmt(SVarExists, [val, Module]); end; begin @@ -9490,7 +9674,7 @@ procedure TPythonThread.Execute; PyThreadState_Swap(global_state); PyGILState_Release(gilstate); end else - raise EPythonError.Create('Could not create a new thread state'); + raise EPythonError.Create(SCannotCreateThreadState); end; end; end; @@ -9698,9 +9882,9 @@ function pyio_GetTypesStats(self, args : PPyObject) : PPyObject; function GetPythonEngine : TPythonEngine; begin if not Assigned( gPythonEngine ) then - raise Exception.Create( 'No Python engine was created' ); + raise Exception.Create(SCannotCreatePythonEngine); if not gPythonEngine.Finalizing and not gPythonEngine.Initialized then - raise Exception.Create( 'The Python engine is not properly initialized' ); + raise Exception.Create(SCannotInitPythonEngine); Result := gPythonEngine; end; @@ -10053,7 +10237,7 @@ procedure ThreadPythonExec(ExecuteProc : TProc; TerminateProc : TProc; Thread: TAnonymousPythonThread; begin if GetCurrentThreadId <> MainThreadID then - raise Exception.Create('ThreadPythonExec should only be called from the main thread'); + raise Exception.Create(SThreadPythonExec); Thread := TAnonymousPythonThread.Create(ExecuteProc, TerminateProc, WaitToFinish, ThreadExecMode); if WaitToFinish then begin @@ -10066,5 +10250,14 @@ procedure ThreadPythonExec(ExecuteProc : TProc; TerminateProc : TProc; {$ENDIF FPC} +{ PyModuleDef_Slot } + +class function PyModuleDef_Slot.Make(slot: integer; + value: Pointer): PyModuleDef_Slot; +begin + Result.slot := slot; + Result.value := value; +end; + end. diff --git a/Source/VarPyth.pas b/Source/VarPyth.pas index b16f91b8..52607cf0 100644 --- a/Source/VarPyth.pas +++ b/Source/VarPyth.pas @@ -2154,7 +2154,7 @@ procedure VarPyToStrings(const AValue : Variant; const AStrings: TStrings); V: Variant; begin for V in VarPyIterate(AValue) do - AStrings.Add(V) + AStrings.Add(VarPythonAsString(V)) end; initialization diff --git a/Source/WrapDelphi.pas b/Source/WrapDelphi.pas index bed385ea..03961972 100644 --- a/Source/WrapDelphi.pas +++ b/Source/WrapDelphi.pas @@ -1003,7 +1003,7 @@ TPyDelphiWrapper = class(TEngineClient, IFreeNotificationSubscriber) implementation -Uses +uses Math, StrUtils, RTLConsts, @@ -1042,7 +1042,7 @@ implementation rs_ExpectedNil = 'In static methods Self should be nil'; rs_ExpectedInterface = 'Expected a Pascal interface'; rs_ExpectedSequence = 'Expected a python sequence'; - rsExpectedPPyObject = 'Expected a PPyObject'; + rsExpectedPointer = 'Expected a Pointer'; rs_InvalidClass = 'Invalid class'; rs_ErrEventNotReg = 'No Registered EventHandler for events of type "%s'; rs_ErrEventNoSuport = 'Class %s does not support events because it must '+ @@ -2188,12 +2188,16 @@ function ValidateDynArray(PyValue: PPyObject; const RttiType: TRttiType; end; end; -function ValidatePPyObject(PyValue: PPyObject; const RttiType: TRttiType; +function ValidatePointer(PyValue: PPyObject; const RttiType: TRttiType; out ParamValue: TValue; out ErrMsg: string): Boolean; var RefType: TRttiType; + PyEngine: TPythonEngine; + P: Pointer; begin Result := False; + PyEngine := GetPythonEngine; + if (RTTIType is TRttiPointerType) then begin RefType := TRttiPointerType(RTTIType).ReferredType; @@ -2201,10 +2205,21 @@ function ValidatePPyObject(PyValue: PPyObject; const RttiType: TRttiType; begin Result := True; ParamValue := TValue.From(PyValue); + end + else if PyEngine.PyLong_Check(PyValue) then + begin + P := PyEngine.PyLong_AsVoidPtr(PyValue); + if PyEngine.PyErr_Occurred = nil then + begin + Result := True; + ParamValue := TValue.From(P); + end + else + PyEngine.PyErr_Clear; end; end; if not Result then - ErrMsg := rsExpectedPPyObject; + ErrMsg := rsExpectedPointer; end; function PyObjectToTValue(PyArg: PPyObject; ArgType: TRttiType; @@ -2238,7 +2253,7 @@ function PyObjectToTValue(PyArg: PPyObject; ArgType: TRttiType; tkDynArray: Result := ValidateDynArray(PyArg, ArgType, Arg, ErrMsg); tkPointer: - Result := ValidatePPyObject(PyArg, ArgType, Arg, ErrMsg); + Result := ValidatePointer(PyArg, ArgType, Arg, ErrMsg); else Result := SimplePythonToValue(PyArg, ArgType.Handle, Arg, ErrMsg); end; @@ -2277,7 +2292,7 @@ function TValueToPyObject(const Value: TValue; DelphiWrapper: TPyDelphiWrapper; out ErrMsg: string): PPyObject; begin if Value.IsEmpty then - Result := GetPythonEngine.ReturnNone + Result := DelphiWrapper.Engine.ReturnNone else case Value.Kind of tkClass: Result := DelphiWrapper.Wrap(Value.AsObject); @@ -2288,13 +2303,10 @@ function TValueToPyObject(const Value: TValue; tkArray, tkDynArray: Result := DynArrayToPython(Value, DelphiWrapper, ErrMsg); tkPointer: - if Value.IsType then + if Value.TypeInfo = TypeInfo(PPyObject) then Result := Value.AsType else - begin - Result := nil; - ErrMsg := rs_ErrValueToPython; - end; + Result := DelphiWrapper.Engine.PyLong_FromVoidPtr(Value.AsType); else Result := SimpleValueToPython(Value, ErrMsg); end; @@ -5407,12 +5419,11 @@ procedure TPyDelphiWrapper.Initialize; with TPythonType(fHelperClassRegister.Objects[i]) do if not Initialized then Initialize; // Initialize module - if Assigned(FModule) then begin + if Assigned(FModule) then + begin + CreateModuleFunctions; if Module.Initialized then - begin - CreateModuleFunctions; - CreateModuleVars; - end + CreateModuleVars else Module.AddClient( Self ); end; @@ -5421,7 +5432,6 @@ procedure TPyDelphiWrapper.Initialize; procedure TPyDelphiWrapper.ModuleReady(Sender : TObject); begin inherited; - CreateModuleFunctions; CreateModuleVars; end; @@ -5530,13 +5540,13 @@ procedure TPyDelphiWrapper.SetModule(const Value: TPythonModule); TPythonType(fHelperClassRegister.Objects[i]).Module := Value; if Assigned(FModule) then if Initialized and (ComponentState * [csDesigning, csLoading] = []) then + begin + CreateModuleFunctions; if FModule.Initialized then - begin - CreateModuleFunctions; - CreateModuleVars; - end + CreateModuleVars else FModule.AddClient(Self); + end; end; end; diff --git a/Tests/WrapDelphiTest.pas b/Tests/WrapDelphiTest.pas index 350b0bb2..d320f53a 100644 --- a/Tests/WrapDelphiTest.pas +++ b/Tests/WrapDelphiTest.pas @@ -55,6 +55,7 @@ TTestRttiAccess = class ObjectField: TObject; RecordField: TTestRecord; InterfaceField: ITestInterface; + PointerField: Pointer; ClassRef: TClass; function GetData: TObject; procedure BuyFruits(AFruits: TFruits); @@ -160,6 +161,8 @@ TTestWrapDelphi = class(TObject) procedure TestVarArgs; [Test] procedure TestPPyObjects; + [Test] + procedure TestPointers; end; implementation @@ -439,6 +442,12 @@ procedure TTestWrapDelphi.TestPPyObjects; Assert.AreEqual(List.GetItem(0), 'abc'); end; +procedure TTestWrapDelphi.TestPointers; +begin + rtti_var.PointerField := $FFFF; + Assert.AreEqual(rtti_var.PointerField, $FFFF); +end; + procedure TTestWrapDelphi.TestRecord; begin Rtti_rec.StringField := 'abcd'; diff --git a/Tutorials/Webinar II/VizDemo/MainFormSVG.dfm b/Tutorials/Webinar II/VizDemo/MainFormSVG.dfm index cda1daa9..0d78949a 100644 --- a/Tutorials/Webinar II/VizDemo/MainFormSVG.dfm +++ b/Tutorials/Webinar II/VizDemo/MainFormSVG.dfm @@ -10,17 +10,12 @@ object Form1: TForm1 Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] - OldCreateOrder = False OnCreate = FormCreate - PixelsPerInch = 96 TextHeight = 13 object Splitter1: TSplitter Left = 489 Top = 0 Height = 613 - ExplicitLeft = 528 - ExplicitTop = 280 - ExplicitHeight = 100 end object SVGIconImage1: TSVGIconImage Left = 960 @@ -35,8 +30,6 @@ object Form1: TForm1 Width = 543 Height = 613 AutoSize = False - ParentDoubleBuffered = False - DoubleBuffered = True Align = alClient end object PageControl1: TPageControl @@ -46,7 +39,7 @@ object Form1: TForm1 Height = 613 ActivePage = TabSheet1 Align = alLeft - TabOrder = 2 + TabOrder = 0 object TabSheet1: TTabSheet Caption = 'matplotlib' object Panel1: TPanel @@ -63,8 +56,6 @@ object Form1: TForm1 Height = 3 Cursor = crVSplit Align = alBottom - ExplicitTop = 10 - ExplicitWidth = 492 end object SynEdit1: TSynEdit Left = 1 @@ -72,6 +63,7 @@ object Form1: TForm1 Width = 479 Height = 444 Align = alClient + CaseSensitive = True Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -13 @@ -85,6 +77,8 @@ object Form1: TForm1 Gutter.Font.Height = -11 Gutter.Font.Name = 'Consolas' Gutter.Font.Style = [] + Gutter.Font.Quality = fqClearTypeNatural + Gutter.Bands = <> Highlighter = SynPythonSyn1 Lines.Strings = ( 'from delphi_module import svg_image' @@ -102,10 +96,16 @@ object Form1: TForm1 '# stores the date as an np.datetime64 with a day unit ('#39'D'#39') in t' + 'he date column.' - - 'price_data = (cbook.get_sample_data('#39'goog.npz'#39', np_load=True)['#39'p' + - 'rice_data'#39']' - ' .view(np.recarray))' + '# Load Google stock price data from Matplotlib'#39's sample data' + 'data = cbook.get_sample_data('#39'goog.npz'#39')' + '' + '# Handle different return types from get_sample_data' + 'if isinstance(data, np.lib.npyio.NpzFile):' + ' # Already an NpzFile, access price_data directly' + ' price_data = data['#39'price_data'#39'].view(np.recarray)' + 'else:' + ' # Assume data is a path or file-like object, use np.load' + ' price_data = np.load(data)['#39'price_data'#39'].view(np.recarray)' 'price_data = price_data[-250:] # get the most recent 250 tradin' + 'g days' @@ -138,6 +138,7 @@ object Form1: TForm1 'svg_image.SvgText = figdata_svg' '' '#plt.show()') + ScrollbarAnnotations = <> end object Panel2: TPanel Left = 1 @@ -183,8 +184,6 @@ object Form1: TForm1 Height = 3 Cursor = crVSplit Align = alBottom - ExplicitTop = 151 - ExplicitWidth = 433 end object SynEdit2: TSynEdit Left = 1 @@ -192,6 +191,7 @@ object Form1: TForm1 Width = 479 Height = 376 Align = alClient + CaseSensitive = True Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -13 @@ -205,6 +205,8 @@ object Form1: TForm1 Gutter.Font.Height = -11 Gutter.Font.Name = 'Consolas' Gutter.Font.Style = [] + Gutter.Font.Quality = fqClearTypeNatural + Gutter.Bands = <> Highlighter = SynPythonSyn1 Lines.Strings = ( 'from delphi_module import svg_image' @@ -222,6 +224,7 @@ object Form1: TForm1 '' '#plt.show()' '') + ScrollbarAnnotations = <> end object Panel6: TPanel Left = 1 @@ -252,9 +255,6 @@ object Form1: TForm1 end end object SynPythonSyn1: TSynPythonSyn - Options.AutoDetectEnabled = False - Options.AutoDetectLineLimit = 0 - Options.Visible = False Left = 632 Top = 40 end @@ -264,6 +264,10 @@ object Form1: TForm1 Top = 89 end object PythonEngine1: TPythonEngine + DllName = 'python313.dll' + APIVersion = 1013 + RegVersion = '3.13' + UseLastKnownVersion = False IO = PythonGUIInputOutput1 Left = 632 Top = 136