Expert Python Programming by Michał Jaworski
Author:Michał Jaworski
Language: eng
Format: epub
Tags: COM051360 - COMPUTERS / Programming Languages / Python, COM051240 - COMPUTERS / Software Development and Engineering / Systems Analysis and Design, COM060090 - COMPUTERS / Internet / Application Development
Publisher: Packt Publishing
Published: 2019-04-30T07:21:28+00:00
Note that the preceding signature does not specify the exact list of arguments but only PyObject* args will hold the pointer to the structure that contains the tuple of the provided values. The actual validation of the argument list must be performed inside the function body and this is exactly what fibonacci_py() does. It parses the args argument list assuming it is the single unsigned int type and uses that value as an argument to the fibonacci() function to retrieve the Fibonacci sequence element as shown in the following code:
static PyObject* fibonacci_py(PyObject* self, PyObject* args) { PyObject *result = NULL; long n; if (PyArg_ParseTuple(args, "l", &n)) { result = Py_BuildValue("L", fibonacci((unsigned int)n)); } return result; }
The preceding example function has a serious bug, which the eyes of an experienced developer should spot very easily. Try to find it as an exercise in working with C extensions. For now, we leave it as it is for the sake of brevity. We will try to fix it later when discussing details of dealing with errors in the Exception handling section.
The "l" string in the PyArg_ParseTuple(args, "l", &n) call means that we expect args to contain only a single long value. In case of failure, it will return NULL and store information about the exception in the per thread interpreter state. The details of exception handling will be described a bit later in the Exception handling section.
The actual signature of the parsing function is int PyArg_ParseTuple(PyObject *args, const char *format, ...) and what goes after the format string is a variable length list of arguments that represents parsed value output (as pointers). This is analogous to how the scanf() function from the C standard library works. If our assumption fails and the user provides an incompatible arguments list, then PyArg_ParseTuple() will raise the proper exception. This is a very convenient way to encode function signatures once you get used to it but has a huge downside when compared to plain Python code. Such Python call signatures implicitly defined by the PyArg_ParseTuple() calls cannot be easily inspected inside the Python interpreter. You need to remember this fact when using the code provided as extensions.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Grails in Action by Glen Smith Peter Ledbrook(7696)
Configuring Windows Server Hybrid Advanced Services Exam Ref AZ-801 by Chris Gill(6536)
Azure Containers Explained by Wesley Haakman & Richard Hooper(6525)
Running Windows Containers on AWS by Marcio Morales(6049)
Kotlin in Action by Dmitry Jemerov(5062)
Microsoft 365 Identity and Services Exam Guide MS-100 by Aaron Guilmette(4897)
Combating Crime on the Dark Web by Nearchos Nearchou(4479)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4414)
Microsoft Cybersecurity Architect Exam Ref SC-100 by Dwayne Natwick(4309)
The Ruby Workshop by Akshat Paul Peter Philips Dániel Szabó and Cheyne Wallace(4155)
The Age of Surveillance Capitalism by Shoshana Zuboff(3946)
Python for Security and Networking - Third Edition by José Manuel Ortega(3715)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3503)
The Ultimate Docker Container Book by Schenker Gabriel N.;(3390)
Mastering Python for Networking and Security by José Manuel Ortega(3344)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3327)
Blockchain Basics by Daniel Drescher(3292)
Learn Wireshark by Lisa Bock(3229)
