Recent

Author Topic: RuntimeError 103 File not open  (Read 6942 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 18726
  • To Europe: simply sell USA bonds: dollar collapses
Re: RuntimeError 103 File not open
« Reply #30 on: July 20, 2025, 05:02:47 pm »
On Lazarus, there is a real terminal emulation at work. One of the virtual pts devices under ptmx control.
Btw I am missing a function in Baseunix:
Code: Pascal  [Select][+][-]
  1. function ttyname(fd: cint): PChar; cdecl; external 'c';
  2. // and my be
  3. {$if defined(unix) and not declared(STDIN_FILENO)}
  4. const
  5.   STDIN_FILENO = 0;
  6.   STDOUT_FILENO = 1;
  7.   STDERR_FILENO = 2;
  8. {$endif}
  9.  
  10. function fpttyname(fd: integer):string;
  11. var
  12.   p:PChar;
  13. begin
  14.   p:=ttyname(STDIN_FILENO );
  15.   Setstring(result,p, strlen(p);
  16. end;
I could not find it elsewhere.



« Last Edit: July 20, 2025, 05:05:12 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12630
  • FPC developer.
Re: RuntimeError 103 File not open
« Reply #31 on: July 20, 2025, 07:01:03 pm »
Ttyname is in unit termio.

And linking to libc is not allowed in baseunix anyways.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6306
  • Compiler Developer
Re: RuntimeError 103 File not open
« Reply #32 on: July 20, 2025, 09:40:21 pm »
That on Windows an error is thrown if a console is missing is simply an incompatibility with Linux and can be fixed.

It doesn't matter that it's an incompatibility to Linux. It would be an incompatiblity to Windows, Delphi and any other language working on that system allowing for development of GUI applications on Windows. And that is much more important, namely that the applications behave nicely like citizens of the system in question and by default a console application does not. Windows is not Linux. If you want Unix-like behavior on Windows then use Interix/Services for Unix Applications (till 7) or WSL (starting from 10).

Thaddy

  • Hero Member
  • *****
  • Posts: 18726
  • To Europe: simply sell USA bonds: dollar collapses
Re: RuntimeError 103 File not open
« Reply #33 on: July 21, 2025, 10:22:54 am »
I do all that except using interix, although I used the old posix layer in the past.
The point I don't get is that Lazarus and Delphi willingly expose the Windows programmer and user to preventable inconveniences.
C# does not seem to do that in GUI apps, it discards the output, effectively using a stringwriter that wraps to System.IO.TextWriter.Null (info from Microsoft) that makes the above arguments against largely invalid.
Like my code, invalid writes in C# are silently ignored.
Anyway, consider that part of the question closed. I simply run my 'NUL' unit on Windows - the same as Microsoft does in C# -if I want that version to match the linux version of my programs. Or want to be C# compatible.... :D ;) ;D
https://forum.lazarus.freepascal.org/index.php/topic,71779.msg561039.html#msg561039

GUI's in python (tested tkinter) also silently ignore writes, so has a valid and open IO device too.

So I apply it to releases, if anything to prevent things that lead to the original question: console writes originating from external libraries.
The only thing left for me to decide is to set IsConsole to true or leave IsConsole false. The latter looks the better option.
« Last Edit: July 21, 2025, 01:43:36 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

gues1

  • Guest
Re: RuntimeError 103 File not open
« Reply #34 on: July 21, 2025, 02:48:40 pm »
So I apply it to releases, if anything to prevent things that lead to the original question: console writes originating from external libraries.

So, if an external library send spam via HTTP you will lock the internet access of the application since you MUST use this lib ?  :o

If you like C# and want that FPC and Lazarus agree with it, you also agree with inline var that C# use ... and it also compatible with Delphi :D

This now it's OT but I think that talknig about compatibility with other program languages (topic about WINDOWS OS, not foreign languages) it's a nosense.

Thaddy

  • Hero Member
  • *****
  • Posts: 18726
  • To Europe: simply sell USA bonds: dollar collapses
Re: RuntimeError 103 File not open
« Reply #35 on: July 21, 2025, 02:54:16 pm »
No that has nothing to do with it: I just tested some popular GUI builders and concluded after consulting Microsoft that Lazarus is pretty much on its own here. That was the point in the first place.
I have nothing against quirks, but this is at least a quirk and seems unique to Delphi and Freepascal.
So I decided to include my solution in most of my projects.
Remember the original question, where third-party libraries run havoc????
I have not seen one valid reason not to do so after I verified some of the competition.
Not even the explanation by @PascalDragon, which merely outsets why it is the case.
Not the exception position from mainstream that Lazarus takes just for Windows.
I am fine leaving it as is under mode delphi.  >:(
Only I will add the unit anyway.
Deniers seem not to grasp what needs to be done.

I suspect Java also behaves like C# and Pyton with tkinter. Gonna test that and report back.
« Last Edit: July 21, 2025, 03:07:33 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

dury

  • Newbie
  • Posts: 1
Re: RuntimeError 103 File not open
« Reply #36 on: December 26, 2025, 04:58:08 pm »
Hi Thaddy, many thanks for your solution to the RT103. ,{$apptype console}  was missing in my code . Tou made my day ! Cheers !


uote author=Thaddy link=topic=71779.msg560864#msg560864 date=1752667979]
103 is one of the errors you can get when stdin/out/err or in real Pascal output/input and erroutput are not set.
On Windows, Lazarus defaults to that in a GUI application. (Why I do not know, it is a windows only issue)
If you add {$apptype console} these handles will be created.
You can subsequently hide the console window in code..

It is a really stupid decision that Lazarus works like that on Windows, but Hey who am I to complain?
The developers are aware of that platform only issue.  >:(
There is no valid reason for it to be not solved after years from a technical point of view.
Btw this non-feature is Delphi compatible....
And the solution is pretty easy:
Code: Pascal  [Select][+][-]
  1. program DummyHandles
  2.  
  3. var
  4.   dummy: TextFile;
  5.  
  6. begin
  7.   // Redirect standard output to NUL
  8.   Assign(dummy, 'NUL');
  9.   Rewrite(dummy);
  10.   Output := dummy;
  11.  
  12.   // Redirect standard input (optional, if used)
  13.   Assign(dummy, 'NUL');
  14.   Reset(dummy);
  15.   Input := dummy;
  16.  
  17.   // Redirect standard error
  18.   Assign(dummy, 'NUL');
  19.   Rewrite(dummy);
  20.   ErrOutput := dummy;
  21.  
  22.   // Test output
  23.   WriteLn('This will be discarded.');
  24.   WriteLn(ErrOutput, 'This error message is also discarded.');
  25.  
  26.   // Don't forget to close when done
  27.   Close(dummy);
  28. end.
I did not write that. Copilot did. But I tested it with Lazarus in a gui app.
I did replace the silly assignfile from that query and replaced it with the proper assign, though. And as such I removed sysutils.
Now this only has to be implemented deep in the sources, where file handling is abstracted away from the basics.
In Linux this is already the case: /dev/nul handles for GUI apps.

The effect is that the early 100 errors no longer occur.
Btw. AI help aside, I thought I had provided a similar example to solve it 15 years ago. NUL is a valid windows device handle.
[/quote]

Thaddy

  • Hero Member
  • *****
  • Posts: 18726
  • To Europe: simply sell USA bonds: dollar collapses
Re: RuntimeError 103 File not open
« Reply #37 on: December 27, 2025, 08:38:29 am »
Copilot was merely quoting that solution almost ad verbatim:
https://forum.lazarus.freepascal.org/index.php/topic,71779.msg561039.html#msg561039
« Last Edit: December 27, 2025, 08:42:38 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

 

TinyPortal © 2005-2018