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

Module runtime

Classes ObjCClass, ObjCInstance, ObjCMethod, ObjCSubclass, etc.


Version: 18.06.16

Classes
  ObjC_t
Base type to pretty-print ctypes c_void_p.
  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
Python class representing an unbound ObjC class- or instance-method (actually an IMP_t).
  ObjCClassMethod
Only to distinguish class methods from instance methods.
  ObjCSubclass
Python class creating 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(objc)
Check whether an object is an ObjC clas.
 
isImmutable(objc, mutableClass, immutableClass, name='ns')
Check that an ObjC object is an instance of the immutable class.
 
isInstanceOf(objc, *Classes, **name_missing)
Check whether an ObjC object is an instance of some ObjC class.
 
isMetaClass(objc)
Check whether an object is an ObjC metaclass.
 
release(objc)
Release an ObjC instance to eventually be garbage collected.
 
register_subclass(subclas)
Register an ObjC sub-class.
 
retain(objc)
Preserve an ObjC instance from destruction.
 
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(objc, name, value, ctype=None)
Set an instance variable of an ObjC object.
Variables
  libobjc = <CDLL '/usr/lib/libobjc.dylib', handle 11685e680 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 ivar (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 (str 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 (str or Object).
  • 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(objc)

 

Check whether an object is an ObjC clas.

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

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

 

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

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

isInstanceOf(objc, *Classes, **name_missing)

 

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

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

See Also: Function isinstanceOf for checking Python instances.

isMetaClass(objc)

 

Check whether an object is an ObjC metaclass.

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

release(objc)

 

Release an ObjC instance to eventually be garbage collected.

Parameters:
Raises:
  • TypeError - If objc is not releasable.

Note: May result in Python memory errors, aborts and/or segfaults. Use 'python3 -X faulthandler ...' to get a Python traceback.

register_subclass(subclas)

 

Register an ObjC sub-class.

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

See Also: ObjCSubclass.register.

retain(objc)

 

Preserve an ObjC instance from destruction.

Parameters:
Returns:
The retained instance objc.
Raises:
  • TypeError - If objc is not retainable.

Note: May result in Python memory errors, aborts and/or segfaults. Use 'python3 -X faulthandler ...' to get a Python traceback.

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(objc, name, value, ctype=None)

 

Set an instance variable of an ObjC object.

Parameters:
  • objc - 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.

Variables Details

libobjc

Value:
<CDLL '/usr/lib/libobjc.dylib', handle 11685e680 at 109f4ab90>