DotNet命令行形式调用Python异常中断

#总结
C# .Net Framework 3.5
Python 26或者27python

C#调用方式ajax

Process process = new Process();
                ProcessStartInfo processStartInfo = new ProcessStartInfo();
                processStartInfo.FileName = "python.exe";
                //no create the cmd windows

                processStartInfo.Arguments = Command;

                processStartInfo.CreateNoWindow = true;
                processStartInfo.RedirectStandardInput = true;
                processStartInfo.RedirectStandardOutput = true;
                processStartInfo.RedirectStandardError = true;
                processStartInfo.UseShellExecute = false;

                try
                {
                    Console.WriteLine("Class Execute Method exec_python Debug:process.Start");
                    process.Start();
                    Console.WriteLine("Class Execute Method exec_python Debug:read process end");
                    Output = process.StandardOutput.ReadToEnd();
                }

##原由
在调整Tactic的外部程序,更新了一些如EXR文件读取、色彩空间、时间码等内容。因为Tactic的最新稳定版本是Tactic_v4.1.0.v05,对应配置了Windows的Python环境。当EXR部分执行经过沾沾自喜的时候,Tactic Checkin上传文件发生错误。诡异的是,上传部分代码没有作过变动,好久以前就能够正常使用了。随即决定好好跟踪下Python代码,看下Tactic Client发送文件的原理。json

##checkin.py的调试windows

from tactic_client_lib import TacticServerStub

#...

try:
    snapshot = server.simple_checkin(search_key,context,path,description=desc,mode="upload",create_icon=True);
    value = "checkin:OK";
except:
    value = "checkin:ERROR";
print value;

##跟踪到tactic_server_stub.pyapp

print "Class tactic_server_stub Method simple_checkin Debug:run";
        mode_options = ['upload', 'uploaded', 'copy', 'move', 'local','inplace']
        print "Class tactic_server_stub Method simple_checkin Debug:[mode_option]:" + mode;
        if mode:
            if mode not in mode_options:
                raise TacticApiException('Mode must be in %s' % mode_options)

            if mode == 'upload':
                print "Class tactic_server_stub Method simple_checkin Debug:run upload_file function [file_path]:" + file_path;
                my.upload_file(file_path)

跟踪到upload_file函数函数

def upload_file(my, path):
        '''API Function: upload_file(path)
        Use http protocol to upload a file through http

        @param:
        path - the name of the file that will be uploaded
        
        '''
        print "Class tactic_server_stub Method upload_file Debug:run";
        from common import UploadMultipart
        print "Class tactic_server_stub Method upload_file Debug:import UploadMultipart from common";
        upload = UploadMultipart()
        print "Class tactic_server_stub Method upload_file Debug:[my.transaction_ticket]:" + my.transaction_ticket;
        upload.set_ticket(my.transaction_ticket)
        if my.server_name.startswith("http://") or my.server_name.startswith("https://"):
            upload_server_url = "%s/tactic/default/UploadServer/" % my.server_name
        else:
            upload_server_url = "http://%s/tactic/default/UploadServer/" % my.server_name

        print "Class tactic_server_stub Method upload_file Debug:[upload_server_url]:" + upload_server_url;
        upload.set_upload_server(upload_server_url)
        print "Class tactic_server_stub Method upload_file Debug:[path]:" + path;
        upload.execute(path)

##跟踪到UploadMultipart测试

def execute(my, path):
        print "Class UploadMultipart Method execute Debug:[path]:" + path;
        assert my.server_url
        f = open(path, 'rb')
        print "Class UploadMultipart Method execute Debug:import codecs";
        import codecs
        #f = codecs.open(path, 'rb')
        print "Class UploadMultipart Method execute Debug:end codecs";
        count = 0
        while 1:
            buffer = f.read(my.chunk_size)
            print "Class UploadMultipart Method execute Debug:[while.f.read]";
            if not buffer:
                break

            if count == 0:
                action = "create"
            else:
                action = "append"

            fields = [
                ("ajax", "true"),
                ("action", action),
            ]
            if my.ticket:
                print "Class UploadMultipart Method execute Debug:[my.ticket]:" + my.ticket;
                fields.append( ("ticket", my.ticket) )
                fields.append( ("login_ticket", my.ticket) )
                basename = os.path.basename(path)
                print "Class UploadMultipart Method execute Debug:[basename]:" + basename;
                from json import dumps as jsondumps
                print "Class UploadMultipart Method execute Debug:import json";
                basename = basename.decode(sys.stdout.encoding)
                print "Class UploadMultipart Method execute Debug:[basename.decode]:" + basename;

才正式跟踪到了异常退出的语句
basename = basename.decode(sys.stdout.encoding)url

##结论 basename = basename.decode(sys.stdout.encoding)
为Python 2所具有的函数,经过Python26 和 Python 27一样调用该函数时,均发生C# Process异常退出的状况。 判断,为Python lib库中的BUG,肯定Python在某些状况下的稳定性测试,并不完备调试

##吐槽 话说Python代码上千了以后,什么鬼!超长的if else 都不知道谁是谁。code

相关文章
相关标签/搜索