Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
gcc-python-plugin/testcpybuilder.py at master · Python3pkg/gcc-python-plugin · 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 }}
Uh oh!
There was an error while loading.
Please reload this page
.
Python3pkg
/
gcc-python-plugin
Public
forked from
davidmalcolm/gcc-python-plugin
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
gcc-python-plugin
/
testcpybuilder.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
214 lines (171 loc) · 6.77 KB
master
Breadcrumbs
gcc-python-plugin
/
testcpybuilder.py
Copy path
Top
File metadata and controls
Code
Blame
214 lines (171 loc) · 6.77 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Copyright 2011 David Malcolm <dmalcolm@redhat.com>
# Copyright 2011 Red Hat, Inc.
#
# This is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# <http://www.gnu.org/licenses/>.
from
distutils
import
sysconfig
as
sc
import
os
import
shutil
from
subprocess
import
Popen
,
PIPE
import
sys
import
sysconfig
import
tempfile
import
unittest
import
six
from
cpybuilder
import
*
def
get_module_filename
(
name
):
# Python 3.2 onwards embeds the SOABI variable in module filenames
# (see PEP 3149):
SOABI
=
sysconfig
.
get_config_var
(
'SOABI'
)
if
SOABI
:
return
'%s.%s.so'
%
(
name
,
SOABI
)
if
hasattr
(
sys
,
"getobjects"
):
# debug build of Python:
# FIXME: this is a Fedora-ism:
return
'%s_d.so'
%
name
else
:
# regular (optimized) build of Python:
return
'%s.so'
%
name
class
CompilationError
(
CommandError
):
def
__init__
(
self
,
bm
):
CommandError
.
__init__
(
self
,
bm
.
out
,
bm
.
err
,
bm
.
p
)
self
.
bm
=
bm
def
_describe_activity
(
self
):
return
'compiling: %s'
%
' '
.
join
(
self
.
bm
.
args
)
class
BuiltModule
:
"""A test build of a SimpleModule using sys.executable, done in a tempdir"""
def
__init__
(
self
,
sm
):
self
.
sm
=
sm
def
write_src
(
self
,
modname
,
extra_cflags
=
None
):
self
.
tmpdir
=
tempfile
.
mkdtemp
()
self
.
srcfile
=
os
.
path
.
join
(
self
.
tmpdir
,
'%s.c'
%
modname
)
self
.
modfile
=
os
.
path
.
join
(
self
.
tmpdir
,
get_module_filename
(
modname
))
f
=
open
(
self
.
srcfile
,
'w'
)
f
.
write
(
self
.
sm
.
cu
.
as_str
())
f
.
close
()
def
compile_src
(
self
,
extra_cflags
=
None
):
self
.
args
=
[
os
.
environ
.
get
(
'CC'
,
'gcc'
)]
self
.
args
+=
[
'-o'
,
self
.
modfile
]
self
.
args
+=
[
'-I'
+
sc
.
get_python_inc
(),
'-I'
+
sc
.
get_python_inc
(
plat_specific
=
True
)]
self
.
args
+=
sc
.
get_config_var
(
'CFLAGS'
).
split
()
self
.
args
+=
[
'-Wall'
,
'-Werror'
]
# during testing
# on some builds of Python, CFLAGS does not contain -fPIC, but it
# appears to still be necessary:
self
.
args
+=
[
'-fPIC'
]
self
.
args
+=
[
'-shared'
]
# not sure why this is necessary
if
extra_cflags
:
self
.
args
+=
extra_cflags
self
.
args
+=
[
self
.
srcfile
]
# print(self.args)
env
=
dict
(
os
.
environ
)
env
[
'LC_ALL'
]
=
'C'
# Invoke the compiler:
self
.
p
=
Popen
(
self
.
args
,
env
=
env
,
stdout
=
PIPE
,
stderr
=
PIPE
)
self
.
out
,
self
.
err
=
self
.
p
.
communicate
()
if
six
.
PY3
:
self
.
out
=
self
.
out
.
decode
()
self
.
err
=
self
.
err
.
decode
()
c
=
self
.
p
.
wait
()
if
c
!=
0
:
raise
CompilationError
(
self
)
assert
os
.
path
.
exists
(
self
.
modfile
)
# print(self.modfile)
def
build
(
self
,
modname
,
extra_cflags
=
None
):
self
.
write_src
(
modname
)
self
.
compile_src
(
extra_cflags
)
def
cleanup
(
self
):
shutil
.
rmtree
(
self
.
tmpdir
)
class
SimpleTest
(
unittest
.
TestCase
):
# We'll be manipulating sys.path during the test
# Save a copy, and restore it after each test:
def
setUp
(
self
):
self
.
saved_sys_path
=
sys
.
path
sys
.
path
=
sys
.
path
[:]
def
tearDown
(
self
):
sys
.
path
=
self
.
saved_sys_path
def
test_simple_compilation
(
self
):
# Verify building and running a trivial module (against multiple Python runtimes)
MODNAME
=
'simple_compilation'
sm
=
SimpleModule
()
sm
.
cu
.
add_decl
(
"""
static PyObject *
example_hello(PyObject *self, PyObject *args);
"""
)
sm
.
cu
.
add_defn
(
"""
static PyObject *
example_hello(PyObject *self, PyObject *args)
{
return Py_BuildValue("s", "Hello world!");
}
"""
)
methods
=
PyMethodTable
(
'example_methods'
,
[
PyMethodDef
(
'hello'
,
'example_hello'
,
METH_VARARGS
,
'Return a greeting.'
)])
sm
.
cu
.
add_defn
(
methods
.
c_defn
())
sm
.
add_module_init
(
MODNAME
,
modmethods
=
methods
,
moddoc
=
'This is a doc string'
)
# print(sm.cu.as_str())
# Build the module:
bm
=
BuiltModule
(
sm
)
bm
.
build
(
MODNAME
)
# Verify that it built:
sys
.
path
.
append
(
bm
.
tmpdir
)
import
simple_compilation
self
.
assertEqual
(
simple_compilation
.
hello
(),
'Hello world!'
)
# Cleanup successful test runs:
bm
.
cleanup
()
def
test_module_with_type
(
self
):
# Verify an extension with a type
MODNAME
=
'module_with_type'
sm
=
SimpleModule
()
sm
.
cu
.
add_decl
(
"""
struct PyExampleType {
PyObject_HEAD
int i;
};
"""
)
sm
.
cu
.
add_defn
(
"PyObject *
\n
"
"example_Example_repr(PyObject * self)
\n
"
"{
\n
"
"#if PY_MAJOR_VERSION < 3
\n
"
" return PyString_FromString(
\"
example.ExampleType('')
\"
);
\n
"
"#else
\n
"
" return PyUnicode_FromString(
\"
example.ExampleType('')
\"
);
\n
"
"#endif
\n
"
"}
\n
"
)
sm
.
add_type_object
(
name
=
'example_ExampleType'
,
localname
=
'ExampleType'
,
tp_name
=
'example.ExampleType'
,
struct_name
=
'struct PyExampleType'
,
tp_repr
=
'example_Example_repr'
)
sm
.
add_module_init
(
MODNAME
,
modmethods
=
None
,
moddoc
=
'This is a doc string'
)
# print sm.cu.as_str()
# Build the module:
bm
=
BuiltModule
(
sm
)
bm
.
build
(
MODNAME
)
# Verify that it built:
sys
.
path
.
append
(
bm
.
tmpdir
)
import
module_with_type
self
.
assertEqual
(
repr
(
module_with_type
.
ExampleType
()),
"example.ExampleType('')"
)
# Cleanup successful test runs:
bm
.
cleanup
()
def
test_version_parsing
(
self
):
vi
=
PyVersionInfo
.
from_text
(
"sys.version_info(major=2, minor=7, micro=1, releaselevel='final', serial=0)"
)
self
.
assertEqual
(
vi
,
PyVersionInfo
(
major
=
2
,
minor
=
7
,
micro
=
1
,
releaselevel
=
'final'
,
serial
=
0
))
# "sys.version_info(major=2, minor=7, micro=1, releaselevel='final', serial=0)"
# "sys.version_info(major=3, minor=2, micro=0, releaselevel='candidate', serial=1)"
if
__name__
==
'__main__'
:
unittest
.
main
()
You can’t perform that action at this time.