summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjosh <josh@jrl.ninja>2020-06-10 20:57:46 +0000
committerGitHub <noreply@github.com>2020-06-11 06:57:46 +1000
commit0a36eac6863fc6db6fb8d815325da8b122c82b08 (patch)
tree55d52b81043a68261cd078f1edb385bcdc4eb296
parent16b462880b1dd4d4cc542002f433f75de3a6b396 (diff)
ref: replace usage of deprecated imp (#449)
* Use types instead of imp.new_module. I can follow up with https://docs.python.org/3/library/importlib.html#importlib.util.module_from_spec if need be. * use source loader from importlib * Revert "use source loader from importlib" This reverts commit 1f255704f7bf618fb7e0432e1fccef6ee22364d4. * use inspect.getsource, but alas * placate linter * use find_spec to resolve a module spec to a file path * better function naming * remove outdated comment
-rw-r--r--sshuttle/assembler.py4
-rw-r--r--sshuttle/ssh.py42
2 files changed, 9 insertions, 37 deletions
diff --git a/sshuttle/assembler.py b/sshuttle/assembler.py
index 3ab5356..31900ee 100644
--- a/sshuttle/assembler.py
+++ b/sshuttle/assembler.py
@@ -1,6 +1,6 @@
import sys
import zlib
-import imp
+import types
verbosity = verbosity # noqa: F821 must be a previously defined global
z = zlib.decompressobj()
@@ -15,7 +15,7 @@ while 1:
% (name, nbytes))
content = z.decompress(sys.stdin.read(nbytes))
- module = imp.new_module(name)
+ module = types.ModuleType(name)
parents = name.rsplit(".", 1)
if len(parents) == 2:
parent, parent_name = parents
diff --git a/sshuttle/ssh.py b/sshuttle/ssh.py
index 6f6b380..8d9115d 100644
--- a/sshuttle/ssh.py
+++ b/sshuttle/ssh.py
@@ -3,7 +3,7 @@ import os
import re
import socket
import zlib
-import imp
+import importlib
import subprocess as ssubprocess
import shlex
from shlex import quote
@@ -14,43 +14,15 @@ import sshuttle.helpers as helpers
from sshuttle.helpers import debug2
-def readfile(name):
- tokens = name.split(".")
- f = None
-
- token = tokens[0]
- token_name = [token]
- token_str = ".".join(token_name)
-
- try:
- f, pathname, description = imp.find_module(token_str)
-
- for token in tokens[1:]:
- module = imp.load_module(token_str, f, pathname, description)
- if f is not None:
- f.close()
-
- token_name.append(token)
- token_str = ".".join(token_name)
-
- f, pathname, description = imp.find_module(
- token, module.__path__)
-
- if f is not None:
- contents = f.read()
- else:
- contents = ""
-
- finally:
- if f is not None:
- f.close()
-
- return contents.encode("UTF8")
+def get_module_source(name):
+ spec = importlib.util.find_spec(name)
+ with open(spec.origin, "rt") as f:
+ return f.read().encode("utf-8")
def empackage(z, name, data=None):
if not data:
- data = readfile(name)
+ data = get_module_source(name)
content = z.compress(data)
content += z.flush(zlib.Z_SYNC_FLUSH)
@@ -116,7 +88,7 @@ def connect(ssh_cmd, rhostport, python, stderr, options):
rhost = host
z = zlib.compressobj(1)
- content = readfile('sshuttle.assembler')
+ content = get_module_source('sshuttle.assembler')
optdata = ''.join("%s=%r\n" % (k, v) for (k, v) in list(options.items()))
optdata = optdata.encode("UTF8")
content2 = (empackage(z, 'sshuttle') +