{ "info": { "author": "Fernando Trias", "author_email": "sub@trias.org", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent" ], "description": "# pyembedc: Python With Embedded C/C++\n\npyembedc enables Python source code to embed C/C++ snippets that seamlessly access and modify Python variables and call Python functions. \n\nhttp://github.com/ftrias/pyembedc\n\nVersion 1.23: 8 Sep 2018\n\nCopyright 2010-2018 by Fernando Trias\nSee LICENSE for MIT license terms of use.\n\n## PREREQUISITES\n\n* gcc (version 3 or higher) or clang\n* Python 2.5, 3 or higher\n\n## INSTALL\n\n```\npython setup.py install\n```\n\n## SAMPLE CODE\n\n```Python\nfrom pyembedc import C\nv = [5,6,7]\nvlen = len(v)\nvsum = 0;\nC(\"for(int i=0;i int`\n#### `pyembedc.inline_c(string) -> int`\n#### `pyembedc.inline_c_precompile(string) -> int`\n\nThese functions will compile `string` containing the C/C++ code or directives (see below) and then link dynamically and run the code.\n\n`string` is C/C++ code that can be used within a function. It can contain any valid C/C++ expression that your compiler will support. \n\nThe `C` function will automatically provide references to all local Python variables for use in your code to read or write as if they were basic types or arrays.\n\nThe `inline_c` and `inline_c_precompile` fucntion will not provide references to local Python variables and thus is faster and consumes less memory. \n\n**Note**: `inline_c_precompile` will compile the code only once at the beginning of the program to increase speed. The code must be given using the Python multi-line string format using three quotes '\"\"\"'. This is because the module will scan the source file and pick out the string. As a consequence,variable substitutions and other string operations will not work. For example, the following code will not work:\n```Python\nC(\"\"\"\nv=%s\n\"\"\" % (var1))\n```\n\n`string` must be valid C/C++ suitable for placment within a function.\n\n--------------\n\n#### `pyembedc.embed_c(string) -> cdll`\n\n#### `pyembedc.embed_c_precompile(string) -> cdll`\n\nThese functions are used to compile code but not execute immediately. They return a CDLL object (see the CDLL python module) that can be executed later.\n\nFor example:\n```\nrun_trial_lib = embed_c(\"\"\"\n int mult(int n1, int n2) {\n return n1*n2;\n }\n \"\"\")\n\ndef multiply(n1, n2):\n return mathlib_lib.mult(n1, n2)\n```\n\n---------------\n\n### DIRECTIVES\n\nDirectives are inserted into the C/C++ code. They provide hints about how to wrap the code. They enumerate variables, add post-exectution cleanup code, reference Python code and modify the return value.\n\n#### `IMPORT type [&]variable`\n\nIMPORT will allow access to a Python variable within the code. Usually, this is not required for local variables because they are imported automatically. But in the case of the function `inline_c_precompile` this is not the case, and you must import all variables using IMPORT. In the case of global or class variables, you must use IMPORT.\n\n`type` is any valid C type, such as `int`, `double`, `int[]`. In addition, `string` is used to access strings, which C will treat as a `const char *`.\n\n`type` can also be an array if the Python type is a tuple, list or array. In this case add `[]` to the end of the type, in the manner of C/C++. For example, in Python `a=(1,5,3)`, the IMPORT line would be `IMPORT int[] a`.\n\n`variable` is the Python variable to import. Normally, variables are imported by value, meaning that any changes in the C/C++ code are lost. \n\nHowever, if you add a `&` in front (the C/C++ symbol for reference), then the variables are imported by reference and any changes will be saved to the Python variables when the code finishes execution.\n\nFor example:\n````\nIMPORT int[] myarray\nIMPORT float &myfloat\n````\n\n#### `DEF return-type function [type1] [type2] [type3]...`\n\nAllow access to a Python function within the C/C++ code. `return-type` is any C/C++ type. At this time, only basic types are allowed. Arrays are not allowed. Instead of returning arrays arrays, you should instead modify local variables. `type1`, `type2`, etc. are the data types of parameters. \n\nFor example:\n```\nDEF double myround double\ndouble x = myround(5.5);\n```\n\n#### `GLOBAL code`\n\nPlaces the code in the global space. Usually, this is used to create a C/C++ global variable. These variables are available in the C/C++ code between calls to `C` or `inline_c_precomiple`. This is is necessary because embedded C/C++ code is placed within a function.\n\nFor example:\n```\nGLOBAL int lock_counter;\nvoid run() { lock_counter++; }\n```\n\n#### `RETURN type value`\n\nReturn a non-int value. Usually, return values for `C` or `inline_c_precompile` are of type `int`. If `return` is not specified in the C/C++ code, then \"return 0\" is assumed. If you want to return a different type rather than `int`, then instead of `return`, use `RETURN` and specify the type.\n\nFor example:\n```\ndouble myfunc() {\n double myvariable = 1.7\n RETURN double myvariable\n}\n```\n\n#### `POST code`\n\nRun code after executing the C/C++ snipet, writing back all the variables and the return value. Sometimes this is necessary wen your function needs to do some cleanup, such as when it allocates memory to return a string.\n\nFor example:\n```\nPOST free(mydata);\n```\n\n#### `CC command`\n\nUse `command` instead of `gcc` as compiler. The command must be suitable for passing into a shell. The file name is appended at the end of the command.\n\n## SPECIAL CONSIDERATIONS\n\nWhen using `embed_c_precompile` and `inline_c_precompile`, pyembedc will read in the source file before it is interpreted. It will look for the function name and the triple-quote `\"\"\"` sequence. It will also look for another triple-quote `\"\"\"` to end. The close parenthesis must be on the same line as the ending triple-quote `\"\"\"` (see examples above). This is because pyembedc uses line numbers to find the precompiled function at run time.\n\n## TESTING\n\nYou don't have to install before running the test:\n\n```Python\npython test.py\n```\n\nThe `test-coverage` script uses multiple python versions (2.5, 2.6, 3.1) and python-coverage to ensure most source lines are covered by the test suite.\n\nThis module has been tested on:\n\n* Windows 7 with Cygwin Python 2.5 and GCC (mixed results, see Notes)\n* Windows 7 with official Python 2.5 and 2.6 and MinGW\n* Ubuntu (Debian Linux 2.6 kernel), Python 2.5, 2.6 and 3.1 and gcc 4.4.1\n* MacOS (High Sierra 10.13), Python 3.5.5, Xcode 9 with command line tools\n\n## TODO\n\n* Allow additional compilers; allow passing GCC command line options.\n\n* Support arrays of strings.\n\n* Support returning strings from Python functions called from within C/C++ (modifying string variables is supported).\n\n* With non-precompiled code, keep a cache of the source code so if it doesn't change, we can just reuse the DLL/so.\n\n\n## REFERENCES AND NOTES\n\npyembedc makes heavy use of the ctypes package.\n\nThere are several alternatives to pyembedc. All of them have their pros and cons. Here are two:\n\n* PyInline is a module for inlining multiple languages in Python last updated in 2001 that predates ctypes. It seeks to copy Perl Inline .\n\n* ezpyinline is a fork of PyInline that provides a very simple interface for embedding C/C++ functions last updated in 2007.\n\nNeither PyInline nor ezpyinline hande arrays. I think C provides the greatest speed advantage when looping over large amounts of data and performing multiple calculations. Without arrays or the ability to return more than one number (PyInline and ezpyinline provide only one return value), that advantage is hard to realize. pyembedc fixes these shortcoming by using ctypes to allow C/C++ to access and modify arrays.\n\nThese modules also don't provide an easy mechanism for accessing and altering Python variables from within the C/C++ code. With them, to modify variables from C, you have to code in the Python API, which is neither easy nor straightforward. \n\nIn addition, they require a working python development environment. This can be complex on Linux if you are not using standard packages, and is also difficult in Windows. By using ctypes, pyembedc does not require a working Python build environment, thus simplifying deployment because there is no need to configure include paths, lib paths and so forth. The only requirement is a working compiler.\n\nPyRex is a Python-like language that mixes C and Python to create Python modules. This is a very interesting approach, but isn't really Python and requires a great deal of expertise to use.\n\n## KNOWN ISSUES:\n\nCygwin:\n Cygwin has numerous problems with the test script because it quickly runs out of resources. The test script covers too many scenarios loading too many different DLLs for Cygwin to handle.\n\n If you get an error like this on Windows:\n *** fatal error - unable to remap tmpE9X11V.cpp.dll to same address as parent(0x2B0000) != 0x3B0000\n python 5368 fork: child 1676 - died waiting for dll loading, errno 11\n Check out http://inamidst.com/eph/cygwin for a possible solution.\n\n Occasionally, this error might come up in the test.py script:\n OSError: [Errno 11] Resource temporarily unavailable\n This is due to the limitations of Cygwin.\n\nLocal Variables: Modifying local variables is tricky and required pyembedc to call internal Python API functions. It has been tested on Python 2.5, 2.6, and 3.1 but may break in future versions if the internal representation of frames and variables changes.\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/ftrias/pyembedc", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "pyembedc", "package_url": "https://pypi.org/project/pyembedc/", "platform": "", "project_url": "https://pypi.org/project/pyembedc/", "project_urls": { "Homepage": "http://github.com/ftrias/pyembedc" }, "release_url": "https://pypi.org/project/pyembedc/1.26/", "requires_dist": null, "requires_python": "", "summary": "Embedded C/C++ in Python Source", "version": "1.26" }, "last_serial": 4251809, "releases": { "1.20": [ { "comment_text": "", "digests": { "md5": "fa502d9e14717b0b1a8dcc70983e0aa3", "sha256": "28819bb9cb15870a407f9efd96ed680dc8b359f7fa405052a6d209c7943572fd" }, "downloads": -1, "filename": "pyembedc-1.20-py3-none-any.whl", "has_sig": false, "md5_digest": "fa502d9e14717b0b1a8dcc70983e0aa3", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 5094, "upload_time": "2018-09-07T13:38:34", "url": "https://files.pythonhosted.org/packages/74/a3/560e718b49403dc385a1386b62c4d1f936efc84c9a6199ba97b3e937dec3/pyembedc-1.20-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "3933a138e9aa30ad9530eea4fc0194bc", "sha256": "4e387ff52f20801171846ae1ff93285b3b3e942e8b3186c51f6591e0b3020aa7" }, "downloads": -1, "filename": "pyembedc-1.20.tar.gz", "has_sig": false, "md5_digest": "3933a138e9aa30ad9530eea4fc0194bc", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11182, "upload_time": "2018-09-07T12:41:11", "url": "https://files.pythonhosted.org/packages/5f/e1/a6588fdd7f633fe7aa44ee6982f49092220a4816973eef9ebe1005039f23/pyembedc-1.20.tar.gz" } ], "1.25": [ { "comment_text": "", "digests": { "md5": "f8cd49fa02259e891bb1d37ba2c243d1", "sha256": "57960fc2dde5dd3f72408eda0951b518d211b4c05c3878daadad073c96f361cc" }, "downloads": -1, "filename": "pyembedc-1.25-py3-none-any.whl", "has_sig": false, "md5_digest": "f8cd49fa02259e891bb1d37ba2c243d1", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 12162, "upload_time": "2018-09-08T15:22:17", "url": "https://files.pythonhosted.org/packages/8a/7d/bdfb4000a33f6d1231aca0a50f85ef6857b964228c869dd9378d99afeb16/pyembedc-1.25-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "cd089e259e66286a1f3388f1834b8fd8", "sha256": "d20f784ed14ecbc385b424fb0a5db1bb9a13d5b54b5451c373e8cc76d63456bc" }, "downloads": -1, "filename": "pyembedc-1.25.tar.gz", "has_sig": false, "md5_digest": "cd089e259e66286a1f3388f1834b8fd8", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 18054, "upload_time": "2018-09-08T15:22:18", "url": "https://files.pythonhosted.org/packages/d5/51/fd43ba8b85469745f3596f9d66a6791dc4ddb7c35fab21064549ab3b3b83/pyembedc-1.25.tar.gz" } ], "1.26": [ { "comment_text": "", "digests": { "md5": "7aaef3f6d698595620230042e747ec04", "sha256": "feadabe000c419040853d7e302cc9267debdeda5362fe2ce46ab9ca2adb046c7" }, "downloads": -1, "filename": "pyembedc-1.26-py3-none-any.whl", "has_sig": false, "md5_digest": "7aaef3f6d698595620230042e747ec04", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 12194, "upload_time": "2018-09-08T15:50:18", "url": "https://files.pythonhosted.org/packages/95/2d/e62eca06fee20472a64369317123b645e91e0c0e6dc718edd9a3a93cfed0/pyembedc-1.26-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "cf80f11e3183e7752f16b5de31bba3ef", "sha256": "c0afa6a694eb4dc3033aaa105549de3a3823f2eb4cc7e578b7a581b6ecb15ce0" }, "downloads": -1, "filename": "pyembedc-1.26.tar.gz", "has_sig": false, "md5_digest": "cf80f11e3183e7752f16b5de31bba3ef", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 18080, "upload_time": "2018-09-08T15:50:19", "url": "https://files.pythonhosted.org/packages/f5/4d/6b4e3fe62b398b9a3f502df69c8aed0337255109d4473788284f480355f9/pyembedc-1.26.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "7aaef3f6d698595620230042e747ec04", "sha256": "feadabe000c419040853d7e302cc9267debdeda5362fe2ce46ab9ca2adb046c7" }, "downloads": -1, "filename": "pyembedc-1.26-py3-none-any.whl", "has_sig": false, "md5_digest": "7aaef3f6d698595620230042e747ec04", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 12194, "upload_time": "2018-09-08T15:50:18", "url": "https://files.pythonhosted.org/packages/95/2d/e62eca06fee20472a64369317123b645e91e0c0e6dc718edd9a3a93cfed0/pyembedc-1.26-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "cf80f11e3183e7752f16b5de31bba3ef", "sha256": "c0afa6a694eb4dc3033aaa105549de3a3823f2eb4cc7e578b7a581b6ecb15ce0" }, "downloads": -1, "filename": "pyembedc-1.26.tar.gz", "has_sig": false, "md5_digest": "cf80f11e3183e7752f16b5de31bba3ef", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 18080, "upload_time": "2018-09-08T15:50:19", "url": "https://files.pythonhosted.org/packages/f5/4d/6b4e3fe62b398b9a3f502df69c8aed0337255109d4473788284f480355f9/pyembedc-1.26.tar.gz" } ] }