Skip to content

Commit f8fdb5e

Browse files
fix: warn Filehandle opened only for input for print/say to read-only fh (#439)
When printing to a filehandle opened for reading only, the builtin print and say now set errno to Bad file descriptor, emit the warning (when warnings enabled via io category), and return undef. This was the last remaining Text::CSV failure: t/70_rt.t test 72. All 40 Text::CSV test files now pass (52723/52723 subtests). Generated with [Devin](https://cli.devin.ai/docs) Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent a7261e4 commit f8fdb5e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/main/java/org/perlonjava/core/Configuration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public final class Configuration {
3333
* Automatically populated by Gradle/Maven during build.
3434
* DO NOT EDIT MANUALLY - this value is replaced at build time.
3535
*/
36-
public static final String gitCommitId = "4aafb6057";
36+
public static final String gitCommitId = "a7261e446";
3737

3838
/**
3939
* Git commit date of the build (ISO format: YYYY-MM-DD).

src/main/java/org/perlonjava/runtime/operators/IOOperator.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,14 @@ public static RuntimeScalar print(RuntimeList runtimeList, RuntimeScalar fileHan
734734
try {
735735
// Write the content to the file handle
736736
return fh.write(sb.toString());
737+
} catch (java.nio.channels.NonWritableChannelException e) {
738+
// Writing to a read-only filehandle (opened with "<")
739+
getGlobalVariable("main::!").set("Bad file descriptor");
740+
WarnDie.warnWithCategory(
741+
new RuntimeScalar("Filehandle opened only for input"),
742+
new RuntimeScalar(""),
743+
"io");
744+
return new RuntimeScalar(); // undef
737745
} catch (Exception e) {
738746
getGlobalVariable("main::!").set("File operation failed: " + e.getMessage());
739747
return scalarFalse;
@@ -779,6 +787,14 @@ public static RuntimeScalar say(RuntimeList runtimeList, RuntimeScalar fileHandl
779787
return scalarFalse;
780788
}
781789
return fh.write(sb.toString());
790+
} catch (java.nio.channels.NonWritableChannelException e) {
791+
// Writing to a read-only filehandle (opened with "<")
792+
getGlobalVariable("main::!").set("Bad file descriptor");
793+
WarnDie.warnWithCategory(
794+
new RuntimeScalar("Filehandle opened only for input"),
795+
new RuntimeScalar(""),
796+
"io");
797+
return new RuntimeScalar(); // undef
782798
} catch (Exception e) {
783799
getGlobalVariable("main::!").set("File operation failed: " + e.getMessage());
784800
return scalarFalse;

0 commit comments

Comments
 (0)