Python in PHP: Internals Jon Parise 2002 International PHP Conference Frankfurt, Germany November 6, 2002.

Slides:



Advertisements
Similar presentations
Build your own PHP Extension Hanoi PHP Day 2010 Bui Dinh Ngoc AiTi-Aptech - CAH Trưng đào to Lp trình viên Quc t AiTi-Aptech.
Advertisements

More about Ruby Maciej Mensfeld Presented by: Maciej Mensfeld More about Ruby dev.mensfeld.pl github.com/mensfeld.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Written by: Dr. JJ Shepherd
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Introduction to Java, OO and IDEs ICW Lecture 2 Errol Thompson Room 134.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Python in PHP: Applications Jon Parise 2002 International PHP Conference Frankfurt, Germany November 6, 2002.
Java Unit 9: Arrays Declaring and Processing Arrays.
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Python.
V Avon High School Tech Club Agenda Old Business –Delete Files New Business –Week 18 Topics: Intro to HTML/CSS: Questions? Summer Work Letter.
PHP Overview CS PHP PHP = PHP: Hypertext Preprocessor Server-side scripting language that may be embedded into HTML One goal is to get PHP files.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Extending Python with C (Part I – the Basics) June 2002 Brian Quinlan
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
C-Language Keywords(C99)
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
H3D API Training  Part 3.1: Python – Quick overview.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
SCRIPTING II/III References:
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Intro Python: Variables, Indexing, Numbers, Strings.
Documentation / References Python Full Documentation – Python Quick Reference –
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Built-in Data Structures in Python An Introduction.
Comments in PHP In PHP, we use // to make a singleline comment or /* and */ to make a large comment block. Comment is a part of your PHP code that will.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Variables, Expressions, and Statements
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke.
PHP vs. Python. Similarities are interpreted, high level languages with dynamic typing are Open Source are supported by large developer communities are.
Programming Paradigms By Tyler Smith. Event Driven Event driven paradigm means that the program executes code in reaction to events. The limitation of.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Written by: Dr. JJ Shepherd
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Higher Computing Science 2016 Prelim Revision. Topics to revise Computational Constructs parameter passing (value and reference, formal and actual) sub-programs/routines,
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Python Basics 본 자료는 다음의 웹 사이트를 정리 한 내용이니 참조 바랍니다. ythonBasics.
Python C API overview References:
IST 210: PHP Basics IST 210: Organization of Data IST2101.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Python C API overview References:
T. Meyer ROD Software Workshop July 2002 Scripting and Interpreted Languages Tom Meyer Iowa State University
EXTENDING PYTHON SCRIPTING I/III References:
Input, Output and Variables GCSE Computer Science – Python.
Objects as a programming concept
University of Central Florida COP 3330 Object Oriented Programming
Topics Introduction to File Input and Output
Introduction to Python
An Introduction to Java – Part I, language basics
COM-152: Computer Programming Types, Variables, Operators Part 1 of 2
Topics Introduction to File Input and Output
Presentation transcript:

Python in PHP: Internals Jon Parise 2002 International PHP Conference Frankfurt, Germany November 6, 2002

November 5, 2002Python in PHP: Internals2 About This Session  Some familiarity with PHP extensions is expected.  Python knowledge is not required, but familiarity will be helpful. Presentation of the internals of the Python in PHP extension

November 5, 2002Python in PHP: Internals3 About Me  Bachelor of Science in Information Technology from the Rochester Institute of Technology  Completing Masters of Entertainment Technology at Carnegie Mellon University  Software engineer at Maxis on The Sims Online  Long history of involvement with PHP, PEAR, and The Horde Project  Co-author of Professional PHP4 Programming  Long-time Pythonista!

November 5, 2002Python in PHP: Internals4 Ground Rules  Questions Ask for clarification at any time. Please save scope-expanding questions until the end.  Pacing Ask me to slow down if I move too quickly. I’m from New Jersey.

November 5, 2002Python in PHP: Internals5 Session Agenda  Overview  Extension architecture  Type conversions  Object handling  PHP Python Module  Next Steps  Questions

November 5, 2002Python in PHP: Internals6 Confessions I am not an expert on PHP internals. I am not an expert on Python internals. I just read a lot of code and documentation.

November 5, 2002Python in PHP: Internals7 What Is The Python Extension?  Embedded Python interpreter  Interface handled by PHP extension  Python-to-PHP object proxy  Handles type conversions  Exposes PHP environment to Python

