Skip to content

Commit cebf7c6

Browse files
authored
Merge pull request #12095 from velconia/port_py3
Port py3
2 parents f0cd493 + 448ca8a commit cebf7c6

File tree

12 files changed

+87
-36
lines changed

12 files changed

+87
-36
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ option(WITH_ANAKIN "Compile with Anakin library" OFF)
6666
option(WITH_GRPC "Use grpc as the default rpc framework" ${WITH_DISTRIBUTE})
6767
option(WITH_BRPC_RDMA "Use brpc rdma as the rpc protocal" OFF)
6868
option(WITH_SYSTEM_BLAS "Use system blas library" OFF)
69+
option(PY_VERSION "Compile PaddlePaddle with python3 support" ${PY_VERSION})
70+
71+
# PY_VERSION
72+
if(NOT PY_VERSION)
73+
set(PY_VERSION 2.7)
74+
endif()
6975

7076
# CMAKE_BUILD_TYPE
7177
if(NOT CMAKE_BUILD_TYPE)

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ RUN pip install pre-commit 'ipython==5.3.0' && \
8080
pip install opencv-python
8181

8282
#For docstring checker
83-
RUN pip install pylint pytest astroid isort
83+
RUN pip install pylint pytest astroid isort LinkChecker
8484

8585
COPY ./python/requirements.txt /root/
8686
RUN pip install -r /root/requirements.txt

cmake/external/python.cmake

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ ENDIF()
1818

1919
INCLUDE(python_module)
2020

21-
FIND_PACKAGE(PythonInterp 2.7)
22-
FIND_PACKAGE(PythonLibs 2.7)
21+
FIND_PACKAGE(PythonInterp ${PY_VERSION})
22+
FIND_PACKAGE(PythonLibs ${PY_VERSION})
23+
2324
# Fixme: Maybe find a static library. Get SHARED/STATIC by FIND_PACKAGE.
2425
ADD_LIBRARY(python SHARED IMPORTED GLOBAL)
2526
SET_PROPERTY(TARGET python PROPERTY IMPORTED_LOCATION ${PYTHON_LIBRARIES})

paddle/legacy/utils/PythonUtil.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,13 @@ std::string callPythonFunc(const std::string& moduleName,
136136
const std::string& funcName,
137137
const std::vector<std::string>& args) {
138138
PyObjectPtr obj = callPythonFuncRetPyObj(moduleName, funcName, args);
139+
#if PY_MAJOR_VERSION >= 3
140+
Py_ssize_t str_size = 0u;
141+
const char* str = PyUnicode_AsUTF8AndSize(obj.get(), &str_size);
142+
return std::string(str, (size_t)str_size);
143+
#else
139144
return std::string(PyString_AsString(obj.get()), PyString_Size(obj.get()));
145+
#endif // PY_MAJOR_VERSION >= 3
140146
}
141147

