Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
BornAgain
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mlz
BornAgain
Commits
73665ae6
Commit
73665ae6
authored
4 months ago
by
AlQuemist
Browse files
Options
Downloads
Patches
Plain Diff
PyInterpreter: minor cleanup and improvements
parent
ad03e951
No related branches found
Branches containing commit
No related tags found
1 merge request
!2755
Improve the construction of a 'Sample' from a Python script
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
PyCore/Embed/PyInterpreter.cpp
+13
-6
13 additions, 6 deletions
PyCore/Embed/PyInterpreter.cpp
with
13 additions
and
6 deletions
PyCore/Embed/PyInterpreter.cpp
+
13
−
6
View file @
73665ae6
...
@@ -151,8 +151,7 @@ PyObjectPtr PyInterpreter::import(const std::string& pymodule_name, const std::s
...
@@ -151,8 +151,7 @@ PyObjectPtr PyInterpreter::import(const std::string& pymodule_name, const std::s
{
{
ASSERT
(
!
pymodule_name
.
empty
());
ASSERT
(
!
pymodule_name
.
empty
());
if
(
!
path
.
empty
())
addPythonPath
(
path
);
addPythonPath
(
path
);
// import the module
// import the module
PyObject
*
pymodule
=
PyImport_ImportModule
(
pymodule_name
.
c_str
());
PyObject
*
pymodule
=
PyImport_ImportModule
(
pymodule_name
.
c_str
());
...
@@ -170,6 +169,10 @@ PyObjectPtr PyInterpreter::import(const std::string& pymodule_name, const std::s
...
@@ -170,6 +169,10 @@ PyObjectPtr PyInterpreter::import(const std::string& pymodule_name, const std::s
// Python stable ABI
// Python stable ABI
void
PyInterpreter
::
DecRef
(
PyObject
*
py_object
)
void
PyInterpreter
::
DecRef
(
PyObject
*
py_object
)
{
{
// Decrementing Python reference-count without
// Python initialized leads to memory access violation (segmentation fault)
ASSERT
(
PyInterpreter
::
isInitialized
());
Py_XDECREF
(
py_object
);
Py_XDECREF
(
py_object
);
}
}
...
@@ -225,7 +228,6 @@ void PyInterpreter::callFunction(PyObject* py_module, const std::string& fn_name
...
@@ -225,7 +228,6 @@ void PyInterpreter::callFunction(PyObject* py_module, const std::string& fn_name
Py_DecRef
(
pFunc
);
Py_DecRef
(
pFunc
);
}
}
std
::
string
PyInterpreter
::
runtimeInfo
()
std
::
string
PyInterpreter
::
runtimeInfo
()
{
{
std
::
string
result
;
std
::
string
result
;
...
@@ -305,8 +307,7 @@ std::string PyInterpreter::errorDescription(const std::string& title)
...
@@ -305,8 +307,7 @@ std::string PyInterpreter::errorDescription(const std::string& title)
PyObjectPtr
PyInterpreter
::
BornAgain
::
import
(
const
std
::
string
&
path
)
PyObjectPtr
PyInterpreter
::
BornAgain
::
import
(
const
std
::
string
&
path
)
{
{
if
(
!
path
.
empty
())
PyInterpreter
::
addPythonPath
(
path
);
PyInterpreter
::
addPythonPath
(
path
);
#ifndef _WIN32
#ifndef _WIN32
// store ctrl-C handler before Numpy messes it up
// store ctrl-C handler before Numpy messes it up
...
@@ -389,6 +390,10 @@ std::vector<std::string> PyInterpreter::BornAgain::listOfFunctions(const std::st
...
@@ -389,6 +390,10 @@ std::vector<std::string> PyInterpreter::BornAgain::listOfFunctions(const std::st
{
{
PyObjectPtr
tmpModule
{
PyInterpreter
::
BornAgain
::
importScript
(
script
,
path
)};
PyObjectPtr
tmpModule
{
PyInterpreter
::
BornAgain
::
importScript
(
script
,
path
)};
if
(
!
tmpModule
.
valid
())
throw
std
::
runtime_error
(
"PyInterpreter::BornAgain: "
"Cannot obtain the dictionary from the script module"
);
PyObject
*
pDict
=
PyModule_GetDict
(
tmpModule
.
get
());
PyObject
*
pDict
=
PyModule_GetDict
(
tmpModule
.
get
());
if
(
!
pDict
)
{
if
(
!
pDict
)
{
checkError
();
checkError
();
...
@@ -402,11 +407,13 @@ std::vector<std::string> PyInterpreter::BornAgain::listOfFunctions(const std::st
...
@@ -402,11 +407,13 @@ std::vector<std::string> PyInterpreter::BornAgain::listOfFunctions(const std::st
while
(
PyDict_Next
(
pDict
,
&
pos
,
&
key
,
&
value
))
{
while
(
PyDict_Next
(
pDict
,
&
pos
,
&
key
,
&
value
))
{
if
(
PyCallable_Check
(
value
))
{
if
(
PyCallable_Check
(
value
))
{
std
::
string
func_name
{
PyInterpreter
::
pyStrtoString
(
key
)};
std
::
string
func_name
{
PyInterpreter
::
pyStrtoString
(
key
)};
// do not import callables whose names begin with '__'
if
(
func_name
.
find
(
"__"
)
==
std
::
string
::
npos
)
if
(
func_name
.
find
(
"__"
)
==
std
::
string
::
npos
)
fn_names
.
push_back
(
func_name
);
fn_names
.
push_back
(
func_name
);
}
}
}
}
PyDict_Clear
(
pDict
);
// do not accumulate history between imports
Py_DecRef
(
pDict
);
return
fn_names
;
return
fn_names
;
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment