Skip to content

Commit b65436b

Browse files
committed
Add stub file and shim for gdb.LazyString class.
1 parent ce4f368 commit b65436b

File tree

5 files changed

+75
-3
lines changed

5 files changed

+75
-3
lines changed

gdbmongo/printer_protocol.py

+9
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,21 @@
2424
# pylint: disable=too-few-public-methods
2525

2626
if typing.TYPE_CHECKING:
27+
import gdb._lazy_string
2728
import gdb.printing
29+
30+
LazyString = gdb._lazy_string.LazyString
2831
PrettyPrinterProtocol = gdb.printing._PrettyPrinterProtocol
2932
SupportsChildren = gdb.printing._SupportsChildren
3033
SupportsDisplayHint = gdb.printing._SupportsDisplayHint
3134
SupportsToString = gdb.printing._SupportsToString
3235
else:
36+
import gdb
37+
38+
# gdb_pymodule_addobject() isn't called for its LazyString class so we expose the type here
39+
# ourselves. This attribute won't be used to construct LazyString instances directly. Instead,
40+
# it'll be used for type checking and satisfying Mypy.
41+
LazyString = type(gdb.parse_and_eval("(char *) 0").lazy_string(length=0))
3342

3443
class SupportsChildren(typing.Protocol):
3544
...

gdbmongo/stdlib_printers.pyi

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import typing
2020

2121
import gdb
2222

23+
from gdbmongo.printer_protocol import LazyString
24+
2325

2426
def num_elements(num: int, /) -> str:
2527
...
@@ -36,7 +38,7 @@ class __PrettyPrinterProtocol(gdb.printing._PrettyPrinterProtocol, typing.Protoc
3638
def __init__(self, typename: str, val: gdb.Value, /) -> None:
3739
...
3840

39-
def to_string(self) -> str | gdb.Value | None:
41+
def to_string(self) -> str | gdb.Value | LazyString | None:
4042
...
4143

4244
def children(self) -> typing.Iterator[typing.Tuple[str, gdb.Value]]:
@@ -164,7 +166,10 @@ class StdDequeIteratorPrinter(__PrettyPrinterProtocol):
164166

165167
class StdStringPrinter(__PrettyPrinterProtocol):
166168

167-
def __init__(self, typename: typing.Literal["std::basic_string"], val: gdb.Value, /):
169+
# Intentionally not constraining `typename` to be typing.Literal["std::basic_string"] because
170+
# the argument is used by StdStringPrinter to set its `new_string` attribute based on whether
171+
# the type contains "::__cxx11::basic_string".
172+
def __init__(self, typename: str, val: gdb.Value, /):
168173
...
169174

170175

stubs/gdb/_lazy_string.pyi

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
###
2+
# Copyright 2022-present MongoDB, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
###
16+
"""https://sourceware.org/gdb/onlinedocs/gdb/Lazy-Strings-In-Python.html"""
17+
18+
import typing
19+
20+
from gdb._type import Type
21+
from gdb._value import Value
22+
23+
24+
class LazyString:
25+
26+
@property
27+
def address(self) -> int:
28+
...
29+
30+
@property
31+
def length(self) -> int:
32+
...
33+
34+
@property
35+
def encoding(self) -> typing.Optional[str]:
36+
...
37+
38+
@property
39+
def type(self) -> Type:
40+
...
41+
42+
def value(self) -> Value:
43+
...

stubs/gdb/_value.pyi

+14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import typing
1919

20+
from gdb._lazy_string import LazyString
2021
from gdb._type import Type
2122

2223
ConstructibleFrom = bool | int | float | str | Value
@@ -87,6 +88,10 @@ class Value(typing.SupportsInt, typing.SupportsFloat):
8788
def dynamic_type(self) -> Type:
8889
...
8990

91+
@property
92+
def is_lazy(self) -> bool:
93+
...
94+
9095
def cast(self, typ: Type, /) -> Value:
9196
...
9297

@@ -101,3 +106,12 @@ class Value(typing.SupportsInt, typing.SupportsFloat):
101106

102107
def referenced_value(self) -> Value:
103108
...
109+
110+
def string(self, *, encoding: str = "", errors: str = "", length: int = -1) -> str:
111+
...
112+
113+
def lazy_string(self, *, encoding: str = "", length: int = -1) -> LazyString:
114+
...
115+
116+
def fetch_lazy(self) -> None:
117+
...

stubs/gdb/printing.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ https://sourceware.org/gdb/onlinedocs/gdb/gdb_002eprinting.html
2121
import abc
2222
import typing
2323

24+
from gdb._lazy_string import LazyString
2425
from gdb._objfile import Objfile
2526
from gdb._progspace import Progspace
2627
from gdb._value import Value
@@ -38,7 +39,7 @@ class _SupportsDisplayHint(typing.Protocol):
3839
class _SupportsToString(typing.Protocol):
3940

4041
@abc.abstractmethod
41-
def to_string(self) -> str | Value | None:
42+
def to_string(self) -> str | Value | LazyString | None:
4243
raise NotImplementedError
4344

4445

0 commit comments

Comments
 (0)