{"action":"closed","issue":{"url":"https://api.github.com/repos/fschulze/pytest-flakes/issues/6","labels_url":"https://api.github.com/repos/fschulze/pytest-flakes/issues/6/labels{/name}","comments_url":"https://api.github.com/repos/fschulze/pytest-flakes/issues/6/comments","events_url":"https://api.github.com/repos/fschulze/pytest-flakes/issues/6/events","html_url":"https://github.com/fschulze/pytest-flakes/issues/6","id":79305869,"number":6,"title":"pytest-flakes doesn't respect PEP 263 encoding markers","user":{"login":"The-Compiler","id":625793,"avatar_url":"https://avatars.githubusercontent.com/u/625793?v=3","gravatar_id":"","url":"https://api.github.com/users/The-Compiler","html_url":"https://github.com/The-Compiler","followers_url":"https://api.github.com/users/The-Compiler/followers","following_url":"https://api.github.com/users/The-Compiler/following{/other_user}","gists_url":"https://api.github.com/users/The-Compiler/gists{/gist_id}","starred_url":"https://api.github.com/users/The-Compiler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/The-Compiler/subscriptions","organizations_url":"https://api.github.com/users/The-Compiler/orgs","repos_url":"https://api.github.com/users/The-Compiler/repos","events_url":"https://api.github.com/users/The-Compiler/events{/privacy}","received_events_url":"https://api.github.com/users/The-Compiler/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-05-22T05:59:39Z","updated_at":"2015-06-01T06:35:05Z","closed_at":"2015-06-01T06:35:05Z","body":"I noticed pytest-flakes doesn't respect [PEP 263](https://www.python.org/dev/peps/pep-0263/) encoding markers.\r\n\r\nThis means with a non-UTF8 aware locale (e.g. `LC_ALL=C`) or with an OS which doesn't use UTF-8 by default (e.g. latin1 on Windows), files containing UTF-8 won't be checkable:\r\n\r\n```\r\n$ LC_ALL=C ./venv/bin/py.test --flakes snowman.py -rs\r\n[...]\r\nplatform linux -- Python 3.4.3 -- py-1.4.27 -- pytest-2.7.1\r\n[...]\r\nvenv/lib/python3.4/site-packages/pytest_flakes.py:75: in runtest\r\n    found_errors, out = check_file(self.fspath, self.flakesignore)\r\nvenv/lib/python3.4/site-packages/pytest_flakes.py:124: in check_file\r\n    codeString = path.read()\r\nvenv/lib/python3.4/site-packages/py/_path/common.py:133: in read\r\n    return f.read()\r\nvenv/lib/python3.4/encodings/ascii.py:26: in decode\r\n    return codecs.ascii_decode(input, self.errors)[0]\r\nE   UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 29: ordinal not in range(128)\r\n```\r\n\r\nsnowman.py:\r\n```python\r\n# encoding=utf-8\r\n\r\nsnowman = \"☃\"\r\n```\r\n\r\nA normal `flake8 snowman.py` works fine.\r\n\r\nThis [breaks pytest's tests](https://bitbucket.org/pytest-dev/pytest/issue/747/flakes-test-fail) with tox >= 2.0.0 as it [isolates environment variables by default](http://tox.readthedocs.org/en/latest/changelog.html#id2).\r\n\r\nA possible fix would be to use [tokenize.open](https://docs.python.org/3.4/library/tokenize.html#tokenize.open):\r\n\r\n```diff\r\n--- pytest_flakes.py.ori\t2015-05-22 07:46:50.005433885 +0200\r\n+++ pytest_flakes.py\t2015-05-22 07:47:35.018767377 +0200\r\n@@ -4,6 +4,7 @@\r\n import py\r\n import pytest\r\n import sys\r\n+import tokenize\r\n \r\n \r\n def assignment_monkeypatched_init(self, name, source):\r\n@@ -121,7 +122,8 @@\r\n \r\n \r\n def check_file(path, flakesignore):\r\n-    codeString = path.read()\r\n+    with tokenize.open(str(path)) as f:\r\n+        codeString = f.read()\r\n     filename = py.builtin._totext(path)\r\n     errors = []\r\n     try:\r\n```\r\n\r\nThat seems to fix it for Python 3 - but I haven't tested Python 2 yet. Maybe it's enough to just use normal reading there, or maybe tokenize.open has to be backported, like [coverage.py did](https://bitbucket.org/ned/coveragepy/src/bd120f2a1d9852f3d68ea258fc6c8d07fe3f49cb/coverage/phystokens.py?at=default#cl-155)."}}