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 1f255704f7.

* 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
This commit is contained in:
josh 2020-06-10 20:57:46 +00:00 committed by GitHub
parent 16b462880b
commit 0a36eac686
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 37 deletions

View File

@ -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

View File

@ -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') +