Can python script know return value of C ++ main function in android environment

There are several ways to call executable C ++ programs. For example, we can use

 def run_exe_return_code(run_cmd):
        process=subprocess.Popen(run_cmd,stdout=subprocess.PIPE,shell=True)
        (output,err)=process.communicate()
        exit_code = process.wait()
        print output
        print err
        print exit_code
        return exit_code

for processing an executable C ++ program: run_exe_return_code('abc')and abcis created by the following C ++ codes:

int main()
{
      return 1;
}

In the above codes, the return value of the program is 1, and if we run this Python script on Linux, we can always see that the return value of the Python script is 1. However, in the Android environment, it seems that the return code in the above python script is 0 that means success. Is there a solution in which a Python script can find out the return value of the main function in an Android environment?

By the way, in the android environment, I use adb shell abcinstead abcto run the program.

+4
5

Android fb-adb, " , 0" () (... ):

def run_exe_return_code(run_cmd):
        process=subprocess.Popen(run_cmd + '; echo $?',stdout=subprocess.PIPE,shell=True)
        (output,err)=process.communicate()
        exit_code = process.wait()
        print output
        print err
        print exit_code
        return exit_code

, echo -ed, , exit_code adb.

$? . , python.


:

. :

.c:

reut@reut-VirtualBox:~/pyh$ cat c.c 
int main() {
    return 1;
}

( a.out ...):

reut@reut-VirtualBox:~/pyh$ gcc c.c

.py :

reut@reut-VirtualBox:~/pyh$ cat tstc.py 
#!/usr/bin/env python

import subprocess

def run_exe_return_code(run_cmd):
    process=subprocess.Popen(run_cmd,stdout=subprocess.PIPE)
    (output,err)=process.communicate()
    exit_code = process.wait()
    print output
    print err
    print exit_code

run_exe_return_code('./a.out')

:

reut@reut-VirtualBox:~/pyh$ ./tstc.py 

None
1

exit_code 1, .

, . , , subprocess.check_output:

.

:

>>> subprocess.check_output(["echo", "Hello World!"])
'Hello World!\n'

. 1, , CalledProcessError ( , ).

+4

, command.getstatusoutput, :

import commands

status, result = commands.getstatusoutput(run_cmd)

print result

+1

, ! , , :

import subprocess
exit_code=subprocess.call('./a.out')`
print exit_code

./a.out , :

int main(){
    return 3;
}

:

python testRun.py
3

Ah, , = True . https://docs.python.org/2/library/subprocess.html

def run_exe_return_code(run_cmd):
    process=subprocess.Popen(run_cmd,stdout=subprocess.PIPE,shell=True)
+1

: fooobar.com/questions/1669/.... , :

def run_exe_return_code(run_cmd):
    process = subprocess.Popen(run_cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = process.communicate()
    process.wait()
    print output
    print err
    print process.returncode
    return process.returncode

, Popen.wait, Popen.poll Popen.communicate, , Popen.returncode.

. Python Popen: https://docs.python.org/2/library/subprocess.html

0
def run_exe_android_return_code(run_cmd):
     #adb shell '{your command here} > /dev/null 2>&1; echo $?'
     process=subprocess.Popen(run_cmd,stdout=subprocess.PIPE,shell=True)
     (output,err)=process.communicate()
     pos1   = output.rfind('\n')
     output = output[:pos1-1]
     pos2   = output.rfind('\n')
     output = output[pos2+1:]
     print output
     return output

Python script, Android.

def run_android_executable(full_path,executable):
    executable = full_path+'/'+executable
    run_cmd = 'adb shell \'LD_LIBRARY_PATH='+full_path+':$LD_LIBRARY_PATH '+executable+'; echo $?\''
    print run_cmd
    error_code=run_exe_android_return_code(run_cmd)
    print 'the error code is'
    print error_code
    if(error_code=='1'):
        print 'the executable returns error'
    else:
        print 'the exectuable runs smoothly'

, . , .

0

All Articles