Python 3 TypeError: bytes or integer address expected instead of str instance

匿名 (未验证) 提交于 2019-12-03 02:16:02

问题:

I am trying to get Python 2 code to run on Python 3, and this line

    argv = (c_char_p * len(args))(*args) 

causes this error

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 

This is the full method

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) 

Which is invoked by this line

fuse = FUSE(GoogleStorageFUSE(username, password, logfile=logfile), mount_point, **fuse_args) 

How do I avoid the error by changing the args into byte[]?

回答1:

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 bytes into the FUSE (username, password, logfile, mount_point, and each arg in fuse_args
  • change all the string literals in FUSE itself to be bytes: b'fuse', b'-f', b'-d', etc.

This is not a small job.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!