Blame texlive-base-20200327-pygmentex-python3-0.10.patch

322b4e6
diff -up texlive-base-20200327/source/texk/texlive/linked_scripts/pygmentex/pygmentex.py.10 texlive-base-20200327/source/texk/texlive/linked_scripts/pygmentex/pygmentex.py
322b4e6
--- texlive-base-20200327/source/texk/texlive/linked_scripts/pygmentex/pygmentex.py.10	2014-08-20 17:53:18.000000000 -0400
322b4e6
+++ texlive-base-20200327/source/texk/texlive/linked_scripts/pygmentex/pygmentex.py	2021-03-18 14:34:36.879392389 -0400
322b4e6
@@ -1,4 +1,4 @@
322b4e6
-#! /usr/bin/env python2
322b4e6
+#! /usr/bin/env python3
322b4e6
 # -*- coding: utf-8 -*-
322b4e6
 
322b4e6
 """
322b4e6
@@ -8,11 +8,11 @@
322b4e6
     PygmenTeX is a converter that do syntax highlighting of snippets of
322b4e6
     source code extracted from a LaTeX file.
322b4e6
 
322b4e6
-    :copyright: Copyright 2014 by José Romildo Malaquias
322b4e6
+    :copyright: Copyright 2020 by José Romildo Malaquias
322b4e6
     :license: BSD, see LICENSE for details
322b4e6
 """
322b4e6
 
322b4e6
-__version__ = '0.8'
322b4e6
+__version__ = '0.10'
322b4e6
 __docformat__ = 'restructuredtext'
322b4e6
 
322b4e6
 import sys
322b4e6
@@ -27,6 +27,7 @@ from pygments.formatters.latex import La
322b4e6
 from pygments.util import get_bool_opt, get_int_opt
322b4e6
 from pygments.lexer import Lexer
322b4e6
 from pygments.token import Token
322b4e6
+from pygments.util import guess_decode
322b4e6
 
322b4e6
 ###################################################
322b4e6
 # The following code is in >=pygments-2.0
