Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pystencils-sfg
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
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
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
pycodegen
pystencils-sfg
Commits
3a4d360d
Commit
3a4d360d
authored
4 months ago
by
Frederik Hennig
Browse files
Options
Downloads
Patches
Plain Diff
update CppType to hold its positional and kw arguments and use them for cloning
parent
1a13ccaf
No related branches found
No related tags found
1 merge request
!12
Improve versatility and robustness of `cpptype`, and document it in the user guide
Pipeline
#71532
passed
4 months ago
Stage: Code Quality
Stage: Tests
Stage: Documentation
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/pystencilssfg/lang/types.py
+70
-27
70 additions, 27 deletions
src/pystencilssfg/lang/types.py
tests/lang/test_types.py
+34
-2
34 additions, 2 deletions
tests/lang/test_types.py
with
104 additions
and
29 deletions
src/pystencilssfg/lang/types.py
+
70
−
27
View file @
3a4d360d
from
__future__
import
annotations
from
typing
import
Any
,
Iterable
from
typing
import
Any
,
Iterable
,
Sequence
,
Mapping
from
abc
import
ABC
from
dataclasses
import
dataclass
import
string
from
pystencils.types
import
PsType
,
PsPointerType
,
PsCustomType
from
.headers
import
HeaderFile
...
...
@@ -24,8 +28,71 @@ class VoidType(PsType):
void
=
VoidType
()
class
_TemplateArgFormatter
(
string
.
Formatter
):
def
format_field
(
self
,
arg
,
format_spec
):
if
isinstance
(
arg
,
PsType
):
arg
=
arg
.
c_string
()
return
super
().
format_field
(
arg
,
format_spec
)
def
check_unused_args
(
self
,
used_args
:
set
[
int
|
str
],
args
:
Sequence
,
kwargs
:
Mapping
[
str
,
Any
]
)
->
None
:
max_args_len
:
int
=
max
((
k
for
k
in
used_args
if
isinstance
(
k
,
int
)),
default
=-
1
)
+
1
if
len
(
args
)
>
max_args_len
:
raise
ValueError
(
f
"
Too many positional arguments: Expected
{
max_args_len
}
, but got
{
len
(
args
)
}
"
)
extra_keys
=
set
(
kwargs
.
keys
())
-
used_args
# type: ignore
if
extra_keys
:
raise
ValueError
(
f
"
Extraneous keyword arguments:
{
extra_keys
}
"
)
@dataclass
(
frozen
=
True
)
class
_TemplateArgs
:
pargs
:
tuple
[
Any
,
...]
kwargs
:
tuple
[
tuple
[
str
,
Any
],
...]
class
CppType
(
PsCustomType
,
ABC
):
includes
:
frozenset
[
HeaderFile
]
template_string
:
str
def
__new__
(
# type: ignore
cls
,
*
args
,
ref
:
bool
=
False
,
**
kwargs
,
)
->
CppType
|
Ref
:
if
ref
:
obj
=
cls
(
*
args
,
**
kwargs
)
return
Ref
(
obj
)
else
:
return
super
().
__new__
(
cls
)
def
__init__
(
self
,
*
template_args
,
const
:
bool
=
False
,
**
template_kwargs
):
# Support for cloning CppTypes
if
template_args
and
isinstance
(
template_args
[
0
],
_TemplateArgs
):
assert
not
template_kwargs
targs
=
template_args
[
0
]
pargs
=
targs
.
pargs
kwargs
=
dict
(
targs
.
kwargs
)
else
:
pargs
=
template_args
kwargs
=
template_kwargs
targs
=
_TemplateArgs
(
pargs
,
tuple
(
sorted
(
kwargs
.
items
(),
key
=
lambda
t
:
t
[
0
]))
)
formatter
=
_TemplateArgFormatter
()
name
=
formatter
.
format
(
self
.
template_string
,
*
pargs
,
**
kwargs
)
self
.
_targs
=
targs
super
().
__init__
(
name
,
const
=
const
)
def
__args__
(
self
)
->
tuple
[
Any
,
...]:
return
(
self
.
_targs
,)
@property
def
required_headers
(
self
)
->
set
[
str
]:
...
...
@@ -42,35 +109,11 @@ def cpptype(typestr: str, include: str | HeaderFile | Iterable[str | HeaderFile]
else
:
headers
=
list
(
include
)
def
_fixarg
(
template_arg
):
if
isinstance
(
template_arg
,
PsType
):
return
template_arg
.
c_string
()
else
:
return
str
(
template_arg
)
class
TypeClass
(
CppType
):
template_string
=
typestr
includes
=
frozenset
(
HeaderFile
.
parse
(
h
)
for
h
in
headers
)
class
TypeClassFactory
(
TypeClass
):
def
__new__
(
# type: ignore
cls
,
*
template_args
,
const
:
bool
=
False
,
ref
:
bool
=
False
,
**
template_kwargs
,
)
->
PsType
:
template_args
=
tuple
(
_fixarg
(
arg
)
for
arg
in
template_args
)
template_kwargs
=
{
key
:
_fixarg
(
value
)
for
key
,
value
in
template_kwargs
.
items
()
}
name
=
typestr
.
format
(
*
template_args
,
**
template_kwargs
)
obj
:
PsType
=
TypeClass
(
name
,
const
)
if
ref
:
obj
=
Ref
(
obj
)
return
obj
return
TypeClassFactory
return
TypeClass
class
Ref
(
PsType
):
...
...
This diff is collapsed.
Click to expand it.
tests/lang/test_types.py
+
34
−
2
View file @
3a4d360d
import
pytest
from
pystencilssfg.lang
import
cpptype
,
HeaderFile
,
Ref
,
strip_ptr_ref
from
pystencils
import
create_type
from
pystencils.types
import
constify
,
deconstify
def
test_cpptypes
():
tclass
=
cpptype
(
"
std::vector< {
T
}, {
Allocator
} >
"
,
"
<vector>
"
)
tclass
=
cpptype
(
"
std::vector< {}, {} >
"
,
"
<vector>
"
)
vec_type
=
tclass
(
T
=
create_type
(
"
float32
"
),
Allocator
=
"
std::allocator< float >
"
)
vec_type
=
tclass
(
create_type
(
"
float32
"
),
"
std::allocator< float >
"
)
assert
str
(
vec_type
).
strip
()
==
"
std::vector< float, std::allocator< float > >
"
assert
(
tclass
.
includes
...
...
@@ -14,8 +16,38 @@ def test_cpptypes():
==
{
HeaderFile
(
"
vector
"
,
system_header
=
True
)}
)
# Cloning
assert
deconstify
(
constify
(
vec_type
))
==
vec_type
# Duplicate Equality
assert
tclass
(
create_type
(
"
float32
"
),
"
std::allocator< float >
"
)
==
vec_type
# Not equal with different argument even though it produces the same string
assert
tclass
(
"
float
"
,
"
std::allocator< float >
"
)
!=
vec_type
# The same with keyword arguments
tclass
=
cpptype
(
"
std::vector< {T}, {Allocator} >
"
,
"
<vector>
"
)
vec_type
=
tclass
(
T
=
create_type
(
"
float32
"
),
Allocator
=
"
std::allocator< float >
"
)
assert
str
(
vec_type
).
strip
()
==
"
std::vector< float, std::allocator< float > >
"
assert
deconstify
(
constify
(
vec_type
))
==
vec_type
def
test_cpptype_invalid_construction
():
tclass
=
cpptype
(
"
std::vector< {}, {Allocator} >
"
,
"
<vector>
"
)
with
pytest
.
raises
(
IndexError
):
_
=
tclass
(
Allocator
=
"
SomeAlloc
"
)
with
pytest
.
raises
(
KeyError
):
_
=
tclass
(
"
int
"
)
with
pytest
.
raises
(
ValueError
,
match
=
"
Too many positional arguments
"
):
_
=
tclass
(
"
int
"
,
"
bogus
"
,
Allocator
=
"
SomeAlloc
"
)
with
pytest
.
raises
(
ValueError
,
match
=
"
Extraneous keyword arguments
"
):
_
=
tclass
(
"
int
"
,
Allocator
=
"
SomeAlloc
"
,
bogus
=
2
)
def
test_cpptype_const
():
tclass
=
cpptype
(
"
std::vector< {T} >
"
,
"
<vector>
"
)
...
...
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