Python C API overview References:

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
Informática II Prof. Dr. Gustavo Patiño MJ
Primitive Data Types byte, short, int, long float, double char boolean Are all primitive data types. Primitive data types always start with a small letter.
CEG 221 Lesson 2: Homogeneous Data Types Mr. David Lippa.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
(…A FEW OF THEM) C++ DESIGN PATTERNS. WHAT ARE THEY? Commonly occurring constructs Could be part of good software engineering Not universally agreed Good.
Moving from C to Java. The Java Syntax We Know Java Types Some of the types we know and love are still there  Integer: byte, short, char, int, long.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
Extending Python with C (Part I – the Basics) June 2002 Brian Quinlan
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
SCRIPTING II/III References:
 Jim Hugunin Partner Architect Microsoft Corporation TL10.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
February 11, 2005 More Pointers Dynamic Memory Allocation.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
2: Everything is an Object You Manipulate Objects Using References Primitives Arrays in Java Scoping You Never Destroy Objects Creating New Data Types:
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Dynamic memory allocation and Pointers Lecture 4.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD.
C HAPTER 03 Pointers Compiled by Dr. Mohammad Alhawarat.
Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
Peyman Dodangeh Sharif University of Technology Spring 2014.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
C is a high level language (HLL)
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Python C API overview References:
Java Programming Language Lecture27- An Introduction.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
SCRIPTING II & III (LAB 21 AND [OPTIONAL] LAB 22) References:
EXTENDING PYTHON SCRIPTING I/III References:
EGR 2261 Unit 11 Pointers and Dynamic Variables
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Lua Extending References:
Chapter 6 – Data Types CSCE 343.
CSC 253 Lecture 8.
Lecturer: Mukhtar Mohamed Ali “Hakaale”
CSC 253 Lecture 8.
Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Variables Title slide variables.
CISC/CMPE320 - Prof. McLeod
Java Programming Language
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Python C API overview References:

Two(or three) types Extending – Write a dll (.pyd), which exports an initialization function a list of "things" in the module – classes – functions – variables – You can import this just like any (pure) python module – Use the c types / functions in your program import mymodule mymodule.fun(4.1) # A c-function

Two (or three) types, cont. Embedding – Create a python interpreter inside a C program – Can execute python code Need to convert to/from python types Hybrid – Both! – In this case, though, you don't actually create a dll (but you do everything you did in the dll) – This is what we'll use for our scripting.

Naming convention PyXXX_YYY – (Python) class XXX, functionYYY – e.g. PyTuple_Size(…) Py_ZZZ – Global function ZZZ – e.g. Py_Compile(…)

PyObject's Literally everything in Python is a PyObject* – Int ref-count (more on this shortly) – A type structure – Any other data e.g. for tuples, they have a length attribute. The python interpreter owns all objects – The user just has references to them – The first time it is referenced, python malloc's it. – When the refcnt reaches 0, it eventually gets free'd – In (normal) python this is done for us… – …but in the C API we have to manage it.

RefCounting Take a simple example def foo(a): return a ** 2 x = 4 y = [foo(x), 3.2] z = y Line 1 – 2 creates a new function object. Line 3 creates a new int x. Line 4 calls foo. o a and x refer to the same thing (refcnt is 2) o Creates another float and returns it o a goes out of scope – the refcnt on the int is now 1 o The 16.0 and 3.2 are part of a new list object (refcnt = 1) Line 5 makes a second reference to the list (Refcnt = 2)

RefCounting, cont. …In the C API, you have to manually do it. – Memory Leaks (if you forget to decrement a ref-count) – Invalid memory access (if you forget to increment, and then the object is destroyed) Three functions Py_INCREF(PyObject*); Py_DECREF(PyObject*); Py_XDECREF(PyObject*); // Like previous, but has a NULL- check

RefCounting, cont. Look in the C/API documentation – – If you see New Reference e.g. PyLong_FromLong(long v) You own a ref-count to it. Make sure to call Py_DECREF when done. – If you see Borrowed Reference e.g. PyList_GetItem(PyObject * list, Py_ssize_t index) Someone else holds a reference Probably safest to: – call Py_INCREF – Use it – call Py_DECREF

Converting between types Integers – C => Python PyObject * PyLong_FromLong(int i); A new reference – make sure you DECREF it! – Python => C Int PyLong_Check(PyObject*); int PyLong_AsLong(PyObject*); float PyLong_AsDouble(PyObject*); …

Converting between types, cont. Floats – C => Python PyObject * PyFloat_FromDouble(double d); A new reference – make sure you DECREF it! – Python => C double PyFloat_AsDouble(PyObject*);

Converting between types, cont. Strings – A bit more complicated because python 3.x uses unicode internally, not ANSI strings. – C => Python PyObject * PyUnicode_FromString(char * s); A new reference – make sure you DECREF it! – Python => C char temps[256]; // PObj is the object (in python) we want to convert PyObject * pBytesObj = PyUnicode_AsUTF8String(pObj); strcpy(temps, PyBytes_AsString(pBytesObj)); Py_DECREF(pBytesObj);

Creating a C extension (pyd) Goal: – A new class (written in C) – A new function (written in C) – A variable (written in C) – Access all 3 in python import myCMod C = myCMod.myClass("Bob") print(C.name) # Bob print(C.timestamp) # 0 C.incTimestamp() print(C.timestamp) # 1 print(myCMod.fact(5)) # 120 print(myCMod.aVariable) # Surprise!

Making an extension (pyd) 0. Set up a project – A Win32 console dll (empty) – Put all your code in a single.cpp file – Change output to xyz.pyd (actually a.dll) – Only do a release build (debug can only be run by a debug build of python) – Include includes / libs Reference:

Making an extension (pyd) 1.Create a C-type ("guts" of a python object) typedef struct { PyObject_HEAD /* Type-specific fields go here. */ PyObject * name; int timestamp; } myClass;

Making an extension (pyd) 2.Create methods for the new type // An "overload" of the allocation func static void myClass_new(PyTypeObject * type, PyObject * args, PyObject * kwds) { myClass * self; self = (myClass*)type->tp_alloc(type, 0); // Note: init is called later. This is // for code that needs to happen before that. self->name = NULL; } // An "overload" of the delete func static void myClass_dealloc(myClass * c) { Py_XDECREF(c->name); Py_TYPE(c)->tp_free((PyObject*)c); }

Making an extension (pyd) // An "overload" of the init method static int myClass_init(myClass * self, PyObject * args, PyObject * kwds) { PyObject * newName; // Get a single objectfrom the args if (!PyArg_ParseTuple(args, "O", &newName)) return -1; // ERROR – nothing in args. Py_INCREF(newName); self->name = newName; self->timestamp = 0; return 0; // Everything OK } // A new method (not an overload) static void incTimeStamp(myClass * self) { self->timestamp++; }

Making an extension (pyd) 3.Create an attribute and method structure for the new type // Define the attributes here static PyMemberDef myClass_members[] = { {"name", T_OBJECT_EX, offsetof(myClass, name), 0, "our name"}, {"timeStamp", T_OBJECT_EX, offsetof(myClass, timeStamp), 0, "timestamp"}, {NULL} /* Sentinel */ }; // Define the non-standard methods here static PyMethodDef myClass_methods[] = { {"incTimestamp", (PyCFunction)incTimestamp, METH_NOARGS, "Increments our timestamp" }, {NULL} /* Sentinel */ };

Making an extension (pyd) 4.Create a "dictionary" of functions and attributes for the new type static PyTypeObject MyClassType = { PyVarObject_HEAD_INIT(NULL, 0) "myClass", /* tp_name */ sizeof(myClass), /* tp_basicsize */ 0, /* tp_itemsize */ (destructor)my_dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_reserved */ 0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ 0, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ 0, /* tp_getattro */ 0, /* tp_setattro */

Making an extension (pyd) 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ "The coolest object ever", /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ myClass_methods, /* tp_methods */ myClass_members, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ (initproc)my_init, /* tp_init */ 0, /* tp_alloc */ Noddy_new, /* tp_new */ };

Making an Extension 5.Create a C function in the module (factorial) – I'll let you experiment with this 6.Create a C variable in the module – I'll let you experiment with this too…

Making an Extension 7.Make a structure containing the module static PyModuleDef my_module = { PyModuleDef_HEAD_INIT, "myCMod", "my awesome module", -1, // No extra memory needed ???, // module funcs. A pointer to a PyMethodDef structure NULL, NULL, NULL, NULL };

Making an Extension 8.Make the init function (exported in the dll). Note: make sure it's the same name as the dll so it's auto-imported. PyMODINIT_FUNC PyInit_mycmod(void) { PyObject* m; // Ideally we'd do this in step 4. Some compilers // complain, though, so more portable to do it here. MyClassType.tp_new = PyType_GenericNew; if (PyType_Ready(&MyClassType) < 0) return NULL; m = PyModule_Create(&my_module); if (m == NULL) return NULL; Py_INCREF(&MyClassType); PyModule_AddObject(m, "myCMod", (PyObject *)&MyClassType); return m; }