# Buggy code
def stringize(value):
    if isinstance(value, (int, long, bool)) or value is None:
        buf.write(str(value))
    elif isinstance(value, unicode):
        text = repr(value)
        if text[0] != "u":
            try:
                value.encode("latin1")
            except UnicodeEncodeError:
                text = "u" + text
        buf.write(text)
    elif isinstance(value, (float, complex, str, bytes)):
        buf.write(repr(value))
    elif isinstance(value, list):
        buf.write("[")
        first = True
        for item in value:
            if not first:
                buf.write(",")
            else:
                first = False
            stringize(item)
        buf.write("]")
    elif isinstance(value, tuple):
        if len(value) > 1:
            buf.write("(")
            first = True
            for item in value:
                if not first:
                    buf.write(",")
                else:
                    first = False
                stringize(item)
            buf.write(")")
        elif value:
            buf.write("(")
            stringize(value[0])
            buf.write(",)")
        else:
            buf.write("()")
    elif isinstance(value, dict):
        buf.write("{")
        first = True
        for key, value in value.items():
            if not first:
                buf.write(",")
            else:
                first = False
            stringize(key)
            buf.write(":")
            stringize(value)
        buf.write("}")
    elif isinstance(value, set):
        buf.write("{")
        first = True
        for item in value:
            if not first:
                buf.write(",")
            else:
                first = False
            stringize(item)
        buf.write("}")
    else:
        raise ValueError("%r cannot be converted into a Python literal" % (value,))


# Traceback (networkx.exception.NetworkXError)
Traceback (most recent call last):
File "test.py", line 8, in <module>
H = nx.read_gml('foo.gml')
File "<decorator-gen-198>", line 2, in read_gml
File "/Users/domoritz/.virtualenvs/ipython/lib/python2.7/site-packages/networkx/utils/decorators.py", line 220, in _open_file
result = func(*new_args, **kwargs)
File "/Users/domoritz/.virtualenvs/ipython/lib/python2.7/site-packages/networkx/readwrite/gml.py", line 210, in read_gml
G = parse_gml_lines(filter_lines(path), label, destringizer)
File "/Users/domoritz/.virtualenvs/ipython/lib/python2.7/site-packages/networkx/readwrite/gml.py", line 383, in parse_gml_lines
graph = parse_graph()
File "/Users/domoritz/.virtualenvs/ipython/lib/python2.7/site-packages/networkx/readwrite/gml.py", line 372, in parse_graph
curr_token, dct = parse_kv(next(tokens))
File "/Users/domoritz/.virtualenvs/ipython/lib/python2.7/site-packages/networkx/readwrite/gml.py", line 357, in parse_kv
curr_token, value = parse_dict(curr_token)
File "/Users/domoritz/.virtualenvs/ipython/lib/python2.7/site-packages/networkx/readwrite/gml.py", line 367, in parse_dict
curr_token, dct = parse_kv(curr_token)
File "/Users/domoritz/.virtualenvs/ipython/lib/python2.7/site-packages/networkx/readwrite/gml.py", line 357, in parse_kv
curr_token, value = parse_dict(curr_token)
File "/Users/domoritz/.virtualenvs/ipython/lib/python2.7/site-packages/networkx/readwrite/gml.py", line 367, in parse_dict
curr_token, dct = parse_kv(curr_token)
File "/Users/domoritz/.virtualenvs/ipython/lib/python2.7/site-packages/networkx/readwrite/gml.py", line 343, in parse_kv
curr_token = next(tokens)
File "/Users/domoritz/.virtualenvs/ipython/lib/python2.7/site-packages/networkx/readwrite/gml.py", line 323, in tokenize
(line[pos:], lineno + 1, pos + 1))
networkx.exception.NetworkXError: cannot tokenize u'True' at (6, 9)

# Fixed code
def stringize(value):
    if isinstance(value, (int, long, bool)) or value is None:
        if value is True:  # GML uses 1/0 for boolean values.
            buf.write(str(1))
        elif value is False:
            buf.write(str(0))
        else:
            buf.write(str(value))
    elif isinstance(value, unicode):
        text = repr(value)
        if text[0] != "u":
            try:
                value.encode("latin1")
            except UnicodeEncodeError:
                text = "u" + text
        buf.write(text)
    elif isinstance(value, (float, complex, str, bytes)):
        buf.write(repr(value))
    elif isinstance(value, list):
        buf.write("[")
        first = True
        for item in value:
            if not first:
                buf.write(",")
            else:
                first = False
            stringize(item)
        buf.write("]")
    elif isinstance(value, tuple):
        if len(value) > 1:
            buf.write("(")
            first = True
            for item in value:
                if not first:
                    buf.write(",")
                else:
                    first = False
                stringize(item)
            buf.write(")")
        elif value:
            buf.write("(")
            stringize(value[0])
            buf.write(",)")
        else:
            buf.write("()")
    elif isinstance(value, dict):
        buf.write("{")
        first = True
        for key, value in value.items():
            if not first:
                buf.write(",")
            else:
                first = False
            stringize(key)
            buf.write(":")
            stringize(value)
        buf.write("}")
    elif isinstance(value, set):
        buf.write("{")
        first = True
        for item in value:
            if not first:
                buf.write(",")
            else:
                first = False
            stringize(item)
        buf.write("}")
    else:
        raise ValueError("%r cannot be converted into a Python literal" % (value,))