-
Content Count
785 -
Joined
-
Last visited
-
Days Won
42
Posts posted by pyscripter
-
-
On 5/18/2023 at 8:05 PM, xpert13 said:Is there any way to immediately terminate script it waiting for
timer.sleep
Code from demo 33 doesn't help in this situation. The same issue if script is waiting for answer for http request.
See multithreading - Python threading interrupt sleep - Stack Overflow for alternatives to sleep that can be interrupted.
-
from io import StringIO import sys old_stderr = sys.stderr sys.stderr = mystderr = StringIO() # examine mystderr.getvalue()
-
I wrote the code without testing, just to give you the idea. Good that you solved the problem.
Also instead of MainModule.builtins._input_prompt you can use BuiltinModule._input_prompt.
And the following would do. No need to convert to AnsiString and back.
procedure TForm1.PythonInputOutput1ReceiveUniData(Sender: TObject; var Data: string); begin InputQuery( 'Query from Python', BuiltinModule._input_prompt, Data); end;
- 1
-
Run a script like this:
import builtins def myinput(prompt): builtins._input_prompt = prompt builtins._input(prompt) builtins._input = builtins.input builtins.input = myinput
Then the prompt will be stored in builtins._input_prompt
-
Dictionaries are downloaded via Windows Language preferences. You just select the language as in the demo.
-
The KeyboardInterrupt is the correct way to terminate a thread from the main thread.
47 minutes ago, iqrf said:<function DebugOutput.write at 0x02C56758> returned a result with an exception set'
This is because print statement executes without clearing the KeyboardInterrupt exception. You need to trap the KeyboardInterrupt exception either in Python or Delphi code.
You can also add to your PythonInputOutput1SendUniData
with GetPythonEngine do if PyErr_Occurred <> nil then begin PyErr_Clear; Exit; end;
-
2 hours ago, iqrf said:when exiting the thread
How are you exiting the thread? Are you raising a KeyboardInterrupt as in demo 33? Then why are you surprised that an exception is raised, when the print statement executes. You can modify your script as follows (not tested):
while True: try: time.sleep(0.1) print(datetime.now()) except KeyboardInterrupt: break
or handle the exception in your Delphi code
-
You are producing output at a high speed from a thread. You need to buffer the output in a way similar to that in python4delphi/Unit1.pas at master · pyscripter/python4delphi (github.com).
-
1 hour ago, David Heffernan said:@pyscripter do you know if it's reasonable to implement the python mapping protocol on the Delphi side so that from python it looks like a Python mapping?
Yes indeed! P4D makes it quite easy. The TStrings wrapper does that for instance.
-
48 minutes ago, shineworld said:Seems to work but is hard to know when to apply Py_XDecRef....
-
13 hours ago, iqrf said:Do I have to worry about releasing the created PyBytes in TPyPoint.DoReceive using Py_XDECREF(pyBytes)
No. You should try to understand the rules behind reference counting. (e.g. Reference Counting in Python (tripod.com))
QuoteMost functions INCREF any object that should continue to exist after the function exits. This includes objects returned via arguments or a return statement. In these cases the calling function usually has responsibility for calling
Py_DECREF()
. The calling function can, in turn, pass responsibility for the DECREF to its caller.Note that PyBytes_FromStringAndSize returns a new reference, so there is no need to increment the count.
Adjust(Self) is needed when you need to access the Delphi object properties or methods.
-
On the Delphi side you can use PyArg_ParseTuple to get the pointer to the python object. Then you can either:
- use the python API to call the desired method on the python object or
- wrap the python object into a custom variant (VarPythonCreate in the VarPyth unit) and call the method in a high-level way. See tutorials and demos on VarPyth for details
- 1
-
Try the sources in pyscripter/python4delphi: Free components that wrap up Python into Delphi and Lazarus (FPC) (github.com) and follow the installation instructions.
You should report the GetIt issue to the fork Embarcadero/python4delphi: Fork of P4D adding FireMonkey wrapper and Android support (github.com) which is used to create the GetIt package.
-
The IPC unit based on named pipes created by Russell Libby in 2003 and updated by @FPiette is worth checking.
Blog article: Behind the connection: Inter Process Communication Using Pipes (francois-piette.blogspot.com)
Source code: OverByte - Blog Source Code
Named pipes in overlapped mode are super efficient and faster than sockets. See Synchronous and Overlapped Pipe I/O - Win32 apps | Microsoft Learn. See also I/O Completion Ports - Win32 apps | Microsoft Learn. I have contributed such a solution to the python library RPyC and it beats the socket based IPC by a large margin. The downside is that it is Windows only.
-
56 minutes ago, IJCdelpi said:if PyArg_ParseTuple(Args, 'i:read_AP_register', @arg1, @arg2) <> 0
It should be 'ii:read_AP_register' if you are expecting two integer arguments.
- 1
-
-
-
1 minute ago, aehimself said:All my breakpoints just become unavailable (like code never gets executed in that function), did you meet the same issue?
Yes.
-
FWIW, I have tried the unofficial LSP patches and I could no longer debug programs. So I went back to the original dlls and debugging was available again.
- 1
- 1
-
On 1/14/2023 at 8:12 PM, uefi said:Hello, I can not get the result of the script execution instead of the result I get:
value=none
What did you expect to get?
Demo03 shows you how to use TPythonDelphiVar.
-
8 hours ago, uefi said:I still don’t understand how you did it and where to insert it
Py_Begin_Allow_Threads; and Py_End_Allow_Threads;Use Demo 33 as a guide. As @David Heffernan said running python in threads is not trivial and unlikely to save you time.
-
FPyModule.Name := '__main__';
meeds to be
FPyModule.ModuleName := '__main__';
Better to call your module something else (delphimodule or anything else. in the demos 'spam' is used) and import that module.
-
9 minutes ago, Juan C.Cilleruelo said:After visualizing all the video tutorials, seeing the demos, and seeing what kind of support the components have, I'm seriously thinking of abandoning this line of investigation.
Please do. You will do everyone a favour.
-
Call you delphi module say "delphimodule" and in your script:
import delphimodule
print(delphimodule.QUANTITY);
or
from delphimodule import QUANTITY
print(QUANTITY)
Please look at the demos, before asking questions here and do yourself a favour. Do watch the two video tutorials. It is only two hours viewing and will save you masses of time if you plan to do any serious work with P4D.
How to catch if the object is assigned None
in Python4Delphi
Posted · Edited by pyscripter
is called whenever a property is set (e.g. spam.myPoint.x = 10). So you can take an action when the object is modified.
However you are not notified when spam.myPoint is assigned a value.
To do what you would have to override the __setattr__ on the module, but this is too complicated.