Search code examples
pythonpython-3.xshellsubprocess

Python2 to Python3 convertion? TypeError: memoryview: a bytes-like object is required, not 'str'


I have found a code:

https://github.com/rtulke/rpen/blob/master/rpen.py

which works fine under Python2 but when run under Python3 there is an error which I can't fix. Can one of you help me here?

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import os
import sys
from optparse import OptionParser
from subprocess import Popen, PIPE, STDOUT

parser = OptionParser("usage: cat logfile | %prog [options] searchterm1 searchterm2...")
parser.add_option("-i", action="store_true", dest="ignore_case", default=False, help="perform a case insensitive search")
parser.add_option("-k", action="store_true", dest="display_all", default=False, help="only highlight, do not filter")
(options, args) = parser.parse_args()

colors = [
    ('green','04;01;32'),
    ('yellow','04;01;33'),
    ('red','04;01;31'),
    ('blue','04;01;34'),
    ('purple','0;04;35'),
    ('magenta','04;01;35'),
    ('cyan','04;01;36'),
    ('brown','0;04;33'),
    ]

if len(args) == 0:
    parser.print_help()
    sys.exit()

op = sys.stdin.read()
if not options.display_all:
    if options.ignore_case:
        p = Popen(["egrep", "|".join(args), "--color=always", "-i"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=os.environ.copy())
    else:
        p = Popen(["egrep", "|".join(args), "--color=always"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=os.environ.copy())
    op = p.communicate(input=op)[0]
for i,srch in enumerate(args):
    color = colors[i%len(colors)][1]
    env=os.environ.copy()
    env['GREP_COLORS'] = "mt="+color

    if options.ignore_case:
        p = Popen(["egrep", srch+"|", "--color=always", "-i"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=env)
    else:
        p = Popen(["egrep", srch+"|", "--color=always"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=env)

    op = p.communicate(input=op)[0]
print(op)

if i change to /usr/bin/env python3 then i get here following python3 error.

Traceback (most recent call last):
  File "/usr/local/bin/rpen", line 45, in <module>
    op = p.communicate(input=op)[0]
  File "/usr/lib/python3.7/subprocess.py", line 939, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "/usr/lib/python3.7/subprocess.py", line 1666, in _communicate
    input_view = memoryview(self._input)
TypeError: memoryview: a bytes-like object is required, not 'str'

Solution

  • setting encoding should probably fix it

    #!/usr/bin/env python2
    # -*- coding: utf-8 -*-
    import os
    import sys
    from optparse import OptionParser
    from subprocess import Popen, PIPE, STDOUT
    
    parser = OptionParser("usage: cat logfile | %prog [options] searchterm1 searchterm2...")
    parser.add_option("-i", action="store_true", dest="ignore_case", default=False, help="perform a case insensitive search")
    parser.add_option("-k", action="store_true", dest="display_all", default=False, help="only highlight, do not filter")
    (options, args) = parser.parse_args()
    
    colors = [
        ('green','04;01;32'),
        ('yellow','04;01;33'),
        ('red','04;01;31'),
        ('blue','04;01;34'),
        ('purple','0;04;35'),
        ('magenta','04;01;35'),
        ('cyan','04;01;36'),
        ('brown','0;04;33'),
        ]
    
    if len(args) == 0:
        parser.print_help()
        sys.exit()
    
    op = sys.stdin.read()
    if not options.display_all:
        if options.ignore_case:
            p = Popen(["egrep", "|".join(args), "--color=always", "-i"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=os.environ.copy(),encoding="utf-8")
        else:
            p = Popen(["egrep", "|".join(args), "--color=always"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=os.environ.copy(),encoding="utf-8")
        op = p.communicate(input=op)[0]
    for i,srch in enumerate(args):
        color = colors[i%len(colors)][1]
        env=os.environ.copy()
        env['GREP_COLORS'] = "mt="+color
    
        if options.ignore_case:
            p = Popen(["egrep", srch+"|", "--color=always", "-i"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=env,encoding="utf-8")
        else:
            p = Popen(["egrep", srch+"|", "--color=always"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=env,encoding="utf-8")
    
        op = p.communicate(input=op)[0]
    print(op)