第一次为开源社区提交bug,那叫激动啊
偶学识尚浅,虽然一直非常喜欢开源软件,但是仅仅是做用户的。今天正值偶学习Python一周年之际(2005/12/25),提交了一个SQLObject的bug,写文庆祝一下。各位大侠恐怕要笑了,不过这是偶艰苦学习中迈出的新的一步,望各位鼓励。
SQLObject是Python的一个ORM映射框架,算是Python里最流行的ORM了吧。偶学习了一段时间,发现其中的StringCol非常不好用,无法存取任何除了ASCII的字符串。GBK之类的自然不好使,即便是UTF-8也不行。于是仔细追查了一下代码,发现col.py中的StringValidator类的类型转换有些问题,直接干巴巴的写着如下代码:
return value.encode("ascii")
见SQLObject 0.7.1的col.py文件513行和522行。就是这个地方抛出了UnicodeEncodeError异常。于是尝试改了一下,将其中的"ascii"改为使用系统默认编码字符集。如下:
import sys
return value.encode(sys.getdefaultencoding())
于是便好了。说实在的,第一次领略到开源软件的威力,实在受益匪浅。在这样修改之后,只要在使用StringCol的脚本中用如下方法设置系统默认编码字符集:
import sys
reload(sys)
sys.setdefaultencoding("GBK")
就可以实现直接对列进行赋值了,方便的很。当然有自虐倾向者还是可以自己转换成unicode类型赋值到UnicodeCol类型的列。
自己修复bug之后,多次尝试提交都不知门路。直到最近几天才找到了SQLObject在sourceforge.net上的家,刚刚也就提交了这个bug。
偶英语实在是不行,加之没有参考别人怎么提交bug,乱写一通,不知语病有多少斤,但愿不会有损国威。原文如下:
StringCol can't treat other encoding except "ASCII" Private: (?)
No
Hi, I'm Chinese. I'm very like SQLObject. But, I found a encoding bug in the class StringValidator. This class is in the script "col.py". When I input a other encoding string input or output from database with StringCol it will raise a UnicodeEncodeError, because I use GBK encoding to access the string. But in StringValidator Only allow "acsii" encoding.
I have fix it, the new code is below:
import sys
class StringValidator(validators.Validator):
def to_python(self, value, state):
if value is None:
return None
if isinstance(value, unicode):
return value.encode(sys.getdefaultencoding())
return value
def from_python(self, value, state):
if value is None:
return None
if isinstance(value, str):
return value
if isinstance(value, unicode):
return value.encode(sys.getdefaultencoding())
return value
Then, I can use StringCol to access a "GBK" encoding string. Also I must set the default encoding before this:
import sys
reload(sys)
sys.setdefaultencoding("GBK")
Please fix it, Thanks.
SQLObject 0.7.1
希望自己将来可以为开源社区贡献更大的力量。也希望大家多多鼓励。