-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Open and load --code file and --py-args arguments in C++ #60134
base: master
Are you sure you want to change the base?
Conversation
@btzy when we introduced @btzy If you need arguments valid only in the context of the |
No strong opinion. What would be the reason to not make it also available for other python code than |
@domi4484 I misunderstood the help string of |
I'm not sure how to trigger the automatic Windows build that other PRs have; can someone help me get the GitHub Action to run? |
Separately, I made #60183 to fix the grammar error in the help, which was what originally misled me to think that |
ef2a7f6
to
20e2318
Compare
🪟 Windows buildsDownload Windows builds of this PR for testing. 🪟 Windows Qt6 buildsDownload Windows Qt6 builds of this PR for testing. |
64c5dda
to
1f44ac4
Compare
I've tested that the Windows build works:
|
@m-kuhn Can I get a review for this? |
The QGIS project highly values your contribution and would love to see this work merged! Unfortunately this PR has not had any activity in the last 14 days and is being automatically marked as "stale". If you think this pull request should be merged, please check
|
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) | ||
{ | ||
ret = "Cannot open file"; | ||
goto error; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please avoid goto
if ( !argsobj ) | ||
{ | ||
ret = "Error occurred in PyList_New"; | ||
goto error; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
ret = QString( "SetArgvTraceback" ) + getTraceback(); | ||
else | ||
ret = "Error occurred in PyImport_ImportModule"; | ||
goto error; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would you like me to rewrite this code? We need to execute the cleanup code at the end whether or not this function succeeds.
The most C++-ic way to do this is to write an RAII wrapper class around PyGILState_Ensure/PyGILState_Release and PyObject/Py_XDECREF. Or for a more ad-hoc mechanism, I might have used the equivalent of std::experimental::scope_exit
] (both Folly and Abseil have this in some form, but QGIS uses neither of them). I would have written code in one of the above two ways if this was for my own project, but I do not see existing code in QGIS that leverages either of them.
If the above solutions are unacceptable, we would have to either invert the guard clauses into nested if-statements, which is generally considered to be bad practice. We are thus left with goto statements, which seem to be an idiom for writing cleanup code in C.
What would be most appropriate for the QGIS codebase? Writing my own scope guard?
Thanks, using sys.argv does look like the correct approach! |
Description
I took a stab at #60120 to see if we can design a better alternative to #60121.
Instead of doing string substitution into Python code, we use the proper Python C API functions to assign to
sys.argv
and execute a Python script from a file. This avoids the issues that arise due to improper escaping of strings (e.g.'
and\
). Unix file names in particular may contain any non-null character (including non-printable characters), and trying to escape such characters manually would otherwise be difficult to get right. String substitution is also possibly vulnerable to code injection attacks, as it is possible to carefully craft a string containing code that will be executed.There was a hack that converts Windows-style directory separators to Unix-style directory separators. This is no longer necessary, and therefore has been removed.
Fixes #60120.