From a25b46f14eae63a90d79da211051eb4f77e620e3 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Mon, 26 Mar 2018 10:02:45 +0200 Subject: [PATCH] Python 3.4+ compatibility (while dropping Python < 2.6). --- .gitignore | 4 ++++ Texml/handler.py | 10 ++++------ Texml/processor.py | 2 +- Texml/texmlwr.py | 17 +++++++++++------ scripts/texml.py | 6 +++--- setup.py | 18 ------------------ tox.ini | 10 ++++++++++ 7 files changed, 33 insertions(+), 34 deletions(-) create mode 100644 .gitignore create mode 100644 tox.ini diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d94120e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.pyc +/.tox +/tests/tmp +MANIFEST diff --git a/Texml/handler.py b/Texml/handler.py index 32a6118..f6b3031 100644 --- a/Texml/handler.py +++ b/Texml/handler.py @@ -4,11 +4,9 @@ import xml.sax.handler from xml.sax.handler import feature_namespaces -import texmlwr -import specmap -import StringIO +from Texml import texmlwr +from Texml import specmap import string -import os, sys # Unbreakable spaces should not be deleted by strip(), but it happens: # http://uucode.com/blog/2010/06/01/python-wtf-strip-eats-too-much/ @@ -256,11 +254,11 @@ class Handler: else: msg += '%s not expected' % (local_name) - raise InvalidXmlException, msg + raise InvalidXmlException(msg) def invalid_xml_other(self, msg): # for other types of invalid XML - raise InvalidXmlException, msg + raise InvalidXmlException(msg) # ------------------------------------------------------------------- diff --git a/Texml/processor.py b/Texml/processor.py index 4f746d5..ac9415c 100644 --- a/Texml/processor.py +++ b/Texml/processor.py @@ -93,7 +93,7 @@ try: encoding = encoding, always_ascii = always_ascii, use_context = use_context) -except Exception, msg: +except Exception as msg: print sys.stderr, 'texml: %s' % str(msg) # Clean up resources diff --git a/Texml/texmlwr.py b/Texml/texmlwr.py index 631517a..0cfa8cc 100644 --- a/Texml/texmlwr.py +++ b/Texml/texmlwr.py @@ -11,13 +11,18 @@ ASIS = 3 PDF = 4 WEAK_WS_IS_NEWLINE = 2 -import unimap -import specmap +from Texml import unimap +from Texml import specmap import codecs import os import sys import string +if sys.version_info[0] >= 3: + byteord = lambda c: c +else: + byteord = ord + # # Writer&Co class # @@ -69,7 +74,7 @@ class texmlwr: if always_ascii: encoding = 'ascii' self.stream = stream_encoder(stream, encoding) - except Exception, e: + except Exception as e: raise ValueError("Can't create encoder: '%s'" % e) # Continue initialization self.after_char0d = 1 @@ -274,9 +279,9 @@ class texmlwr: try: bytes = ch.encode(self.encoding) for by in bytes: - self.write('^^%02x' % ord(by), 0) + self.write('^^%02x' % byteord(by), 0) return - except Exception, e: + except Exception as e: pass # # Symbol have to be rewritten. Let start with math mode. @@ -336,7 +341,7 @@ class texmlwr: """ Write char in Acrobat utf16be encoding """ bytes = ch.encode('utf_16_be') for by in bytes: - self.write('\\%03o' % ord(by), 0) + self.write('\\%03o' % byteord(by), 0) # # Wrapper over output stream to write is desired encoding diff --git a/scripts/texml.py b/scripts/texml.py index 40dd6b6..6822dd8 100755 --- a/scripts/texml.py +++ b/scripts/texml.py @@ -24,7 +24,7 @@ use_namespace = 1 import getopt try: opts, args = getopt.getopt(sys.argv[1:], 'hcaw:e:', ['help', 'context', 'ascii', 'width=', 'encoding=', ]) -except getopt.GetoptError, e: +except getopt.GetoptError as e: print >>sys.stderr, 'texml: Can\'t parse command line: %s' % e print >>sys.stderr, usage sys.exit(2) @@ -71,7 +71,7 @@ if '-' == infile: if '-' == outfile: f = sys.stdout else: - f = file(outfile, 'wb') + f = open(outfile, 'wb') # # An error handler @@ -88,7 +88,7 @@ import Texml.processor try: Texml.processor.process(infile, f, encoding, width, always_ascii, use_context) -except Exception, msg: +except Exception as msg: msg = 'texml: %s\n' % (str(msg)) quit(msg) diff --git a/setup.py b/setup.py index a5eeacf..2a17358 100644 --- a/setup.py +++ b/setup.py @@ -4,22 +4,6 @@ import os.path import re import glob -def test_for_sax(): - try: - import xml.sax - except ImportError: - sys.stderr.write('Please install the python pyxml modules\n') - sys.stdout.write('You must have this module before you can install texml\n') - sys.exit(1) - -def get_dtd_location(): - """ - For now, this is not used - - """ - return - return '/home/paul/Documents/data/dtds/' - def get_version(): # Take the version from "scripts/texml.py" @@ -36,8 +20,6 @@ def get_version(): raise "Can't find version" return version -if 'build' in sys.argv: - test_for_sax() version = get_version() diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..306a88d --- /dev/null +++ b/tox.ini @@ -0,0 +1,10 @@ +[tox] +envlist = + py{26,27,34,35,36,37}, + +[testenv] +changedir = tests +commands = + make clean + make +whitelist_externals=make -- 2.20.1