Nan Zhang | ced2751 | 2017-06-19 18:01:34 -0700 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Nan Zhang | ced2751 | 2017-06-19 18:01:34 -0700 | [diff] [blame] | 15 | #include <Python.h> |
| 16 | #include <android-base/file.h> |
| 17 | #include <osdefs.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string> |
| 21 | |
| 22 | int main(int argc, char *argv[]) { |
Nan Zhang | ced2751 | 2017-06-19 18:01:34 -0700 | [diff] [blame] | 23 | // PYTHONEXECUTABLE is only used on MacOs X, when the Python interpreter |
| 24 | // embedded in an application bundle. It is not sure that we have this use case |
Dan Willemsen | eba50a5 | 2018-11-16 17:27:26 -0800 | [diff] [blame] | 25 | // for Android hermetic Python. So remove this environment variable to make |
| 26 | // our self-contained environment more strict. |
Nan Zhang | ced2751 | 2017-06-19 18:01:34 -0700 | [diff] [blame] | 27 | // For user (.py) program, it can access hermetic .par file path through |
| 28 | // sys.argv[0]. |
| 29 | unsetenv(const_cast<char *>("PYTHONEXECUTABLE")); |
| 30 | |
Dan Willemsen | eba50a5 | 2018-11-16 17:27:26 -0800 | [diff] [blame] | 31 | // Always enable Python "-s" option. We don't need user-site directories, |
| 32 | // everything's supposed to be hermetic. |
| 33 | Py_NoUserSiteDirectory = 1; |
| 34 | |
| 35 | // Ignore PYTHONPATH and PYTHONHOME from the environment. |
| 36 | Py_IgnoreEnvironmentFlag = 1; |
| 37 | |
| 38 | Py_DontWriteBytecodeFlag = 1; |
| 39 | |
Nan Zhang | ced2751 | 2017-06-19 18:01:34 -0700 | [diff] [blame] | 40 | // Resolving absolute path based on argv[0] is not reliable since it may |
| 41 | // include something unusable, too bad. |
| 42 | // android::base::GetExecutablePath() also handles for Darwin/Windows. |
| 43 | std::string executable_path = android::base::GetExecutablePath(); |
| 44 | |
Dan Willemsen | 99f2754 | 2019-02-14 21:21:48 -0800 | [diff] [blame^] | 45 | // Set the equivalent of PYTHONHOME internally. |
| 46 | Py_SetPythonHome(strdup(executable_path.c_str())); |
| 47 | |
Dan Willemsen | eba50a5 | 2018-11-16 17:27:26 -0800 | [diff] [blame] | 48 | int new_argc = argc + 1; |
| 49 | char **new_argv = reinterpret_cast<char**>(calloc(new_argc, sizeof(*argv))); |
| 50 | |
| 51 | // Inject the path to our binary into argv[1] so the Py_Main won't parse any |
| 52 | // other options, and will execute the __main__.py script inside the zip file |
| 53 | // attached to our executable. |
| 54 | new_argv[0] = argv[0]; |
| 55 | new_argv[1] = strdup(executable_path.c_str()); |
| 56 | for (int i = 1; i < argc; i++) { |
| 57 | new_argv[i+1] = argv[i]; |
Nan Zhang | ced2751 | 2017-06-19 18:01:34 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Dan Willemsen | eba50a5 | 2018-11-16 17:27:26 -0800 | [diff] [blame] | 60 | return Py_Main(new_argc, new_argv); |
Nan Zhang | ced2751 | 2017-06-19 18:01:34 -0700 | [diff] [blame] | 61 | } |