Extending Python with C (Part I – the Basics) June 2002 Brian Quinlan

Slides:



Advertisements
Similar presentations
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Advertisements

Exception Handling Genome 559. Review - classes 1) Class constructors - class myClass: def __init__(self, arg1, arg2): self.var1 = arg1 self.var2 = arg2.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
Optimizing Python with Pyrex. Pyrex is cool but not magic Pyrex very seldom makes pure-Python code faster just through a recompile But if you understand.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
Python in PHP: Internals Jon Parise 2002 International PHP Conference Frankfurt, Germany November 6, 2002.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Exam Objective : Legal return types PRESENTED BY : SRINIVAS VG.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.
University of Limerick1 Work with API’s. University of Limerick2 Learning OO programming u Learning a programming language can be broadly split into two.
“Everything Else”. Find all substrings We’ve learned how to find the first location of a string in another string with find. What about finding all matches?
Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - Extended.
17. Python Exceptions Handling Python provides two very important features to handle any unexpected error in your Python programs and to add debugging.
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
H3D API Training  Part 3.1: Python – Quick overview.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
SCRIPTING II/III References:
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
SE-1010 Dr. Mark L. Hornick 1 Some Java Classes using & calling methodsS.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
1 Cisco Unified Application Environment Developers Conference 2008© 2008 Cisco Systems, Inc. All rights reserved.Cisco Public Introduction to Etch Scott.
Extending Python with Pyrex. Poll 1. How many people here have been to more than two Python conferences?
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Built-in Data Structures in Python An Introduction.
Vladimir Misic: Java1 Basic Java Syntax The java language will be described by working through its features: –Variable types and expressions.
Getting Started with Python: Constructs and Pitfalls Sean Deitz Advanced Programming Seminar September 13, 2013.
Looping and Counting Lecture 3 Hartmut Kaiser
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Python Overview  Last week Python 3000 was released  Python 3000 == Python 3.0 == Py3k  Designed to break backwards compatibility with the 2.x.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Copyright Curt Hill Variables What are they? Why do we need them?
An Introduction. What is Python? Interpreted language Created by Guido Van Rossum – early 90s Named after Monty Python
1 Developing WBEM Clients Using Python Tim Potter Hewlett-Packard Company
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
Digging into the GAT API Comparing C, C++ and Python API‘s Hartmut Kaiser
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
PHP vs. Python. Similarities are interpreted, high level languages with dynamic typing are Open Source are supported by large developer communities are.
Python Let’s get started!.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
The Python Language Petr Přikryl Part IIb Socrates IP, 15th June 2004 TU of Brno, FIT, Czech Republic.
Python Programing: An Introduction to Computer Science
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Python C API overview References:
Introduction to Scripting Languages: Python Some slides are based upon Python Documentation - Extended.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Python C API overview References:
Integration and development within the CCPN framework Expanded from presentation, Paris Oct 2006.
Pyragen A PYTHON WRAPPER GENERATOR TO APPLICATION CORE LIBRARIES Fernando PEREIRA, Christian THEIS - HSE/RP EDMS tech note:
SCRIPTING II & III (LAB 21 AND [OPTIONAL] LAB 22) References:
EXTENDING PYTHON SCRIPTING I/III References:
Java Fundamentals MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Defining Your Own Classes II
Information and Computer Sciences University of Hawaii, Manoa
Key Words / Reserved Words
C Basics.
Introduction to Python
An Introduction to Java – Part I, language basics
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
In this class, we will cover:
Presentation transcript:

Extending Python with C (Part I – the Basics) June 2002 Brian Quinlan

Slide 2©2002 Brian Quinlan Why Mess with C? There is a preexisting library, available in C, that you would like to access from Python (e.g. pyexpat, math, zlib) Solving the problem in Python would be too CPU or memory inefficient (e.g. Numeric, PIL) Only way to create new fundament types (doesn’t seem too important to me; save it for another talk)

Slide 3©2002 Brian Quinlan Jumpin’ In Create our own extension module containing an eclectic set of functions We’ll call it: Vanpy Basic steps: –Do module initialization –Write functions –Write build script –Testing

Slide 4©2002 Brian Quinlan A module is just a dictionary foo.py magic_number = 5 def divide(x,y): return x / y >>> import foo >>> foo.__dict__ {'__doc__': None, 'magic_number': 5, 'divide':, …} >>> foo.__dict__['divide'](8,4) 2

Slide 5©2002 Brian Quinlan Module Initialization void initVanpy(void) { PyObject * module; module = Py_InitModule3("Vanpy", module_functions, module_doc); PyModule_AddStringConstant(module, "__version__", "0.0.1"); }

Slide 6©2002 Brian Quinlan A VERY Simple Function static PyObject * is_even(PyObject * self, PyObject * args) { long number; if (!PyArg_ParseTuple(args, "i", &number)) return NULL; return PyBuildValue("i", number % 2 == 0) } static PyMethodDef module_functions[] = { {"is_even", is_even, METH_VARARGS, is_even_doc}, {NULL, NULL} }

Slide 7©2002 Brian Quinlan A Simple Function static PyObject * classify_characters(PyObject * self, PyObject * args) { char * string; long length; long alpha, other = 0; if (!PyArg_ParseTuple(args, "s#", &string, &length)) return NULL; for (int i = 0; i < length; ++i) { isalnum(string[i]) ? ++alpha : ++other } return PyBuildValue("(iif)", alpha, other, ((float) alpha) / length) }

Slide 8©2002 Brian Quinlan Objects and Types typedef struct { struct _typeobject *ob_type; int ob_refcnt; /* The rest depends on the type */ } PyObject; typedef struct _typeobject { char *tp_name; /* Stuff to discuss in another talk */ }

Slide 9©2002 Brian Quinlan Reference Counting >>> a = “Hello” PyObject * a = Py_BuildValue(“s”, “Hello”) a 1Hello

Slide 10©2002 Brian Quinlan Reference Counting >>> a = “Hello” PyObject * a = Py_BuildValue(“s”, “Hello”) >>> b = a PyObject * b = a Py_INCREF(b) a 1Hello a 2 b

Slide 11©2002 Brian Quinlan Reference Counting >>> a = “Hello” PyObject * a = Py_BuildValue(“s”, “Hello”) >>> b = a PyObject * b = a Py_INCREF(b) >>> del a Py_DECREF(a) a 1Hello a 2 b b 1

Slide 12©2002 Brian Quinlan Reference Counting >>> a = “Hello” PyObject * a = Py_BuildValue(“s”, “Hello”) >>> b = a PyObject * b = a Py_INCREF(b) >>> del a Py_DECREF(a) >>> del b Py_DECREF(b) a 1Hello a 2 b b 1 0

Slide 13©2002 Brian Quinlan Reference Counting Rules When returning a PyObject from an extension function or method, always increment the object’s reference count USUALLY, when calling a Python API function, you need not increment the reference counts of PyObject arguments (only 2 important exceptions) USUALLY, when a PyObject is returned from a Python API function, you must decrement the object’s reference count when you are done with it (> 4 important exceptions)

Slide 14©2002 Brian Quinlan A “Realistic” Example static PyObject * password = NULL; static PyObject * set_password(PyObject * self, PyObject * args) { PyObject * new_password; if (!PyArg_ParseTuple(args, "O", &new_password)) return NULL; if (password != NULL) /* or Py_XDECREF(password) */ Py_DECREF(password); password = new_password; Py_INCREF(password); Py_INCREF(Py_None); return Py_None; }

