Skip to content

Commit

Permalink
Repair pager for Python2.6
Browse files Browse the repository at this point in the history
The pager used with `stg show`, `stg patches`, and `stg diff` comes from
the pydoc module in the Python standard library.

For Python 2.6, the pager attempts to encode the unicode text as ascii and
thus any non-ascii text causes the pager to crash.

To repair, we adopt the strategy used in Python 2.7: encode the text to be
paged using the locale's preferred encoding and 'xmlcharrefreplace' error
handling.

Repairs #19.

Signed-off-by: Peter Grayson <jpgrayson@gmail.com>
  • Loading branch information
jpgrayson committed Feb 6, 2018
1 parent 8fa67da commit df9fb86
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion stgit/commands/diff.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from pydoc import pager

from stgit import argparse, git
from stgit.argparse import opt
from stgit.commands.common import (DirectoryHasRepository,
color_diff_flags,
git_id)
from stgit.compat import pager
from stgit.lib import git as gitlib
from stgit.out import out

Expand Down
2 changes: 1 addition & 1 deletion stgit/commands/patches.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from pydoc import pager

from stgit import argparse, git
from stgit.argparse import opt
from stgit.commands.common import CmdException, DirectoryHasRepository
from stgit.compat import pager
from stgit.out import out

__copyright__ = """
Expand Down
3 changes: 1 addition & 2 deletions stgit/commands/show.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from pydoc import pager

from stgit import argparse, git
from stgit.argparse import opt
from stgit.commands.common import (DirectoryHasRepository,
color_diff_flags,
git_id,
parse_patches)
from stgit.compat import pager
from stgit.lib import git as gitlib

__copyright__ = """
Expand Down
11 changes: 11 additions & 0 deletions stgit/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
unicode_literals)
import email
import os
import pydoc
import sys

# With unicode_literals enabled, the type of a string literal will be `unicode`
Expand Down Expand Up @@ -135,3 +136,13 @@ def decode_utf8_with_latin1(input, errors='strict'):
else:
break
return s


if sys.version_info[0:2] <= (2, 6):
def pager(text):
import locale
encoding = locale.getpreferredencoding()
b = text.encode(encoding, 'xmlcharrefreplace')
return pydoc.pager(b)
else:
pager = pydoc.pager
3 changes: 1 addition & 2 deletions stgit/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import traceback

from stgit import argparse, run, utils
from stgit.compat import environ_get, fsdecode_utf8
from stgit.compat import environ_get, fsdecode_utf8, pager
from stgit.config import config
from stgit.out import out
import stgit.commands
Expand Down Expand Up @@ -136,7 +136,6 @@ def _main():
parser = argparse.make_option_parser(command)
if is_cmd_alias(command):
parser.remove_option('-h')
from pydoc import pager
pager(parser.format_help())
else:
print_help()
Expand Down

0 comments on commit df9fb86

Please sign in to comment.