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 stream = options.stream ?? null;
const filemode = options.filemode ?? 'a';
const dateformat = options.filemode ?? null;
const style = options.filemode ?? '%';
const dateformat = options.datefmt ?? null;
const style = options.style ?? '%';
const level = options.level ?? null;
if (!Object.keys(STYLES).includes(style)) {
@ -139,64 +139,54 @@ export function basicConfig(options: BasicConfigOptions) {
}
}
if (handlers.length == 0) {
if (handlers === null && stream && filename) {
if (MANAGER.root.handlers.length > 0 && !force) {
return;
}
if (handlers.length > 0) {
if (stream || filename) {
throw new ValueError(
"'stream' or 'filename' should not be specified together " +
"with 'handlers'"
);
}
}
else {
if (stream && filename) {
throw new ValueError(
"'stream' and 'filename' should not be specified together"
);
}
else if (stream || filename) {
throw new ValueError(
"'stream' or 'filename' should not be specified together" +
"with 'handlers'"
);
if (filename) {
if (filemode.match('b')) { errors = undefined }
else { encoding = 'utf-8' }
handlers = [new FileHandler({
filename: filename,
filemode: filemode,
encoding: encoding,
errors: errors
})];
}
if (handlers === null) {
var h: Handler;
if (filename) {
if (filemode.match('b')) { errors = undefined }
else { encoding = 'utf-8' }
h = new FileHandler({
filename: filename,
filemode: filemode,
'encoding': encoding,
errors: errors
});
}
else { h = new StreamHandler(stream) }
handlers = [h];
}
for (var i = 0; i < handlers.length; i += 1) {
let h = handlers[i];
if (h.formatter === null) {
h.formatter = new Formatter({
fmt: options.format ?? STYLES[style][1],
datefmt: dateformat,
style: style
});
}
MANAGER.root.addHandler(h);
}
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}`);
else {
handlers = [new StreamHandler(stream)];
}
}
for (let i = 0; i < handlers.length; i += 1) {
const h = handlers[i];
if (h.formatter === null) {
h.formatter = new Formatter({
fmt: options.format ?? STYLES[style][1],
datefmt: dateformat,
style: style
});
}
MANAGER.root.addHandler(h);
}
if (level !== null) { MANAGER.root.setLevel(level) }
}