//============================================================================ // FUNCTION : Log_Install //============================================================================ void Log_Install() { if (PyType_Ready(&Log_Type) < 0 ) { THROW_EXCEPTION("Cannot install LOG", "Unable to register object type", SPELL_ERROR_PYTHON_API); } PyObject* logobj = Log_New( &Log_Type, NULL, NULL ); SPELLpythonHelper::instance().install(logobj, "LOG", "spell.utils.log"); int count = 0; for( count = 0; count < LOG_LEV_MAX; count ++) { SPELLpythonHelper::instance().install(SSTRPY(LOG_LEVEL_STR[count]), LOG_LEVEL_LBL[count], "spell.utils.log"); str_to_lev.insert( std::make_pair( LOG_LEVEL_STR[count], (LogLevel)count )); } for( count = 0; count < LOG_SEV_MAX; count ++) { SPELLpythonHelper::instance().install(SSTRPY(LOG_SEVERITY_STR[count]), LOG_SEVERITY_LBL[count], "spell.utils.log"); str_to_sev.insert( std::make_pair( LOG_SEVERITY_STR[count], (LogSeverity)count )); } }
//============================================================================= // METHOD : SPELLdriverManager::setup //============================================================================= void SPELLdriverManager::setup( const std::string& ctxName, const std::string& interfaces ) { SPELLsafePythonOperations ops("SPELLdriverManager::setup()"); PyObject* pmgr = getDriverManagerObject(); if (interfaces.empty()) { SPELLpythonHelper::instance().callMethod( pmgr, "setup", SSTRPY(ctxName), NULL); } else { SPELLpythonHelper::instance().callMethod( pmgr, "setup", SSTRPY(ctxName), SSTRPY(interfaces), NULL); } SPELLpythonHelper::instance().checkError(); }
//============================================================================= // METHOD : SPELLdriverManager::setup //============================================================================= void SPELLdriverManager::setup( std::string ctxName ) { SPELLsafePythonOperations ops; PyObject* pmgr = getDriverManagerObject(); SPELLpythonHelper::instance().callMethod( pmgr, "setup", SSTRPY(ctxName), NULL); SPELLpythonHelper::instance().checkError(); }
//============================================================================= // METHOD: SPELLvariableMonitor::getVariableRef //============================================================================= PyObject* SPELLvariableMonitor::getVariableRef( const std::string& name ) { if (PyDict_Contains(m_frame->f_globals, SSTRPY(name))) { return PyDict_GetItemString(m_frame->f_globals, name.c_str()); } return NULL; }
//============================================================================= // METHOD: SPELLvariableMonitor::retrieveLocalVariables() //============================================================================= void SPELLvariableMonitor::retrieveLocalVariables(std::vector<SPELLvarInfo>& vars) { DEBUG("[VM] Retrieve Locals"); /* * Bottom stack frame is discarded, * as globals and locals are the same dictionary */ if (m_frame->f_back == NULL) return; /* * Get the names defined in the current code, including arguments */ std::vector<std::string> varNames = retrieveNames(); /* * Iterate over the locals dictionary, retrieving the names contained in * varNames */ PyFrame_FastToLocals(m_frame); PyObject* dict = m_frame->f_locals; DEBUG("[VM] Frame: " + PYCREPR(m_frame)); for( unsigned int index = 0; index< varNames.size(); index++) { std::string varName = varNames[index]; PyObject* pyVarName = SSTRPY(varName); if (PyDict_Contains( dict, pyVarName )) { PyObject* object = PyDict_GetItem( dict, pyVarName ); if (!SPELLpythonHelper::instance().isInstance(object, "Database", "spell.lib.adapter.databases.database")) { if (PyCallable_Check(object)) continue; if (PyClass_Check(object)) continue; if (PyModule_Check(object)) continue; if (PyInstance_Check(object)) continue; } DEBUG("[VM] Processing " + varName); std::string type = PYSSTR( PyObject_Type(object) ); DEBUG("[VM] Type : " + type); std::string value = PYREPR( object ); DEBUG("[VM] Value : " + value); DEBUG("[VM] Global : " + BSTR(false)); DEBUG("[VM] Registered: " + BSTR(isRegistered(varName))); // Mark empty values (empty strings) as "<empty>" if (value == "") value = EMPTY_STRING; vars.push_back( SPELLvarInfo(varName, type, value, false, isRegistered(varName)) ); } } PyFrame_LocalsToFast(m_frame,0); }
//============================================================================= // METHOD : SPELLdriverManager::onCommand //============================================================================= void SPELLdriverManager::onCommand( const std::string& commandId ) { PyObject* pmgr = getDriverManagerObject(); PyObject* cmd = SSTRPY(commandId); Py_INCREF(cmd); try { SPELLpythonHelper::instance().callMethod( pmgr, "onCommand", cmd, NULL ); SPELLpythonHelper::instance().checkError(); } catch(SPELLcoreException& ex) { LOG_ERROR("OnCommand failed: " + ex.what()); } }
//============================================================================= // METHOD : SPELLwsWarmStartImpl::saveState() //============================================================================= void SPELLwsWarmStartImpl::saveState() { DEBUG("[WS] Saving state ============================================"); std::cerr << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" << std::endl; std::cerr << "SAVE STATE START" << std::endl; std::cerr << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" << std::endl; // Synchronize so that nothing can be done while saving SPELLmonitor m(m_lock); SPELLsafePythonOperations ops("SPELLwsWarmstartImpl::saveState()"); //TODO check, possibly there are several states that need to be saved PyThreadState* state = m_topFrame->getFrameObject()->f_tstate; DEBUG("Current thread state: " + PSTR(state)); assert(state != NULL); // Reset the storage. Remove this if we want the full history, but // the load algorith would need to be modified. m_storage->reset(); DEBUG(" - Recursion depth : " + ISTR(state->recursion_depth)); m_storage->storeLong( state->recursion_depth ); DEBUG(" - Tick counter : " + ISTR(state->tick_counter)); m_storage->storeLong( state->tick_counter ); DEBUG(" - GIL counter : " + ISTR(state->gilstate_counter)); m_storage->storeLong( state->gilstate_counter ); DEBUG(" - Number of frames: " + ISTR(m_frames.size())); m_storage->storeLong( m_frames.size() ); DEBUG("[WS] Saving frames"); unsigned int frameCount = m_frames.size(); for( unsigned int index = 0; index < frameCount; index++) { std::string id = m_frames[index]->getCodeId(); DEBUG(" - Saving frame '" + id + "'"); m_storage->storeObject(SSTRPY(id)); m_frames[index]->saveState(); } std::cerr << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" << std::endl; std::cerr << "SAVE STATE END" << std::endl; std::cerr << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" << std::endl; DEBUG("[WS] Saving state done ======================================="); }
//============================================================================ // METHOD: SPELLpyValue::get //============================================================================ PyObject* SPELLpyValue::get() const { if (m_type == BOOLEAN ) { if (m_boolValue) { Py_RETURN_TRUE; } else { Py_RETURN_FALSE; } } else if (m_type == STRING ) { return SSTRPY(m_stringValue); } else if (m_type == LONG ) { return PyLong_FromLong(m_intValue); } else if (m_type == DOUBLE) { return PyFloat_FromDouble(m_floatValue); } else if (m_type == RELTIME ) { PyObject* time = SPELLpythonHelper::instance().pythonTime(m_timeValue); return time; } else if (m_type == ABSTIME ) { PyObject* time = SPELLpythonHelper::instance().pythonTime(m_timeValue); return time; } else if (m_type == LIST ) { PyObject* list = PyList_New(m_listValue.size()); ValueList::const_iterator it = m_listValue.begin(); unsigned int idx = 0; for( it = m_listValue.begin(); it != m_listValue.end(); it++ ) { const SPELLpyValue& value = *it; PyObject* item = value.get(); Py_INCREF(item); PyList_SetItem(list,idx,item); idx++; } return list; } else if (m_type == DICT ) { PyObject* dict = PyDict_New(); ValueMap::const_iterator it = m_dictValue.begin(); for( it = m_dictValue.begin(); it != m_dictValue.end(); it++ ) { std::string key = it->first; const SPELLpyValue& value = it->second; PyObject* item = value.get(); Py_INCREF(item); PyDict_SetItemString(dict,key.c_str(),item); } return dict; } Py_RETURN_NONE; }
//============================================================================= // METHOD: SPELLdatabaseFileSPB::load() //============================================================================= void SPELLdatabaseFileSPB::load() { std::ifstream file; std::string filename = getFilename(); file.open( filename.c_str(), std::ios::in ); if (!file.is_open()) { filename = getFilename() + "." + SPELLutils::toLower(getExtension()); DEBUG("[SDBF] Second try: " + filename); file.open( filename.c_str(), std::ios::in ); if (!file.is_open()) { filename = getFilename() + "." + SPELLutils::toUpper(getExtension()); DEBUG("[SDBF] Third try: " + filename); file.open( filename.c_str(), std::ios::in ); if (!file.is_open()) { THROW_EXCEPTION("Cannot load database", "Cannot open file '" + getFilename() + "'", SPELL_ERROR_FILESYSTEM); } } } std::vector<std::string> lines; while(!file.eof()) { std::string line = ""; std::getline(file,line); lines.push_back(line); } file.close(); std::vector<std::string>::iterator it; for( it = lines.begin(); it != lines.end(); it++) { std::string lineToProcess = *it; // Remove spaces SPELLutils::trim(lineToProcess); // Process only lines starting with $, and having := if ((lineToProcess.find("$")!=0)||(lineToProcess.find(":=")==std::string::npos)) { continue; } // Replace tabs SPELLutils::replace(lineToProcess,"\t"," "); // Remove \r SPELLutils::replace(lineToProcess,"\n",""); SPELLutils::replace(lineToProcess,"\r",""); // Now process line data int idx = lineToProcess.find(":="); std::string key = lineToProcess.substr(0,idx); idx += 2; std::string origValue = lineToProcess.substr(idx,lineToProcess.size()-idx); SPELLutils::trim(key); SPELLutils::trim(origValue); // Post-process value if (matchesShortDate(origValue)) { // Convert SPB short dates to SPELL short dates origValue = "+" + origValue; } PyObject* value = NULL; //std::string vtype = ""; value = importValue(origValue); std::string format = ""; if (PyLong_Check(value) || PyInt_Check(value)) { if (origValue.find("0x") == 0) { format = LanguageConstants::HEX; } else if (origValue.find("0b") == 0) { format = LanguageConstants::BIN; } else if (origValue.find("0") == 0 && origValue != "0") { format = LanguageConstants::OCT; } } if (value != NULL) { SPELLdatabase::set( SSTRPY(key), value, format ); //TODO if types needed to commit: m_types.insert( std::make_pair(key,vtype)); } } }