322b4e6
@@ -56,24 +57,24 @@ class EnhancedLatexFormatter(LatexFormat
322b4e6
             realoutfile = outfile
322b4e6
             outfile = StringIO()
322b4e6
 
322b4e6
-        outfile.write(u'\\begin{Verbatim}[commandchars=\\\\\\{\\}')
322b4e6
+        outfile.write(r'\begin{Verbatim}[commandchars=\\\{\}')
322b4e6
         if self.linenos:
322b4e6
             start, step = self.linenostart, self.linenostep
322b4e6
-            outfile.write(u',numbers=left' +
322b4e6
-                          (start and u',firstnumber=%d' % start or u'') +
322b4e6
-                          (step and u',stepnumber=%d' % step or u''))
322b4e6
+            outfile.write(',numbers=left' +
322b4e6
+                          (start and ',firstnumber=%d' % start or '') +
322b4e6
+                          (step and ',stepnumber=%d' % step or ''))
322b4e6
         if self.mathescape or self.texcomments or self.escapeinside:
322b4e6
-            outfile.write(u',codes={\\catcode`\\$=3\\catcode`\\^=7\\catcode`\\_=8}')
322b4e6
+            outfile.write(r',codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8}')
322b4e6
         if self.verboptions:
322b4e6
-            outfile.write(u',' + self.verboptions)
322b4e6
-        outfile.write(u']\n')
322b4e6
+            outfile.write(',' + self.verboptions)
322b4e6
+        outfile.write(']\n')
322b4e6
 
322b4e6
         for ttype, value in tokensource:
322b4e6
             if ttype in Token.Comment:
322b4e6
                 if self.texcomments:
322b4e6
                     # Try to guess comment starting lexeme and escape it ...
322b4e6
                     start = value[0:1]
322b4e6
-                    for i in xrange(1, len(value)):
322b4e6
+                    for i in range(1, len(value)):
322b4e6
                         if start[0] != value[i]:
322b4e6
                             break
322b4e6
                         start += value[i]
322b4e6
@@ -129,7 +130,7 @@ class EnhancedLatexFormatter(LatexFormat
322b4e6
             else:
322b4e6
                 outfile.write(value)
322b4e6
 
322b4e6
-        outfile.write(u'\\end{Verbatim}\n')
322b4e6
+        outfile.write('\\end{Verbatim}\n')
322b4e6
 
322b4e6
         if self.full:
322b4e6
             realoutfile.write(DOC_TEMPLATE %
322b4e6
@@ -232,7 +233,7 @@ DISPLAY_LINENOS_SNIPPET_TEMPLATE = r'''
322b4e6
 '''
322b4e6
 
322b4e6
 
322b4e6
-def pyg(outfile, n, opts, extra_opts, text, usedstyles, inline_delim = ''):
322b4e6
+def pyg(outfile, outencoding, n, opts, extra_opts, text, usedstyles, inline_delim = ''):
322b4e6
     try:
322b4e6
         lexer = get_lexer_by_name(opts['lang'])
322b4e6
     except ClassNotFound as err:
322b4e6
@@ -260,27 +261,8 @@ def pyg(outfile, n, opts, extra_opts, te
322b4e6
     if tabsize:
322b4e6
         lexer.tabsize = tabsize
322b4e6
 
322b4e6
-    encoding = opts['encoding']
322b4e6
-    if encoding == 'guess':
322b4e6
-        try:
322b4e6
-            import chardet
322b4e6
-        except ImportError:
322b4e6
-            try:
322b4e6
-                text = text.decode('utf-8')
322b4e6
-                if text.startswith(u'\ufeff'):
322b4e6
-                    text = text[len(u'\ufeff'):]
322b4e6
-                    encoding = 'utf-8'
322b4e6
-            except UnicodeDecodeError:
322b4e6
-                text = text.decode('latin1')
322b4e6
-                encoding = 'latin1'
322b4e6
-        else:
322b4e6
-            encoding = chardet.detect(text)['encoding']
322b4e6
-            text = text.decode(encoding)
322b4e6
-    else:
322b4e6
-        text = text.decode(encoding)
322b4e6
-
322b4e6
     lexer.encoding = ''
322b4e6
-    _fmter.encoding = encoding
322b4e6
+    # _fmter.encoding = outencoding
322b4e6
 
322b4e6
     stylename = opts['sty']
322b4e6
 
322b4e6
@@ -367,7 +349,7 @@ _re_input = re.compile(
322b4e6
     r'^<@@pygmented@input@(\d+)\n(.*)\n([\s\S]*?)\n>@@pygmented@input@\1$',
322b4e6
     re.MULTILINE)
322b4e6
 
322b4e6
-def convert(code, outfile):
322b4e6
+def convert(code, outfile, outencoding):
322b4e6
     """
322b4e6
     Convert ``code``
322b4e6
     """
322b4e6
@@ -393,6 +375,7 @@ def convert(code, outfile):
322b4e6
         m = _re_inline.match(code, pos)
322b4e6
         if m:
322b4e6
             pyg(outfile,
322b4e6
+                outencoding,
322b4e6
                 m.group(1),
322b4e6
                 parse_opts(opts.copy(), m.group(2)),
322b4e6
                 '',
322b4e6
@@ -405,6 +388,7 @@ def convert(code, outfile):
322b4e6
         m = _re_display.match(code, pos)
322b4e6
         if m:
322b4e6
             pyg(outfile,
322b4e6
+                outencoding,
322b4e6
                 m.group(1),
322b4e6
                 parse_opts(opts.copy(), m.group(2)),
322b4e6
                 '',
322b4e6
@@ -415,15 +399,16 @@ def convert(code, outfile):
322b4e6
 
322b4e6
         m = _re_input.match(code, pos)
322b4e6
         if m:
322b4e6
+            opts_new = parse_opts(opts, m.group(2))
322b4e6
             try:
322b4e6
-                filecontents = open(m.group(3), 'rb').read()
322b4e6
+                filecontents, inencoding = read_input(m.group(3), opts_new['encoding'])
322b4e6
             except Exception as err:
322b4e6
-                sys.stderr.write('Error: cannot read input file: ')
322b4e6
-                sys.stderr.write(str(err))
322b4e6
+                print('Error: cannot read input file: ', err, file=sys.stderr)
322b4e6
             else:
322b4e6
                 pyg(outfile,
322b4e6
+                    outencoding,
322b4e6
                     m.group(1),
322b4e6
-                    parse_opts(opts, m.group(2)),
322b4e6
+                    opts_new,
322b4e6
                     "",
322b4e6
                     filecontents,
322b4e6
                     usedstyles)
322b4e6
@@ -435,6 +420,16 @@ def convert(code, outfile):
322b4e6
 
322b4e6
     outfile.write(GENERIC_DEFINITIONS_2)
322b4e6
 
322b4e6
+def read_input(filename, encoding):
322b4e6
+    with open(filename, 'rb') as infp:
322b4e6
+        code = infp.read()
322b4e6
+
322b4e6
+    if not encoding or encoding == 'guess':
322b4e6
+        code, encoding = guess_decode(code)
322b4e6
+    else:
322b4e6
+        code = code.decode(encoding)
322b4e6
+
322b4e6
+    return code, encoding
322b4e6
 
322b4e6
 
322b4e6
 USAGE = """\
322b4e6
@@ -486,7 +481,7 @@ def main(args = sys.argv):
322b4e6
         return 0
322b4e6
 
322b4e6
     if opts.pop('-V', None) is not None:
322b4e6
-        print('PygmenTeX version %s, (c) 2010 by José Romildo.' % __version__)
322b4e6
+        print('PygmenTeX version %s, (c) 2020 by José Romildo.' % __version__)
322b4e6
         return 0
322b4e6
  
322b4e6
     if len(args) != 1:
322b4e6
@@ -494,10 +489,9 @@ def main(args = sys.argv):
322b4e6
         return 2
322b4e6
     infn = args[0]
322b4e6
     try:
322b4e6
-        code = open(infn, 'rb').read()
322b4e6
+        code, inencoding = read_input(infn, "guess")
322b4e6
     except Exception as err:
322b4e6
-        sys.stderr.write('Error: cannot read input file: ')
322b4e6
-        sys.stderr.write(str(err))
322b4e6
+        print('Error: cannot read input file: ', err, file=sys.stderr)
322b4e6
         return 1
322b4e6
 
322b4e6
     outfn = opts.pop('-o', None)
322b4e6
@@ -507,11 +501,10 @@ def main(args = sys.argv):
322b4e6
     try:
322b4e6
         outfile = open(outfn, 'w')
322b4e6
     except Exception as err:
322b4e6
-        sys.stderr.write('Error: cannot open output file: ')
322b4e6
-        sys.stderr.write(str(err))
322b4e6
+        print('Error: cannot open output file: ', err, file=sys.stderr)
322b4e6
         return 1
322b4e6
 
322b4e6
-    convert(code, outfile)
322b4e6
+    convert(code, outfile, inencoding)
322b4e6
 
322b4e6
     return 0
322b4e6