f



Solution [ Perl/Tk: 'selection conversion left too many bytes unconverted' error on large text selection ]

After I got bitten enough by this error and found no solution on the
web I had to debug it myself. For me it looks like the following:

- text get selected from Tk::Text

- TkSelPropProc in ./pTk/mTk/unix/tkUnixSelect.c gets called to handle
   the selection which determines that the display supports utf-8 and
  thus  gets Encoding::utf8 as the responsable encoder (see
  Encode::Encoding  for the interface)

- then it calls Tcl_UtfToExternal to convert the selection from
  internal  format to utf8,  this function is defined in encGlue.c and
  just  calls  CallEncode in the same file.

- callEncode calls the perl Method 'encode' on the encoding object,
  e.g.  Encode::utf8->encode( $string, $check ).  According to the
  interface  this method should in-place modify $string, delete the
  converted chars  from $string and return the converted string. So
  callEncode checks  if $string is empty at the end, e.g. that
  everything  got converted fine.  If not it will set the error code to

  TCL_CONVERT_UNKNOWN which  will be the cause of the PANIC.
  According to the example program below encode fails to convert
  everything and thus  the panic gets triggered. But if you set the
  variable  $check at the call to encode   everything  will be fine.
  Setting the variable is easy and this is the simple solution which
  works  for me:

            $Tk::encodeFallback=1

Example code to trigger the problem in Encode::utf8

  use Encode;
  my $text = (( '1234456789 ' x 10 )."\n" ) x 1000;
  for my $check ( 0,1 ) {
      my $t = $text;
      my $v = Encode::utf8->encode( $t,$check );
      printf "encode with check=$check %s\n",
          length($t) ? 'failed':'succeeded';
  }

which gives

  encode with check=0 failed
  encode with check=1 succeeded

0
coyote
12/14/2005 5:32:31 PM
comp.lang.perl.tk 4721 articles. 0 followers. pharrendorf (19) is leader. Post Follow

1 Replies
687 Views

Similar Articles

[PageSpeed] 52

coyote.frank@gmx.net wrote:
> After I got bitten enough by this error and found no solution on the
> web I had to debug it myself. For me it looks like the following:
<snip>
>             $Tk::encodeFallback=1

Argh! So simple! Thank you so much for this - works like a charm!

Phew - now I can go back to pasting whole texts rather than in little 
chunks...

MB
0
Matthew
12/20/2005 2:43:54 AM
Reply: