Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
cpython/Programs/_testembed.c at master · areshaha/cpython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
areshaha
/
cpython
Public
forked from
python/cpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
cpython
/
Programs
/
_testembed.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
178 lines (156 loc) · 5.25 KB
master
Breadcrumbs
cpython
/
Programs
/
_testembed.c
Copy path
Top
File metadata and controls
Code
Blame
178 lines (156 loc) · 5.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include
<Python.h>
#include
<inttypes.h>
#include
<stdio.h>
/*********************************************************
* Embedded interpreter tests that need a custom exe
*
* Executed via 'EmbeddingTests' in Lib/test/test_capi.py
*********************************************************/
static
void
_testembed_Py_Initialize
(
void
)
{
/* HACK: the "./" at front avoids a search along the PATH in
Modules/getpath.c */
Py_SetProgramName
(
L"./_testembed"
);
Py_Initialize
();
}
/*****************************************************
* Test repeated initialisation and subinterpreters
*****************************************************/
static
void
print_subinterp
(
void
)
{
/* Output information about the interpreter in the format
expected in Lib/test/test_capi.py (test_subinterps). */
PyThreadState
*
ts
=
PyThreadState_Get
();
PyInterpreterState
*
interp
=
ts
->
interp
;
int64_t
id
=
PyInterpreterState_GetID
(
interp
);
printf
(
"interp %"
PRId64
" <0x%"
PRIXPTR
">, thread state <0x%"
PRIXPTR
">: "
,
id
, (
uintptr_t
)
interp
, (
uintptr_t
)
ts
);
fflush
(
stdout
);
PyRun_SimpleString
(
"import sys;"
"print('id(modules) =', id(sys.modules));"
"sys.stdout.flush()"
);
}
static
int
test_repeated_init_and_subinterpreters
(
void
)
{
PyThreadState
*
mainstate
,
*
substate
;
#ifdef
WITH_THREAD
PyGILState_STATE
gilstate
;
#endif
int
i
,
j
;
for
(
i
=
0
;
i
<
15
;
i
++
) {
printf
(
"--- Pass %d ---\n"
,
i
);
_testembed_Py_Initialize
();
mainstate
=
PyThreadState_Get
();
#ifdef
WITH_THREAD
PyEval_InitThreads
();
PyEval_ReleaseThread
(
mainstate
);
gilstate
=
PyGILState_Ensure
();
#endif
print_subinterp
();
PyThreadState_Swap
(
NULL
);
for
(
j
=
0
;
j
<
3
;
j
++
) {
substate
=
Py_NewInterpreter
();
print_subinterp
();
Py_EndInterpreter
(
substate
);
}
PyThreadState_Swap
(
mainstate
);
print_subinterp
();
#ifdef
WITH_THREAD
PyGILState_Release
(
gilstate
);
#endif
PyEval_RestoreThread
(
mainstate
);
Py_Finalize
();
}
return
0
;
}
/*****************************************************
* Test forcing a particular IO encoding
*****************************************************/
static
void
check_stdio_details
(
const
char
*
encoding
,
const
char
*
errors
)
{
/* Output info for the test case to check */
if
(
encoding
) {
printf
(
"Expected encoding: %s\n"
,
encoding
);
}
else
{
printf
(
"Expected encoding: default\n"
);
}
if
(
errors
) {
printf
(
"Expected errors: %s\n"
,
errors
);
}
else
{
printf
(
"Expected errors: default\n"
);
}
fflush
(
stdout
);
/* Force the given IO encoding */
Py_SetStandardStreamEncoding
(
encoding
,
errors
);
_testembed_Py_Initialize
();
PyRun_SimpleString
(
"import sys;"
"print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));"
"print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));"
"print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));"
"sys.stdout.flush()"
);
Py_Finalize
();
}
static
int
test_forced_io_encoding
(
void
)
{
/* Check various combinations */
printf
(
"--- Use defaults ---\n"
);
check_stdio_details
(
NULL
,
NULL
);
printf
(
"--- Set errors only ---\n"
);
check_stdio_details
(
NULL
,
"ignore"
);
printf
(
"--- Set encoding only ---\n"
);
check_stdio_details
(
"latin-1"
,
NULL
);
printf
(
"--- Set encoding and errors ---\n"
);
check_stdio_details
(
"latin-1"
,
"replace"
);
/* Check calling after initialization fails */
Py_Initialize
();
if
(
Py_SetStandardStreamEncoding
(
NULL
,
NULL
)
==
0
) {
printf
(
"Unexpected success calling Py_SetStandardStreamEncoding"
);
}
Py_Finalize
();
return
0
;
}
/* *********************************************************
* List of test cases and the function that implements it.
*
* Names are compared case-sensitively with the first
* argument. If no match is found, or no first argument was
* provided, the names of all test cases are printed and
* the exit code will be -1.
*
* The int returned from test functions is used as the exit
* code, and test_capi treats all non-zero exit codes as a
* failed test.
*********************************************************/
struct
TestCase
{
const
char
*
name
;
int
(
*
func
)(
void
);
};
static
struct
TestCase
TestCases
[]
=
{
{
"forced_io_encoding"
,
test_forced_io_encoding
},
{
"repeated_init_and_subinterpreters"
,
test_repeated_init_and_subinterpreters
},
{
NULL
,
NULL
}
};
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
>
1
) {
for
(
struct
TestCase
*
tc
=
TestCases
;
tc
&&
tc
->
name
;
tc
++
) {
if
(
strcmp
(
argv
[
1
],
tc
->
name
)
==
0
)
return
(
*
tc
->
func
)();
}
}
/* No match found, or no test name provided, so display usage */
printf
(
"Python "
PY_VERSION
" _testembed executable for embedded interpreter tests\n"
"Normally executed via 'EmbeddingTests' in Lib/test/test_capi.py\n\n"
"Usage: %s TESTNAME\n\nAll available tests:\n"
,
argv
[
0
]);
for
(
struct
TestCase
*
tc
=
TestCases
;
tc
&&
tc
->
name
;
tc
++
) {
printf
(
" %s\n"
,
tc
->
name
);
}
/* Non-zero exit code will cause test_capi.py tests to fail.
This is intentional. */
return
-1
;
}
You can’t perform that action at this time.