November 5, 2002Python in PHP: Internals8 PHP Extension Architecture

November 5, 2002Python in PHP: Internals9 Python Extension Architecture

November 5, 2002Python in PHP: Internals10 How It Happens 1.PHP starts and initializes the Python extension. 2.The Python extension initializes the Python interpreter. 3.Python-related operations are performed in PHP. 4.PHP shuts down the Python extension, which cleans up the Python interpreter.

November 5, 2002Python in PHP: Internals11 Executing Python Code Python: print "Hello, Frankfurt!" PHP: echo py_eval('print "Hello, Frankfurt!"'); Output: Hello, Frankfurt!

November 5, 2002Python in PHP: Internals12 Executing More Python Code Python: fruits = ['apples', 'oranges', 'pears'] for fruit in fruits: print fruit PHP: $code = <<<END fruits = ['apples', 'oranges', 'pears'] for fruit in fruits: print fruit END; py_eval($code);

November 5, 2002Python in PHP: Internals13 How It Works 1.Extension initialization 2.Python initialization 3.Python code execution 4.Extension shutdown 5.Python shutdown  PHP_MINIT_FUNCTION  Py_Initialize()  PyRun_SimpleString()  PHP_MSHUTDOWN_FUNCTION  Py_Finalize()

November 5, 2002Python in PHP: Internals14 py_eval()  Executes a string of Python code  Uses PyRun_SimpleString()  Only returns success or failure  Always executes in the same Python environment py_eval('where = "Frankfurt"'); py_eval('print "Hello, " + where');

November 5, 2002Python in PHP: Internals15 Calling Python Functions Python: import math print math.cos(0) PHP: echo py_call('math', 'cos', array(0)); Output: 1

November 5, 2002Python in PHP: Internals16 py_call()  Calls a function of a module  Uses PyObject_CallObject()  Implicitly imports the module  Allows parameter passing  Returns the result of the function call echo py_call('math', 'cos', array(0));

November 5, 2002Python in PHP: Internals17 PHP to Python Type Conversion PHP  Boolean  Long (Integer)  Double (Float)  String  Null Python  Integer  Long  Double  String  None

November 5, 2002Python in PHP: Internals18 Python to PHP Type Conversion Python  Integer  Long  Float  String  None PHP  Long  Double  String  NULL

November 5, 2002Python in PHP: Internals19 Arrays, Sequences & Mappings  PHP only has hashes, indexed by: Numbers: array(1, 2) Strings: array('one' => 1, 'two' => 2)  Python has sequences: Tuples: (1, 2) Lists: [1, 2]  And mappings: Dictionaries: {'one': 1, 'two': 2}

November 5, 2002Python in PHP: Internals20 Array Conversions  PHP arrays (hashes) are always converted to Python dictionaries.  Results in no data loss. PHP: $a = array(1, 2); $b = array('one' => 1, 'two' => 2); Python: a = {'1': 1, '2': 2} b = {'one': 1, 'two' : 2}

November 5, 2002Python in PHP: Internals21 Array Conversion Exceptions  Arrays of arguments are always passed as a tuple.  String keys are discarded. PHP: py_call('math', 'cos', array(0)); py_call('math', 'cos', array('zero' => 0));

November 5, 2002Python in PHP: Internals22 Sequence Conversions  Python tuples and lists are always converted to PHP arrays.  The numerical indices are preserved. Python: a = (1, 2) b = [1, 2] PHP: $a = array(1, 2); $b = array(1, 2);

November 5, 2002Python in PHP: Internals23 Mapping Conversions  Python dictionaries are always converted to PHP associative arrays.  Keys will be converted to strings. Python: a = {'one': 1, 'two': 2} b = {0.123: 1, 0.456: 2} PHP: $a = array('one' => 1, 'two' => 2); $b = array('0.123' => 1, '0.456' => 2);

November 5, 2002Python in PHP: Internals24 About Python Objects  The Python extension proxies Python objects  Python objects are represented as instances of a "python" class in PHP PHP: object(python)(1) { [0]=> int(4) }

November 5, 2002Python in PHP: Internals25 Creating Python Objects  Python objects are creating using the Python() object constructor Python (test.py): class TestClass: def __init__(self, s): print 'TestClass:', s PHP: $test = new Python('test', 'TestClass', array('Test Argument'));

November 5, 2002Python in PHP: Internals26 Manipulating Python Objects  Python objects work like PHP objects Python (test.py): class TestClass: def __init__(self): self.name = 'Testing' def get_name(self): return self.name PHP: $test = new Python('test', 'TestClass'); echo $test->name; echo $test->get_name();

November 5, 2002Python in PHP: Internals27 Python Class Internals static int le_pyobject = 0; static zend_class_entry python_class_entry; INIT_OVERLOADED_CLASS_ENTRY( python_class_entry, /* Class container */ "python", /* Class name */ NULL, /* Functions */ python_call_function_handler, /* Function call handler */ python_get_property_handler, /* Get property handler */ python_set_property_handler); /* Set property handler */ zend_register_internal_class(&python_class_entry TSRMLS_CC); le_pyobject = zend_register_list_destructors_ex( python_destructor, NULL, "python", module_number);

November 5, 2002Python in PHP: Internals28 Storing Python Objects  Once created, Python objects are stored in the engine symbol hash ALLOC_ZVAL(handle); ZVAL_LONG(handle, zend_list_insert(obj, le_pyobject)); pval_copy_constructor(handle); INIT_PZVAL(handle); zend_hash_index_update(Z_OBJPROP_P(object), 0, &handle, sizeof(pval *), NULL);

November 5, 2002Python in PHP: Internals29 Retrieving Python Objects  Python objects are retrieved by their handle pval *object = property_reference->object; PyObject *obj; pval **handle; int type; zend_hash_index_find(Z_OBJPROP_P(object), 0, (void **) &handle); obj = (PyObject *) zend_list_find(Z_LVAL_PP(handle), &type); if (type == le_pyobject) {

November 5, 2002Python in PHP: Internals30 Handling Method Calls If the method name is 'python': 1.Import the requested module 2.Construct a new Python object 3.Register and return the new object Else: 1.Retrieve the Python object handle 2.Look for the requested method 3.Call the method with any arguments 4.Convert and return the result

November 5, 2002Python in PHP: Internals31 The Case-Sensitivity Problem  PHP converts all function and method calls to lowercase internally You type: $test->GetSomeValue() PHP sees: $test->getsomevalue()  Python is case-sensitive, making it impossible to call any function or method with capital letters from PHP!

November 5, 2002Python in PHP: Internals32 The Case-Sensitivity Solution  Build a map of Python object methods! PyObject *dir = PyObject_Dir(obj) PyObject *map = PyDict_New(); for (i = 0; i < PyList_Size(dir); i++) { item = PyList_GetItem(dir, i); key = estrdup(PyString_AsString(item)); key_len = PyString_Size(item); PyDict_SetItemString(map, php_strtolower(key, key_len), item); efree(key); } Py_DECREF(dir);

November 5, 2002Python in PHP: Internals33 Handling Object Attributes  Both "get" and "set" operations call the same attribute handler 1.Retrieve the requested Python object 2.Find the named attribute 3.Convert and return its value Note: No case-sensitivity hacks necessary here!

November 5, 2002Python in PHP: Internals34 The PHP Python Module  Allows access to the PHP environment from within the embedded Python environment  Functionality is still very limited! PHP: $test = 'This is a test'; Python: import php print php.var('test')

November 5, 2002Python in PHP: Internals35 php.var() Implementation static PyObject * py_php_var(PyObject *self, PyObject *args) { char *name; zval **data; TSRMLS_FETCH(); if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } if (zend_hash_find(&EG(symbol_table), name, strlen(name) + 1, (void **) &data) != SUCCESS) { return NULL; } return convert_zval_to_pyobject(data); }

November 5, 2002Python in PHP: Internals36 Building the Python Extension $ cd pear/PECL/python $ pear build running: phpize PHP Api Version : Zend Module Api No : Zend Extension Api No : Python installation directory? [autodetect] : building in /var/tmp/pear-build-jon/python-0.1 running: /home/jon/src/pear/PECL/python/configure --with-python running: make python.so copied to /home/jon/src/pear/PECL/python/python.so

November 5, 2002Python in PHP: Internals37 Next Steps  Extending Python objects from PHP  Exposing PHP objects to Python  More namespace sharing Global and local variables  Multiple Python interpreters  Better threading support  Fix bugs

November 5, 2002Python in PHP: Internals38 Questions

November 5, 2002Python in PHP: Internals39 References Presentation Slides Python in PHP Python