|
10 | 10 |
|
11 | 11 | #ifdef HAVE_PYTHON
|
12 | 12 |
|
| 13 | +#define _STRINGIFY_VERSION(a, b) (#a "." #b) |
| 14 | +#define STRINGIFY_VERSION(a, b) _STRINGIFY_VERSION(a, b) |
| 15 | + |
13 | 16 | using std::string;
|
14 | 17 |
|
15 | 18 | /**
|
@@ -531,22 +534,27 @@ Dtool_TypeMap *Dtool_GetGlobalTypeMap() {
|
531 | 534 | }
|
532 | 535 | }
|
533 | 536 |
|
| 537 | +#define PY_MAJOR_VERSION_STR #PY_MAJOR_VERSION "." #PY_MINOR_VERSION |
| 538 | + |
534 | 539 | #if PY_MAJOR_VERSION >= 3
|
535 | 540 | PyObject *Dtool_PyModuleInitHelper(const LibraryDef *defs[], PyModuleDef *module_def) {
|
536 | 541 | #else
|
537 | 542 | PyObject *Dtool_PyModuleInitHelper(const LibraryDef *defs[], const char *modulename) {
|
538 | 543 | #endif
|
539 | 544 | // Check the version so we can print a helpful error if it doesn't match.
|
540 | 545 | string version = Py_GetVersion();
|
| 546 | + size_t version_len = version.find('.', 2); |
| 547 | + if (version_len != string::npos) { |
| 548 | + version.resize(version_len); |
| 549 | + } |
541 | 550 |
|
542 |
| - if (version[0] != '0' + PY_MAJOR_VERSION || |
543 |
| - version[2] != '0' + PY_MINOR_VERSION) { |
| 551 | + if (version != STRINGIFY_VERSION(PY_MAJOR_VERSION, PY_MINOR_VERSION)) { |
544 | 552 | // Raise a helpful error message. We can safely do this because the
|
545 | 553 | // signature and behavior for PyErr_SetString has remained consistent.
|
546 | 554 | std::ostringstream errs;
|
547 | 555 | errs << "this module was compiled for Python "
|
548 | 556 | << PY_MAJOR_VERSION << "." << PY_MINOR_VERSION << ", which is "
|
549 |
| - << "incompatible with Python " << version.substr(0, 3); |
| 557 | + << "incompatible with Python " << version; |
550 | 558 | string error = errs.str();
|
551 | 559 | PyErr_SetString(PyExc_ImportError, error.c_str());
|
552 | 560 | return nullptr;
|
|
0 commit comments