在Windows上使用Python Behave + non-ascii步骤

时间:2016-03-24 14:56:44

标签: python windows unicode python-behave

Python BDD框架Behave在其Histogram frame 0=[684213;291263;126683;78313;50989;30860;93541;76835;154938;55532;38304;93131;479949345;34612;34889;39014;25014;13223;14521;9534;5310;3310;2650;19581]

中包含以下代码
runner.py

如您所见,with open(filename) as f: # -- FIX issue #80: exec(f.read(), globals, locals) # try: filename2 = os.path.relpath(filename, os.getcwd()) code = compile(f.read(), filename2, 'exec') 没有提供字符集。根据其文档,open用于此类案例。

但在Windows上,此函数总是返回一个字节的字符集(所谓的“非字符串程序的字符集”)。拉丁语为Windows-1252,西里尔语为Windows-1251等。

因此,UTF-8 py文件总是被破坏。

我的问题是,如果想要不使用1字节字符集,我如何在Windows上使用非ascii步骤定义?

1 个答案:

答案 0 :(得分:5)

错误是代码不会在二进制模式中打开文件,这会绕过本地编码的任何问题。

似乎python-behave devs have already fixed this issue,所以你的问题的一个解决方案是更新到最新的git版本。但是如果你不能这样做,那么使用exec_file函数的当前git版本对runner.py模块进行猴子修补应该很容易。