Source

pyobjc / pyobjc-core / Doc / metadata / manual.rst

Full commit

Manual metadata loading

Introduction

When the other two metadata systems aren't suitable it is also possible to load metadata through code. The other two systems use the functionality described in this section to actually load the metadata.

Metadata dictionaries

One or the arguments for :func:`loadBundleFunctions`, :func:`loadFunctionList` and :func:`registerMetaDataForSelector` contains (or "is" for :func:`registerMetaDataForSelector`) a metadata dictionary containing information about function and method interfaces that cannot be extract from a basic type signature for that function or method.

Copies of these structures can be also retrieved at runtime using the __metadata__() method on both :class:`function` and :class:`selector` objects, which makes it possible to introspect the metadata information when needed.

The metadata is a Python dictionary with a particular structure (all keys are optional):

  • arguments: A dictionary containing more information on arguments. The keys of this dictionary are integers with the argument offset (for methods index 0 is the first implicit argument, index 2 is the first argument that is visible in a prototype). The values are metadata dictionaries for the arguments and are decribed later on.

    In metadata that is returned the __metadata__() method of :class:`function` and :class:`selector` objects the arguments value is a tuple with items for all arguments.

  • retval: A metadata dictionary with more information on the return value. The contents of this dictionary is described later on.

  • suggestion: For methods only: the method should not be called from Python, and calling it will raise and exception with the suggestion value in the exception message.

  • variadic: If present and the value is :data:`True` the function or method takes a variable number of arguments. PyObjC can only call such functions when either on of the arguments is a printf_format, or the dictionary contains information on the argument array (as described by keys further on in this list).

  • c_array_delimited_by_null: If present and the value is :data:`True`, and the function is a variadic function, the variable part of the function argument list is a list of values where the last item of the list is a null value. All elements of the list are the same type, that of the last type that is present in the prototype.

    In python the function is called with the additional arguments after the fixed arguments (just like in C), but without a null value at the end of the argument array.

    An example of such a function is execlp(3)

  • c_array_length_in_arg: If present and the value is an integer, and the function is a variadic function, the variable part of the function argument list is a list of values and the value for this key indicates which function argument contains the length of that list. All elements of the list are the same type, that of the last type that is present in the prototype.

    In python the function is called with the additional arguments after the fixed arguments (just like in C).

Keys not listed above will be ignored by the bridge.

Note

The bridge currently does not copy the metadata when you register it with the functions listed above. Don't rely on that behavior, it might change in a future release and changes to metadata dictionaries may or may not affect bridge behavior (basicly depending on if the change occurs before or after the bridge first uses the metadata)

c_array_lenght_in... interpretation

The keys c_array_length_in_arg (function, argument and return value metadata) and c_array_length_in_result (argument metadata) describe the index of an argument that contains the size of a C array. This section describes how the bridge determines the value to use for the array.

For :data:`_C_INOUT` argument the value to use for converting to C and back from C is calculated both before and after the function call, which means that the length of the array passed to the function can have a different length than the array returned from the function. This is used for function where a buffer is passed into the function and the function indicates the useable size of that buffer by modifiying a pass-by-reference argument.

How the size of the C array is calculated depends on the type of the argument:

  • When the type is a pointer type the value is calculated by dereferencing the pointer.
  • When the type is a integer (for example, :c:type:`int`, :c:type:`unsigned long` or :c:type:`NSInteger`) the length is the value of the C argument.
  • When the type is :c:type:`NSRange` or :c:type:`CFRange` the length of the C array is the lenght of the range.
  • When the type is :c:type:`id` and the Objective-C instance responds to the "-count" selector the length of the C array is the result of calling that selector.
  • In all other cases the length cannot be calculated and the bridge raises an exception.

Argument and return value metadata