Slide 15©2002 Brian Quinlan A “Realistic” Example static PyObject * get_password(PyObject * self, PyObject * args) { if (!PyArg_ParseTuple(args, "")) return NULL; if (password == NULL) { Py_INCREF(Py_None); return Py_None; } else { Py_INCREF(password); return password; }

Slide 16©2002 Brian Quinlan Exceptions Each Python thread has 3 exception variables associated with it: –The type of the exception (e.g. ValueError) –The value of the exception (e.g. “0 denominator”) –A traceback object (e.g. file “foo.py”, line 1, in “divide”) def divide(x,y): raise ValueError, “0 denominator” static PyObject * divide(PyObject * self, PyObject * args) { PyErr_SetString(PyExc_ValueError, “0 denominitor”); return NULL; }

Slide 17©2002 Brian Quinlan Passwords Revised static PyObject * set_password(PyObject * self, PyObject * args) { PyObject * new_password; if (!PyArg_ParseTuple(args, "O", &new_password)) return NULL; if (!PyString_Check(new_password) && !PyUnicode_Check(new_password)) { PyErr_Format(PyExc_TypeError, "expected string or " "Unicode, %80s found", new_password->ob_type->tp_name); return NULL; }...

Slide 18©2002 Brian Quinlan More Exceptions If you want to ignore an error, use PyErr_Clear to clear the current exception You can check to see if an exception has been set using PyErr_Occurred (this is not usually need since the API return value indicates failure) It is an error to return NULL without setting an exception

Slide 19©2002 Brian Quinlan Custom Exceptions I static PyObject * WeatherError; void initVanpy(void) { PyObject * module; PyObject * module_dict; module = Py_InitModule3("Vanpy", module_methods, module_doc); WeatherError = PyErr_NewException("Vanpy.WeatherError", NULL, NULL); module_dict = PyModule_GetDict(module); PyDict_SetItemString( module_dict, "WeatherError", WeatherError); }

Slide 20©2002 Brian Quinlan Custom Exceptions II static PyObject * goto_beach(PyObject * self, PyObject * args) { if (!PyArg_ParseTuple(args, "")) return NULL; if (rand() % 2) { PyObject * e = Py_BuildValue("(si)", "too cold", 5); if (e == NULL) return NULL; PyErr_SetObject(WeatherError, e); Py_DECREF(e); } else { PyErr_SetString(WeatherError, "too rainy"); } return NULL; }

Slide 21©2002 Brian Quinlan Building setup.py #!/usr/bin/env python from distutils.core import setup, Extension setup( name = "Vanpy", version = "0.0.1", description = "An eclectic set of functions", author = "Brian Quinlan", author_ = ext_modules = [ Extension("Vanpy", ["Vanpy.c"]) ] )

Slide 22©2002 Brian Quinlan Python API organization Exception handling Memory Management Threads Utilities –OS Utilities –Process Control –Importing Modules –Data Marshalling –Parsing Arguments & Building Values Abstract Object Support Concrete Object Support

Slide 23©2002 Brian Quinlan Abstract Object Support Applies to classes of objects, not specific types Divided into protocols: –Object –Number –Sequence –Mapping –Iterator –Buffer

Slide 24©2002 Brian Quinlan Object Protocol A sampling of functions in the object protocol: int PyObject_HasAttrString(PyObject *o, char *attr_name) int PyObject_Cmp(PyObject *o1, PyObject *o2, int *result) PyObject* PyObject_Repr(PyObject *o) PyObject* PyObject_Str(PyObject *o) int PyObject_IsInstance(PyObject *inst, PyObject *cls) PyObject* PyObject_CallFunction(PyObject *callable, char *format,...) PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format,...) int PyObject_IsTrue(PyObject *o)

Slide 25©2002 Brian Quinlan Number Protocol A sampling of functions in the number protocol: int PyNumber_Check(PyObject *o) PyObject* PyNumber_Add(PyObject *o1, PyObject *o2) PyObject* PyNumber_Subtract(PyObject *o1, PyObject *o2) PyObject* PyNumber_Multiply(PyObject *o1, PyObject *o2) PyObject* PyNumber_Negative(PyObject *o) PyObject* PyNumber_Absolute(PyObject *o) PyObject* PyNumber_Lshift(PyObject *o1, PyObject *o2) PyObject* PyNumber_And(PyObject *o1, PyObject *o2) PyObject* PyNumber_Int(PyObject *o) PyObject* PyNumber_Long(PyObject *o) PyObject* PyNumber_Float(PyObject *o)

Slide 26©2002 Brian Quinlan Sequence Protocol A sampling of functions in the sequence protocol: int PySequence_Check(PyObject *o) int PySequence_Length(PyObject *o) PyObject* PySequence_Repeat(PyObject *o, int count) PyObject* PySequence_GetItem(PyObject *o, int i) PyObject* PySequence_GetSlice(PyObject *o, int i1, int i2) int PySequence_SetItem(PyObject *o, int i, PyObject *v) int PySequence_DelItem(PyObject *o, int i) PyObject* PySequence_Tuple(PyObject *o) int PySequence_Contains(PyObject *o, PyObject *value) int PySequence_Count(PyObject *o, PyObject *value)

Slide 27©2002 Brian Quinlan Concrete Object Support Applies to specific object types Different set of functions for every built-in Python type There any many Python object types: plain int, long int, float, complex, string, unicode, buffer, tuple, list, dictionary, file, instance, method, module, iterator, slice, weak reference, etc. Reference counting rules are less intuitive

Slide 28©2002 Brian Quinlan Int Objects This is the complete list of int functions: int PyInt_Check(PyObject* o) int PyInt_CheckExact(PyObject* o) PyObject* PyInt_FromLong(long ival) long PyInt_AsLong(PyObject *io) long PyInt_AS_LONG(PyObject *io) long PyInt_GetMax()

Slide 29©2002 Brian Quinlan String Objects A sampling of string functions: int PyString_Check(PyObject *o) int PyString_CheckExact(PyObject *o) PyObject* PyString_FromString(const char *v) PyObject* PyString_FromStringAndSize(const char *v, int len) PyObject* PyString_FromFormat(const char *format,...) PyObject* PyString_FromFormatV(const char *format, va_list vargs) int PyString_Size(PyObject *string) int PyString_GET_SIZE(PyObject *string) char* PyString_AsString(PyObject *string) char* PyString_AS_STRING(PyObject *string) void PyString_Concat(PyObject **string, PyObject *newpart)

Slide 30©2002 Brian Quinlan List Objects This is the complete list of list functions: int PyList_Check(PyObject *p) PyObject* PyList_New(int len) int PyList_Size(PyObject *list) int PyList_GET_SIZE(PyObject *list) PyObject* PyList_GetItem(PyObject *list, int index) PyObject* PyList_GET_ITEM(PyObject *list, int i) int PyList_SetItem(PyObject *list, int index, PyObject *item) void PyList_SET_ITEM(PyObject *list, int i, PyObject *o) int PyList_Insert(PyObject *list, int index, PyObject *item) int PyList_Append(PyObject *list, PyObject *item) PyObject* PyList_GetSlice(PyObject *list, int low, int high) int PyList_SetSlice(PyObject *list, int low, int high, PyObject *itemlist) int PyList_Sort(PyObject *list)

Slide 31©2002 Brian Quinlan Helpful Technologies SWIG/SIP - generate C/C++ interface code for Python Boost Python - an interface generator for wrapping C++ classes with a Python interface CXX - C++ wrapper around the Python API Pyrex - a language for writing Python extension modules Pyfort - a wrapper generator for interfacing Fortran with Python PyInline/Weave – embed C/C++/Perl code inside your Python script