
import io

from .platform import PY3


def file_writer(path=None, encoding='UTF-8', newline=None):
    if path:
        f = io.open(path, 'w', encoding=encoding, newline=newline)
    else:
        f = io.StringIO(newline=newline)
    if PY3:
        return f
    # TODO: Consider removing this and using u'' or `from __future__ import
    # unicode_literals` everywhere.
    write = f.write
    f.write = lambda text: write(unicode(text))
    return f


def binary_file_writer(path=None):
    if path:
        return io.open(path, 'wb')
    f = io.BytesIO()
    getvalue = f.getvalue
    f.getvalue = lambda encoding='UTF-8': getvalue().decode(encoding)
    return f


import io

from .platform import PY3


def file_writer(path=None, encoding='UTF-8', newline=None):
    if path:
        f = io.open(path, 'w', encoding=encoding, newline=newline)
    else:
        f = io.StringIO(newline=newline)
    if PY3:
        return f
    # These streams require written text to be Unicode. We don't want to add
    # `u` prefix to all our strings in Python 2, and cannot really use
    # `unicode_literals` either because many other Python 2 APIs accept only
    # byte strings.
    write = f.write
    f.write = lambda text: write(unicode(text))
    return f


def binary_file_writer(path=None):
    if path:
        return io.open(path, 'wb')
    f = io.BytesIO()
    getvalue = f.getvalue
    f.getvalue = lambda encoding='UTF-8': getvalue().decode(encoding)
    return f

Replace TODO with comment explaining why it wasn't possible
Replace TODO with comment explaining why it wasn't possible

  
  import io
  
  from .platform import PY3
  
  
  def file_writer(path=None, encoding='UTF-8', newline=None):
      if path:
          f = io.open(path, 'w', encoding=encoding, newline=newline)
      else:
          f = io.StringIO(newline=newline)
      if PY3:
          return f
-     # TODO: Consider removing this and using u'' or `from __future__ import
-     # unicode_literals` everywhere.
+     # These streams require written text to be Unicode. We don't want to add
+     # `u` prefix to all our strings in Python 2, and cannot really use
+     # `unicode_literals` either because many other Python 2 APIs accept only
+     # byte strings.
      write = f.write
      f.write = lambda text: write(unicode(text))
      return f
  
  
  def binary_file_writer(path=None):
      if path:
          return io.open(path, 'wb')
      f = io.BytesIO()
      getvalue = f.getvalue
      f.getvalue = lambda encoding='UTF-8': getvalue().decode(encoding)
      return f
  
Replace TODO with comment explaining why it wasn't possible
## Code Before:

import io

from .platform import PY3


def file_writer(path=None, encoding='UTF-8', newline=None):
    if path:
        f = io.open(path, 'w', encoding=encoding, newline=newline)
    else:
        f = io.StringIO(newline=newline)
    if PY3:
        return f
    # TODO: Consider removing this and using u'' or `from __future__ import
    # unicode_literals` everywhere.
    write = f.write
    f.write = lambda text: write(unicode(text))
    return f


def binary_file_writer(path=None):
    if path:
        return io.open(path, 'wb')
    f = io.BytesIO()
    getvalue = f.getvalue
    f.getvalue = lambda encoding='UTF-8': getvalue().decode(encoding)
    return f

## Instruction:
Replace TODO with comment explaining why it wasn't possible
## Code After:

import io

from .platform import PY3


def file_writer(path=None, encoding='UTF-8', newline=None):
    if path:
        f = io.open(path, 'w', encoding=encoding, newline=newline)
    else:
        f = io.StringIO(newline=newline)
    if PY3:
        return f
    # These streams require written text to be Unicode. We don't want to add
    # `u` prefix to all our strings in Python 2, and cannot really use
    # `unicode_literals` either because many other Python 2 APIs accept only
    # byte strings.
    write = f.write
    f.write = lambda text: write(unicode(text))
    return f


def binary_file_writer(path=None):
    if path:
        return io.open(path, 'wb')
    f = io.BytesIO()
    getvalue = f.getvalue
    f.getvalue = lambda encoding='UTF-8': getvalue().decode(encoding)
    return f
