我在Python3中使用基於Python 2的C++引擎工作時遇到了一些嚴重的麻煩。我知道整個IO堆棧已經改變,但是我似乎嘗試的一切都以失敗告終。以下是預代碼(Python2)和後代碼(Python3)。我希望有人能幫我弄清楚我做錯了什麼。我也使用boost::python
來控制參考。Python 3 C-API IO和文件執行
該程序假設經由映射到Python對象加載到存儲器中,然後在使用時的運行功能它然後發現在存儲器中加載的文件並運行它。我將我的代碼從delta3d python管理器的示例中提取出來,並將它們加載到文件中並立即運行。我在Python3中沒有看到任何相同的東西。
Python2代碼從這裏開始:
// what this does is first calls the Python C-API to load the file, then pass the returned
// PyObject* into handle, which takes reference and sets it as a boost::python::object.
// this takes care of all future referencing and dereferencing.
try{
bp::object file_object(bp::handle<>(PyFile_FromString(fullPath(filename), "r")));
loaded_files_.insert(std::make_pair(std::string(fullPath(filename)), file_object));
}
catch(...)
{
getExceptionFromPy();
}
接下來,我從性病::地圖加載該文件,並試圖執行它:
bp::object loaded_file = getLoadedFile(filename);
try
{
PyRun_SimpleFile(PyFile_AsFile(loaded_file.ptr()), fullPath(filename));
}
catch(...)
{
getExceptionFromPy();
}
Python3代碼從這裏開始:這是我迄今爲止基於這裏的一些建議... SO Question 加載:
PyObject *ioMod, *opened_file, *fd_obj;
ioMod = PyImport_ImportModule("io");
opened_file = PyObject_CallMethod(ioMod, "open", "ss", fullPath(filename), "r");
bp::handle<> h_open(opened_file);
bp::object file_obj(h_open);
loaded_files_.insert(std::make_pair(std::string(fullPath(filename)), file_obj));
運行:
bp::object loaded_file = getLoadedFile(filename);
int fd = PyObject_AsFileDescriptor(loaded_file.ptr());
PyObject* fileObj = PyFile_FromFd(fd,fullPath(filename),"r",-1,"", "\n","", 0);
FILE* f_open = _fdopen(fd,"r");
PyRun_SimpleFile(f_open, fullPath(filename));
最後,在這一點上,程序的總體狀況是文件被加載在爲TextIOWrapper
,並在運行:部分返回的fd是永遠3由於某種原因,_fdopen
永遠不能打開FILE
,這意味着我不能做類似PyRun_SimpleFile
的事情。錯誤本身是_fdopen
上的調試ASSERTION
。有沒有更好的方法來做這一切,我真的很感謝任何幫助。
如果你想看到Github
我在給你正在嘗試做的困惑。上面哪個Python 3示例應該取代上面的Python 2示例中的哪一個?他們都如此不同。你的實際錯誤信息是什麼? – 2011-01-05 15:37:14
夠公平的,我做了一些編輯,並在第二段中解釋了我想要完成的事情。 – 2011-01-05 15:42:18
我可能找到了解決方案,但我無法測試它,直到我回家。當我這樣做,我會發布它。 – 2011-01-05 20:35:32