Floobits / floobits-sublime

Floobits real-time collaboration plugin for Sublime Text 2 and 3
Apache License 2.0
294 stars 31 forks source link

diff_match_patch.py references old urllib.parse, breaking in ST3 build 3126 #241

Open armahillo opened 7 years ago

armahillo commented 7 years ago

Spent the last hour poking around about this. I saw a few issues in this repo discussed by @wbond and some others, and while it appears that this was working with ST3 build 3120, the current stable build (3126) is not working again, and I cannot install Floobits either via package installer or manually via git.

It seems that the issue is the splitting of the urllib module, discussed on this SO post: https://stackoverflow.com/a/29358613

urllib was split into urllib.parse, urllib.request, and urllib.error in Python 3.

(Relevant PY3 docs: https://docs.python.org/3.3/library/urllib.parse.html#module-urllib.parse )

My instance of ST3 is indeed using PY3 v3.3.6

>>> import sys
>>> print(sys.version)
3.3.6 (default, Sep 22 2016, 23:41:32)

and urllib is sourcing from there as well:

>>> urllib.__file__
'/opt/sublime_text/python3.3.zip/urllib/__init__.pyo'
>>> urllib.__loader__
<zipimporter object "/opt/sublime_text/python3.3.zip">

In file .../Floobits/floo/common/lib/diff_match_patch.py, line 32, it has this:

try:
    from urllib import parse
    assert parse

    def unquote_py3(x):
        return parse.unquote(x)
    unquote = unquote_py3
    str_instances = str
    unichr = chr
except ImportError:
    import urllib as parse

The from urllib import parse seems to be the offending line; and it looks like the script is trying to gracefully accommodate that in the exception block import urllib as parse.

Attempting these in the ST3 terminal, I get:

>>> from urllib import parse
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name parse
>>> import urllib as parse
>>> parse.unquote
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'unquote'

However, in 3.3, these methods are found in the urllib.parse module, and I was able to get a confirmation of loading for parse.unquote by doing this:

>>> from urllib.parse import quote
>>> quote
<function quote at 0x7f1a41b82290>
>>> from urllib.parse import unquote
>>> unquote
<function unquote at 0x7f1a41b82050>

So it appears that these methods are available, but the existing code in that file (.../diff_match_patch.py) is not importing them correctly.

Just on a lark, I tried changing that opening block to:

try:
#    from urllib import parse    # omitted this
#    assert parse                     # omitted this
    from urllib.parse import quote   # added this
    from urllib.parse import unquote  # added this

#    def unquote_py3(x):  # omitted this
#        return parse.unquote(x) # omitted this
#    unquote = unquote_py3 # omitted this
    str_instances = str
    unichr = chr
except ImportError:
    import urllib as parse

    def unquote_py2(x):
        return parse.unquote(x.encode('utf-8')).decode('utf-8')
    unquote = unquote_py2
    import __builtin__
    str_instances = (str, __builtin__.basestring)

And then did a find/replace (s/parse.quote/quote/).

When I closed and reopened ST3, the errors about urllib.parse were no longer showing, however I did see a couple other errors related to urllib, including:

HTTPError = urllib.error.HTTPError
AttributeError: 'module' object has no attribute 'error'

and

Traceback (most recent call last):
  File "/home/aaron/.config/sublime-text-3/Packages/Floobits/floo/common/reactor.py", line 11, in <module>
    from . import api, msg
  File "/home/aaron/.config/sublime-text-3/Packages/Floobits/floo/common/api.py", line 28, in <module>
    import urllib2
ImportError: No module named 'urllib2'

So I suspect that the module refactoring they did in this version has had some effects in other places as well.

I'm super rusty in python (and am looking through this repo for the first time ever tonight) and while I'd be happy to submit a PR for these superficial changes, someone more experienced should really audit this as well.

Happy to answer any followup questions about this issue or provide additional information, if that's needed. (I'm on Ubuntu 17.04)

ggreer commented 7 years ago

I'm very confused by this. I'm using the same platform (Ubuntu 17.04) & build of Sublime (3126) and I can collaborate just fine.

Maybe another plugin is monkey patching urllib?

CromFr commented 6 years ago

Same issue for me (ArchLinux, sublime 3143) Full error log: errors.txt

Naurand commented 6 years ago

Same probleme here, (Ubuntu 14.04.5 LTS) (sublime 3126) Exact same error log. Edit: Ok just worked with new version 3143