Skip to content

Commit

Permalink
Revert "Python 3: file() --> open() in yaml-highlight example (#236)"
Browse files Browse the repository at this point in the history
This reverts commit a5f4329.

Still not working with python3:
    % PYTHONPATH=$PWD/lib3 python3 examples/yaml-highlight/yaml_hl.py <.appveyor.yml
    Traceback (most recent call last):
      File "examples/yaml-highlight/yaml_hl.py", line 126, in <module>
        hl.highlight()
      File "examples/yaml-highlight/yaml_hl.py", line 66, in highlight
        if input.startswith(codecs.BOM_UTF16_LE):
    TypeError: startswith first arg must be str or a tuple of str, not bytes

and getting this warning in python2:
Exception AttributeError: "'NoneType' object has no attribute 'stdin'" in <bound method YAMLHighlight.__del__ of <__main__.YAMLHighlight instance at 0x7fb9b81a47a0>> ignored
  • Loading branch information
perlpunk committed Dec 8, 2019
1 parent a5f4329 commit daf6041
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions examples/yaml-highlight/yaml_hl.py
Expand Up @@ -2,12 +2,6 @@

import yaml, codecs, sys, os.path, optparse

try:
unicode
except NameError:
unicode = str


class Style:

def __init__(self, header=None, footer=None,
Expand Down Expand Up @@ -43,24 +37,17 @@ def __setstate__(self, state):
class YAMLHighlight:

def __init__(self, options):
with open(options.config, 'rb') as yaml_file:
config = yaml.load(yaml_file.read())
config = yaml.load(file(options.config, 'rb').read())
self.style = config[options.style]
if options.input:
self.input = open(options.input, 'rb')
self.input = file(options.input, 'rb')
else:
self.input = sys.stdin
if options.output:
self.output = open(options.output, 'wb')
self.output = file(options.output, 'wb')
else:
self.output = sys.stdout

def __del__(self):
if self.input not in (sys.stdin, None):
self.input.close()
if self.output not in (sys.stdout, None):
self.output.close()

def highlight(self):
input = self.input.read()
if input.startswith(codecs.BOM_UTF16_LE):
Expand Down

0 comments on commit daf6041

Please sign in to comment.