Open and load --code file and arguments in C++ #60134
Open
+225
−11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
(Note: Not yet tested yet; it seems that if I create a PR, the bot will make a Windows build for me.)
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.
Note that this PR changes the behaviour when
--py-args
is specified without--code
. Previously, we would setsys.argv
to the arguments given, but with this PR we do not do that. As far as I can tell,--py-args
is meant to be used with--code
, and therefore the former was unintended.Fixes #60120.