Skip to main content
 首页 » 编程设计

python之奇怪的代码缩进问题

2025年05月04日29xiaohuochai

我可能遗漏了一些明显的东西,但我已经尝试解决这个问题大约一个小时了,但没有成功。当找到解决方案时,可能会感到非常愚蠢。这是我遇到的错误:

  File "xpc_connection.py", line 77 
    def remoteObjectProxy(): 
    ^ 
IndentationError: unexpected indent 

如果我删除那部分代码,我会得到下一行的缩进错误。我的缩进出现了一些非常奇怪的问题......

这是我的代码(是的,我知道它相当不完整,可能有一些问题):

from collections import namedtuple; 
import socket; 
import sys; 
from recvTimeout import recv_timeout 
from recvTimeout import recv_end 
from recvTimeout import sendAllWithEnd 
 
 
# Named tuple that defines struct-like structure. 
# field1 - body length, field2 - headerChecksum, field3 - checksum 
XPCMessageHeader = namedtuple("XPCMessageHeader", "field1 field2 field3"); 
 
 
class XPCConnection(object): 
    """An class that represents a connection made between processes using XPC. 
 
    Attributes: 
 
    """ 
 
    def __init__(self, serviceName): 
        self._serviceName = serviceName; 
        self._exportedObject = None;            # This process's "representation" of itself. 
        self._remoteObjectProxy = None;         # This process's "representation" of the remote process. 
        self._exportedObjectInterface = None;   # Methods allowed to be received by exported object on this connection. 
        self._remoteObjectInterface = None;     # Methods allowed to be received by object that has been "imported" 
                                                # to this connection. 
        self._connectionSocket = None           # Domain socket that is endpoint of connection between processes.  
 
 
    def connect(): 
        # Create a UDS socket 
        _connectionSocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) 
 
        # Change this to port where remote process is listening. 
 
        print >>sys.stderr, 'connecting to %s' % _serviceName 
 
        try: 
            _connectionSocket.connect(_serviceName) 
        except socket.error, msg: 
            print >>sys.stderr, msg 
            sys.exit(1) 
 
        print >>sys.stderr, 'Attempting to connect.' 
 
        try: 
            # Send data 
            sendAllWithEnd(_connectionSocket,'Connected Successfully.') 
 
            data = recv_end(_connectionSocket); 
 
            print >>sys.stderr, 'received "%s"' % data; 
 
        except socket.error: 
            print >>sys.stderr, 'Connection Failed.'; 
 
    def disconnect(): 
        print >>sys.stderr, 'closing socket'; 
        _connectionSocket.close(); 
 
    def serviceName(): 
        return serviceName; 
 
#TODO 
 
''' 
    def readMessage(): 
 
 
    def readMessage(): 
 
 
    def resume(): 
 
    def invalidate(): 
                        ''' 
 
    def remoteObjectProxy(): 
        return _remoteObjectProxy; 
 
    @classmethod 
    def setRemoteObjectProxy(cls): 
        _remoteObjectProxy = CreateWithConnection(cls);     # Create a remoteObjectProxy object with connection 
                                                            # field that is this connection object. 
 
    def exportedObject(): 
        return exportedObject; 
 
    def setExportedObject(exportedObject): 
        _exportedObject = exportedObject; 
 