142148
PyObjectPtr createPythonClass(

paddle/legacy/utils/PythonUtil.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,33 @@ PyObjectPtr createPythonClass(const std::string& moduleName,
8888
namespace py {
8989
PyObjectPtr import(const std::string& moduleName);
9090

91+
#if PY_MAJOR_VERSION >= 3
92+
/**
93+
* Cast a PyLong to int type T.
94+
* @tparam T return type.
95+
* @param [in] obj PyLong object.
96+
* @param [out] ok status for casting. False if error occured. nullptr if user
97+
* don't care is ok or not.
98+
* @return The value of python object, or 0 if not ok.
99+
*/
100+
template <typename T>
101+
T castInt(PyObject* obj, bool* ok = nullptr) {
102+
// Refer to https://www.python.org/dev/peps/pep-0237/, the int and long object
103+
// were unified to long since python3
104+
if (PyLong_Check(obj)) {
105+
if (ok) *ok = true;
106+
return (T)PyLong_AsUnsignedLong(obj);
107+
} else {
108+
if (ok) *ok = false;
109+
return (T)0;
110+
}
111+
}
112+
113+
// Convert PyAPI from 2.x to 3.x
114+
#define PyString_FromString PyUnicode_FromString
115+
#define PyString_AsString PyUnicode_AsUTF8
116+
117+
#else
91118
/**
92119
* Cast a PyLong or PyInt to int type T.
93120
* @tparam T return type.
@@ -109,6 +136,7 @@ T castInt(PyObject* obj, bool* ok = nullptr) {
109136
return (T)0;
110137
}
111138
}
139+
#endif // PY_MAJOR_VERSION >= 3
112140

113141
/**
114142
* Invoke repr of python object.

paddle/scripts/paddle_build.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ function cmake_gen() {
7878
PYTHON_FLAGS="-DPYTHON_EXECUTABLE:FILEPATH=/opt/python/cp27-cp27mu/bin/python
7979
-DPYTHON_INCLUDE_DIR:PATH=/opt/python/cp27-cp27mu/include/python2.7
8080
-DPYTHON_LIBRARIES:FILEPATH=/opt/_internal/cpython-2.7.11-ucs4/lib/libpython2.7.so"
81+
elif [ "$1" == "cp35-cp35m" ]; then
82+
export LD_LIBRARY_PATH=/opt/_internal/cpython-3.5.1/lib/:${LD_LIBRARY_PATH}
83+
export PATH=/opt/_internal/cpython-3.5.1/bin/:${PATH}
84+
export PYTHON_FLAGS="-DPYTHON_EXECUTABLE:FILEPATH=/opt/_internal/cpython-3.5.1/bin/python3
85+
-DPYTHON_INCLUDE_DIR:PATH=/opt/_internal/cpython-3.5.1/include/python3.5m
86+
-DPYTHON_LIBRARIES:FILEPATH=/opt/_internal/cpython-3.5.1/lib/libpython3.so"
8187
fi
8288
fi
8389

@@ -108,6 +114,7 @@ function cmake_gen() {
108114
-DWITH_CONTRIB=${WITH_CONTRIB:-ON}
109115
-DWITH_ANAKIN=${WITH_ANAKIN:-OFF}
110116
-DWITH_INFERENCE_DEMO=${WITH_INFERENCE_DEMO:-ON}
117+
-DPY_VERSION=${PY_VERSION:-2.7}
111118
========================================
112119
EOF
113120
# Disable UNITTEST_USE_VIRTUALENV in docker because
@@ -136,7 +143,8 @@ EOF
136143
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
137144
-DWITH_CONTRIB=${WITH_CONTRIB:-ON} \
138145
-DWITH_ANAKIN=${WITH_ANAKIN:-OFF} \
139-
-DWITH_INFERENCE_DEMO=${WITH_INFERENCE_DEMO:-ON}
146+
-DWITH_INFERENCE_DEMO=${WITH_INFERENCE_DEMO:-ON} \
147+
-DPY_VERSION=${PY_VERSION:-2.7}
140148
}
141149

142150
function abort(){

python/paddle/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
try:
15-
from version import full_version as __version__
16-
from version import commit as __git_commit__
15+
from paddle.version import full_version as __version__
16+
from paddle.version import commit as __git_commit__
1717

1818
except ImportError:
1919
import sys
20-
sys.stderr.write('''Warning with import paddle: you should not
20+
sys.stderr.write('''Warning with import paddle: you should not
2121
import paddle from the source directory; please install paddlepaddle*.whl firstly.'''
2222
)
2323

24-
import reader
25-
import dataset
26-
import batch
24+
import paddle.reader
25+
import paddle.dataset
26+
import paddle.batch
2727
batch = batch.batch

python/paddle/dataset/__init__.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
Dataset package.
1616
"""
1717

18-
import mnist
19-
import imikolov
20-
import imdb
21-
import cifar
22-
import movielens
23-
import conll05
24-
import uci_housing
25-
import sentiment
26-
import wmt14
27-
import wmt16
28-
import mq2007
29-
import flowers
30-
import voc2012
31-
import image
18+
import paddle.dataset.mnist
19+
import paddle.dataset.imikolov
20+
import paddle.dataset.imdb
21+
import paddle.dataset.cifar
22+
import paddle.dataset.movielens
23+
import paddle.dataset.conll05
24+
import paddle.dataset.uci_housing
25+
import paddle.dataset.sentiment
26+
import paddle.dataset.wmt14
27+
import paddle.dataset.wmt16
28+
import paddle.dataset.mq2007
29+
import paddle.dataset.flowers
30+
import paddle.dataset.voc2012
31+
import paddle.dataset.image
3232

3333
__all__ = [
3434
'mnist',

python/paddle/reader/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ def reader():
6666
TODO(yuyang18): Should we add whole design doc here?
6767
"""
6868

69-
import decorator
70-
from decorator import *
69+
import paddle.reader.decorator
70+
from paddle.reader.decorator import *
7171

72-
import creator
72+
import paddle.reader.creator
7373

7474
__all__ = decorator.__all__ + ['creator']

python/paddle/reader/decorator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from threading import Thread
2121
import subprocess
2222

23-
from Queue import Queue
23+
from six.moves.queue import Queue
2424
import itertools
2525
import random
2626
import zlib

0 commit comments

Comments
 (0)