Skip to content

Commit 5c4f3b4

Browse files
committed
py scope
1 parent 0014762 commit 5c4f3b4

File tree

4 files changed

+87
-41
lines changed

4 files changed

+87
-41
lines changed

backend/Python/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ target_sources(ScriptX PRIVATE
99
${CMAKE_CURRENT_LIST_DIR}/PyNative.cc
1010
${CMAKE_CURRENT_LIST_DIR}/PyNative.hpp
1111
${CMAKE_CURRENT_LIST_DIR}/PyReference.hpp
12+
${CMAKE_CURRENT_LIST_DIR}/PyScope.h
13+
${CMAKE_CURRENT_LIST_DIR}/PyScope.cc
1214
${CMAKE_CURRENT_LIST_DIR}/PyUtils.cc
1315
${CMAKE_CURRENT_LIST_DIR}/PyValue.cc
1416
)

backend/Python/PyScope.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making ScriptX available.
3+
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "PyScope.h"
19+
20+
// reference
21+
// https://docs.python.org/3.8/c-api/init.html#thread-state-and-the-global-interpreter-lock
22+
23+
namespace script::py_backend {
24+
25+
EngineScopeImpl::EngineScopeImpl(PyEngine &, PyEngine *) { gilState_ = PyGILState_Ensure(); }
26+
EngineScopeImpl::~EngineScopeImpl() { PyGILState_Release(gilState_); }
27+
28+
ExitEngineScopeImpl::ExitEngineScopeImpl(PyEngine &) { threadState = PyEval_SaveThread(); }
29+
ExitEngineScopeImpl::~ExitEngineScopeImpl() { PyEval_RestoreThread(threadState); }
30+
31+
} // namespace script::py_backend

backend/Python/PyScope.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making ScriptX available.
3+
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#pragma once
19+
#include "../../src/Reference.h"
20+
#include "PyHelper.h"
21+
22+
namespace script::py_backend {
23+
24+
class PyEngine;
25+
26+
class EngineScopeImpl {
27+
PyGILState_STATE gilState_ = PyGILState_UNLOCKED;
28+
29+
public:
30+
explicit EngineScopeImpl(PyEngine &, PyEngine *);
31+
32+
~EngineScopeImpl();
33+
};
34+
35+
class ExitEngineScopeImpl {
36+
PyThreadState *threadState = nullptr;
37+
38+
public:
39+
explicit ExitEngineScopeImpl(PyEngine &);
40+
41+
~ExitEngineScopeImpl();
42+
};
43+
44+
class StackFrameScopeImpl {
45+
public:
46+
explicit StackFrameScopeImpl(PyEngine &) {}
47+
48+
template <typename T>
49+
Local<T> returnValue(const Local<T> &localRef) {
50+
return localRef;
51+
}
52+
};
53+
} // namespace script::py_backend

backend/Python/trait/TraitScope.h

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,51 +17,11 @@
1717

1818
#pragma once
1919

20+
#include "../PyScope.h"
2021
#include "TraitEngine.h"
2122

2223
namespace script {
2324

24-
namespace py_backend {
25-
26-
class EngineScopeImpl {
27-
public:
28-
explicit EngineScopeImpl(PyEngine &, PyEngine *) {
29-
// enter engine;
30-
}
31-
32-
~EngineScopeImpl() {
33-
// exit engine;
34-
}
35-
};
36-
37-
class ExitEngineScopeImpl {
38-
public:
39-
explicit ExitEngineScopeImpl(PyEngine &) {
40-
// exit current entered engine
41-
}
42-
43-
~ExitEngineScopeImpl() {
44-
// reenter engine;
45-
}
46-
};
47-
48-
class StackFrameScopeImpl {
49-
public:
50-
explicit StackFrameScopeImpl(PyEngine &) {
51-
// enter stack;
52-
}
53-
54-
~StackFrameScopeImpl() {
55-
// exit stack;
56-
}
57-
58-
template <typename T>
59-
Local<T> returnValue(const Local<T> &localRef) {
60-
return localRef;
61-
}
62-
};
63-
} // namespace py_backend
64-
6525
template <>
6626
struct internal::ImplType<EngineScope> {
6727
using type = py_backend::EngineScopeImpl;

0 commit comments

Comments
 (0)