fix(config): correct basicConfig option assignments and control flow

Fix dateformat and style variables reading from wrong option fields
(filemode instead of datefmt/style). Restructure control flow to
match CPython logic: early return when handlers exist and force is
not set, proper mutual exclusion of stream/filename/handlers args.
Remove unreachable code block.
This commit is contained in:
Tiara Rodney 2026-03-13 22:41:29 +01:00
parent e76d8fb77b
commit 9fed1ffe6b
No known key found for this signature in database
GPG key ID: 5CD8EC1D46106723

View file

@ -121,8 +121,8 @@ export function basicConfig(options: BasicConfigOptions) {
const filename = options.filename ?? null; const filename = options.filename ?? null;
const stream = options.stream ?? null; const stream = options.stream ?? null;
const filemode = options.filemode ?? 'a'; const filemode = options.filemode ?? 'a';
const dateformat = options.filemode ?? null; const dateformat = options.datefmt ?? null;
const style = options.filemode ?? '%'; const style = options.style ?? '%';
const level = options.level ?? null; const level = options.level ?? null;
if (!Object.keys(STYLES).includes(style)) { if (!Object.keys(STYLES).includes(style)) {
@ -139,42 +139,43 @@ export function basicConfig(options: BasicConfigOptions) {
} }
} }
if (handlers.length == 0) { if (MANAGER.root.handlers.length > 0 && !force) {
if (handlers === null && stream && filename) { return;
throw new ValueError(
"'stream' and 'filename' should not be specified together"
);
} }
else if (stream || filename) { if (handlers.length > 0) {
if (stream || filename) {
throw new ValueError( throw new ValueError(
"'stream' or 'filename' should not be specified together " + "'stream' or 'filename' should not be specified together " +
"with 'handlers'" "with 'handlers'"
); );
} }
}
if (handlers === null) { else {
var h: Handler; if (stream && filename) {
throw new ValueError(
"'stream' and 'filename' should not be specified together"
);
}
if (filename) { if (filename) {
if (filemode.match('b')) { errors = undefined } if (filemode.match('b')) { errors = undefined }
else { encoding = 'utf-8' } else { encoding = 'utf-8' }
h = new FileHandler({ handlers = [new FileHandler({
filename: filename, filename: filename,
filemode: filemode, filemode: filemode,
'encoding': encoding, encoding: encoding,
errors: errors errors: errors
}); })];
}
else {
handlers = [new StreamHandler(stream)];
}
} }
else { h = new StreamHandler(stream) } for (let i = 0; i < handlers.length; i += 1) {
const h = handlers[i];
handlers = [h];
}
for (var i = 0; i < handlers.length; i += 1) {
let h = handlers[i];
if (h.formatter === null) { if (h.formatter === null) {
h.formatter = new Formatter({ h.formatter = new Formatter({
@ -188,15 +189,4 @@ export function basicConfig(options: BasicConfigOptions) {
} }
if (level !== null) { MANAGER.root.setLevel(level) } if (level !== null) { MANAGER.root.setLevel(level) }
if (options) {
// runtime interface guard, please let me stay. 🥺
// the interface does not allow for additional members, but the
// runtime environment has no concept of interfaces. We can stick to
// the original implementation
const keys = Object.keys(options).join(', ');
throw new ValueError(`Unrecognised argument(s): ${keys}`);
}
}
} }