Package pycocoa :: Module runtime
[frames] | no frames]

Module runtime

Classes ObjCClass, ObjCInstance, ObjCMethod, ObjCSubclass, etc.


Version: 18.04.24

Classes
  ObjC_t
Base type to pretty-print ctypes c_void_p.
  ObjCBase
Base class for ObjC...
  ObjCBoundMethod
Python wrapper for an ObjC class or instance method, an IMP_t.
  ObjCBoundClassMethod
Only to distinguish bound class from bound instance methods.
  ObjCClass
Python wrapper for an ObjC class.
  ObjCInstance
Python wrapper for an ObjC instance.
  ObjCMethod
Represent an unbound ObjC class or instance method (really an IMP_t).
  ObjCClassMethod
Only to distinguish class methods from instance methods.
  ObjCSubclass
Python class to create an ObjC sub-class of an existing ObjC (super-)class.
Functions
 
add_ivar(clas, name, ctype)
Add an instance variable to an ObjC class,
 
add_method(clas, name_, method, encoding)
Add a method to an ObjC class.
 
add_protocol(clas, protocol)
Add a protocol to an ObjC class.
 
add_subclass(superclas, name, register=False)
Create a new sub-class of the given super-class.
 
isClass(obj)
Check whether an object is an ObjC clas.
 
isImmutable(obj, mutableClass, immutableClass, name='ns')
Check that an Obj object is an instance of the immutable class.
 
isInstanceOf(obj, *Classes, **name_missing)
Check whether an ObjC object is an instance of some ObjC class.
 
isMetaClass(obj)
Check whether an object is an ObjC metaclass.
 
register_subclass(subclas)
Register an ObjC sub-class.
 
send_message(receiver, name_, *args, **resargtypes)
Send message to an ObjC object.
 
send_super(receiver, name_, *args, **resargtypes)
Send message to the super-class of an ObjC object.
 
set_ivar(obj, name, value, ctype=None)
Set an instance variable of an ObjC object.
 
nsDeallocObserver(obj)
Create a de-allocation observer for an ObjC instance.
Variables
  libobjc = <CDLL '/usr/lib/libobjc.dylib', handle 10a509680 at ...
  OBJC_ASSOCIATION_COPY = 771
  OBJC_ASSOCIATION_COPY_NONATOMIC = 3
  OBJC_ASSOCIATION_RETAIN = 769
  OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1
Function Details

add_ivar(clas, name, ctype)

 

Add an instance variable to an ObjC class,

Parameters:
  • clas - Class to add the ivar to (ObjCClass/Subclass).
  • name - Name of the iver (str).
  • ctype - The ivar type code (ctypes or encoding).
Returns:
True if the ivar was added, False otherwise.
Raises:

Note: The ctype must be a ctypes type or a valid ObjC type encoding.

See Also: The _NSDeallocObserver below.

add_method(clas, name_, method, encoding)

 

Add a method to an ObjC class.

Parameters:
  • clas - Class to add the method to (ObjCClass/Subclass).
  • name_ - Selector name (str).
  • method - Decorated class or instance method (callable).
  • encoding - Method signature (encoding).
Returns:
The method (IMP_t) if added, None otherwise.
Raises:
  • TypeError - If method is not a Python callable.

add_protocol(clas, protocol)

 

Add a protocol to an ObjC class.

Parameters:
  • clas - Class to add the protocol to (ObjCClass/Subclass).
  • protocol - The protocol to add (string or Protocol_t instance).
Returns:
The protocol (Protocol_t) if added, None otherwise.

add_subclass(superclas, name, register=False)

 

Create a new sub-class of the given super-class.

Parameters:
  • superclas - The parent class (string or NS...).
  • name - The name of the sub-class (str).
  • register - Optionally, register the new sub-class (bool).
Returns:
The sub-class (Class_t) if added, None otherwise.

Note: After calling add_subclass, you MUST register the new sub-class with register_subclass, before using the new sub-class. New methods can be added after the sub-class has been registered, but any ivars must be added BEFORE the class is registrated.

