@@ -44,19 +44,26 @@ static PyMethodDef AndroidEmbedMethods[] = {
4444 {NULL , NULL , 0 , NULL }
4545};
4646
47- static struct PyModuleDef androidembed =
48- {
49- PyModuleDef_HEAD_INIT ,
50- "androidembed" ,
51- "" ,
52- -1 ,
53- AndroidEmbedMethods
54- };
55-
56- PyMODINIT_FUNC initandroidembed (void ) {
57- return PyModule_Create (& androidembed );
58- /* (void) Py_InitModule("androidembed", AndroidEmbedMethods); */
59- }
47+
48+ #if PY_MAJOR_VERSION >= 3
49+ static struct PyModuleDef androidembed =
50+ {
51+ PyModuleDef_HEAD_INIT ,
52+ "androidembed" ,
53+ "" ,
54+ -1 ,
55+ AndroidEmbedMethods
56+ };
57+
58+ PyMODINIT_FUNC initandroidembed (void ) {
59+ return PyModule_Create (& androidembed );
60+ /* (void) Py_InitModule("androidembed", AndroidEmbedMethods); */
61+ }
62+ #else
63+ PyMODINIT_FUNC initandroidembed (void ) {
64+ (void ) Py_InitModule ("androidembed" , AndroidEmbedMethods );
65+ }
66+ #endif
6067
6168/* int dir_exists(char* filename) */
6269/* /\* Function from http://stackoverflow.com/questions/12510874/how-can-i-check-if-a-directory-exists-on-linux-in-c# *\/ */
@@ -132,53 +139,33 @@ int main(int argc, char *argv[]) {
132139 LOG (env_argument );
133140 chdir (env_argument );
134141
135-
136142 Py_SetProgramName (L"android_python" );
137143
144+ #if PY_MAJOR_VERSION >= 3
138145 /* our logging module for android
139146 */
140147 PyImport_AppendInittab ("androidembed" , initandroidembed );
148+ #endif
141149
142150 LOG ("Preparing to initialize python" );
143151
144- char errstr [256 ];
145- snprintf (errstr , 256 , "errno before is %d" ,
146- errno );
147- LOG (errstr );
148-
149- if (dir_exists ("crystax_python" )) {
150- LOG ("exists without slash" );
151- }
152-
153- snprintf (errstr , 256 , "errno after is %d" ,
154- errno );
155- LOG (errstr );
156-
157- if (dir_exists ("../libs" )) {
158- LOG ("libs exists" );
159- } else {
160- LOG ("libs does not exist" );
161- }
162-
163- if (file_exists ("main.py" )) {
164- LOG ("The main.py does exist" );
165- }
166-
167- if (file_exists ("main.py" ) == 1 ) {
168- LOG ("The main.py does exist2" );
169- }
170-
171152 if (dir_exists ("crystax_python/" )) {
172- /* if (1) { */
173153 LOG ("crystax_python exists" );
174154 char paths [256 ];
175155 snprintf (paths , 256 , "%s/crystax_python/stdlib.zip:%s/crystax_python/modules" , env_argument , env_argument );
176156 /* snprintf(paths, 256, "%s/stdlib.zip:%s/modules", env_argument, env_argument); */
177157 LOG ("calculated paths to be..." );
178158 LOG (paths );
179159
160+ #if PY_MAJOR_VERSION >= 3
180161 wchar_t * wchar_paths = Py_DecodeLocale (paths , NULL );
181162 Py_SetPath (wchar_paths );
163+ #else
164+ char * wchar_paths = paths ;
165+ LOG ("Can't Py_SetPath in python2, so crystax python2 doesn't work yet" );
166+ exit (1 );
167+ #endif
168+
182169 LOG ("set wchar paths..." );
183170 } else { LOG ("crystax_python does not exist" );}
184171
@@ -196,8 +183,11 @@ int main(int argc, char *argv[]) {
196183 PyEval_InitThreads ();
197184
198185
186+ #if PY_MAJOR_VERSION < 3
187+ initandroidembed ();
188+ #endif
189+
199190 PyRun_SimpleString ("import androidembed\nandroidembed.log('testing python print redirection')" );
200- LOG ("tried to run simple androidembed test" );
201191
202192 /* inject our bootstrap code to redirect python stdin/stdout
203193 * replace sys.path with our path
@@ -215,7 +205,6 @@ int main(int argc, char *argv[]) {
215205 " private + '/lib/python2.7/site-packages/', \n" \
216206 " argument ]\n" );
217207 } else {
218-
219208 char add_site_packages_dir [256 ];
220209 snprintf (add_site_packages_dir , 256 , "sys.path.append('%s/crystax_python/site-packages')" ,
221210 env_argument );
@@ -275,6 +264,7 @@ int main(int argc, char *argv[]) {
275264 /* "print 'Android kivy bootstrap done. __name__ is', __name__"); */
276265
277266 LOG ("AND: Ran string" );
267+
278268 /* run it !
279269 */
280270 LOG ("Run user program, change dir and execute main.py" );
0 commit comments