pynfs: Avoid changing sys.path in generated code
Subsystems (2)
PATCH 1/1 pynfs: Avoid changing sys.path in generated code
Previously importing the rpc module had side effects on sys.path which is not preferable when using the it in larger projects. This has now been fixed by removing the code modifying sys.path and using relative over absolute imports in the impacted files.
---
rpc/rpc.py | 10 +++++-----
rpc/rpclib.py | 4 ++--
rpc/security.py | 16 ++++++++--------
xdr/xdrgen.py | 7 +++----
4 files changed, 18 insertions(+), 19 deletions(-)
rpc/rpc.py +0 -0
@@ -8,12 +8,12 @@ import logging
from collections import deque as Deque
from errno import EINPROGRESS, EWOULDBLOCK
-import rpc.rpc_pack as rpc_pack
-from rpc_const import *
-from rpc_type import *
+from . import rpc_pack
+from .rpc_const import *
+from .rpc_type import *
-import security
-import rpclib
+from . import security
+from . import rpclib
import random
log_p = logging.getLogger("rpc.poll") # polling loop thread
rpc/rpclib.py +2 -2
@@ -1,5 +1,5 @@ -from rpc_const import * -from rpc_type import * +from .rpc_const import * +from .rpc_type import *
import logging
log = logging.getLogger("rpc.lib")
rpc/security.py +1 -1
@@ -1,13 +1,13 @@ -from rpc_const import AUTH_NONE, AUTH_SYS, RPCSEC_GSS, SUCCESS, CALL, \ +from .rpc_const import AUTH_NONE, AUTH_SYS, RPCSEC_GSS, SUCCESS, CALL, \
MSG_ACCEPTED
-from rpc_type import opaque_auth, authsys_parms
-from rpc_pack import RPCPacker, RPCUnpacker
-from gss_pack import GSSPacker, GSSUnpacker
+from .rpc_type import opaque_auth, authsys_parms
+from .rpc_pack import RPCPacker, RPCUnpacker
+from .gss_pack import GSSPacker, GSSUnpacker
from xdrlib import Packer, Unpacker
-import rpclib
-from gss_const import *
-import gss_type
-from gss_type import rpc_gss_init_res
+from . import rpclib
+from .gss_const import *
+from . import gss_type
+from .gss_type import rpc_gss_init_res
try:
import gssapi
except ImportError:
xdr/xdrgen.py +0 -0
@@ -1355,9 +1355,8 @@ allow_attr_passthrough = True # Option which allows substructure attrs to
# is a unique substructure to search. pack_header = """\ import sys,os -sys.path.append(os.path.dirname(__file__)) -import %s as const -import %s as types +from . import %s as const +from . import %s as types import xdrlib from xdrlib import Error as XDRError
@@ -1440,7 +1439,7 @@ def run(infile, filters=True, pass_attrs=True, debug=False):
const_fd.write(comment_string)
type_fd = open(types_file + ".py", "w")
type_fd.write(comment_string)
- type_fd.write("import sys,os\nsys.path.append(os.path.dirname(__file__))\nimport %s as const\n" % constants_file) + type_fd.write("import sys,os\nfrom . import %s as const\n" % constants_file)
pack_fd = open(packer_file + ".py", "w")
pack_fd.write(comment_string)
pack_fd.write(pack_header % (constants_file, types_file))
Signature · 1 line
2.31.1