# Buggy code
def _encode(value, fallback=None):
    """Encode the value using the specified encoding.

    It fallbacks to the specified encoding or SYS_ENCODING if not defined

    :param value: the value to be encoded
    :type value: str
    :param encoding: the encoding to be used
    :type encoding: str
    :param fallback: the fallback encoding to be used
    :type fallback: str
    :return: the encoded value
    :rtype: str
    """
    encoding = "utf-8" if os.name != "nt" else app.SYS_ENCODING

    try:
        return value.encode(encoding)
    except UnicodeEncodeError:
        logger.debug(
            "Failed to encode to %s, falling back to %s: %r",
            encoding,
            fallback or app.SYS_ENCODING,
            value,
        )
        return value.encode(fallback or app.SYS_ENCODING)


# Traceback (TypeError)
2018-02-04 19:57:28 DEBUG    Thread-17 :: [bd08325] Current quality: 720p HDTV
2018-02-04 19:57:28 DEBUG    Thread-17 :: [bd08325] New quality: 720p HDTV
2018-02-04 19:57:28 DEBUG    Thread-17 :: [bd08325] Proper: False
2018-02-04 19:57:28 DEBUG    Thread-17 :: [bd08325] This episode is a priority download: True
2018-02-04 19:57:28 INFO     Thread-17 :: [bd08325] New file: /srv/media/automatic/The.Flash.2014.S04E12.FASTSUB.VOSTFR.720p.HDTV.x264-ZKH.mkv
2018-02-04 19:57:28 INFO     Thread-17 :: [bd08325] New size: 941.42 MB
2018-02-04 19:57:28 DEBUG    Thread-17 :: [bd08325] There is no existing file so there's no worries about replacing it
2018-02-04 19:57:28 INFO     Thread-17 :: [bd08325] This download is marked a priority download so I'm going to replace an existing file if I find one
2018-02-04 19:57:28 DEBUG    Thread-17 :: [bd08325] Trying to determine free space on destination drive
2018-02-04 19:57:28 DEBUG    Thread-17 :: [bd08325] Found release name The.Flash.2014.S04E12.FASTSUB.VOSTFR.720p.HDTV.x264-ZKH
2018-02-04 19:57:29 DEBUG    Thread-17 :: [bd08325] Folder name (automatic) appears to be a valid release name. Using it.
2018-02-04 19:57:29 DEBUG    Thread-17 :: [bd08325] Matched release The.Flash.2014.S04E12.FASTSUB.VOSTFR.720p.HDTV.x264-ZKH to a series in your database: The Flash (2014)
2018-02-04 19:57:29 DEBUG    Thread-17 :: [bd08325] Parsed The.Flash.2014.S04E12.FASTSUB.VOSTFR.720p.HDTV.x264-ZKH into title: The Flash, alias: The Flash 2014, year: 2014, season: 4, episode: [12], other: Fastsub, subtitle_language: fr, screen_size: 720p, format: HDTV, video_codec: h264, video_encoder: x264, release_group: ZKH, type: episode, parsing_time: 0.0328960418701, total_time: 0.0329329967499, absolute_episode: [], quality: 720p HDTV
2018-02-04 19:57:29 DEBUG    Thread-17 :: [bd08325] Found codec for The Flash (2014) Honey, I Shrunk Team Flash
2018-02-04 19:57:29 DEBUG    Thread-17 :: [bd08325] 279121: Formatting pattern: %SN S%0SE%0E -> The Flash (2014) S04E12
2018-02-04 19:57:29 DEBUG    Thread-17 :: [bd08325] Matched release The.Flash.2014.S04E12.FASTSUB.VOSTFR.720p.HDTV.x264-ZKH to a series in your database: The Flash (2014)
2018-02-04 19:57:29 DEBUG    Thread-17 :: [bd08325] Parsed The.Flash.2014.S04E12.FASTSUB.VOSTFR.720p.HDTV.x264-ZKH into title: The Flash, alias: The Flash 2014, year: 2014, season: 4, episode: [12], other: Fastsub, subtitle_language: fr, screen_size: 720p, format: HDTV, video_codec: h264, video_encoder: x264, release_group: ZKH, type: episode, parsing_time: 0.0343871116638, total_time: 0.0344219207764, absolute_episode: [], quality: 720p HDTV
2018-02-04 19:57:29 DEBUG    Thread-17 :: [bd08325] Found codec for The Flash (2014) Honey, I Shrunk Team Flash
2018-02-04 19:57:29 DEBUG    Thread-17 :: [bd08325] 279121: Formatting pattern: %SN S%0S -> The Flash (2014) S04
2018-02-04 19:57:29 DEBUG    Thread-17 :: [bd08325] Destination folder for this episode: /srv/media/series/Flash (2014)/The Flash (2014) S04
2018-02-04 19:57:29 DEBUG    Thread-17 :: [bd08325] Checking if the path /srv/media/series/Flash (2014)/The Flash (2014) S04 already exists
2018-02-04 19:57:29 DEBUG    Thread-17 :: [bd08325] No associated files were found for /srv/media/automatic/The.Flash.2014.S04E12.FASTSUB.VOSTFR.720p.HDTV.x264-ZKH.mkv
2018-02-04 19:57:29 DEBUG    Thread-17 :: [bd08325] Moving file from /srv/media/automatic/The.Flash.2014.S04E12.FASTSUB.VOSTFR.720p.HDTV.x264-ZKH.mkv to /srv/media/series/Flash (2014)/The Flash (2014) S04/The Flash (2014) S04E12.mkv 
2018-02-04 19:57:29 DEBUG    Thread-17 :: [bd08325] Not running as root or owner of /srv/media/series/Flash (2014)/The Flash (2014) S04/The Flash (2014) S04E12.mkv, not trying to set permissions
2018-02-04 19:57:29 DEBUG    Thread-17 :: [bd08325] 279121: Setter sets location to /srv/media/series/Flash (2014)/The Flash (2014) S04/The Flash (2014) S04E12.mkv
2018-02-04 19:57:29 DEBUG    Thread-17 :: [bd08325] Cached video information under key medusa.subtitles:video|/srv/media/series/Flash (2014)/The Flash (2014) S04/The Flash (2014) S04E12.mkv was invalidated
2018-02-04 19:57:29 ERROR    Thread-17 :: [bd08325] Exception generated: decoding Unicode is not supported
Traceback (most recent call last):
  File "/srv/medusa/medusa/server/web/core/base.py", line 280, in async_call
    result = function(**kwargs)
  File "/srv/medusa/medusa/server/web/home/post_process.py", line 49, in processEpisode
    ignore_subs=argToBool(ignore_subs)
  File "/srv/medusa/medusa/process_tv.py", line 146, in process
    ignore_subs=ignore_subs)
  File "/srv/medusa/medusa/process_tv.py", line 335, in process_files
    self.process_media(path, self.video_files, force, is_priority, ignore_subs)
  File "/srv/medusa/medusa/process_tv.py", line 536, in process_media
    self.result = processor.process()
  File "/srv/medusa/medusa/post_processor.py", line 1229, in process
    cur_ep.refresh_subtitles()
  File "/srv/medusa/medusa/tv/episode.py", line 422, in refresh_subtitles
    current_subtitles = subtitles.get_current_subtitles(self)
  File "/srv/medusa/medusa/subtitles.py", line 590, in get_current_subtitles
    video = get_video(tv_episode, video_path)
  File "/srv/medusa/medusa/subtitles.py", line 712, in get_video
    subtitles_dir = _encode(subtitles_dir or get_subtitles_dir(video_path))
  File "/srv/medusa/medusa/subtitles.py", line 760, in get_subtitles_dir
    return _decode(app.SUBTITLES_DIR)
  File "/srv/medusa/medusa/subtitles.py", line 638, in _decode
    return text_type(value, encoding)
