Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GH-134394: Add native support for t-strings in the stdlib logging library#134395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
pR0Ps wants to merge2 commits intopython:main
base:main
Choose a base branch
Loading
frompR0Ps:feature/tstring-logging
Open
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
Add native support for t-strings in the stdlib logging library
  • Loading branch information
@pR0Ps
pR0Ps committedMay 20, 2025
commit3b9610a3388df3899304f76326d24e1a41cb32e9
39 changes: 32 additions & 7 deletionsLib/logging/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,7 @@
from types import GenericAlias
from string import Template
from string import Formatter as StrFormatter
from string.templatelib import Template as TStringTemplate, Interpolation


__all__ = ['BASIC_FORMAT', 'BufferingFormatter', 'CRITICAL', 'DEBUG', 'ERROR',
Expand DownExpand Up@@ -289,11 +290,14 @@ class LogRecord(object):

LogRecord instances are created every time something is logged. They
contain all the information pertinent to the event being logged. The
main information passed in is in msg and args, which are combined
using str(msg) % args to create the message field of the record. The
record also includes information such as when the record was created,
the source line where the logging call was made, and any exception
information to be logged.
main information passed in is in msg and args. These are combined
using str(msg) % args to create the message field of the record. A
special case here is that if the msg is a t-string, it will be
formatted as if it was an f-string without using the provided args.

The record also includes information such as when the record was
created, the source line where the logging call was made, and any
exception information to be logged.
"""
def __init__(self, name, level, pathname, lineno,
msg, args, exc_info, func=None, sinfo=None, **kwargs):
Expand All@@ -303,7 +307,14 @@ def __init__(self, name, level, pathname, lineno,
ct = time.time_ns()
self.name = name
self.msg = msg
#

# When logging a t-string, pull the values out of it and prepend
# them to any provided args
if isinstance(msg, TStringTemplate):
args = (
{x.expression: x.value for x in msg if isinstance(x, Interpolation)},
*args
)
# The following statement allows passing of a dictionary as a sole
# argument, so that you can do something like
# logging.debug("a %(a)d b %(b)s", {'a':1, 'b':2})
Expand DownExpand Up@@ -394,7 +405,21 @@ def getMessage(self):

Return the message for this LogRecord after merging any user-supplied
arguments with the message.
"""
If the messsage is a t-string object it will be formatted in the same
way as an f-string without any extra user-supplied arguments.
"""
if isinstance(self.msg, TStringTemplate):
return "".join(
(
_str_formatter.format_field(
_str_formatter.convert_field(x.value, x.conversion),
x.format_spec
)
) if isinstance(x, Interpolation)
else x
for x in self.msg
)

msg = str(self.msg)
if self.args:
msg = msg % self.args
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp