0

Python BDD framework Behave has the following code in its 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')

As you can see, no charset is provided to open. According to its documentation, locale.getpreferredencoding is used for such cases.

But on Windows this function always return one-byte charset (so called "charset for non-unicode programs"). It is Windows-1252 for latin, Windows-1251 for Cyrillic etc.

So,UTF-8 py file is always broken.

My question is how can I use non-ascii step definitions with Behave on Windows if want to stay out of 1-byte charset?

user996142
  • 2,753
  • 3
  • 29
  • 48

1 Answers1

5

The bug there is that the code does not take care to open the file in binary mode, which would bypass any issues with local encodings.

And it seems that python-behave devs have already fixed this issue, so one solution to your problem would be to update to the latest git version. But if you can't do that, it should be easy enough to monkey-patch the runner.py module with the current git version of the exec_file function.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336