''' 
    # PRIVATE Helper Methods 
 
    # invocation of type XPCInvocation, completionHandler of type XPCInvocationCompletionHandler 
    # return type void 
    def _invokeOnRemoteObject(invocation, completionHandler): 
 
 
    # invocation of type XPCInvocation 
    # return type XPCValue 
    def _invokeOnExportedObject(invocation): 
 
 
    # May be unnecessary for now. 
    # fd of type int (represents a file descriptor) 
    # return type bool (indicates success?) 
    def _setFileDescriptor(int fd): 
 
 
    # data of type DataRef 
    # return type void 
    def _write(data): 
 
    # Probably not necessary 
    # estimatedBytesToRead of type int 
    # return type void 
    def _readDataOnReadQueue(estimatedBytesToRead): 
 
    # Likely necessary 
    # bytes of type string, length of type int 
    # return type void 
    def _processDataOnReadQueue(bytes, length): 
 
    # Likely unecessary 
    # data of type DataRef 
    # return type void 
    def _writeDataOnWriteQueue(data): 
 
    # error of type ErrorRef 
    # return type void 
    def _terminateWithErrorSync(error): 
 
    # error of type Error Ref 
    # return type void 
    def _terminateWithErrorOnUserQueue(error): 
 
    # return type bool. Returns true if connected, false otherwise 
    def _isConnected(): 
 
 
    # delayInSecond of type int. Not sure if you can pass in an argument like this. 
    # return type void 
    def _connectWithExponentialBackoff(delayInSeconds = 0.0625): 
 
    # return type bool. Returns true iff connected successfully 
    def _attemptToConnect(): 
 
 
 
    # Likely unecessary 
    # return type void 
    def _flushRequestQueue(): 
 
    # closes connection 
    # return type void 
    def _disconnect(): 
                            ''' 
 
 
 
#TODO: Invocation handler equivalent. 
 
# "This" process's representation of the other process. 
class XPCRemoteObjectProxy(object): 
 
    def __init__(self, connection): 
 
        # Reference to the connection Remote Object is a part of. Necessary so that when 
        # you invoke a method and get stuff back, reomte object proxy knows where to send stuff back to. 
        self._connection = connection; 
 
    def invoke(invocation): 
        _connection.invokeOneRemoteObject(invocation);      # TODO: invokeOneRemoteObject 
 
 
# Used to represent "this" process. 
class XPCExportedObject(object): 
 
    def __init__(self): 
        self._invocationHandlersByMethodSignature = {}; # Invocation handlers stored in dictionary. Keyed by method signatures. 
 
    # invocation is XPCInfocation object, returnValue XPCValue object 
    def invoke(invocation, returnValue): 
 
        try: 
            # We directly modify returnValue here. Unsure if this acutally works. 
            returnValue = _invocationHandlersByMethodSignature[methodSignature()]; 
            return True; 
 
        except KeyError: 
            return False; 
 
    # Handler is of type XPCInvocationHandler and methodName is of type string. 
    # Come back to this 
    def registerInvocationHandlerForMethodSignature(handler, methodName): 
        return True 
 
 
# Used to call a method across an XPC connection. 
class XPCInvocation(object): 
 
    def __init__(self): 
        self._methodSignature = "";     # Signature of method to be called. 
        self._arguments = [];           # List of arguments for the called method. Elements are XPCValue objects. 
 
 
    # TODO: This is definitely incorrect. 
    # Make this a classmethod? 
    def createFromSerializedRepresentation(serializedRepresentation): 
        invocation = self.__class__(); 
        invocation.setMethodSignature(serializedRepresentation.methodsignature()); 
 
        for serializedValue in serializedRepresentation.values(): 
            invocation._arguments.append(FromSerializedRepresentation(serializedValue)); 
            # Note: FromSerializedRepresentation function defined in XPCValue class. 
            # TODO: XPCValue Class 
 
        return invocation; 
 
 
    def getMethodSignature(): 
        return _methodSignature; 
 
    def setMethodSignature(methodSignature): 
        _methodSignature = methodSignature; 
 
    def getArguments(): 
        return _arguments 
 
    # Takes in an XPCValue as an argument. 
    def appendArgument(value): 
        _arguments.append(value); 
 
 
    # TODO: This is definitely incorrect. 
    # NOTE: XPCInvocationMessage has yet to be written. It is provided by protobuf. 
    def serializedRepresentation(): 
        message = XPCInvocationMessage(); 
        message.set_methodsignature(_methodSignature); 
 
        for value in _arguments: 
            message.add_values().CopyFrom(value.serializedRepresentation()); 
 
        return message 

请您参考如下方法:

您正在使用多行字符串来替代多行注释。然而,它们不是注释,当它们像那样完全缩进时,它会终止类作用域,您将无法返回它。

快速修复,缩进开头的 ''' 以匹配类范围。