@@ -23,8 +23,10 @@ int main(int argc, char *argv[]) {
23
23
NSString *path;
24
24
NSString *traceback_str;
25
25
wchar_t *wtmp_str;
26
+ wchar_t *app_packages_path_str;
26
27
const char * app_module_str;
27
28
const char * nslog_script;
29
+ PyObject *app_packages_path;
28
30
PyObject *app_module;
29
31
PyObject *module;
30
32
PyObject *module_attr;
@@ -125,18 +127,6 @@ int main(int argc, char *argv[]) {
125
127
}
126
128
PyMem_RawFree (wtmp_str);
127
129
128
- // Add the app_packages path
129
- path = [NSString stringWithFormat: @" %@ /app_packages" , resourcePath, nil ];
130
- NSLog (@" - %@ " , path);
131
- wtmp_str = Py_DecodeLocale ([path UTF8String ], NULL );
132
- status = PyWideStringList_Append (&config.module_search_paths , wtmp_str);
133
- if (PyStatus_Exception (status)) {
134
- crash_dialog ([NSString stringWithFormat: @" Unable to set app packages path: %s " , status.err_msg, nil ]);
135
- PyConfig_Clear (&config);
136
- Py_ExitStatusException (status);
137
- }
138
- PyMem_RawFree (wtmp_str);
139
-
140
130
// Add the app path
141
131
path = [NSString stringWithFormat: @" %@ /app" , resourcePath, nil ];
142
132
NSLog (@" - %@ " , path);
@@ -189,6 +179,48 @@ int main(int argc, char *argv[]) {
189
179
}
190
180
}
191
181
182
+
183
+ // Adding the app_packages as site directory.
184
+ //
185
+ // This adds app_packages to sys.path and executes any .pth
186
+ // files in that directory.
187
+ path = [NSString stringWithFormat: @" %@ /app_packages" , resourcePath, nil ];
188
+ app_packages_path_str = Py_DecodeLocale ([path UTF8String ], NULL );
189
+
190
+ NSLog (@" Adding app_packages as site directory: %@ " , path);
191
+
192
+ module = PyImport_ImportModule (" site" );
193
+ if (module == NULL ) {
194
+ crash_dialog (@" Could not import site module" );
195
+ exit (-11 );
196
+ }
197
+
198
+ module_attr = PyObject_GetAttrString (module, " addsitedir" );
199
+ if (module_attr == NULL || !PyCallable_Check (module_attr)) {
200
+ crash_dialog (@" Could not access site.addsitedir" );
201
+ exit (-12 );
202
+ }
203
+
204
+ app_packages_path = PyUnicode_FromWideChar (app_packages_path_str, wcslen (app_packages_path_str));
205
+ if (app_packages_path == NULL ) {
206
+ crash_dialog (@" Could not convert app_packages path to unicode" );
207
+ exit (-13 );
208
+ }
209
+ PyMem_RawFree (app_packages_path_str);
210
+
211
+ method_args = Py_BuildValue (" (O)" , app_packages_path);
212
+ if (method_args == NULL ) {
213
+ crash_dialog (@" Could not create arguments for site.addsitedir" );
214
+ exit (-14 );
215
+ }
216
+
217
+ result = PyObject_CallObject (module_attr, method_args);
218
+ if (result == NULL ) {
219
+ crash_dialog (@" Could not add app_packages directory using site.addsitedir" );
220
+ exit (-15 );
221
+ }
222
+
223
+
192
224
// Start the app module.
193
225
//
194
226
// From here to Py_ObjectCall(runmodule...) is effectively
0 commit comments