@@ -2118,10 +2118,8 @@ TPythonEngine = class(TPythonInterface)
21182118 function Run_CommandAsString (const command: AnsiString; mode: Integer; const FileName: string = ' <string>' ): string;
21192119 function Run_CommandAsObject (const command: AnsiString; mode: Integer; const FileName: string = ' <string>' ): PPyObject;
21202120 function Run_CommandAsObjectWithDict (const command: AnsiString; mode: Integer; locals, globals: PPyObject; const FileName: string = ' <string>' ): PPyObject;
2121- function EncodeString (const str: UnicodeString): AnsiString; { $IFDEF FPC} overload;{ $ENDIF}
2122- { $IFDEF FPC}
2123- function EncodeString (const str: AnsiString): AnsiString; overload;
2124- { $ENDIF}
2121+ function EncodeString (const str: UnicodeString): AnsiString; overload;
2122+ function EncodeString (const str: AnsiString): AnsiString; overload;
21252123 function EncodeWindowsFilePath (const str: string): AnsiString;
21262124 procedure ExecString (const command: AnsiString; const FileName: string = ' <string>' ); overload;
21272125 procedure ExecStrings (strings: TStrings; const FileName: string = ' <string>' ); overload;
@@ -2498,8 +2496,8 @@ TError = class(TCollectionItem)
24982496 destructor Destroy; override;
24992497 procedure Assign (Source: TPersistent); override;
25002498 procedure BuildError ( const ModuleName : AnsiString );
2501- procedure RaiseError ( const msg : AnsiString );
2502- procedure RaiseErrorObj ( const msg : AnsiString; obj : PPyObject );
2499+ procedure RaiseError (const msg : AnsiString);
2500+ procedure RaiseErrorObj (const msg : AnsiString; obj : PPyObject);
25032501 function Owner : TErrors;
25042502 property Error : PPyObject read FError write FError;
25052503 published
@@ -3098,9 +3096,8 @@ function CleanString(const s : UnicodeString; AppendLF : Boolean = True) : Unico
30983096implementation
30993097
31003098uses
3101- { $IFDEF FPC}
31023099 StrUtils,
3103- { $ELSE }
3100+ { $IFNDEF FPC }
31043101 AnsiStrings,
31053102{ $ENDIF}
31063103{ $IFDEF MSWINDOWS}
@@ -5627,17 +5624,15 @@ function TPythonEngine.FindClient( const aName : string ) : TEngineClient;
56275624 end ;
56285625end ;
56295626
5630- function TPythonEngine.EncodeString (const str: UnicodeString): AnsiString; { $IFDEF FPC } overload; { $ENDIF }
5627+ function TPythonEngine.EncodeString (const str: UnicodeString): AnsiString;
56315628begin
5632- Result := UTF8Encode(str)
5629+ Result := UTF8Encode(str);
56335630end ;
56345631
5635- { $IFDEF FPC}
5636- function TPythonEngine.EncodeString (const str: AnsiString): AnsiString; overload;
5632+ function TPythonEngine.EncodeString (const str: AnsiString): AnsiString;
56375633begin
5638- Result := str;
5634+ Result := UTF8Encode( str) ;
56395635end ;
5640- { $ENDIF}
56415636
56425637function TPythonEngine.EncodeWindowsFilePath (const str: string): AnsiString;
56435638{ PEP 529}
@@ -7276,14 +7271,14 @@ procedure TError.BuildError( const ModuleName : AnsiString );
72767271 raise Exception.CreateFmt( ' Could not create error "%s"' , [Name ] );
72777272end ;
72787273
7279- procedure TError.RaiseError ( const msg : AnsiString );
7274+ procedure TError.RaiseError (const msg : AnsiString);
72807275begin
72817276 Owner.Owner.CheckEngine;
72827277 with Owner.Owner.Engine do
7283- PyErr_SetString( Error, PAnsiChar(msg) );
7278+ PyErr_SetString(Error, PAnsiChar(EncodeString( msg)) );
72847279end ;
72857280
7286- procedure TError.RaiseErrorObj ( const msg : AnsiString; obj : PPyObject );
7281+ procedure TError.RaiseErrorObj (const msg : AnsiString; obj : PPyObject);
72877282var
72887283 args, res, str : PPyObject;
72897284 i : Integer;
@@ -7327,10 +7322,11 @@ procedure TError.RaiseErrorObj( const msg : AnsiString; obj : PPyObject );
73277322 end
73287323 else
73297324 raise Exception.Create(' TError.RaiseErrorObj: I didn'' t get an instance' );
7330- PyErr_SetObject( Error, res );
7325+ PyErr_SetObject(Error, res);
7326+ Py_XDECREF(res);
73317327 end
73327328 else
7333- PyErr_SetObject( Error, obj );
7329+ PyErr_SetObject(Error, obj);
73347330end ;
73357331
73367332function TError.Owner : TErrors;
@@ -7790,8 +7786,8 @@ function TPyObject.SetAttr(key : PAnsiChar; value : PPyObject) : Integer;
77907786 with GetPythonEngine do
77917787 begin
77927788 Result := -1 ;
7793- PyErr_SetString (PyExc_AttributeError^,
7794- PAnsiChar(AnsiString (Format(' Unknown attribute "%s"' ,[key]))));
7789+ PyErr_SetString(PyExc_AttributeError^,
7790+ PAnsiChar(EncodeString (Format(' Unknown attribute "%s"' ,[key]))));
77957791 end ;
77967792end ;
77977793
@@ -7828,7 +7824,7 @@ function TPyObject.GetBuffer(view: PPy_buffer; flags: Integer): Integer;
78287824begin
78297825 view ^.obj := nil ;
78307826 with GetPythonEngine do
7831- PyErr_SetObject (PyExc_BufferError^, PyUnicodeFromString( ' ' ) );
7827+ PyErr_SetString (PyExc_BufferError^, ' ' );
78327828 Result := -1 ;
78337829end ;
78347830
0 commit comments