diff --git a/bazaar/PyCon/PyCon.cpp b/bazaar/PyCon/PyCon.cpp new file mode 100644 index 000000000..b768a00b8 --- /dev/null +++ b/bazaar/PyCon/PyCon.cpp @@ -0,0 +1,65 @@ +#include "PyCon.h" + +PyObject* PyCon::ToStdOut(PyObject* self, PyObject* pArgs) +{ + char* ps = NULL; + if(!PyArg_ParseTuple(pArgs, "s", &ps)) + return NULL; + + String s(ps); + GetStream().Put(ps); + + Py_INCREF(Py_None); + return Py_None; +} + +PyObject* PyCon::ToStdErr(PyObject* self, PyObject* pArgs) +{ + char* ps = NULL; + if(!PyArg_ParseTuple(pArgs, "s", &ps)) + return NULL; + + String s(ps); + GetStream().Put(ps); + + Py_INCREF(Py_None); + return Py_None; +} + +PyMethodDef PyCon::m[] = +{ + { "ToStdOut", ToStdOut, METH_VARARGS, "writes to stdout" }, + { "ToStdErr", ToStdErr, METH_VARARGS, "writes to stderr" }, + { NULL, NULL, 0, NULL } +}; + +bool PyCon::enabled = false; +Stream* PyCon::ps = NULL; + +void PyCon::Init() +{ + Py_InitModule3("log", m, "Logs stdout and stdin"); + PyRun_SimpleString ( + "import sys\n" + "import log\n" + "class StdOutIncept:\n" + "\tdef write(self, str):\n" + "\t\tlog.ToStdOut(str)\n" + "class StdErrIncept:\n" + "\tdef write(self, str):\n" + "\t\tlog.ToStderr(str)\n" + ); + Enable(); +} + +void PyCon::Enable(bool b) +{ + if(enabled = b) + PyRun_SimpleString ( + "sys.stdout = StdOutIncept()\n" + "sys.stderr = StdErrIncept()\n" ); + else + PyRun_SimpleString ( + "sys.stdout = sys.__stdout__\n" + "sys.stderr = sys.__stderr__\n" ); +} diff --git a/bazaar/PyCon/PyCon.h b/bazaar/PyCon/PyCon.h new file mode 100644 index 000000000..665e8abc3 --- /dev/null +++ b/bazaar/PyCon/PyCon.h @@ -0,0 +1,35 @@ +#ifndef _PyCon_PyCon_h_ +#define _PyCon_PyCon_h_ + +#include +using namespace Upp; + +#include +#include + +void InitLogger(); + +class PyCon +{ +public: + static PyObject* ToStdOut(PyObject* self, PyObject* pArgs); + static PyObject* ToStdErr(PyObject* self, PyObject* pArgs); + + static void Init(); + static void Enable(bool b = true); + static void Disable() { Enable(false); } + static bool IsEnabled() { return enabled; } + + static void SetStream(Stream& _ps) { ps = &_ps; } + static Stream& GetStream() { return (ps)?(*ps):(StdLog()); } + +protected: + static PyMethodDef m[]; + static bool enabled; + static Stream* ps; +}; + + + + +#endif diff --git a/bazaar/PyCon/PyCon.upp b/bazaar/PyCon/PyCon.upp new file mode 100644 index 000000000..7f822e3c8 --- /dev/null +++ b/bazaar/PyCon/PyCon.upp @@ -0,0 +1,9 @@ +uses + Core, + Python, + LogCtrl; + +file + PyCon.h, + PyCon.cpp; + diff --git a/bazaar/PyCon/init b/bazaar/PyCon/init new file mode 100644 index 000000000..e55689c19 --- /dev/null +++ b/bazaar/PyCon/init @@ -0,0 +1,6 @@ +#ifndef _PyCon_icpp_init_stub +#define _PyCon_icpp_init_stub +#include "Core/init" +#include "Python/init" +#include "LogCtrl/init" +#endif diff --git a/bazaar/PyConsoleCtrl/PyConsoleCtrl.cpp b/bazaar/PyConsoleCtrl/PyConsoleCtrl.cpp new file mode 100644 index 000000000..9740afcbb --- /dev/null +++ b/bazaar/PyConsoleCtrl/PyConsoleCtrl.cpp @@ -0,0 +1,61 @@ +#include "PyConsoleCtrl.h" + +void PyConsoleCtrl::LoadDlg() +{ + fs.Types("Python Files\t*.py\nAll Files\t*.*\n"); + if(!fs.ExecuteOpen("Load a Python script")) return; + + FileIn in(fs.Get()); + cmd.Load(in); +} + +void PyConsoleCtrl::SaveDlg() +{ + fs.Types("Python Files\t*.py\nAll Files\t*.*\n"); + if(!fs.ExecuteSaveAs("Save a Python script")) return; + + FileOut out(fs.Get()); + cmd.Save(out); +} + +void PyConsoleCtrl::Clear() +{ + cmd.Clear(); +} + +void PyConsoleCtrl::Exec() +{ + String c = cmd.GetData(); + if(c.IsEmpty()) return; + + int ret = PyRun_SimpleString(c); + if(ret == 0 && clonex.Get()) + cmd.Clear(); + + ex.Clear(); + ex <<= ret; +} + +PyConsoleCtrl::PyConsoleCtrl() +{ + CtrlLayout(*this); + + spl.Vert(log, cmd); + spl.SetPos(6600); + Add(spl.HSizePos().VSizePos(0,20)); + + load <<= THISBACK(LoadDlg); + save <<= THISBACK(SaveDlg); + clear <<= THISBACK(Clear); + exec <<= THISBACK(Exec); + cmd.WhenCmdDone = THISBACK(Exec); + + savelog <<= THISBACK(SaveLog); + clearlog <<= THISBACK(ClearLog); + + clonex.Set(1); + + PyCon::Init(); + PyCon::SetStream(log); +} + diff --git a/bazaar/PyConsoleCtrl/PyConsoleCtrl.h b/bazaar/PyConsoleCtrl/PyConsoleCtrl.h new file mode 100644 index 000000000..fa8c23cca --- /dev/null +++ b/bazaar/PyConsoleCtrl/PyConsoleCtrl.h @@ -0,0 +1,57 @@ +#ifndef _PyConsoleCtrl_PyConsoleCtrl_h +#define _PyConsoleCtrl_PyConsoleCtrl_h + +#include +using namespace Upp; + +#include + +#define LAYOUTFILE +#include + +class CmdDocEdit : public DocEdit +{ +public: + typedef CmdDocEdit CLASSNAME; + typedef DocEdit D; + + CmdDocEdit() + { + ProcessEnter().ProcessTab(); + } + + virtual bool Key(dword key, int count) + { + bool b = true; + if(key == K_CTRL_ENTER) + WhenCmdDone(); + else b = D::Key(key, count); + return b; + } + + Callback WhenCmdDone; +}; + +class PyConsoleCtrl : public WithPyConsoleCtrlLay +{ +public: + typedef PyConsoleCtrl CLASSNAME; + PyConsoleCtrl(); + + void LoadDlg(); + void SaveDlg(); + void Exec(); + void Clear(); + + void ClearLog() { log.Clear(); } + void SaveLog() { log.Save(); } + + LoggerCtrl log; + CmdDocEdit cmd; + +protected: + FileSel fs; +}; + +#endif + diff --git a/bazaar/PyConsoleCtrl/PyConsoleCtrl.lay b/bazaar/PyConsoleCtrl/PyConsoleCtrl.lay new file mode 100644 index 000000000..780ec7ab5 --- /dev/null +++ b/bazaar/PyConsoleCtrl/PyConsoleCtrl.lay @@ -0,0 +1,13 @@ +LAYOUT(PyConsoleCtrlLay, 600, 400) + ITEM(Button, exec, SetLabel(t_("Exec")).RightPosZ(0, 56).BottomPosZ(0, 20)) + ITEM(Button, load, SetLabel(t_("Load")).LeftPosZ(0, 56).BottomPosZ(0, 20)) + ITEM(Button, save, SetLabel(t_("Save")).LeftPosZ(56, 56).BottomPosZ(0, 20)) + ITEM(Button, clear, SetLabel(t_("Clear")).LeftPosZ(112, 56).BottomPosZ(0, 20)) + ITEM(Button, savelog, SetLabel(t_("Save Log")).LeftPosZ(196, 56).BottomPosZ(0, 20)) + ITEM(Button, clearlog, SetLabel(t_("Clear Log")).LeftPosZ(252, 56).BottomPosZ(0, 20)) + ITEM(EditString, ex, SetEditable(false).WantFocus(false).RightPosZ(56, 56).BottomPosZ(1, 19)) + ITEM(StaticText, dv___7, SetText(t_("exit:")).SetAlign(ALIGN_RIGHT).RightPosZ(112, 24).BottomPosZ(0, 20)) + ITEM(Splitter, spl, LeftPosZ(0, 600).TopPosZ(0, 380)) + ITEM(Option, clonex, SetLabel(t_("Clear On Exec")).LeftPosZ(308, 88).BottomPosZ(4, 16)) +END_LAYOUT + diff --git a/bazaar/PyConsoleCtrl/PyConsoleCtrl.upp b/bazaar/PyConsoleCtrl/PyConsoleCtrl.upp new file mode 100644 index 000000000..8f0eeec5b --- /dev/null +++ b/bazaar/PyConsoleCtrl/PyConsoleCtrl.upp @@ -0,0 +1,9 @@ +uses + CtrlLib, + PyCon; + +file + PyConsoleCtrl.h, + PyConsoleCtrl.cpp, + PyConsoleCtrl.lay; + diff --git a/bazaar/PyConsoleCtrl/init b/bazaar/PyConsoleCtrl/init new file mode 100644 index 000000000..62c6366fa --- /dev/null +++ b/bazaar/PyConsoleCtrl/init @@ -0,0 +1,5 @@ +#ifndef _PyConsoleCtrl_icpp_init_stub +#define _PyConsoleCtrl_icpp_init_stub +#include "CtrlLib/init" +#include "PyCon/init" +#endif diff --git a/bazaar/PyConsoleCtrlTest/PyConsoleCtrlTest.h b/bazaar/PyConsoleCtrlTest/PyConsoleCtrlTest.h new file mode 100644 index 000000000..ca952479f --- /dev/null +++ b/bazaar/PyConsoleCtrlTest/PyConsoleCtrlTest.h @@ -0,0 +1,20 @@ +#ifndef _PyConsoleCtrlTest_PyConsoleCtrlTest_h +#define _PyConsoleCtrlTest_PyConsoleCtrlTest_h + +#include + +using namespace Upp; + +#include + +#define LAYOUTFILE +#include + +class PyConsoleCtrlTest : public WithPyConsoleCtrlTestLayout { +public: + typedef PyConsoleCtrlTest CLASSNAME; + PyConsoleCtrlTest(); +}; + +#endif + diff --git a/bazaar/PyConsoleCtrlTest/PyConsoleCtrlTest.lay b/bazaar/PyConsoleCtrlTest/PyConsoleCtrlTest.lay new file mode 100644 index 000000000..9b8e8520c --- /dev/null +++ b/bazaar/PyConsoleCtrlTest/PyConsoleCtrlTest.lay @@ -0,0 +1,4 @@ +LAYOUT(PyConsoleCtrlTestLayout, 640, 480) + ITEM(PyConsoleCtrl, dv___0, HSizePosZ(0, 0).VSizePosZ(0, 0)) +END_LAYOUT + diff --git a/bazaar/PyConsoleCtrlTest/PyConsoleCtrlTest.upp b/bazaar/PyConsoleCtrlTest/PyConsoleCtrlTest.upp new file mode 100644 index 000000000..6db78ad7b --- /dev/null +++ b/bazaar/PyConsoleCtrlTest/PyConsoleCtrlTest.upp @@ -0,0 +1,12 @@ +uses + CtrlLib, + PyConsoleCtrl; + +file + PyConsoleCtrlTest.h, + main.cpp, + PyConsoleCtrlTest.lay; + +mainconfig + "" = "GUI MT"; + diff --git a/bazaar/PyConsoleCtrlTest/main.cpp b/bazaar/PyConsoleCtrlTest/main.cpp new file mode 100644 index 000000000..95c16ddf3 --- /dev/null +++ b/bazaar/PyConsoleCtrlTest/main.cpp @@ -0,0 +1,13 @@ +#include "PyConsoleCtrlTest.h" + +PyConsoleCtrlTest::PyConsoleCtrlTest() +{ + CtrlLayout(*this, "Window title"); + Sizeable().Zoomable(); +} + +GUI_APP_MAIN +{ + PyConsoleCtrlTest().Run(); +} + diff --git a/bazaar/PyShell/PyShell.cpp b/bazaar/PyShell/PyShell.cpp new file mode 100644 index 000000000..33cf64ee5 --- /dev/null +++ b/bazaar/PyShell/PyShell.cpp @@ -0,0 +1,34 @@ +#include "PyShell.h" + +// Arkon, 09 July 2004 RageStorm +// http://ragestorm.net +// Feel free to do with this whatever you want! +// Thanks goes to Sagiv Malihi for this great idea and help. +//http://www.ragestorm.net/tutorial?id=21 +//http://www.ragestorm.net/sample?id=79 + +void PyShell::Proc() +{ + /* + The following code is what we run below, it will open PyShell using TkInter. + it runs blocking + */ + AtomicInc(a); + int ret = PyRun_SimpleString( + "from Tkinter import Tk\n" + "from idlelib.PyShell import PyShell, PyShellFileList, fixwordbreaks\n" + "import idlelib.PyShell\n" + "\n" + "idlelib.PyShell.use_subprocess = False\n" + "root = Tk()\n" + "fixwordbreaks(root)\n" + "flist = PyShellFileList(root)\n" + "flist.pyshell = PyShell(flist)\n" + "\n" + "root.withdraw()\n" + "flist.pyshell.begin()\n" + "root.mainloop()\n" + "root.destroy()\n" + ); + AtomicDec(a); +} diff --git a/bazaar/PyShell/PyShell.h b/bazaar/PyShell/PyShell.h new file mode 100644 index 000000000..1c704cb9f --- /dev/null +++ b/bazaar/PyShell/PyShell.h @@ -0,0 +1,33 @@ +#ifndef _PyShell_PyShell_h +#define _PyShell_PyShell_h + +#include + +#include +using namespace Upp; + +//ONLY WORKS IN OPTIMAL, DEBUG needs TkTcl as DEBUG???? + +class PyShell +{ +public: + typedef PyShell CLASSNAME; + PyShell() : a(0) {} + + void Start() + { + if(IsRunning()) return; + Wait(); + if(!t.IsOpen()) t.Run(THISBACK(Proc)); + } + void Wait() { t.Wait(); } + bool IsRunning() const { return AtomicRead(a>0); } + + void Proc(); + +protected: + Atomic a; + Thread t; +}; + +#endif diff --git a/bazaar/PyShell/PyShell.upp b/bazaar/PyShell/PyShell.upp new file mode 100644 index 000000000..81bd2cf50 --- /dev/null +++ b/bazaar/PyShell/PyShell.upp @@ -0,0 +1,8 @@ +uses + CtrlLib, + Python; + +file + PyShell.h, + PyShell.cpp; + diff --git a/bazaar/PyShell/init b/bazaar/PyShell/init new file mode 100644 index 000000000..381817e64 --- /dev/null +++ b/bazaar/PyShell/init @@ -0,0 +1,5 @@ +#ifndef _PyShell_icpp_init_stub +#define _PyShell_icpp_init_stub +#include "CtrlLib/init" +#include "Python/init" +#endif diff --git a/bazaar/PyShellTest/PyShellTest.h b/bazaar/PyShellTest/PyShellTest.h new file mode 100644 index 000000000..d01f272fe --- /dev/null +++ b/bazaar/PyShellTest/PyShellTest.h @@ -0,0 +1,26 @@ +#ifndef _PyShellTest_PyShellTest_h +#define _PyShellTest_PyShellTest_h + +#include + +using namespace Upp; + +#define LAYOUTFILE +#include + +#include + +class PyShellTest : public WithPyShellTestLayout { +public: + typedef PyShellTest CLASSNAME; + PyShellTest(); + ~PyShellTest(); + + void StartNew(); + +protected: + PyShell ps; +}; + +#endif + diff --git a/bazaar/PyShellTest/PyShellTest.lay b/bazaar/PyShellTest/PyShellTest.lay new file mode 100644 index 000000000..d9f88e332 --- /dev/null +++ b/bazaar/PyShellTest/PyShellTest.lay @@ -0,0 +1,4 @@ +LAYOUT(PyShellTestLayout, 308, 164) + ITEM(Button, b, SetLabel(t_("Start Shell")).LeftPosZ(20, 56).TopPosZ(16, 15)) +END_LAYOUT + diff --git a/bazaar/PyShellTest/PyShellTest.upp b/bazaar/PyShellTest/PyShellTest.upp new file mode 100644 index 000000000..b78d2db5e --- /dev/null +++ b/bazaar/PyShellTest/PyShellTest.upp @@ -0,0 +1,12 @@ +uses + CtrlLib, + PyShell; + +file + PyShellTest.h, + main.cpp, + PyShellTest.lay; + +mainconfig + "" = "GUI MT SSE2"; + diff --git a/bazaar/PyShellTest/init b/bazaar/PyShellTest/init new file mode 100644 index 000000000..db158e0db --- /dev/null +++ b/bazaar/PyShellTest/init @@ -0,0 +1,5 @@ +#ifndef _PyShellTest_icpp_init_stub +#define _PyShellTest_icpp_init_stub +#include "CtrlLib/init" +#include "PyShell/init" +#endif diff --git a/bazaar/PyShellTest/main.cpp b/bazaar/PyShellTest/main.cpp new file mode 100644 index 000000000..c8ec42e24 --- /dev/null +++ b/bazaar/PyShellTest/main.cpp @@ -0,0 +1,57 @@ +#include "PyShellTest.h" + +// When PyShell is opened write: import newmodule newmodule.test("is it working?") +PyObject* newmodule_test(PyObject* pSelf, PyObject* pArgs) +{ + char* str = NULL; + if (!PyArg_ParseTuple(pArgs, "s", &str)) return NULL; + PromptOK("exported function"); + Py_INCREF(Py_None); + return Py_None; +} + +static PyMethodDef newmoduleMethods[] = { + {"test", newmodule_test, METH_VARARGS, "Exported function testing."}, + {NULL, NULL, 0, NULL} +}; + +void PyShellTest::StartNew() +{ + ps.Start(); + + // You could do whatever you want, usually your message loop will be here. + PromptOK("I am blocking, pyshell is working from the other thread"); +} + +PyShellTest::PyShellTest() +{ + CtrlLayout(*this, "PyShell Test"); + b <<= THISBACK(StartNew); +} + +PyShellTest::~PyShellTest() +{ + ps.Wait(); +} + +GUI_APP_MAIN +{ + char *argv[] = {"", NULL}; + PySys_SetArgv(1, argv); + PyRun_SimpleString("import sys; sys.path.pop(0)\n"); + + PyObject* pyRet = PyString_FromString(">>> "); + PySys_SetObject("ps1", pyRet); + Py_XDECREF(pyRet); + // ps2 is not used in PyShell, to be honset it's more comfortable, think of it + // that you want to copy/paste the code and you have "..." in every line then you have to + // remove it manually. + +#if 0 + // Init our newmodule. + Py_InitModule3("newmodule", newmoduleMethods, "This is a new module"); +#endif + + PyShellTest().Run(); +} + diff --git a/bazaar/PyTest/PyTest.h b/bazaar/PyTest/PyTest.h new file mode 100644 index 000000000..189f9b4b2 --- /dev/null +++ b/bazaar/PyTest/PyTest.h @@ -0,0 +1,18 @@ +#ifndef _PyTest_PyTest_h +#define _PyTest_PyTest_h + +//must go first cause has some preprocessor things, says docu +#include + +#include + +using namespace Upp; + +void SimpleCall(); +void EmbedPython(); +void ExtendPython(); + +void DestroyNewModule(); + +#endif + diff --git a/bazaar/PyTest/PyTest.lay b/bazaar/PyTest/PyTest.lay new file mode 100644 index 000000000..9bcbfbba8 --- /dev/null +++ b/bazaar/PyTest/PyTest.lay @@ -0,0 +1,3 @@ +LAYOUT(PyTestLayout, 200, 100) +END_LAYOUT + diff --git a/bazaar/PyTest/PyTest.upp b/bazaar/PyTest/PyTest.upp new file mode 100644 index 000000000..192275893 --- /dev/null +++ b/bazaar/PyTest/PyTest.upp @@ -0,0 +1,17 @@ +uses + Core, + Python; + +library + python27; + +file + PyTest.h, + embedd.cpp, + extend.cpp, + main.cpp; + +mainconfig + "" = "MT SSE2", + "" = "MT"; + diff --git a/bazaar/PyTest/embedd.cpp b/bazaar/PyTest/embedd.cpp new file mode 100644 index 000000000..0b1f04f61 --- /dev/null +++ b/bazaar/PyTest/embedd.cpp @@ -0,0 +1,48 @@ +#include "PyTest.h" + +void EmbedPython() +{ + Cout() << "Embedding Python:"<< EOL; + +// + if(!FileExists("pytest.py")) + { + String s = + "'''py_function.py - Python source designed to '''\n" + "'''demonstrate the use of python embedding'''\n" + "\n" + "def multiply():\n" + " c = 12345*6789\n" + " print 'The result of 12345 x 6789 :', c\n" + " return c\n" + ; + SaveFile("pytest.py", s); + } +// + + PyObject *pName, *pModule, *pDict, *pFunc, *pValue; + + // Build the name object + pName = PyString_FromString("pytest"); + + // Load the module object + pModule = PyImport_Import(pName); + + // pDict is a borrowed reference + pDict = PyModule_GetDict(pModule); + + // pFunc is also a borrowed reference + pFunc = PyDict_GetItemString(pDict, "multiply"); + + if (PyCallable_Check(pFunc)) + { + PyObject_CallObject(pFunc, NULL); + } else + { + PyErr_Print(); + } + + Cout() << "Clean up" << EOL; + Py_DECREF(pModule); + Py_DECREF(pName); +} diff --git a/bazaar/PyTest/extend.cpp b/bazaar/PyTest/extend.cpp new file mode 100644 index 000000000..294aa631e --- /dev/null +++ b/bazaar/PyTest/extend.cpp @@ -0,0 +1,121 @@ +#include "PyTest.h" + +using namespace std; + +struct FuncID +{ + DWORD id; // An index number of the function. + PyObject* cbf; // Pointer to the callback function. +}; + +Array FunctionsList; + +PyObject* newmodule_RegisterFunction ( PyObject* pSelf, PyObject* pArgs ) +{ + DWORD index = 0; + PyObject* CallbackFunction = NULL; + + if ( !PyArg_ParseTuple ( pArgs, "lO", &index, &CallbackFunction ) ) + return NULL; + + if ( !PyCallable_Check ( CallbackFunction ) ) + { + PyErr_SetString ( PyExc_StandardError, "The object should be callable!" ); + return NULL; + } + + // Make sure the index isn't in the list already. + + for ( int i = 0; FunctionsList.GetCount(); i++ ) + { + if ( FunctionsList[i].id == index ) + { + // Sorry mates. + return Py_BuildValue ( "i", FALSE ); + } + } + + // And most important, add a reference to it! + Py_INCREF ( CallbackFunction ); + + // Try to insert it to the list. + FuncID info = {index, CallbackFunction}; + + try + { + FunctionsList.Add(info); + } + + catch ( ... ) + { + // Oh uh! + Py_DECREF ( CallbackFunction ); + PyErr_SetString ( PyExc_StandardError, "An error occurred while inserting the given function into the DB!" ); + return NULL; + } + + return Py_BuildValue ( "i", TRUE ); +} + +PyObject* newmodule_CallFunctionByID ( PyObject* pSelf, PyObject* pArgs ) +{ + DWORD index = 0; + + if ( !PyArg_ParseTuple ( pArgs, "l", &index ) ) + return NULL; + + // Find the ID in the list, and call it's appropriate function. + for ( int i = 0; FunctionsList.GetCount(); i++ ) + { + if ( FunctionsList[i].id == index ) + { + // We pass no args, I leave it for you :) + // Py_XDECREF checks if the object isn't NULL first. + PyObject* ret = PyEval_CallObject ( FunctionsList[i].cbf, NULL ); + Py_XDECREF ( ret ); + Py_INCREF ( Py_None ); + return Py_None; + } + } + + // Damn function wasn't found. + return Py_BuildValue ( "i", FALSE ); +} + +static PyMethodDef newmoduleMethods[] = +{ + {"RegisterFunction", newmodule_RegisterFunction, METH_VARARGS, "Register a function to a given ID."}, + {"CallFunctionByID", newmodule_CallFunctionByID, METH_VARARGS, "Call a registered function by its ID."}, + {NULL, NULL, 0, NULL} +}; + +void InitNewModule() +{ + // When you'll run in Python: newmodule.__doc__, you'll get the last string. :) + Py_InitModule3 ( "newmodule", newmoduleMethods, "This is a new module" ); + PyModule_AddIntConstant ( PyImport_AddModule ( "newmodule" ), "INT1", 2003 ); + PyModule_AddStringConstant ( PyImport_AddModule ( "newmodule" ), "STR1", "http://ragestorm.net" ); + + FunctionsList.Clear(); +} + +void DestroyNewModule() +{ + // Clean up the list. + for ( int i = 0; FunctionsList.GetCount(); i++ ) + { + Py_DECREF ( FunctionsList[i].cbf ); + } + + FunctionsList.Clear(); + + // And free the newmodule. +} + +// + +void ExtendPython() +{ + Cout() << "Extending Python:"<< EOL; + InitNewModule(); +} diff --git a/bazaar/PyTest/init b/bazaar/PyTest/init new file mode 100644 index 000000000..80693a603 --- /dev/null +++ b/bazaar/PyTest/init @@ -0,0 +1,5 @@ +#ifndef _PyTest_icpp_init_stub +#define _PyTest_icpp_init_stub +#include "Core/init" +#include "Python/init" +#endif diff --git a/bazaar/PyTest/main.cpp b/bazaar/PyTest/main.cpp new file mode 100644 index 000000000..23cb3710f --- /dev/null +++ b/bazaar/PyTest/main.cpp @@ -0,0 +1,33 @@ +#include "PyTest.h" + +void SimpleCall() +{ + Cout() << "Invoking a python statement:" << EOL; + PyRun_SimpleString( "from time import time,ctime\n" + "print 'Today is',ctime(time())\n"); + PyRun_SimpleString(String("print 'python can calculate', 2+3")); +} + +void StartConsole() +{ + PyRun_SimpleString( "import code\n" + "code.interact()\n"); +} + +CONSOLE_APP_MAIN +{ + Cout() << "Starting Python..." << EOL; + + SimpleCall(); + + EmbedPython(); + ExtendPython(); + + StartConsole(); + + //leftover + DestroyNewModule(); + + Cout() << "DONE." << EOL; +} + diff --git a/bazaar/Python/Python.cpp b/bazaar/Python/Python.cpp new file mode 100644 index 000000000..a10960f0e --- /dev/null +++ b/bazaar/Python/Python.cpp @@ -0,0 +1,2 @@ +#include "Python.h" + diff --git a/bazaar/Python/Python.h b/bazaar/Python/Python.h new file mode 100644 index 000000000..60062dfba --- /dev/null +++ b/bazaar/Python/Python.h @@ -0,0 +1,10 @@ +#ifndef _Python_Python_h +#define _Python_Python_h + +#include + +#include + +using namespace Upp; + +#endif diff --git a/bazaar/Python/Python.upp b/bazaar/Python/Python.upp new file mode 100644 index 000000000..25f19fbfe --- /dev/null +++ b/bazaar/Python/Python.upp @@ -0,0 +1,13 @@ +uses + Core; + +library(!DEBUG) python27; + +library(DEBUG) python27_d; + +file + Python.h, + init.icpp, + Python.cpp, + install.txt; + diff --git a/bazaar/Python/init b/bazaar/Python/init new file mode 100644 index 000000000..16f1da5d3 --- /dev/null +++ b/bazaar/Python/init @@ -0,0 +1,7 @@ +#ifndef _Python_icpp_init_stub +#define _Python_icpp_init_stub +#include "Core/init" +#define BLITZ_INDEX__ FD8ACAF7616D7D6DCA7F843B2FA317F1F +#include "init.icpp" +#undef BLITZ_INDEX__ +#endif diff --git a/bazaar/Python/init.icpp b/bazaar/Python/init.icpp new file mode 100644 index 000000000..f8df68249 --- /dev/null +++ b/bazaar/Python/init.icpp @@ -0,0 +1,11 @@ +#include "Python.h" + +INITBLOCK +{ + Py_Initialize(); +} + +EXITBLOCK +{ + Py_Finalize(); +} diff --git a/bazaar/Python/install.txt b/bazaar/Python/install.txt new file mode 100644 index 000000000..5b0646b2e --- /dev/null +++ b/bazaar/Python/install.txt @@ -0,0 +1,60 @@ +to make python work in upp there are few steps to do.. + +go to the python page +http://www.python.org/getit/ + +and choose the installer for windows, version 2.7.1 is good and current. +linux should have python 2.7.1 already shipped. + +the windows installer will put it in 'C:\Python27' folder + +setup your build method for windows to add to PATH: +C:\Python27 +to INCLUDE +C:\Python27\include +to LIB +C:\Python27\ +C:\Python27\libs +C:\Python27\DLLs + +again, linux should already have python available from start. + +make sure NOT to choose DEBUG compile, since it will fail. the reason: +python27 headers need some more functions in debug mode, which are not provided in release lib. +but the Upp Python package is ready to be used as DEBUG as well. if needed, read on. + + +DEBUG: +debug is needed, when you need to debug at least *your* application. so you will need +the target library for DEBUG, named python27_d.dll / python27_d.so respectively. + +for linux, download the python debug package which should be available for your distro +or recompile it from source with the needed flags for configure. + +for windows you will need to compile it from source, because, it is not provided in installation. +download the sources at same location or directly +http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tgz +extract and move the Python-2.7.1 folder with the sources to C:\ + +go to C:\Python-2.7.1\PCbuild +and open the pcbuild.sln with VisualStudio 2008, or express, should do as well. +do a 'build solution', which will fail in some packages, ssl and the like. no matter.. + +PCbuild folder will contain the files + python27_d.dll + python27_d.lib + python27_d.pdb + python27_d.exp + +copy the dll to C:\Python27 +and the remaining 3 files to C:\Python27\libs + +this should enable you to compile your app as DEBUG.. + +to further information visit: +http://wiki.python.org/moin/Building%20Python%20with%20the%20free%20MS%20C%20Toolkit +http://timgolden.me.uk/python/win32_how_do_i.html +http://docs.python.org/using/windows.html + +NOTE: +for shorthand, use the provided precompiled python27_d diff --git a/bazaar/Python/python27_d/python27_d.rar b/bazaar/Python/python27_d/python27_d.rar new file mode 100644 index 000000000..7486e2a89 Binary files /dev/null and b/bazaar/Python/python27_d/python27_d.rar differ