Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

SetProgramArgs() modifies sys.argv #430

Answered by pyscripter
LennertP asked this question in Q&A
Discussion options

Hi!

Recently I noticed that sys.argv is modified when extending Python using PythonEngine.pas. I found that this is because SetProgramArgs() inserts an empty string in the call to PySys_SetArgv(). My understanding is that calling PySys_SetArgv() is useful when embedding Python, but when extending Python by using PythonEngine.pas to create a .pyd this seems undesirable.

I created this small pargs.py file to reproduce the issue:
import sys
print(sys.argv)

Initially the output running 'python pargs.py' is:
['pargs.py']

When I also import my own package implemented using PythonEngine.pas like this:
import mypackage
import sys
print(sys.argv)

running 'python pargs.py' again now results in;
['', 'pargs.py']

Do you think it is a good idea to skip calling the SetProgramArgs procedure when extending Python? If yes, how can we discriminate between extending Python and embedding Python in TPythonEngine.Initialize?

Best regards and many thanks for maintaining this excellent PythonEngine,

Lennert.

You must be logged in to vote

Sorry for the late reply. I have just seen your response.

As I am writing an extension

Do you mean a python extension module (a pyd file that can be loaded into python)?

If yes, please see how LoadDllInExtensionModule is used in the demo test module.

Replies: 1 comment · 3 replies

Comment options

You are most likely not using LoadDllInExtensionModule to load the python dll in your Delphi created python module.
See https://github.com/pyscripter/python4delphi/blob/master/Modules/DemoModule/uMain.pas

You must be logged in to vote
3 replies
@LennertP
Comment options

I am not sure what you mean. As I am writing an extension I don't load the python DLL at all. Or do you mean something else? If needed I can try to make a small extension that does (nearly) nothing but should prove my point that sys.argv is being modified.

@pyscripter
Comment options

Sorry for the late reply. I have just seen your response.

As I am writing an extension

Do you mean a python extension module (a pyd file that can be loaded into python)?

If yes, please see how LoadDllInExtensionModule is used in the demo test module.

Answer selected by pyscripter
@LennertP
Comment options

Thanks, using LoadDllInExtensionModule did the trick!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.