Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception on unencodable (file)name #533

nedbat opened this issue Nov 16, 2016 · 3 comments

Exception on unencodable (file)name #533

nedbat opened this issue Nov 16, 2016 · 3 comments
bug Something isn't working


Copy link

nedbat commented Nov 16, 2016

Originally reported by Antoine Pitrou (Bitbucket: pitrou, GitHub: pitrou)

Some code generators (e.g. Template engines) can produce fake module names. Recently a test in the Tornado test suite has started failing with a non-ascii fake module name and a C locale:

I would suggest that catch UnicodeEncodeError at that point and consider the file as non-existing.

Copy link
Owner Author

nedbat commented Nov 17, 2016

Fixed in 696d259b6031 (bb). Figuring out how to reproduce and test the problem was the biggest challenge!

Copy link
Owner Author

nedbat commented Nov 17, 2016

Original comment by Antoine Pitrou (Bitbucket: pitrou, GitHub: pitrou)

Thanks for being so quick :-)

Copy link
Owner Author

nedbat commented Dec 27, 2016

This fix was released as part of 4.3.

@nedbat nedbat closed this as completed Dec 27, 2016
@nedbat nedbat added minor bug Something isn't working labels Jun 23, 2018
nedbat added a commit that referenced this issue Jan 3, 2022
…nly on 3.6

This was the failure it was protecting against on Python 3.6:

  ___________________________ ProcessTest.test_lang_c ____________________________
  [gw0] linux -- Python 3.6.15 /home/runner/work/coveragepy/coveragepy/.tox/py36/bin/python

  self = <tests.test_process.ProcessTest object at 0x7fe57e63b198>

      @pytest.mark.skipif(env.PYPY, reason="PyPy is unreliable with this test")
      # Jython as of 2.7.1rc3 won't compile a filename that isn't utf-8.
      @pytest.mark.skipif(env.JYTHON, reason="Jython can't handle this test")
      def test_lang_c(self):
          # LANG=C forces getfilesystemencoding on Linux to 'ascii', which causes
          # failures with non-ascii file names. We don't want to make a real file
          # with strange characters, though, because that gets the test runners
          # tangled up.  This will isolate the concerns to the code.
          # #533
          self.make_file("", r"""
              globs = {}
              code = "a = 1\nb = 2\n"
              exec(compile(code, "wut\xe9\xea\xeb\xec\x01\", 'exec'), globs)
          self.set_environ("LANG", "C")
          out = self.run_command("coverage run")
  >       assert out == "1\n2\n"
  E       assert 'Traceback (m...ion by zero\n' == '1\n2\n'
  E         - 1
  E         - 2
  E         + Traceback (most recent call last):
  E         +   File "/home/runner/work/coveragepy/coveragepy/coverage/", line 149, in abs_file
  E         +     path = os.path.realpath(path)
  E         +   File "/opt/hostedtoolcache/Python/3.6.15/x64/lib/python3.6/", line 395, in realpath
  E         +     path, ok = _joinrealpath(filename[:0], filename, {})
  E         +   File "/opt/hostedtoolcache/Python/3.6.15/x64/lib/python3.6/", line 429, in _joinrealpath
  E         +     if not islink(newpath):
  E         +   File "/opt/hostedtoolcache/Python/3.6.15/x64/lib/python3.6/", line 171, in islink
  E         +     st = os.lstat(path)
  E         + UnicodeEncodeError: 'ascii' codec can't encode characters in position 3-6: ordinal not in range(128)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
bug Something isn't working
None yet

No branches or pull requests

1 participant