我有一些我在Python中编写的代码,它应该带有一个带有ANSII标头的压缩文件,该文件的扩展名是用大写字母编写的。但是,当我剥离尝试剥离内容时,我可以将它传递给zlib进行解压缩以便导入文件,因为执行它的代码是在导入钩子中。
然而,它一直在钩子上失败了这个回溯:
Traceback (most recent call last):
File "path/to/make_zip.py", line 11, in <module>
from pathlib import Path
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 646, in _load_unlocked
File "<frozen importlib._bootstrap>", line 616, in _load_backward_compatible
File "path/to/py2pycx/_pycx_hook.py", line 96, in load_module
filedata = f[len(b'PYCX'):].strip()
TypeError: '_io.BufferedReader' object is not subscriptable
最后我正在使用的代码:有这个问题:
# other class stuff
def load_module(self, fullname):
self.premodule = super().load_module(fullname)
for ext in self.extensions:
if ext == '.pycx':
if self.premodule is None:
try:
f = open(self.path, 'rb')
filedata = f[len(b'PYCX'):].strip() # traceback happens here.
try:
decczfiledata = zlib.decompress(filedata)
decoded_data = base64.b64decode(decczfiledata)
except zlib.error as ex:
raise ImportError(
('Could not import {0} because of exception:\n{1}.'
).format(fullname, str(ex)))
self.module = decoded_data.decode("utf-8")
sys.modules[fullname] = self.module
f.close()
return self.module
except FileNotFoundError:
raise ImportError('Could not import {0}.'.format(fullname))