[pykd_ext_2.0] added : python version can be note in the first script line ( shebang line )

git-svn-id: https://pykd.svn.codeplex.com/svn@91049 9b283d60-5439-405e-af05-b73fd8c4d996
This commit is contained in:
SND\ussrhero_cp 2016-10-01 10:12:57 +00:00 committed by Mikhail I. Izmestev
parent 584eb08fa7
commit 85a36df11e
4 changed files with 43 additions and 17 deletions

View File

@ -2,17 +2,18 @@
#include <boost/tokenizer.hpp>
#include <regex>
#include <list>
#include "arglist.h"
namespace {
typedef boost::escaped_list_separator<char> char_separator_t;
typedef boost::tokenizer< char_separator_t > char_tokenizer_t;
ArgsList getArgsList(const char* args)
ArgsList getArgsList(const std::string& argsStr)
{
std::string argsStr(args);
char_tokenizer_t token(argsStr, char_separator_t("", " \t", "\""));
ArgsList argsList;
@ -25,16 +26,19 @@ ArgsList getArgsList(const char* args)
return argsList;
}
} // anonymous namespace
static const std::regex versionRe("^-([2,3])(?:\\.(\\d+))?$");
Options::Options(const ArgsList& argList) :
Options::Options(const std::string& cmdline) :
pyMajorVersion(-1),
pyMinorVersion(-1),
global(true),
showHelp(false)
{
args = argList;
args = getArgsList( cmdline );
for (auto it = args.begin(); it != args.end();)
{
@ -72,7 +76,7 @@ Options::Options(const ArgsList& argList) :
it = args.erase(it);
continue;
}
break;
}

View File

@ -5,8 +5,6 @@
typedef std::vector< std::string > ArgsList;
ArgsList getArgsList(const char* args);
struct Options
{
int pyMajorVersion;
@ -22,6 +20,5 @@ struct Options
showHelp(false)
{}
Options(const ArgsList& argList);
Options(const std::string& cmdline);
};

View File

@ -197,7 +197,7 @@ public:
{
module->PyEval_RestoreThread(module->m_globalState);
module->m_globalInterpreter = new PythonInterpreter(module);
}
}
m_currentInterpter = module->m_globalInterpreter;
m_currentIsGlobal = true;
@ -210,7 +210,8 @@ public:
}
m_currentInterpter->m_module->PyEval_RestoreThread(m_currentInterpter->m_state);
m_currentInterpter->m_module->checkPykd();
return m_currentInterpter;
}
@ -544,6 +545,8 @@ void PyModule::checkPykd()
} while( false);
PyErr_Clear();
if (mainName) Py_DecRef(mainName);
if (mainMod) Py_DecRef(mainMod);
if (globals) Py_DecRef(globals);

View File

@ -2,7 +2,9 @@
#include <algorithm>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <regex>
#include <DbgEng.h>
@ -265,6 +267,9 @@ help(
//////////////////////////////////////////////////////////////////////////////
static const std::regex shebangRe("^#!\\s*python([2,3])(?:\\.(\\d))?$");
extern "C"
HRESULT
CALLBACK
@ -279,7 +284,7 @@ py(
try {
Options opts(getArgsList(args));
Options opts(args);
if (opts.showHelp)
throw std::exception(printUsageMsg);
@ -287,6 +292,25 @@ py(
int majorVersion = opts.pyMajorVersion;
int minorVersion = opts.pyMinorVersion;
if ( opts.args.size() > 0 && majorVersion == -1 && minorVersion == -1 )
{
std::ifstream scriptFile(opts.args[0]);
std::string firstline;
std::getline(scriptFile, firstline);
std::smatch mres;
if (std::regex_match(firstline, mres, shebangRe))
{
majorVersion = atol(std::string(mres[1].first, mres[1].second).c_str());
if (mres[2].matched)
{
minorVersion = atol(std::string(mres[2].first, mres[2].second).c_str());
}
}
}
getPythonVersion(majorVersion, minorVersion);
AutoInterpreter autoInterpreter(opts.global, majorVersion, minorVersion);
@ -304,8 +328,6 @@ py(
PyObjectRef mainMod = PyImport_Import(mainName);
PyObjectRef globals = PyObject_GetAttrString(mainMod, "__dict__");
checkPykd();
InterruptWatch interruptWatch(client);
if (opts.args.empty())
@ -395,7 +417,7 @@ pip(
try {
Options opts(getArgsList(args));
Options opts(args);
int majorVersion = opts.pyMajorVersion;
int minorVersion = opts.pyMinorVersion;