LightTable / Python

Python language plugin for Light Table
MIT License
98 stars 51 forks source link

NameError: name 'unicode' is not defined #24

Open ossplus opened 9 years ago

ossplus commented 9 years ago

just a very simple code : print ("first"), have issue:

Traceback (most recent call last): File "~\AppData\Local\LightTable\plugins\Python\py-src\ltmain.py", line 193, in handleEval code= compile(ensureUtf(code), ensureUtf(data[2]["name"]), 'eval') File "~\AppData\Local\LightTable\plugins\Python\py-src\ltmain.py", line 50, in ensureUtf if type(s) == unicode: NameError: name 'unicode' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "~AppData\Local\LightTable\plugins\Python\py-src\ltmain.py", line 197, in handleEval code= compile(ensureUtf(code), ensureUtf(data[2]["name"]), 'exec') File "~\AppData\Local\LightTable\plugins\Python\py-src\ltmain.py", line 50, in ensureUtf if type(s) == unicode: NameError: name 'unicode' is not defined

berendbaas commented 9 years ago

Same problem here, are you running python 3 by chance? In that case, It seems to be because python 3 doesn't have the unicode type anymore as it's been replaced by/incorperated into the 'str' type. Is development being done on python 3 with this plugin? Would be great if it was. e2bed09776f954308f335b6a7d5654d2692edd08

ossplus commented 9 years ago

yes. python3.4

berendbaas commented 9 years ago

If you want to hotfix it for now, you can simply replace the "unicode" by "str" on line 51 of the /py-src/ltmain.py file in the python plugin directory for Light Table. Don't forget to switch it back if you ever need to use python 2 ;) It would be great, if there was functionality for doing this which was backwards compatible, or if you could switch in your settings between python2 and python3 evaluation, that would then select the corresponding plugin to run. I don't know which design would be preferred by the Light Table team however.

ergodicbreak commented 9 years ago

@berendbaas maybe this would be a reasonable compatible fix?


def ensureUtf(s):
  try:
      if type(s) == unicode:
        return s.encode('utf8', 'ignore')
  except: 
    return str(s)
berendbaas commented 9 years ago

@ergodicbreak Seems like a good way to go to me.

kenny-evitt commented 9 years ago

@berendbaas @ergodicbreak – care to comment on PR #28?

RockyRoad29 commented 8 years ago

Hi, I'm completely new to LightTable and I might be wrong but I think I found a better fix that I just submitted

    def ensureUtf(s, encoding='utf8'):
      """Converts input to unicode if necessary.

      If `s` is bytes, it will be decoded using the `encoding` parameters.

      This function is used for preprocessing /source/ and /filename/ arguments
      to the builtin function `compile`.
      """
      # In Python2, str == bytes.
      # In Python3, bytes remains unchanged, but str means unicode
      # while unicode is not defined anymore
      if type(s) == bytes:
        return s.decode(encoding, 'ignore')
      else:
        return s

I tested it with python2.7.11 and python3.5.1 on my archlinux platform.

Then, if accepted it makes sense to rename the function to reflect its actual role, which I did in a later commit.

dav009 commented 8 years ago

Is this issue moving at all? It seems quite relevant yet quite inactive

keith-hall commented 8 years ago

@dav009 see https://github.com/LightTable/Python/pull/47#issuecomment-173007386 for why no Python fixes are being merged at the moment

kenny-evitt commented 8 years ago

@dav009 We need someone to takeover the Python plugin. As Keith pointed out, we've got potential fixes, but we have no-one to review and merge them. Even if you (or anyone else) doesn't want to take responsibility for the entirety of Python in LT or even just the LT Python plugin, I'm willing to merge PRs if enough commenters agreed on which ones should be merged.

meownoid commented 8 years ago

Still not working in 8.0.1. Is LT dead?

sbauer322 commented 8 years ago

Sorry to hear this is still a problem, @meownoid.

LightTable is not dead, but the current python plugin maintainer, @stemd, appears to be inactive or otherwise not available. That said, we are probably in need of someone to actively work on the plugin.

I am not familiar with Python, but there is an open pull request that may contain a fix https://github.com/LightTable/Python/pull/47. If anyone is able to review or test the fix then I am happy to merge it in.

stemd commented 8 years ago

I managed to get Python3 working on my setup, idea was to test that on all platforms (Linux, Mac, Windows) before merging. Also there was an idea to detect Python2 and Python3, and to adapt automatically, but ClojureScript is not so straightforward, and not all code is well commented.

sbauer322 commented 8 years ago

@stemd, would you be able to point out the poorly documented Clojurescript files or code that are relevant to you? Happy to bump them up in the list of things to document soon.

Aurora11111 commented 6 years ago

in python2.x: item = unicode(item, 'utf-8') in python3.x: item = str(item.encode('utf-8'))

omkarvijay5 commented 5 years ago

@berendbaas maybe this would be a reasonable compatible fix?

def ensureUtf(s):
  try:
      if type(s) == unicode:
        return s.encode('utf8', 'ignore')
  except: 
    return str(s)

Python 3 renamed the unicode type to str, the old str type has been replaced by bytes renaming unicode occurrences with str worked for me