2014-04-04 192 views
1

我試圖讓Python 2的代碼在Python 3中運行,該行的Python 3類型錯誤:字節或整數地址預期,而不是STR實例

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[]來避免該錯誤?

+0

也許這可能有所幫助:http://stackoverflow.com/questions/8480376/using-ctypes-methods-in-python-gives-unexpected-error – ThD

回答

1

在Python 3中,默認情況下,所有字符串文字都是unicode。所以短語'fuse''-f','-d'等等都會創建str實例。爲了得到bytes實例,而你需要都這樣做:

  • 通字節到FUSE(usernamepasswordlogfilemount_point,每精氨酸在fuse_args
  • 改變所有的字符串文字保險絲本身是字節:b'fuse'b'-f'b'-d'

這是一個不小的工作

相關問題