Python pyarg_parsetuple string




















We can also set errors ourselves. This code is analogous to raise ValueError "Cannot be negative". Note that we return NULL to indicate that we raised an error. For the next function we implement, it will take in a list of list of doubles, and then add up all the doubles. Functions that return object references generally return NULL only to indicate that an exception occurred. The reason for not testing for NULL arguments is that functions often pass the objects they receive on to other function — if each function were to test for NULL , there would be a lot of redundant tests and the code would run more slowly.

There are no variants with NULL checking. The C function calling mechanism guarantees that the argument list passed to C functions args in the examples is never NULL — in fact it guarantees that it is always a tuple 4. Some restrictions apply. If the main program the Python interpreter is compiled and linked by the C compiler, global or static objects with constructors cannot be used.

Functions that will be called by the Python interpreter in particular, module initialization functions have to be declared using extern "C". Many extension modules just provide new functions and types to be used from Python, but sometimes the code in an extension module can be useful for other extension modules. Just like the standard Python list type has a C API which permits extension modules to create and manipulate lists, this new collection type should have a set of C functions for direct manipulation from other extension modules.

At first sight this seems easy: just write the functions without declaring them static , of course , provide an appropriate header file, and document the C API.

And in fact this would work if all extension modules were always linked statically with the Python interpreter. When modules are used as shared libraries, however, the symbols defined in one module may not be visible to another module. The details of visibility depend on the operating system; some systems use one global namespace for the Python interpreter and all extension modules Windows, for example , whereas others require an explicit list of imported symbols at module link time AIX is one example , or offer a choice of different strategies most Unices.

And even if symbols are globally visible, the module whose functions one wishes to call might not have been loaded yet!

Portability therefore requires not to make any assumptions about symbol visibility. And it means that symbols that should be accessible from other extension modules must be exported in a different way. Python provides a special mechanism to pass C-level information pointers from one extension module to another one: Capsules.

Capsules can only be created and accessed via their C API, but they can be passed around like any other Python object. Other extension modules can then import this module, retrieve the value of this name, and then retrieve the pointer from the Capsule. Each function could get its own Capsule, or all C API pointers could be stored in an array whose address is published in a Capsule. And the various tasks of storing and retrieving the pointers can be distributed in different ways between the module providing the code and the client modules.

Properly named Capsules provide a degree of runtime type-safety; there is no feasible way to tell one unnamed Capsule from another. The following example demonstrates an approach that puts most of the burden on the writer of the exporting module, which is appropriate for commonly used library modules. It stores all C API pointers just one in the example! The header file corresponding to the module provides a macro that takes care of importing the module and retrieving its C API pointers; client modules only have to call this macro before accessing the C API.

The exporting module is a modification of the spam module from section A Simple Example. The function spam. The define is used to tell the header file that it is being included in the exporting module, not a client module. The bulk of the work is in the header file spammodule. The main disadvantage of this approach is that the file spammodule. However, the basic structure is the same for each function that is exported, so it has to be learned only once.

Finally it should be mentioned that Capsules offer additional functionality, which is especially useful for memory allocation and deallocation of the pointer stored in a Capsule.

An interface for this function already exists in the standard module os — it was chosen as a simple and straightforward example. Checking that the reference count is at least 1 does not work — the reference count itself could be in freed memory and may thus be reused for another object!

Navigation index modules next previous Python ». Note The C extension interface is specific to CPython, and extension modules do not work on other Python implementations. Note Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python. The Python API defines a number of functions to set various types of exceptions. If this step fails, it will be a fatal error.

Note Removing entries from sys. Note Unlike our spam example, xxmodule uses multi-phase initialization new in Python 3. Note Nested tuples cannot be parsed when using keyword arguments!

In particular, Capsules used to expose C APIs should be given a name following this convention: modulename. Table of Contents 1. A Simple Example 1. Intermezzo: Errors and Exceptions 1. Back to the Example 1. Compilation and Linkage 1. Calling Python Functions from C 1. Extracting Parameters in Extension Functions 1. Keyword Parameters for Extension Functions 1. Building Arbitrary Values 1. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses.

Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. When memory buffers are passed as parameters to supply data to build objects, as for the s and s formats, the required data is copied. In the following description, the quoted form is the format unit; the entry in round parentheses is the Python object type that the format unit will return; and the entry in [square] brackets is the type of the C value s to be passed.

The characters space, tab, colon and comma are ignored in format strings but not within format units such as s. This can be used to make long format strings a tad more readable. Convert a null-terminated C string to a Python str object using 'utf-8' encoding. Convert a C string and its length to a Python str object using 'utf-8' encoding. This converts a C string to a Python bytes object. This converts a C string and its lengths to a Python object.

Convert a C int representing a byte to a Python bytes object of length 1. Convert a C int representing a character to Python str object of length 1. Pass a Python object untouched except for its reference count, which is incremented by one. If the object passed in is a NULL pointer, it is assumed that this was caused because the call producing the argument found an error and set an exception.

If no exception has been raised yet, SystemError is set. Useful when the object is created by a call to an object constructor in the argument list. Convert anything to a Python object through a converter function.

Convert a sequence of C values to a Python dictionary. Each pair of consecutive C values adds one item to the dictionary, serving as key and value, respectively. Navigation index modules next previous Python ». Unless otherwise stated, buffers are not NUL-terminated. Note For all variants of formats s , y , etc. Note This format does not accept bytes-like objects. B int [unsigned char] Convert a Python integer to a tiny int without overflow checking, stored in a C unsigned char.

H int [unsigned short int] Convert a Python integer to a C unsigned short int , without overflow checking. I int [unsigned int] Convert a Python integer to a C unsigned int , without overflow checking. L int [long long] Convert a Python integer to a C long long. K int [unsigned long long] Convert a Python integer to a C unsigned long long without overflow checking. Part of the Stable ABI.

B int [unsigned char] Convert a C unsigned char to a Python integer object. H int [unsigned short int] Convert a C unsigned short int to a Python integer object. I int [unsigned int] Convert a C unsigned int to a Python integer object. L int [long long] Convert a C long long to a Python integer object. K int [unsigned long long] Convert a C unsigned long long to a Python integer object.

C str of length 1 [int] Convert a C int representing a character to Python str object of length 1. See History and License for more information.



0コメント

  • 1000 / 1000