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

Python3 compatibility breaks brew file init command #69

Closed
zebernst opened this issue Dec 2, 2016 · 11 comments
Closed

Python3 compatibility breaks brew file init command #69

zebernst opened this issue Dec 2, 2016 · 11 comments

Comments

@zebernst
Copy link

zebernst commented Dec 2, 2016

When executing the brew file init command (using Python 3.5.2), the following error is thrown:

Traceback (most recent call last):
  File "/usr/local/bin/brew-file", line 3162, in <module>
    main()
  File "/usr/local/bin/brew-file", line 3159, in main
    b.execute()
  File "/usr/local/bin/brew-file", line 2786, in execute
    self.initialize()
  File "/usr/local/bin/brew-file", line 1790, in initialize
    ans = self.ask_yn("Do you want to overwrite it?")
  File "/usr/local/bin/brew-file", line 880, in ask_yn
    yn = my_input(question + " [y/n]: ").lower()
  File "/usr/local/bin/brew-file", line 66, in my_input
    return input(word)
UnboundLocalError: local variable 'input' referenced before assignment

I believe that I fixed the issue on my installation by modifying the my_input function, and then making a similar revision to the my_decode function because it looked like it might cause similar errors in the future. These are my modifications:

from builtins import str, input
def my_decode(word):  # pragma: no cover
    """Decode when python3 is used."""

    return str(word.decode())
def my_input(word):  # pragma: no cover
    """Input method compatibility."""

    return input(word)

Now, type(my_decode(word)) returns <class 'str'> in Python 3 and <class 'future.types.newstr.newstr'> in Python 2. According to python-future,

"future contains a newstr type that is a backport of the str object from Python 3. This inherits from the Python 2 unicode class but has customizations to improve compatibility with Python 3’s str object."

Additionally, the input() method is now equivalent to raw_input() in Python 2, while remaining unchanged in Python 3. I didn't see any Python 2 input() calls that needed to be changed to eval(input()), but I could have missed one.

I don't know if you had your own reasons for not importing from the builtins module that come with the future module, and granted I did no testing other than looking to see if the brew file init command worked, but it seems to be working so far.

@rcmdnk
Copy link
Owner

rcmdnk commented Dec 4, 2016

Thank you for the feedback.

First of all, could you please upgrade brerw-file? It seems older version.
Current version is:

$ brew file --version
brew-file v4.1.3 15/Nov/2016

input function is built-in and I'm not sure why it shows before assignment error.
(could be something wrong in the old code, so it could be helpful if you could tell current version.)

About builtin module, future package is needed to be installed for python2,
so that these functions were implemented.

@rcmdnk rcmdnk closed this as completed Dec 13, 2016
@zebernst
Copy link
Author

Hey, I'm sorry I totally forgot about this thread. It's been pretty hectic with the end of the semester and all. I am, in fact, on the latest version of brew-file.

$ brew file --version
brew-file v4.1.3 15/Nov/2016

@rcmdnk
Copy link
Owner

rcmdnk commented Dec 14, 2016

Could you please check followings?

$ which python
$ type python
$ python --version
$ echo $PYTHONPATH

And test input w/o import, like:

$ python
Python 3.5.2 (default, Oct 12 2016, 06:30:24)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> input("test: ")
test: aaa
'aaa'
>>>

@rcmdnk rcmdnk reopened this Dec 14, 2016
@zebernst
Copy link
Author

I use pyenv to manage my Python installations and virtual environments, so I doubt that these will be very helpful to you (especially the blank $PYTHONPATH:

% which python
/usr/local/var/pyenv/shims/python
% pyenv which python
/usr/local/var/pyenv/versions/3.5.2/bin/python
% type python
python is /usr/local/var/pyenv/shims/python
% python --version
Python 3.5.2
% echo $PYTHONPATH    

Here's the test of input() in the python cli:

% python
Python 3.5.2 (default, Nov 28 2016, 12:44:30)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> input('test: ')
test: aaa

@rcmdnk
Copy link
Owner

rcmdnk commented Dec 14, 2016

brew-file uses shebang of #!/usr/bin/env python, so it is same python you are using.
And you can use input directly, so I don't know why you got an error of input...

Here is the minimum script of my_input:

#!/usr/bin/env python
is_python3 = -1


def my_input(word):
    """Input method compatibility."""

    global is_python3
    if is_python3 == -1:
        try:
            input = raw_input
            is_python3 = 0
        except:
            is_python3 = 1
    elif is_python3 == 0:
        input = raw_input
    return input(word)

print my_input("test: ")

Could you please try to run this script?

@rcmdnk rcmdnk closed this as completed Jan 30, 2017
@rcmdnk
Copy link
Owner

rcmdnk commented Jan 30, 2017

if you still have a problem, re-open the issue.

@zebernst
Copy link
Author

zebernst commented Feb 6, 2017

The problem had stopped occurring, but it just happened again today. I ran the script you gave (and changed the print statement on the last line to be correct for python 3) and got the following:

Traceback (most recent call last):
  File "/Users/zach/Downloads/inputtest.py", line 21, in <module>
    print(my_input("test: "))
  File "/Users/zach/Downloads/inputtest.py", line 19, in my_input
    return input(word)
UnboundLocalError: local variable 'input' referenced before assignment

I also added a statement to the script to test which version of python is running (print(sys.version)) and I verified that the script is being run with Python 3.5.2.

@rcmdnk
Copy link
Owner

rcmdnk commented Feb 7, 2017

Now I understand the problem.
It is actually problematic code, sorry.

fixed input problem at python3 · rcmdnk/homebrew-file@cbbbc70

This fix should solve it, in v4.1.8.

@victorloux
Copy link

victorloux commented Mar 24, 2017

Hi, I'm still having the same problem despite using the latest version (v4.1.14 20/Mar/2017).

Running brew file init or init:

Traceback (most recent call last):
  File "/usr/local/bin/brew-file", line 3251, in <module>
    main()
  File "/usr/local/bin/brew-file", line 3248, in main
    b.execute()
  File "/usr/local/bin/brew-file", line 2875, in execute
    self.initialize()
  File "/usr/local/bin/brew-file", line 1834, in initialize
    "((n) for local Brewfile).")
  File "/usr/local/bin/brew-file", line 883, in ask_yn
    yn = my_input(question + " [y/n]: ").lower()
  File "/usr/local/bin/brew-file", line 60, in my_input
    return _input(word)
UnboundLocalError: local variable '_input' referenced before assignment

I sort of got a workaround working it by changing the shebang, to force using python2 instead of python3. Let me know if I can give you any other thing that could help you.

@rcmdnk
Copy link
Owner

rcmdnk commented Mar 24, 2017

yes, sry, I put another bug...

I'v fixed and v4.1.15 must be fine...

@victorloux
Copy link

Thanks for the quick reply! It works now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants