Python 3 类型错误:期望bytes或整数地址而非str实例
我正在尝试让Python 2的代码在Python 3上运行,而这一行
argv = (c_char_p * len(args))(*args)
导致了这个错误
File "/Users/hanxue/Code/Python/gsfs/src/gsfs.py", line 381, in main
fuse = FUSE(GoogleStorageFUSE(username, password, logfile=logfile), mount_point, **fuse_args)
File "/Users/hanxue/Code/Python/gsfs/src/fuse.py", line 205, in __init__
argv = (c_char_p * len(args))(*args)
TypeError: bytes or integer address expected instead of str instance
这是整个方法的内容
class FUSE(object):
"""This class is the lower level interface and should not be subclassed
under normal use. Its methods are called by fuse"""
def __init__(self, operations, mountpoint, raw_fi=False, **kwargs):
"""Setting raw_fi to True will cause FUSE to pass the fuse_file_info
class as is to Operations, instead of just the fh field.
This gives you access to direct_io, keep_cache, etc."""
self.operations = operations
self.raw_fi = raw_fi
args = ['fuse']
if kwargs.pop('foreground', False):
args.append('-f')
if kwargs.pop('debug', False):
args.append('-d')
if kwargs.pop('nothreads', False):
args.append('-s')
kwargs.setdefault('fsname', operations.__class__.__name__)
args.append('-o')
args.append(','.join(key if val == True else '%s=%s' % (key, val)
for key, val in kwargs.items()))
args.append(mountpoint)
argv = (c_char_p * len(args))(*args)
这个方法是通过这一行调用的
fuse = FUSE(GoogleStorageFUSE(username, password, logfile=logfile), mount_point, **fuse_args)
我该如何通过将参数改成 byte[]
来避免这个错误呢?
1 个回答
1
在Python 3中,所有的字符串默认都是unicode格式的。也就是说,像 'fuse'
、'-f'
、'-d'
这些字符串,实际上都是 str
类型的实例。如果你想要得到 bytes
类型的实例,你需要做两件事:
- 把字节类型的数据传入FUSE,比如
username
、password
、logfile
、mount_point
,还有fuse_args
中的每一个参数。 - 把FUSE里面所有的字符串都改成字节类型,比如
b'fuse'
、b'-f'
、b'-d'
等等。
这可不是一件简单的事情。