本文介绍了Python 3 TypeError:预期为字节或整数地址,而不是str实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试让Python 2代码在Python 3上运行,并且此行
I am trying to get Python 2 code to run on Python 3, and this line
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)
怎么办我通过将args更改为 byte []
?
How do I avoid the error by changing the args into byte[]
?
推荐答案
在Python 3中,默认情况下,所有字符串文字都是unicode。因此,短语'fuse'
,'-f'
,'-d'
等都创建 str
实例。为了获得个字节
实例,您将需要同时执行以下操作:
In Python 3 all string literals are, by default, unicode. So the phrases 'fuse'
, '-f'
, '-d'
, etc, all create str
instances. In order to get bytes
instances instead you will need to do both:
- pass字节进入FUSE(
用户名
,密码
,日志文件
,mount_point
,并且fuse_args
中的每个arg - 更改其中的所有字符串文字FUSE本身为字节:
b'fuse'
,b'-f'
,b '-d'
等。
- pass bytes into the FUSE (
username
,password
,logfile
,mount_point
, and each arg infuse_args
- change all the string literals in FUSE itself to be bytes:
b'fuse'
,b'-f'
,b'-d'
, etc.
这不是一件小事。
这篇关于Python 3 TypeError:预期为字节或整数地址,而不是str实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!