chiark / gitweb /
fshash.in: Add Python 3 versions of the compatibility veneers.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 1 Jun 2024 03:28:54 +0000 (04:28 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 9 Jun 2024 11:10:59 +0000 (12:10 +0100)
This completes the port to Python 3.

fshash.in

index 75d004f777b9a79fe54856b70cb3442927c0ebf1..f19a81e05e8205a6a9b3148f471bf753a78ce279 100644 (file)
--- a/fshash.in
+++ b/fshash.in
@@ -41,11 +41,23 @@ VERSION = '@VERSION@'
 ###--------------------------------------------------------------------------
 ### Utilities.
 
-from cStringIO import StringIO; BytesIO = StringIO
-def bin(x): return x
-def text(x): return x
-def bytechr(x): return chr(x)
-def byteord(x): return ord(x)
+import sys as _SYS
+_PYVER = _SYS.version_info
+if _PYVER >= (3,):
+  _FSENC = _SYS.getfilesystemencoding()
+  if _PYVER >= (3, 1): _FSENCERR = "surrogateescape"
+  else: _FSENCERR = "strict"
+  from io import BytesIO, StringIO
+  def bin(x): return x.encode(_FSENC, _FSENCERR)
+  def text(x): return x.decode(_FSENC, _FSENCERR)
+  def bytechr(x): return bytes([x])
+  def byteord(x): return x
+else:
+  from cStringIO import StringIO; BytesIO = StringIO
+  def bin(x): return x
+  def text(x): return x
+  def bytechr(x): return chr(x)
+  def byteord(x): return ord(x)
 def excval(): return exc_info()[1]
 
 QUIS = OS.path.basename(argv[0])