this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#include "Python.h"
3
4#include "capi-fixture.h"
5#include "capi-testing.h"
6
7namespace py {
8namespace testing {
9
10using PathconfigExtensionApiTest = ExtensionApi;
11
12// Defaults to `/usr/local` only if python is run from a build dir
13// TODO(T67620993): Compare against built-in defaults
14// TODO(T67625250): Make sure we test against different values depending on
15// whether Pyro is being run from a build directory or not.
16TEST_F(PathconfigExtensionApiTest, GetPrefixReturnsUsrLocalPyro) {
17 EXPECT_STREQ(Py_GetPrefix(), L"/usr/local");
18 EXPECT_STREQ(Py_GetExecPrefix(), L"/usr/local");
19}
20
21TEST_F(PathconfigExtensionApiTest, SetPathClearsPrefixAndExecPrefix) {
22 Py_SetPath(L"test");
23 EXPECT_STREQ(Py_GetPrefix(), L"");
24 EXPECT_STREQ(Py_GetExecPrefix(), L"");
25 EXPECT_STREQ(Py_GetPath(), L"test");
26}
27
28TEST(PathconfigExtensionApiTestNoFixture, PySetPathSetsSysPath) {
29 // Because we can't rely on os.h (due to tests being
30 // shared between CPython and Pyro, we can't link the runtime), we use
31 // the default sys.path's element as the canonical location of paths.
32 resetPythonEnv();
33 Py_Initialize();
34 std::wstring old_path(Py_GetPath());
35 Py_FinalizeEx();
36
37 int length = old_path.length() + 30;
38 std::unique_ptr<wchar_t[]> sys_path(new wchar_t[length]);
39 std::swprintf(sys_path.get(), length, L"%ls:/usr/local/setbyapi",
40 old_path.c_str());
41
42 resetPythonEnv();
43 Py_SetPath(sys_path.get());
44 Py_Initialize();
45
46 {
47 PyImport_ImportModule("sys");
48 ASSERT_EQ(PyErr_Occurred(), nullptr);
49 PyObjectPtr path(moduleGet("sys", "path"));
50 ASSERT_NE(path, nullptr);
51 PyObjectPtr path_last(PySequence_GetItem(path, -1));
52 ASSERT_NE(path_last, nullptr);
53 const char* cstring = PyUnicode_AsUTF8AndSize(path_last, nullptr);
54 EXPECT_STREQ(cstring, "/usr/local/setbyapi");
55 }
56
57 PyErr_Clear();
58 Py_FinalizeEx();
59 std::setlocale(LC_CTYPE, "C");
60}
61
62} // namespace testing
63} // namespace py