From f9c04ab20c471f7feb39292e9a275c99de761665 Mon Sep 17 00:00:00 2001 From: Adrien Pouyet Date: Sun, 17 Feb 2019 15:40:48 +0100 Subject: [PATCH] Python3.7 fix (retro-compatible) (#10) * add char * copy(const char*) and fix error lines * Small style changes. * Fix syntax error * Use Python 3.7 within CI build. * Travis CI work-around for Python 3.7. * Update requirements to numpy/scipy versions that have Python 3.7 wheels/support. --- .travis.yml | 6 ++++++ requirements.txt | 4 ++-- src/pytrec_eval.cpp | 10 ++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index b1b3952..d6031b5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,12 @@ python: - "3.4" - "3.5" - "3.6" +# Follow the work-around proposed in https://github.com/travis-ci/travis-ci/issues/9815 to enable Python 3.7 on Travis. +matrix: + include: + - python: 3.7 + dist: xenial + sudo: true install: - pip install -r requirements.txt - pip install . diff --git a/requirements.txt b/requirements.txt index 5632ab5..902a49c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -numpy==1.11.1 -scipy==0.18.1 \ No newline at end of file +numpy>=1.15.1 +scipy>=1.1.0 \ No newline at end of file diff --git a/src/pytrec_eval.cpp b/src/pytrec_eval.cpp index 8cbd141..e45bb50 100644 --- a/src/pytrec_eval.cpp +++ b/src/pytrec_eval.cpp @@ -56,6 +56,12 @@ int PyDict_SetItemAndSteal(PyObject* p, PyObject* key, PyObject* val) { return ret; } +char* CopyCString(const char* originalCString) { + char* const newCString = new char[strlen(originalCString) + 1]; + strcpy(newCString, originalCString); + return newCString; +} + static PyTypeObject RelevanceEvaluatorType; // RelevanceEvaluator @@ -128,7 +134,7 @@ class RankingBuilder { Py_INCREF(key); - queries[query_idx].qid = PyUnicode_AsUTF8(key); + queries[query_idx].qid = CopyCString(PyUnicode_AsUTF8(key)); CHECK_NOTNULL(queries[query_idx].qid); PairT* const query_document_pairs = Malloc(PyDict_Size(value), PairT); @@ -147,7 +153,7 @@ class RankingBuilder { return false; // TODO(cvangysel): need to clean up here! } - query_document_pairs[pair_idx].docno = PyUnicode_AsUTF8(inner_key); + query_document_pairs[pair_idx].docno = CopyCString(PyUnicode_AsUTF8(inner_key)); CHECK_NOTNULL(query_document_pairs[pair_idx].docno); if (!ProcessQueryDocumentPair(&query_document_pairs[pair_idx],