python-rope / rope

a python refactoring library
GNU Lesser General Public License v3.0
1.95k stars 164 forks source link

Uncaught LookupError with an unknown coding: line #83

Open jorgenschaefer opened 10 years ago

jorgenschaefer commented 10 years ago

The following code produces an uncaught LookupError. I would expect Rope to produce a ModuleSyntaxError, as it does when the string passed to code_assist is str and not unicode.

import shutil
import tempfile

import rope.base.project
import rope.contrib.codeassist

project_root = tempfile.mkdtemp()
try:

    project = rope.base.project.Project(project_root)
    rope.contrib.codeassist.code_assist(
        project, u"""\                                                          
# coding: utf-8X                                                                
        """, 0, maxfixes=5
    )

finally:
    shutil.rmtree(project_root)

Backtrace:

Traceback (most recent call last):
  File "foo.py", line 15, in <module>
    """, 0, maxfixes=5
  File "/home/forcer/.virtualenvs/elpy/local/lib/python2.7/site-packages/rope/contrib/codeassist.py", line 33, in code_assist
    return assist()
  File "/home/forcer/.virtualenvs/elpy/local/lib/python2.7/site-packages/rope/contrib/codeassist.py", line 321, in __call__
    completions = list(self._code_completions().values())
  File "/home/forcer/.virtualenvs/elpy/local/lib/python2.7/site-packages/rope/contrib/codeassist.py", line 390, in _code_completions
    pymodule = fixer.get_pymodule()
  File "/home/forcer/.virtualenvs/elpy/local/lib/python2.7/site-packages/rope/base/utils.py", line 11, in _wrapper
    setattr(self, name, func(self, *args, **kwds))
  File "/home/forcer/.virtualenvs/elpy/local/lib/python2.7/site-packages/rope/contrib/fixsyntax.py", line 28, in get_pymodule
    code, resource=self.resource, force_errors=True)
  File "/home/forcer/.virtualenvs/elpy/local/lib/python2.7/site-packages/rope/base/pycore.py", line 114, in get_string_module
    return PyModule(self, code, resource, force_errors=force_errors)
  File "/home/forcer/.virtualenvs/elpy/local/lib/python2.7/site-packages/rope/base/pyobjectsdef.py", line 155, in __init__
    source, node = self._init_source(pycore, source, resource)
  File "/home/forcer/.virtualenvs/elpy/local/lib/python2.7/site-packages/rope/base/pyobjectsdef.py", line 179, in _init_source
    source_bytes = fscommands.unicode_to_file_data(source_code)
  File "/home/forcer/.virtualenvs/elpy/local/lib/python2.7/site-packages/rope/base/fscommands.py", line 196, in unicode_to_file_data
    return contents.encode(encoding)
LookupError: unknown encoding: utf-8X
mcepl commented 9 years ago

No, really, this is nonsense. rope is not expected to fix broken code, and I don't want to make it into corrector of one.

jorgenschaefer commented 9 years ago

No, really, this is nonsense. rope is not expected to fix broken code, and I don't want to make it into corrector of one.

Thank you for the clarification.

I would like to disagree on the "nonsense" part, though. Certain features of Rope, for example completion suggestions, calltips, etc., are primarily useful when editing a source file. While editing source code, those files will not be syntactically correct. Relying on syntactically correct source files makes those features in Rope mostly useless.

Additionally, rope already does some automatic corrections in source files (max_fixes). In addition to that, it already does catch syntax errors in certain cases (ModuleSyntaxError). This allows users of Rope to distinguish errors in Rope from errors caused by malformed input sources. You are now asking users of Rope to ignore all exceptions Rope might throw as "probably because the input was malformed", which means it will be impossible to detect and report actual errors in Rope.

I can do this of course. I just want to make sure that you are aware of this problem.

mcepl commented 9 years ago

First of all, let me apologize for that “nonsense”. I shouldn’t use the term. On the other hand, let me present to you my “mission statement” in https://github.com/python-rope/rope/issues/57#issuecomment-47872067 . If you have any free capacity and produce a pull request, I will certainly consider it. On the third hand, being syntactically correct is not that difficult with help of flake8 or similar validator and proper plugin to your $EDITOR.

mcepl commented 8 years ago

OK, following on dicussing on jorgenschaefer/elpy#702 reopening this ticket. If anybody provides pull request, I am willing to consider it.