The argument and return value metadata is also a dictionary with a specific structure. As with the complete metadata dictionary all keys are optional unless the description mentions otherwise.

  • type: A byte string with the type encoding for the value. The default is extracted from the type encoding for the entire prototype (for methods this is extracted from the Objective-C runtime, for functions this is passed as one of the items in the function info tuple).

    This key is always present in the metadata returned by the __metadata__() method.

  • type_override: A byte string with value :data:`_C_IN`, :data:`_C_OUT` or :data:`_C_INOUT` to indicate that the argument is an input, output or input/output argument. Ignored unless the type is a pointer type that isn't a CoreFoundation instance.

    The value is assumed to be a single value (a pass-by-reference argument), unless there are keys in the dictionary that say otherwise (see further on in this list).

    This key is not used for return value metadata.

  • printf_format: If present and the value is :data:`True` the argument value is a printf(3) style format string for a variadic function or method. Ignored unless the function or method actually is variadic.

  • sel_of_type: A byte string that describes the expected selector prototype for arguments of the :data:`_C_SEL`.

    Used by the decorator :func:`selectorFor` to calculate the signature of the decorated method.

  • already_retained: Value :data:`True` indicates that the return value, or a pass-by-reference output parameter, is returned to the caller with an increased reference count. An Objective-C caller will have to call "-retain" on the value when the value is no longer used.

    Used by the bridge to correctly maintain the Objective-C reference count. Python users do not have to maintain the reference count themselfes.

  • already_cfretained: Value :data:`True` indicates that the return value, or a pass-by-reference output parameter, is returned to the caller with an increased reference count. An Objective-C caller will have to call "CFRelease" on the value when the value is no longer used.

    Used by the bridge to correctly maintain the Objective-C reference count. Python users do not have to maintain the reference count themselfes.

    Note

    Use either already_retained, or already_cfretained but not both.

    The two different keys are present to be able to support Objective-C Garbage Collection: in process with GC enabled the CoreFoundation and Cocoa retain count APIs behave differently. Because GC is on the way out and PyObjC doesn't properly support GC anyway it is better to use already_retained where approprate and not use already_cfretained.

  • c_array_delimited_by_null: When :data:`True`, and the argument or return value type is a pointer type, the value is a C array with a null value at the end. Python users do not have to supply the null value on calls, and the bridge will strip the null value in return values.

    When the type_override is :data:`_C_IN` or :data:`_C_INOUT` the input value must be a sequence of values (list, tuple, ...). The bridge allocates a buffer of the right size, converts all values and appends the approprate null value. The value can also be a buffer (such as an :class:`array.array` of the approprate structure), which then must contain a null value at the end.

    When the type_override is :data:`_C_OUT` the argument must be either :data:`NULL` to indicate that a :c:data:`NULL` pointer should be passed to the Objective-C function or a buffer object of the appropriate structure, and with enough room to store the function output including the null delimiter.

  • c_array_length_in_arg: The argument or return value is a C array where the length of the array is specified in another argument. Ignored when the type is not a pointer type. The value for this key is either a single integer, or two integers (for :data:`_C_INOUT` arguments).

    When the type_override is :data:`_C_IN` or :data:`_C_INOUT` the input value must be a sequence of values of the correct lenght (at least the length that's expected by the function, additional items in the sequence are ignored).

    When the type_override is :data:`_C_OUT` the value can be :data:`NULL` (:c:data:`NULL` pointer passed to the function) or :data:`None` (PyObjC allocates a C array of the right size and writes nul bytes in the entire buffer).

    When the value of the key is a single integer this argument index for the argument that contains the expected size of the array. When the value of the key is a tuple of two integers these are the indexes for the argument that contains the size that should be used when calling the function and the argument that contains the size of the array that is useable after the call.

  • c_array_of_fixed_length: When the type is a pointer type the actual argument (or result) is an C array of a fixed length. The value for this key is an integer that is the length of the C array.

  • c_array_of_variable_length: When the type is a pointer type the actual argument (or result) is a C array, but the lenght of the array is unknown or cannot be described in metadata.

    For results the bridge will return a value of :class:`varlist`.

    For arguments with type_override value :data:`_C_IN` or :data:`_C_INOUT` the value for the arugment must be a Python sequence and the bridge will allocate a C array that is long enough to contain all items of that sequence; alternatively the argument can be a Python buffer object (simular to :data:`_C_OUT` arugments). For :data:`_C_OUT` arguments the value for the argument must be either :data:`NULL` or a Python buffer object that will be passed to the function.

  • c_array_length_in_result: Only valid for argument metadata. When the argument type is a pointer type and the type_override is :data:`_C_INOUT` or :data:`_C_OUT` the usuable length of the array is calculated from the return value.

    The size of the buffer that needs to be allocated is determined using one of the other c_array... keys in the metadata dictionary.

  • null_accepted: If :data:`True` and the argument is a pointer it is safe to pass a :data:`NULL` as the value. Defaults to :data:`True`.

    This key is not used in return value metadata.

    Note

    The metadata that is currently shipped with PyObjC does not contain null_accepted data. This means that the bridge won't check if it safe to pass :data:`NULL` as a value for pointer arguments, read the Cocoa documentation to check if passing :data:`NULL` is safe.

  • callable: When type argument or return value has type "^?" or "@?" the method or function takes a function or block as the argument. In Python an arbitrary callable can be passed (but see callable_retained for some limitations).

    The value of this attribute contains the metadata describing the callable. It is a metadata structure as described in this section, with some additional limitations: the arguments key of the dictionary must describe all arguments of the callable (that is all keys in range(len(arguments)) must be present), the type key of the argument and return value metadata must be present (although it is allowed to leave out the return value metadata when the function has return type :c:type:`void`).

    For blocks the argument array must include the first implicit argument at index 0 or the arguments array, and with type b"^v".

  • callable_retained: Then :data:`True` and callable is present and the argument type is b"^?" the callable argument will be retained by the Objective-C funtion or method beyond the call.

    This key is not used in return value metadata.

    When this value is :data:`True` the argument must be a global object that is annotated with the decorator :func:`callbackFor`. That decorator ensures that the C representation of the function is always present to ensure that it is safe to store a reference on the Objective-C side of the bridge.

API description

Loading frameworks and other bundles

Creating and registering types

Loading variable/constants

Loading functions

Metadata for Objective-C methods and classes