TypeError: decoding Unicode is not supported
2018-02-04 19:57:34 DEBUG    Thread-22 :: [bd08325] Processing path: /srv/media/automatic
2018-02-04 19:57:34 DEBUG    Thread-22 :: [bd08325] Processing folder: /srv/media/automatic
2018-02-04 19:57:34 DEBUG    Thread-22 :: [bd08325] Post-processing files: [u'Supergirl.S03E12.FASTSUB.VOSTFR.720p.HDTV.x264-FDS.mkv', u'South.Park.S20E01.FRENCH.HDTV.x264-AMB3R.mkv', u'Supernatural.S13E11.VOSTFR.720p.WEB-DL.DD5.1.H264-ARK01.mkv', u'Supernatural.S13E12.VOSTFR.720p.HDTV.x264-ARK01.mkv', u'[DBS EN VF] DRAGON BALL SUPER EPISODE 126 VOSTFR V1 HD 720P.mp4', u'The.Flash.2014.S04E12.VOSTFR.WEB-DL.x264-ARK01.mp4', u'The.Magicians.US.S03E01.FASTSUB.VOSTFR.720p.HDTV.x264-FDS.mkv', u'The.Magicians.US.S03E03.FASTSUB.VOSTFR.720p.HDTV.x264-FDS.mkv', u'The.Flash.2014.S04E11.FASTSUB.VOSTFR.720p.HDTV.x264-ZKH.mkv', u'South.Park.S20E02.FRENCH.HDTV.x264-AMB3R.mkv', u'Lucifer.S03E12.SUBFRENCH.WEB-DL.H264-Yn1D.mkv', u'The.Magicians.US.S03E02.FASTSUB.VOSTFR.720p.HDTV.x264-FDS.mkv', u'Beyond.S02E02.FASTSUB.VOSTFR.720p.WEB.x264-WEEDS.mkv', u'south.park.s19e06.french.720p.hdtv.x264-sh0w.mkv', u'South.Park.S19E10.FiNAL.FRENCH.HDTV.x264-AMB3R.mkv', u'Blindspot.S03E11.VOSTFR.720p.WEB-DL.x264-FDS.mkv', u'South.Park.S19E07.FRENCH.HDTV.x264-AMB3R.mkv', u'Marvels.Agents.of.S.H.I.E.L.D.S05E10.SUBFRENCH.1080p.WEB-DL.H264-Yn1D.mkv', u'Lucifer.S03E12.FASTSUB.VOSTFR.720p.HDTV.x264-ZKH.mkv', u'South.Park.S20E04.FRENCH.720p.HDTV.x264-SH0W.mkv', u'Marvels.Agents.of.S.H.I.E.L.D.S05E09.SUBFRENCH.1080p.WEB-DL.H264-Yn1D.mkv', u'Supernatural.S13E11.FASTSUB.VOSTFR.720p.HDTV.x264-FDS.mkv']
2018-02-04 19:57:34 DEBUG    Thread-22 :: [bd08325] Post-processing video files: [u'Supergirl.S03E12.FASTSUB.VOSTFR.720p.HDTV.x264-FDS.mkv', u'South.Park.S20E01.FRENCH.HDTV.x264-AMB3R.mkv', u'Supernatural.S13E11.VOSTFR.720p.WEB-DL.DD5.1.H264-ARK01.mkv', u'Supernatural.S13E12.VOSTFR.720p.HDTV.x264-ARK01.mkv', u'[DBS EN VF] DRAGON BALL SUPER EPISODE 126 VOSTFR V1 HD 720P.mp4', u'The.Flash.2014.S04E12.VOSTFR.WEB-DL.x264-ARK01.mp4', u'The.Magicians.US.S03E01.FASTSUB.VOSTFR.720p.HDTV.x264-FDS.mkv', u'The.Magicians.US.S03E03.FASTSUB.VOSTFR.720p.HDTV.x264-FDS.mkv', u'The.Flash.2014.S04E11.FASTSUB.VOSTFR.720p.HDTV.x264-ZKH.mkv', u'South.Park.S20E02.FRENCH.HDTV.x264-AMB3R.mkv', u'Lucifer.S03E12.SUBFRENCH.WEB-DL.H264-Yn1D.mkv', u'The.Magicians.US.S03E02.FASTSUB.VOSTFR.720p.HDTV.x264-FDS.mkv', u'Beyond.S02E02.FASTSUB.VOSTFR.720p.WEB.x264-WEEDS.mkv', u'south.park.s19e06.french.720p.hdtv.x264-sh0w.mkv', u'South.Park.S19E10.FiNAL.FRENCH.HDTV.x264-AMB3R.mkv', u'Blindspot.S03E11.VOSTFR.720p.WEB-DL.x264-FDS.mkv', u'South.Park.S19E07.FRENCH.HDTV.x264-AMB3R.mkv', u'Marvels.Agents.of.S.H.I.E.L.D.S05E10.SUBFRENCH.1080p.WEB-DL.H264-Yn1D.mkv', u'Lucifer.S03E12.FASTSUB.VOSTFR.720p.HDTV.x264-ZKH.mkv', u'South.Park.S20E04.FRENCH.720p.HDTV.x264-SH0W.mkv', u'Marvels.Agents.of.S.H.I.E.L.D.S05E09.SUBFRENCH.1080p.WEB-DL.H264-Yn1D.mkv', u'Supernatural.S13E11.FASTSUB.VOSTFR.720p.HDTV.x264-FDS.mkv']
2018-02-04 19:57:34 INFO     Thread-22 :: [bd08325] Processing /srv/media/automatic/Supergirl.S03E12.FASTSUB.VOSTFR.720p.HDTV.x264-FDS.mkv
2018-02-04 19:57:34 DEBUG    Thread-22 :: [bd08325] Analyzing name: Supergirl.S03E12.FASTSUB.VOSTFR.720p.HDTV.x264-FDS.mkv
2018-02-04 19:57:34 DEBUG    Thread-22 :: [bd08325] Matched release Supergirl.S03E12.FASTSUB.VOSTFR.720p.HDTV.x264-FDS.mkv to a series in your database: Supergirl
2018-02-04 19:57:34 DEBUG    Thread-22 :: [bd08325] Parsed Supergirl.S03E12.FASTSUB.VOSTFR.720p.HDTV.x264-FDS.mkv into title: Supergirl, season: 3, episode: [12], other: Fastsub, subtitle_language: fr, screen_size: 720p, format: HDTV, video_codec: h264, video_encoder: x264, release_group: FDS, container: mkv, mimetype: video/x-matroska, type: episode, parsing_time: 0.0294349193573, total_time: 0.0295779705048, absolute_episode: [], quality: 720p HDTV
2018-02-04 19:57:34 DEBUG    Thread-22 :: [bd08325] Show information parsed from file name
2018-02-04 19:57:34 DEBUG    Thread-22 :: [bd08325] Retrieving episode object for Supergirl S03E12
2018-02-04 19:57:34 DEBUG    Thread-22 :: [bd08325] Looking up XEM scene mapping for show ID 295759 on TVDBv2
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] GET URL: http://thexem.de/map/havemap?origin=tvdb [Status: 200]
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] User-Agent: Medusa/0.1.23 (Linux; 3.16.0-4-amd64; b49bc2e0-09dc-11e8-934e-2c27d73517ee)
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] No XEM data for show ID 295759 on TVDBv2
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] The episode file has a quality in it, using that: 720p HDTV
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] This episode was found in history as SNATCHED.
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Quality of the episode we are processing: 720p HDTV
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Snatch in history: True
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Manually snatched: False
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Info hash: 6efdd077d8aba48eceae87d90aed3b16aafcf980
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] NZB: False
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Current quality: 720p HDTV
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] New quality: 720p HDTV
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Proper: False
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] This episode is a priority download: True
2018-02-04 19:57:35 INFO     Thread-22 :: [bd08325] New file: /srv/media/automatic/Supergirl.S03E12.FASTSUB.VOSTFR.720p.HDTV.x264-FDS.mkv
2018-02-04 19:57:35 INFO     Thread-22 :: [bd08325] New size: 828.84 MB
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] There is no existing file so there's no worries about replacing it
2018-02-04 19:57:35 INFO     Thread-22 :: [bd08325] This download is marked a priority download so I'm going to replace an existing file if I find one
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Trying to determine free space on destination drive
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Found release name Supergirl.S03E12.FASTSUB.VOSTFR.720p.HDTV.x264-FDS
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Folder name (automatic) appears to be a valid release name. Using it.
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Matched release Supergirl.S03E12.FASTSUB.VOSTFR.720p.HDTV.x264-FDS to a series in your database: Supergirl
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Parsed Supergirl.S03E12.FASTSUB.VOSTFR.720p.HDTV.x264-FDS into title: Supergirl, season: 3, episode: [12], other: Fastsub, subtitle_language: fr, screen_size: 720p, format: HDTV, video_codec: h264, video_encoder: x264, release_group: FDS, type: episode, parsing_time: 0.0255208015442, total_time: 0.0255551338196, absolute_episode: [], quality: 720p HDTV
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Found codec for Supergirl 
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] 295759: Formatting pattern: %SN S%0SE%0E -> Supergirl S03E12
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Matched release Supergirl.S03E12.FASTSUB.VOSTFR.720p.HDTV.x264-FDS to a series in your database: Supergirl
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Parsed Supergirl.S03E12.FASTSUB.VOSTFR.720p.HDTV.x264-FDS into title: Supergirl, season: 3, episode: [12], other: Fastsub, subtitle_language: fr, screen_size: 720p, format: HDTV, video_codec: h264, video_encoder: x264, release_group: FDS, type: episode, parsing_time: 0.0256450176239, total_time: 0.025680065155, absolute_episode: [], quality: 720p HDTV
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Found codec for Supergirl 
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] 295759: Formatting pattern: %SN S%0S -> Supergirl S03
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Destination folder for this episode: /srv/media/series/Supergirl/Supergirl S03
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Checking if the path /srv/media/series/Supergirl/Supergirl S03 already exists
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] No associated files were found for /srv/media/automatic/Supergirl.S03E12.FASTSUB.VOSTFR.720p.HDTV.x264-FDS.mkv
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Moving file from /srv/media/automatic/Supergirl.S03E12.FASTSUB.VOSTFR.720p.HDTV.x264-FDS.mkv to /srv/media/series/Supergirl/Supergirl S03/Supergirl S03E12.mkv 
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Not running as root or owner of /srv/media/series/Supergirl/Supergirl S03/Supergirl S03E12.mkv, not trying to set permissions
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] 295759: Setter sets location to /srv/media/series/Supergirl/Supergirl S03/Supergirl S03E12.mkv
2018-02-04 19:57:35 DEBUG    Thread-22 :: [bd08325] Cached video information under key medusa.subtitles:video|/srv/media/series/Supergirl/Supergirl S03/Supergirl S03E12.mkv was invalidated
2018-02-04 19:57:35 ERROR    Thread-22 :: [bd08325] Exception generated: decoding Unicode is not supported
Traceback (most recent call last):
  File "/srv/medusa/medusa/server/web/core/base.py", line 280, in async_call
    result = function(**kwargs)
  File "/srv/medusa/medusa/server/web/home/post_process.py", line 49, in processEpisode
    ignore_subs=argToBool(ignore_subs)
  File "/srv/medusa/medusa/process_tv.py", line 146, in process
    ignore_subs=ignore_subs)
  File "/srv/medusa/medusa/process_tv.py", line 335, in process_files
    self.process_media(path, self.video_files, force, is_priority, ignore_subs)
  File "/srv/medusa/medusa/process_tv.py", line 536, in process_media
    self.result = processor.process()
  File "/srv/medusa/medusa/post_processor.py", line 1229, in process
    cur_ep.refresh_subtitles()
  File "/srv/medusa/medusa/tv/episode.py", line 422, in refresh_subtitles
    current_subtitles = subtitles.get_current_subtitles(self)
  File "/srv/medusa/medusa/subtitles.py", line 590, in get_current_subtitles
    video = get_video(tv_episode, video_path)
  File "/srv/medusa/medusa/subtitles.py", line 712, in get_video
    subtitles_dir = _encode(subtitles_dir or get_subtitles_dir(video_path))
  File "/srv/medusa/medusa/subtitles.py", line 760, in get_subtitles_dir
    return _decode(app.SUBTITLES_DIR)
  File "/srv/medusa/medusa/subtitles.py", line 638, in _decode
    return text_type(value, encoding)
TypeError: decoding Unicode is not supported

# Fixed code
def _encode(value, fallback=None):
    """Encode the value using the specified encoding.

    It fallbacks to the specified encoding or SYS_ENCODING if not defined

    :param value: the value to be encoded
    :type value: str
    :param encoding: the encoding to be used
    :type encoding: str
    :param fallback: the fallback encoding to be used
    :type fallback: str
    :return: the encoded value
    :rtype: str
    """
    if isinstance(value, binary_type):
        return value

    encoding = "utf-8" if os.name != "nt" else app.SYS_ENCODING

    try:
        return value.encode(encoding)
    except UnicodeEncodeError:
        logger.debug(
            "Failed to encode to %s, falling back to %s: %r",
            encoding,
            fallback or app.SYS_ENCODING,
            value,
        )
        return value.encode(fallback or app.SYS_ENCODING)