ShlubluLib  v0.5
ShlubluLib is a lightweight, modular, general purpose, open-source C++ library for Linux and Windows.
Python.h
Go to the documentation of this file.
1 #pragma once
2 
10 #include <string>
11 #include <vector>
12 
15 
16 
17 namespace shlublu
18 {
19 
175 namespace Python
176 {
177  using PathEntriesList = std::vector<std::string>;
178  using ObjectHandlersList = std::vector<ObjectHandler>;
180  using ObjectPointer = PyObject*;
182  using RawCode = std::string;
183  using Program = std::vector<RawCode>;
185  extern const std::string moduleMain;
186  extern const std::string moduleBuiltins;
194 
206  void init(std::string const& programName, PathEntriesList const& pythonSysPath = PathEntriesList());
207 
218  void shutdown();
219 
231  void execute(RawCode const& code);
232 
245  void execute(Program const& program);
246 
247 
263  ObjectPointer import(std::string const& moduleName);
264 
265 
283  ObjectPointer module(std::string const& moduleName);
284 
285 
307  ObjectHandler const& object(ObjectPointer scope, std::string const& objectName);
308 
309 
328  ObjectHandler const& object(std::string const& moduleName, std::string const& objectName);
329 
352  ObjectPointer callable(ObjectPointer scope, std::string const& callableName, bool forceReload = false);
353 
376  ObjectPointer callable(std::string const& moduleName, std::string const& callableName, bool forceReload = false);
377 
402  ObjectHandler const& call(ObjectPointer callableObject, ObjectHandlersList const& args = {}, bool keepArguments = false);
403 
434  ObjectHandler tuple(ObjectHandlersList const& args = {}, bool keepArguments = false);
435 
466  ObjectHandler list(ObjectHandlersList const& args = {}, bool keepArguments = false);
467 
496  void addList(ObjectHandler list, ObjectHandler item, bool keepArg = false);
497 
517  ObjectHandler fromAscii(std::string const& str);
518 
537  std::string toAscii(ObjectHandler utfStr, bool keepArg = false);
538 
569  ObjectHandler const& keepArgument(ObjectHandler const& object);
570 
599  ObjectHandler const& controlArgument(ObjectHandler object); // Give Python the ownership of the passed objet created from outside without changing its references count. Object should not be controlled already.
600 
630  void forgetArgument(ObjectHandler const& object); // Forgets an object under control after having decreased its references count
631 
709 
710 
719 }
720 
721 }
shlublu::Python::keepArgument
ObjectHandler const & keepArgument(ObjectHandler const &object)
Prevents an object reference from being stolen.
shlublu::Python::ObjectHandler
Handler of CPython object pointers used by Python to handle references counts.
Definition: Python_ObjectHandler.h:71
shlublu::Python::module
ObjectPointer module(std::string const &moduleName)
Retrieves a previously imported module.
shlublu::Python::forgetArgument
void forgetArgument(ObjectHandler const &object)
Get rid of a reference of an object under control.
shlublu::Python::list
ObjectHandler list(ObjectHandlersList const &args={}, bool keepArguments=false)
Creates a list object initialized with the given arguments.
shlublu::Python::endCriticalSection
void endCriticalSection()
Exits a critical section, allowing other threads to enter a Python critical section.
shlublu::Python::callable
ObjectPointer callable(ObjectPointer scope, std::string const &callableName, bool forceReload=false)
Retrieves a callable object (function or method) by its name from a scope pointer.
shlublu::Python::isInitialized
bool isInitialized()
Check whether Python is initialized.
shlublu::Python::toAscii
std::string toAscii(ObjectHandler utfStr, bool keepArg=false)
Converts a UTF-8 CPython string object to a std::string.
shlublu::Python::ObjectHandlersList
std::vector< ObjectHandler > ObjectHandlersList
Parameters list to pass to call() or to functions that create collections.
Definition: Python.h:178
shlublu::Python::ObjectPointer
PyObject * ObjectPointer
Pointer to scope objects (either imported modules or instances of a class) or callable objects (funct...
Definition: Python.h:180
shlublu
shlublu::Python::beginCriticalSection
void beginCriticalSection()
Enters a critical section, preventing other threads from entering any Python critical section.
shlublu::Python::addList
void addList(ObjectHandler list, ObjectHandler item, bool keepArg=false)
Adds an item to the end of a list.
shlublu::Python::object
ObjectHandler const & object(ObjectPointer scope, std::string const &objectName)
Retrieves an object by its name from a scope pointer.
shlublu::Python::Program
std::vector< RawCode > Program
Complete program.
Definition: Python.h:183
shlublu::Python::RawCode
std::string RawCode
Plain Python code.
Definition: Python.h:182
shlublu::Python::fromAscii
ObjectHandler fromAscii(std::string const &str)
Converts a string to a UTF-8 CPython string object.
shlublu::Python::PathEntriesList
std::vector< std::string > PathEntriesList
Path, as a vector of strings.
Definition: Python.h:177
Python_BindingExceptions.h
shlublu::Python::moduleBuiltins
const std::string moduleBuiltins
Built-ins module ("builtins").
Python_ObjectHandler.h
shlublu::Python::controlArgument
ObjectHandler const & controlArgument(ObjectHandler object)
Places an CPython object under control of Python.
shlublu::Python::execute
void execute(RawCode const &code)
Executes raw code.
shlublu::Python::moduleMain
const std::string moduleMain
Main module ("__main__").
shlublu::Python::init
void init(std::string const &programName, PathEntriesList const &pythonSysPath=PathEntriesList())
Initializes Python.
shlublu::Python::call
ObjectHandler const & call(ObjectPointer callableObject, ObjectHandlersList const &args={}, bool keepArguments=false)
Calls a callable with the given arguments.
shlublu::Python::shutdown
void shutdown()
Shuts down Python.
shlublu::Python::tuple
ObjectHandler tuple(ObjectHandlersList const &args={}, bool keepArguments=false)
Creates a tuple object initialized with the given arguments.