Skip to content

function "VarPyToStrings" #497

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

Closed
LucaBertoncini opened this issue Apr 12, 2025 · 1 comment
Closed

function "VarPyToStrings" #497

LucaBertoncini opened this issue Apr 12, 2025 · 1 comment
Labels

Comments

@LucaBertoncini
Copy link

Hi, today I started experimenting with your amazing library. I am using FreePascal version 3.3.1 and this is my OS: Linux Mint21 5.15.0-136-generic.

This is a simple test I did.

program TestPython1;

{$mode objfpc}{$H+}

uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
Classes, PythonEngine, VarPyth;

var
PyEngine : TPythonEngine;
Code : String;
Items : TStringList;

begin
PyEngine := TPythonEngine.Create(nil);
PyEngine.LoadDll;

MainModule.aList := VarPythonCreate(['First element', 'Second element', 'Third element'], stList);

Code := 'aList.append("Py first element!")' + sLineBreak +
'aList.append("Py second element!")' + sLineBreak +
'aList = sorted(aList)' + sLineBreak;

PyEngine.ExecString(Code);

if VarIsPythonList(MainModule.aList) then
begin
Items := TStringList.Create;
VarPyToStrings(MainModule.aList, Items);
writeln(Items.Text);
Items.Free;
end;

PyEngine.Free;
end.

I noticed a strange behavior of the "VarPyToStrings" function. I had to modify it in this way to get the desired output.

procedure VarPyToStrings(const AValue : Variant; const AStrings: TStrings);
var
V: Variant;
begin
for V in VarPyIterate(AValue) do
AStrings.Add(VarPythonAsString(V));
end;

@pyscripter
Copy link
Owner

pyscripter commented Apr 12, 2025

It worked will with Delphi, but unfortunately FPC custom variant support is still lacking.
See freepascal.org/fpc/source#39815

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants