Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 03240dbc71604cbab64ae80c693f0e7e3f59cbd61485734a70f298d6ce3389ec
Ticket: 8a13caa31dd8e3a546b8d44c235bb98cd52bd774
Setting of system encoding isn't applied to stdout on certain circumstances
User & Date: apnadkarni on 2025-08-18 05:47:52
Changes

  1. icomment:
    So, as suspected, it is not an encoding issue as much as initialization of std channels. The test failure is a result of `stdout` being already open and configured for utf-8 when the child process in encoding-24.3 calls `encoding system`. Thus the output from the child is encoded in utf-8, as the `encoding system` command has no effect on existing channels.
    
    The difference between the `main` or `core-9-0-branch` (which do not see the test failure) and the `apn-oo-lazy-init-sbmod` branch is that tcltest calls `Tcl_OOInitStubs` as part of its initialization. That in turn does a package require on `tcl::oo`. 
    
    In the case of the `apn-oo-lazy-init-sbmod`, `tcl::oo` is only loaded on demand (via `oo::_init`) so the package loading code runs when `Tcl_OOInitStubs` is called and at some point loads `tclIndex`. The call to open this file leads to the initialization of the channel table which as a side effect creates the standard channel including stdout. As stdout already exists, its encoding is not changed by `encoding system`.
    
    In the `main` branch, oo is already loaded as a package as part of Tcl initialization. Thus the package require does not go rummaging for tclIndex and stdout therefore remains uninitialized and is created only after the `encoding system` command and therefore has the encoding specified in `encoding system`.
    
    As an illustration, if the child process script in encoding-24.3 is modified to add a `package require platform` at the top, the same failure is seen in the main and core-9-0-branch as well.
    
    First, I think this is a bug in encoding-24.3 as it presumes stdout does not exist when a script is run and Tcl does not guarantee that is the case. Fixing the test by adding a fconfigure (as Sergey originally reported) may be one option but I am not sure if the original bug specifically was intended to the system encoding being attached to the standard channel.
    
    To fix the issue regardless, one possibility is to change `Tcl_OOInitStubs` to statically load the package instead of doing a package require. I think the package require was only needed for 8.5 where oo was not built in. Doing a load, assuming it is feasible, would bypass the package implementation and leave channels unchanged. However, I don't think this is really mandated or absolutely necessary since scripts should not really assume the state of std channels at startup.
    
    As an aside, I'm surprised the test output prints nuls (\x00) as the line read. That is a separate issue that might need to be looked at (perhaps it's the tcltest output ascii-fication getting it wrong).
    
  2. login: "apnadkarni"
  3. mimetype: "text/x-markdown"