| 884 | // -------------------------------------------------------------------- |
| 885 | |
| 886 | int getSWIGVersionFromModule(const std::string& module) |
| 887 | { |
| 888 | static std::map<std::string, int> moduleMap; |
| 889 | std::map<std::string, int>::iterator it = moduleMap.find(module); |
| 890 | if (it != moduleMap.end()) { |
| 891 | return it->second; |
| 892 | } |
| 893 | try { |
| 894 | // Get the module and check its __file__ attribute |
| 895 | Py::Dict dict(PyImport_GetModuleDict()); |
| 896 | if (!dict.hasKey(module)) { |
| 897 | return 0; |
| 898 | } |
| 899 | Py::Module mod(module); |
| 900 | Py::String file(mod.getAttr("__file__")); |
| 901 | std::string filename = (std::string)file; |
| 902 | // file can have the extension .py or .pyc |
| 903 | filename = filename.substr(0, filename.rfind('.')); |
| 904 | filename += ".py"; |
| 905 | boost::regex rx("^# Version ([1-9])\\.([0-9])\\.([0-9]+)"); |
| 906 | boost::cmatch what; |
| 907 | |
| 908 | std::string line; |
| 909 | Base::FileInfo fi(filename); |
| 910 | |
| 911 | Base::ifstream str(fi, std::ios::in); |
| 912 | while (str && std::getline(str, line)) { |
| 913 | if (boost::regex_match(line.c_str(), what, rx)) { |
| 914 | int major = std::atoi(what[1].first); |
| 915 | int minor = std::atoi(what[2].first); |
| 916 | int micro = std::atoi(what[3].first); |
| 917 | int version = (major << 16) + (minor << 8) + micro; |
| 918 | moduleMap[module] = version; |
| 919 | return version; |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | catch (Py::Exception& e) { |
| 924 | e.clear(); |
| 925 | } |
| 926 | |
| 927 | #if (defined(HAVE_SWIG) && (HAVE_SWIG == 1)) |
| 928 | moduleMap[module] = 0; |
| 929 | #endif |
| 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | #if (defined(HAVE_SWIG) && (HAVE_SWIG == 1)) |
| 934 | namespace Swig_python |