isClass(obj)

 

Check whether an object is an ObjC clas.

Parameters:
  • obj - Object to check (Object or Class).
Returns:
True if the obj is a clas, False otherwise.

isImmutable(obj, mutableClass, immutableClass, name='ns')

 

Check that an Obj object is an instance of the immutable class.

Parameters:
  • obj - The instance to check (ObjCInstance).
  • mutableClass - The mutable ObjC classes (NSMutable...).
  • immutableClass - The immutable ObjC classes (NS...).
  • name - The name of the instance (str).
Returns:
True if obj is an immutableClass instance, False otherwise.
Raises:
  • TypeError - If obj is a mutableClass instance, provided keyword argument name='...' is given.

isInstanceOf(obj, *Classes, **name_missing)

 

Check whether an ObjC object is an instance of some ObjC class.

Parameters:
  • obj - The instance to check (ObjCInstance or c_void_p).
  • Classes - One or several ObjC classes (NS...).
  • name - The name of the instance (str).
Returns:
The matching Class from Classes, None otherwise.
Raises:
  • TypeError - If obj is not an ObjCInstance or c_void_p or if obj does not match any of the Classes and only if keyword name='...' is provided.

See Also: Function instanceof for checking Python instances.

isMetaClass(obj)

 

Check whether an object is an ObjC metaclass.

Parameters:
  • obj - Object to check (Object or Class).
Returns:
True if the obj is a metaclass, False otherwise.

register_subclass(subclas)

 

Register an ObjC sub-class.

Parameters:
  • subclas - Class to be registered (Class).

See Also: nsDeallocObserver below.

send_message(receiver, name_, *args, **resargtypes)

 

Send message to an ObjC object.

Parameters:
  • receiver - The recipient (Object).
  • name_ - Message selector (str).
  • args - Message arguments (all positional).
  • resargtypes - Optional, result and argument types (ctypes).
Returns:
Message result (restype).
Raises:
  • ArgumentError - Invalid receiver, name, args or resargtypes.
  • TypeError - Invalid receiver, name, args or resargtypes type.

Note: By default, the result and all arguments are c_void_p wrapped. Use keyword arguments restype=c_void_p and argtypes=[] to change the defaults. The restype defines the ctypes type for the returned result and argtypes is the list of ctypes types for the message arguments only (without the Id/self and SEL/cmd arguments).

send_super(receiver, name_, *args, **resargtypes)

 

Send message to the super-class of an ObjC object.

Parameters:
  • receiver - The recipient (Object).
  • name_ - Message selector (str).
  • args - Message arguments (all positional).
  • resargtypes - Optional, result and argument types (ctypes).
Returns:
Message result (restype).
Raises:
  • ArgumentError - Invalid receiver, name, args or resargtypes.
  • TypeError - Invalid receiver, name, args or resargtypes type.

Note: By default, the result and all arguments are c_void_p wrapped. Use keyword arguments restype=c_void_p and argtypes=[] to change the defaults. The restype defines the ctypes type for the returned result and argtypes is the list of ctypes types for the message arguments only (without the Id/self and SEL/cmd arguments).

set_ivar(obj, name, value, ctype=None)

 

Set an instance variable of an ObjC object.

Parameters:
  • obj - The instance (Object).
  • name - Name of the ivar (str).
  • value - New value for the ivar (any).
  • ctype - Optional, the ivar type (ctypes).
Returns:
The ivar (Ivar_t).
Raises:
  • ArgumentError - Invalid name, value or ctype.
  • TypeError - Invalid name, value or ctype type.

nsDeallocObserver(obj)

 

Create a de-allocation observer for an ObjC instance.

Parameters:
Returns:
The observer (_NSDeallocObserver).

Note: When the observed ObjC object is de-allocated, the _NSDeallocObserver removes the corresponding ObjCInstance from the dictionary of cached objects ObjCInstance._objc_cache_, effectively destroying the ObjCInstance.


Variables Details

libobjc

Value:
<CDLL '/usr/lib/libobjc.dylib', handle 10a509680 at 102f6c250>