Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion python/paddle/dataset/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@
if six.PY3:
import subprocess
import sys
if sys.platform == 'win32':
interpreter = sys.exec_prefix + "\\" + "python.exe"
else:
interpreter = sys.executable
import_cv2_proc = subprocess.Popen(
[sys.executable, "-c", "import cv2"],
[interpreter, "-c", "import cv2"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = import_cv2_proc.communicate()
Expand Down
8 changes: 7 additions & 1 deletion python/paddle/fluid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,13 @@ def __bootstrap__():
]

core.init_gflags(["--tryfromenv=" + ",".join(read_env_flags)])
core.init_glog(sys.argv[0])
# Note(zhouwei25): sys may not have argv in some cases,
# Such as: use Python/C API to call Python from C++
try:
core.init_glog(sys.argv[0])
except Exception:
sys.argv = [""]
core.init_glog(sys.argv[0])
# don't init_p2p when in unittest to save time.
core.init_devices()

Expand Down
4 changes: 4 additions & 0 deletions python/paddle/fluid/tests/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,10 @@ if (WITH_XPU_BKCL)
py_test(test_collective_allreduce_api_xpu SRCS "test_collective_allreduce_api.py")
endif()

if(WIN32)
cc_test(cc_imp_py_test SRCS cc_imp_py_test.cc DEPS python)
endif()

if (WITH_ASCEND_CL)
add_subdirectory(npu)
endif()
Expand Down
60 changes: 60 additions & 0 deletions python/paddle/fluid/tests/unittests/cc_imp_py_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>
#include <iostream>
#include "Python.h"

TEST(CC, IMPORT_PY) {
// Initialize python environment
Py_Initialize();
ASSERT_TRUE(Py_IsInitialized());

// 1. C/C++ Run Python simple string
ASSERT_FALSE(PyRun_SimpleString("import paddle"));
ASSERT_FALSE(PyRun_SimpleString("print(paddle.to_tensor(1))"));

// 2. C/C++ Run Python funciton
PyRun_SimpleString("import sys");
PyRun_SimpleString("import os");
PyRun_SimpleString("sys.path.append(os.getcwd())");
PyObject* pModule = PyImport_ImportModule("test_install_check");
ASSERT_TRUE(pModule != NULL);

PyObject* pTestInt = PyObject_GetAttrString(pModule, "TestInt");
ASSERT_TRUE(pTestInt != NULL);
PyObject* pArg1 = PyObject_CallObject(pTestInt, NULL);
ASSERT_TRUE(pArg1 != NULL);
int result;
ASSERT_TRUE(PyArg_Parse(pArg1, "i", &result));
ASSERT_EQ(result, 100);

PyObject* pTestString = PyObject_GetAttrString(pModule, "TestString");
ASSERT_TRUE(pTestString != NULL);
PyObject* pArg2 = PyObject_CallObject(pTestString, NULL);
ASSERT_TRUE(pArg2 != NULL);
char* cwd;
ASSERT_TRUE(PyArg_Parse(pArg2, "s", &cwd));

// 3. C/C++ Run Python file
std::string file_name(cwd);
file_name.append("/test_install_check.py");
FILE* fp = _Py_fopen(file_name.c_str(), "r+");
ASSERT_TRUE(fp != NULL);
ASSERT_FALSE(PyRun_SimpleFile(fp, file_name.c_str()));

// Uninitialize python environment
Py_Finalize();
ASSERT_FALSE(Py_IsInitialized());
}
13 changes: 10 additions & 3 deletions python/paddle/fluid/tests/unittests/test_install_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,26 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function
import unittest
import paddle
import paddle.fluid as fluid
import os


class TestInstallCheck(unittest.TestCase):
def test_paddle_fluid(self):
fluid.install_check.run_check()
paddle.fluid.install_check.run_check()

def test_paddle_utils(self):
paddle.utils.run_check()


def TestInt():
return 100


def TestString():
return os.getcwd()


if __name__ == '__main__':
unittest.main()