def test_format_unicode_filename(self): # Filenames in Python2 may be bytestrings that will fail to implicit # decode. fname = u('\u5341').encode(sys.getfilesystemencoding()) lines = u("1\n2\n3\n") fake_module = dict( __name__='fred', __loader__=FakeLoader(lines) ) linecache.updatecache(fname, fake_module) e = SyntaxError("uh oh") e.filename = fname e.lineno = 2 e.text = b('something wrong') e.offset = 1 c = test_code(fname, 'method') f = test_frame(c, fake_module, {'something': 1}) tb = test_tb(f, 2, None) exc = traceback.TracebackException(SyntaxError, e, tb) list(exc.format_exception_only()) self.assertEqual([ u('Traceback (most recent call last):\n'), u(' File "\u5341", line 2, in method\n 2\n'), u(' File "\u5341", line 2\n'), u(' something wrong\n'), u(' ^\n'), u('SyntaxError: uh oh\n')], list(exc.format()))
def test_format_bad_filename(self): # Filenames in Python2 may be bytestrings that will fail to implicit # decode. # This won't decode via the implicit(ascii) codec or the default # fs encoding (unless the encoding is a wildcard encoding). fname = b('\x8b') lines = u("1\n2\n3\n") fake_module = dict(__name__='fred', __loader__=FakeLoader(lines)) linecache.updatecache(fname, fake_module) e = SyntaxError("uh oh") e.filename = fname e.lineno = 2 e.text = b('something wrong') e.offset = 1 c = test_code(fname, 'method') f = test_frame(c, fake_module, {'something': 1}) tb = test_tb(f, 2, None) exc = traceback.TracebackException(SyntaxError, e, tb) list(exc.format_exception_only()) self.assertEqual([ u('Traceback (most recent call last):\n'), b(' File "b\'\\x8b\'", line 2, in method\n 2\n').decode(), b(' File "b\'\\x8b\'", line 2\n').decode(), u(' something wrong\n'), u(' ^\n'), u('SyntaxError: uh oh\n') ], list(exc.format()))
def test_format_unicode_filename(self): # Filenames in Python2 may be bytestrings that will fail to implicit # decode. fname = u('\u5341').encode(sys.getfilesystemencoding()) lines = u("1\n2\n3\n") fake_module = dict(__name__='fred', __loader__=FakeLoader(lines)) linecache.updatecache(fname, fake_module) e = SyntaxError("uh oh") e.filename = fname e.lineno = 2 e.text = b('something wrong') e.offset = 1 c = test_code(fname, 'method') f = test_frame(c, fake_module, {'something': 1}) tb = test_tb(f, 2, None) exc = traceback.TracebackException(SyntaxError, e, tb) list(exc.format_exception_only()) self.assertEqual([ u('Traceback (most recent call last):\n'), u(' File "\u5341", line 2, in method\n 2\n'), u(' File "\u5341", line 2\n'), u(' something wrong\n'), u(' ^\n'), u('SyntaxError: uh oh\n') ], list(exc.format()))
def test_format_bad_filename(self): # Filenames in Python2 may be bytestrings that will fail to implicit # decode. # This won't decode via the implicit(ascii) codec or the default # fs encoding (unless the encoding is a wildcard encoding). fname = b('\x8b') lines = u("1\n2\n3\n") fake_module = dict( __name__='fred', __loader__=FakeLoader(lines) ) linecache.updatecache(fname, fake_module) e = SyntaxError("uh oh") e.filename = fname e.lineno = 2 e.text = b('something wrong') e.offset = 1 c = test_code(fname, 'method') f = test_frame(c, fake_module, {'something': 1}) tb = test_tb(f, 2, None) exc = traceback.TracebackException(SyntaxError, e, tb) list(exc.format_exception_only()) self.assertEqual([ u('Traceback (most recent call last):\n'), b(' File "b\'\\x8b\'", line 2, in method\n 2\n').decode(), b(' File "b\'\\x8b\'", line 2\n').decode(), u(' something wrong\n'), u(' ^\n'), u('SyntaxError: uh oh\n')], list(exc.format()))
def test_syntax_undecoded_lines(self): # If the interpreter returns bytestrings, we have to decode ourselves. lines = u("1\n\u5341\n3\n") fake_module = dict( __name__='fred', __loader__=FakeLoader(lines) ) linecache.updatecache('/foo.py', fake_module) e = SyntaxError("uh oh") e.filename = '/foo.py' e.lineno = 2 e.text = b('something wrong') e.offset = 1 c = test_code('/foo.py', 'method') f = test_frame(c, fake_module, {'something': 1}) tb = test_tb(f, 2, None) exc = traceback.TracebackException(SyntaxError, e, tb) list(exc.format_exception_only()) self.assertEqual([ u('Traceback (most recent call last):\n'), u(' File "/foo.py", line 2, in method\n \u5341\n'), u(' File "/foo.py", line 2\n'), u(' \u5341\n'), u(' ^\n'), u('SyntaxError: uh oh\n')], list(exc.format()))
def test_no_locals(self): linecache.updatecache('/foo.py', fake_module) e = Exception("uh oh") c = test_code('/foo.py', 'method') f = test_frame(c, fake_module, {'something': 1}) tb = test_tb(f, 6, None) exc = traceback.TracebackException(Exception, e, tb) self.assertEqual(exc.stack[0].locals, None)
def test_extract_stackup_deferred_lookup_lines(self): linecache.clearcache() c = test_code('/foo.py', 'method') f = test_frame(c, None, None) s = traceback.StackSummary.extract(iter([(f, 8)]), lookup_lines=False) self.assertEqual({}, linecache.cache) linecache.updatecache('/foo.py', fake_module) self.assertEqual(s[0].line, "import sys")
def test_locals(self): linecache.updatecache('/foo.py', fake_module) e = Exception("uh oh") c = test_code('/foo.py', 'method') f = test_frame(c, globals(), {'something': 1, 'other': 'string'}) tb = test_tb(f, 6, None) exc = traceback.TracebackException( Exception, e, tb, capture_locals=True) self.assertEqual( exc.stack[0].locals, {'something': '1', 'other': "'string'"})
def test_lookup_lines(self): linecache.clearcache() e = Exception("uh oh") c = test_code('/foo.py', 'method') f = test_frame(c, None, None) tb = test_tb(f, 8, None) exc = traceback.TracebackException(Exception, e, tb, lookup_lines=False) self.assertEqual({}, linecache.cache) linecache.updatecache('/foo.py', fake_module) self.assertEqual(exc.stack[0].line, "import sys")
def test_syntax_no_extras(self): linecache.updatecache('/foo.py', fake_module) e = SyntaxError("uh oh") c = test_code('/foo.py', 'method') f = test_frame(c, fake_module, {'something': 1}) tb = test_tb(f, 6, None) exc = traceback.TracebackException(SyntaxError, e, tb) self.assertEqual([ u('Traceback (most recent call last):\n'), u(' File "/foo.py", line 6, in method\n from io import StringIO\n'), u(' File "<string>", line None\n'), u('SyntaxError: uh oh\n')], list(exc.format()))
def test_lazycache_provide_after_failed_lookup(self): linecache.clearcache() lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) linecache.clearcache() linecache.getlines(NONEXISTENT_FILENAME) linecache.lazycache(NONEXISTENT_FILENAME, globals()) self.assertEqual(lines, linecache.updatecache(NONEXISTENT_FILENAME))
def filedirectorycatchdata( filedirectory): #获取log csv文件数据 输出格式为[[all屏体数据][all按键数据]]数组 global L, usefuldatafile listfile = os.listdir(filedirectory) L = [ filename for filename in listfile if filename[-4:] == '.csv' and not filename.find('summary') != -1 ] print(' ' + '-' * 19 + '导入文件' + '-' * 20) alldata = [] allsampledata = [] allsamplekeydata = [] allsamplecptestdata = [] allsamplecpshortdata = [] nodatafile = [] usefuldatafile = [] for fileadr in L: try: #解决文件存在非法编码导致无法linecache问题 linecache.updatecache(filedirectory + fileadr) linefile = linecache.getlines(filedirectory + fileadr) except Exception: print(str(fileadr) + '该文件数据存在非法字符') newfile = codecs.open(filedirectory + fileadr, 'r', 'gbk', 'ignore') text = newfile.read() newfile.close() with codecs.open(filedirectory + fileadr, 'w') as newfile2: newfile2.write(text) linecache.updatecache(filedirectory + fileadr) linefile = linecache.getlines(filedirectory + fileadr) '''print(filedirectory+fileadr)''' linenumber = 0 starline = 0 endline = 0 sampledata = [] keyarray = [] cpteststartline = 0 cptestendline = 0 cpshortstartline = 0 cpshortendline = 0 sampledata = [] keyarray = [] samplecpselfdata = [] samplecpshortdata = [] for line in linefile: linenumber += 1 if line.find('CMDelta Test Start') != -1: starline = linenumber if line.find('CMDelta Test End') != -1: endline = linenumber if line.find('Self Cp Test Start') != -1: #加入Self CP test cpteststartline = linenumber if line.find('Self Cp Test End') != -1: cptestendline = linenumber if line.find('CP_SHORT Test Start') != -1: #加入CP Short test cpshortstartline = linenumber #print(cpshortstartline) if line.find('CP_SHORT Test End') != -1: cpshortendline = linenumber datanumber = 0 if starline != 0 and endline != 0: dataline = linefile[starline:endline] for data in dataline: if data.find('[Row00]') != -1: datastar = datanumber if data.find('CM Delta Key') != -1: dataend = datanumber datanumber += 1 keydata = dataline[dataend:endline] del keydata[0] del keydata[-1] keyarray = [] for k in keydata: if k == '\n': pass else: keyread = k.split(',') keyrealdata = keyread[:-1] for i in keyrealdata: if i == '' or i == '\n': pass else: newkey = (((((i.replace('[', '')).replace( ']', '')).replace('{', '')).replace( '}', '')).replace('\n', '')).replace('\t', '') keyarray.append(int(newkey)) data = dataline[datastar:dataend - 1] for datare in data: if datare == '\n': pass else: dataread = datare.split(',') d = dataread[1:] slist = [] for s in d: if s == '' or s == '\n': pass else: news = (((((s.replace('[', '')).replace( ']', '')).replace('{', '')).replace( '}', '')).replace('\n', '')).replace('\t', '') slist.append(int(news)) if len(slist) != 0: sampledata.append(slist) usefuldatafile.append(str(fileadr)) else: nodatafile.append(str(fileadr)) if (len(sampledata) != 0): allsampledata.append(sampledata) if (len(keyarray) != 0): allsamplekeydata.append(keyarray) if cpteststartline != 0 and cptestendline != 0: #提取 Self CP 测试数据 #print('try to catch self cp data') selfcpdatanumber = 0 selfcpdataline = linefile[cpteststartline:cptestendline] for selfcpdata in selfcpdataline: if selfcpdata.find('Row00') != -1: selfdatastart = selfcpdatanumber if selfcpdata.find(' Self Cp Test End') != -1: selfdataend = selfcpdatanumber selfcpdatanumber += 1 selfcpdatafile = selfcpdataline[selfdatastart:selfdataend] for datare in selfcpdatafile: if datare == '\n': pass else: dataread = datare.split(',') d = dataread[1:] slist2 = [] for s in d: if s == '' or s == '\n': pass else: news = (((((s.replace('[', '')).replace( ']', '')).replace('{', '')).replace( '}', '')).replace('\n', '')).replace('\t', '') slist2.append(int(news)) if len(slist2) != 0: samplecpselfdata.append(slist2) if (len(samplecpselfdata) != 0): #print(samplecpselfdata) allsamplecptestdata.append(samplecpselfdata) if cpshortstartline != 0 and cpshortendline != 0: #提取 CP SHORT 测试数据 #print('try to catch SHORT data') selfshortnumber = 0 cpshortline = linefile[cpshortstartline:cpshortendline] #print(cpshortline) for cpshortdata in cpshortline: if cpshortdata.find('Row00') != -1: cpshortstart = selfshortnumber if cpshortdata.find(' CP_SHORT Test End') != -1: cpshortend = selfshortnumber selfshortnumber += 1 cpshortfile = cpshortline[cpshortstart:cpshortend] #print(cpshortfile) for datare in cpshortfile: if datare == '\n': pass else: dataread = datare.split(',') d = dataread[1:] slist3 = [] for s in d: if s == '' or s == '\n': pass else: news = (((((s.replace('[', '')).replace( ']', '')).replace('{', '')).replace( '}', '')).replace('\n', '')).replace('\t', '') slist3.append(int(news)) if len(slist3) != 0: samplecpshortdata.append(slist3) if (len(samplecpshortdata) != 0): #print(samplecpshortdata) allsamplecpshortdata.append(samplecpshortdata) print('*' * 19 + '数据不存在样品' + '*' * 19) print(nodatafile) print('\n') '''print('-'*19+'有效文件'+'-'*19) print(usefuldatafile)''' alldata.append(allsampledata) if (len(allsamplekeydata) != 0): alldata.append(allsamplekeydata) return alldata
def test_no_locals(self): linecache.updatecache('/foo.py', globals()) c = test_code('/foo.py', 'method') f = test_frame(c, globals(), {'something': 1}) s = traceback.StackSummary.extract(iter([(f, 6)])) self.assertEqual(s[0].locals, None)