Skip to content

Commit

Permalink
添加更多报错信息
Browse files Browse the repository at this point in the history
  • Loading branch information
testacount1 committed Feb 18, 2019
1 parent 3853aea commit 1c23c87
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"python.unitTest.unittestArgs": [
"-v",
"-s",
"1-基础/控制台",
"-p",
"test*.py"
],
"python.unitTest.pyTestEnabled": false,
"python.unitTest.nosetestsEnabled": false,
"python.unitTest.unittestEnabled": true
}
22 changes: 22 additions & 0 deletions 1-基础/控制台/test翻译.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import unittest
from 解释器 import ZhPyConsole

class test功能(unittest.TestCase):

控制台 = ZhPyConsole()

def test_报错信息(self):
self.assertEqual(self.控制台.中文化("Traceback (most recent call last):"), "回溯 (最近的调用在最后):")
self.assertEqual(self.控制台.中文化("NameError: name '学' is not defined"), "命名错误: 命名'学'未定义")
self.assertEqual(self.控制台.中文化("SyntaxError: invalid syntax"), "语法错误: 不正确的语法")
self.assertEqual(self.控制台.中文化("ZeroDivisionError: division by zero"), "除零错误: 不能被0除")
# self.assertEqual(self.控制台.中文化("NameError: free variable 'type' referenced before assignment in enclosing scope"), "命名错误: 在闭合作用域中, 自由变量'type'在引用之前未被赋值")
# self.assertEqual(self.控制台.中文化("UnboundLocalError: local variable '学生' referenced before assignment"), "本地变量未定义错误: 本地变量'学生'在引用之前未被赋值")
self.assertEqual(self.控制台.中文化("TypeError: must be str, not int"), "类型错误: 不能将整数自动转换为字符串")
# self.assertEqual(self.控制台.中文化("TypeError: unsupported operand type(s) for /: 'str' and 'str'"), "类型错误: 不支持/操作数: 字符串和字符串")
# self.assertEqual(self.控制台.中文化("TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'"),"类型错误: 不支持**或pow()的操作数: 字符串和整数"
# self.assertEqual(self.控制台.中文化("TypeError: can't multiply sequence by non-int of type 'str'"), "类型错误: 不能用非整数的类型--字符串对序列进行累乘")
self.assertEqual(self.控制台.中文化("AttributeError: 'list' object has no attribute 'length'"), "属性错误: 'list'个体没有属性'length'")

if __name__ == '__main__':
unittest.main()
27 changes: 19 additions & 8 deletions 1-基础/控制台/解释器.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,40 @@ class ZhPyConsole(InteractiveConsole):
封装Python控制台, 对输出进行转换
"""
字典 = {
"Traceback (most recent call last):": "回溯 (最近的调用在最后):",
"SyntaxError: invalid syntax": "语法错误: 不正确的语法",
"ZeroDivisionError: division by zero": "除零错误: 不能被0除",
"TypeError: must be str, not int": "类型错误: 不能将整数自动转换为字符串",
"AttributeError": "属性错误",
" object has no attribute ": "个体没有属性",
" is not defined": "未定义",
"NameError": "命名错误",
"name ": "命名"
}

def showtraceback(self):
sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
sys.last_traceback = last_tb
sys.last_type, sys.last_value, 回溯信息 = 运行信息 = sys.exc_info()
sys.last_traceback = 回溯信息
try:
= traceback.format_exception(ei[0], ei[1], last_tb.tb_next)
= traceback.format_exception(运行信息[0], 运行信息[1], 回溯信息.tb_next)
汉化行 = []
if sys.excepthook is sys.__excepthook__:
for 某行 in :
for 英文 in self.字典:
某行 = 某行.replace(英文, self.字典[英文])
汉化行.append(某行)
汉化行.append(self.中文化(某行))
self.write(''.join(汉化行))
else:
# If someone has set sys.excepthook, we let that take precedence
# over self.write
sys.excepthook(ei[0], ei[1], last_tb)
sys.excepthook(运行信息[0], 运行信息[1], 回溯信息)
finally:
last_tb = ei = None
回溯信息 = 运行信息 = None

def 中文化(self, 原始信息):
if 原始信息 in self.字典:
return self.字典[原始信息]
for 英文 in self.字典:
原始信息 = 原始信息.replace(英文, self.字典[英文])
return 原始信息

def 解释器(lang=None):
"""
Expand Down
23 changes: 16 additions & 7 deletions 1-基础/控制台/设计.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### 语法错误
参考: https://docs.python.org/3/tutorial/errors.html#syntax-errors
```
```python
>>> while True print('好啊')
File "<stdin>", line 1
while True print('好啊')
Expand All @@ -16,7 +16,7 @@ SyntaxError: invalid syntax
### 运行时错误

#### 除零错误(ZeroDivisionError)
```
```python
>>> 10 * (1/0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Expand All @@ -26,7 +26,7 @@ ZeroDivisionError: division by zero

#### 命名错误(NameError)
- 实例1
```
```python
>>>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Expand All @@ -35,7 +35,7 @@ NameError: name '学' is not defined
```
- 实例2
参考: https://stackoverflow.com/questions/43778685/nameerror-free-variable-type-referenced-before-assignment-in-enclosing-scop?rq=1
```
```python
>>> def foo():
... def bar():
... print(type)
Expand All @@ -53,7 +53,7 @@ NameError: free variable 'type' referenced before assignment in enclosing scope

#### 本地变量未定义错误(UnboundLocalError)
参考: https://stackoverflow.com/questions/18002794/local-variable-referenced-before-assignment-in-python
```
```python
>>> def 上课():
... 学生 = 学生*2
...
Expand All @@ -68,15 +68,15 @@ UnboundLocalError: local variable '学生' referenced before assignment
#### 类型错误(TypeError)
参考: https://docs.python.org/3.6/tutorial/errors.html#exceptions
但与文档不一致"TypeError: Can't convert 'int' object to str implicitly"
```
```python
>>> '2'+2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be str, not int
类型错误: 不能将整数自动转换为字符串
```

```
```python
>>> '2'/'1'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Expand All @@ -94,6 +94,15 @@ TypeError: can't multiply sequence by non-int of type 'str'
类型错误: 不能用非整数的类型--字符串对序列进行累乘
```

#### 属性错误(AttributeError)
```python
>>> [1,2,3].length
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'list' object has no attribute 'length'
属性错误: 'list'个体没有'length'属性
```

### Python编译器源码

#### 尚待找实例:
Expand Down

0 comments on commit 1c23c87

Please sign in to comment.