简体   繁体   中英

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

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[] ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM